summaryrefslogtreecommitdiffstats
path: root/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/webui/UiAction.java
blob: b9d15d2042284646a2d6797a04d9a275af729d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.extensions.webui;

import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.conditions.BooleanCondition;
import com.google.gerrit.extensions.restapi.RestResource;
import com.google.gerrit.extensions.restapi.RestView;

public interface UiAction<R extends RestResource> extends RestView<R> {
  /**
   * Get the description of the action customized for the resource.
   *
   * @param resource the resource the view would act upon if the action is invoked by the client.
   *     Information from the resource can be used to customize the description.
   * @return a description of the action. The server will populate the {@code id} and {@code method}
   *     properties. If null the action will assumed unavailable and not presented. This is usually
   *     the same as {@code setVisible(false)}.
   */
  @Nullable
  Description getDescription(R resource);

  /** Describes an action invokable through the web interface. */
  class Description {
    private String method;
    private String id;
    private String label;
    private String title;
    private BooleanCondition visible = BooleanCondition.TRUE;
    private BooleanCondition enabled = BooleanCondition.TRUE;

    public String getMethod() {
      return method;
    }

    /** {@code PrivateInternals_UiActionDescription.setMethod()} */
    void setMethod(String method) {
      this.method = method;
    }

    public String getId() {
      return id;
    }

    /** {@code PrivateInternals_UiActionDescription.setId()} */
    void setId(String id) {
      this.id = id;
    }

    public String getLabel() {
      return label;
    }

    /** Set the label to appear on the button to activate this action. */
    public Description setLabel(String label) {
      this.label = label;
      return this;
    }

    public String getTitle() {
      return title;
    }

    /** Set the tool-tip text to appear when the mouse hovers on the button. */
    public Description setTitle(String title) {
      this.title = title;
      return this;
    }

    public boolean isVisible() {
      return getVisibleCondition().value();
    }

    public BooleanCondition getVisibleCondition() {
      return visible;
    }

    /**
     * Set if the action's button is visible on screen for the current client. If not visible the
     * action description may not be sent to the client.
     */
    public Description setVisible(boolean visible) {
      return setVisible(BooleanCondition.valueOf(visible));
    }

    /**
     * Set if the action's button is visible on screen for the current client. If not visible the
     * action description may not be sent to the client.
     */
    public Description setVisible(BooleanCondition visible) {
      this.visible = visible;
      return this;
    }

    public boolean isEnabled() {
      return getEnabledCondition().value();
    }

    public BooleanCondition getEnabledCondition() {
      return BooleanCondition.and(enabled, visible);
    }

    /** Set if the button should be invokable (true), or greyed out (false). */
    public Description setEnabled(boolean enabled) {
      return setEnabled(BooleanCondition.valueOf(enabled));
    }

    /** Set if the button should be invokable (true), or greyed out (false). */
    public Description setEnabled(BooleanCondition enabled) {
      this.enabled = enabled;
      return this;
    }
  }
}