summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/ProjectInfoScreen.java
blob: d6bca78767c2092ea0523fb615bbe486e0f72071 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (C) 2008 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.client.admin;

import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.ScreenLoadCallback;
import com.google.gerrit.client.ui.OnEditEnabler;
import com.google.gerrit.client.ui.SmallHeading;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ProjectDetail;
import com.google.gerrit.reviewdb.Project;
import com.google.gerrit.reviewdb.Project.SubmitType;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwtexpui.globalkey.client.NpTextArea;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ProjectInfoScreen extends ProjectScreen {
  private Project project;

  private Panel projectOptionsPanel;
  private CheckBox requireChangeID;
  private CheckBox allowTopicReview;
  private ListBox submitType;
  private CheckBox useContentMerge;

  private Panel agreementsPanel;
  private CheckBox useContributorAgreements;
  private CheckBox useSignedOffBy;

  private Panel cherryPickPanel;
  private CheckBox includeReviewedOn;
  private CheckBox includeOnlyMaxApproval;
  private Map<String, CheckBox> approvalsInFooter;

  private NpTextArea descTxt;
  private Button saveProject;

  private OnEditEnabler saveEnabler;

  public ProjectInfoScreen(final Project.NameKey toShow) {
    super(toShow);
  }

  @Override
  protected void onInitUI() {
    super.onInitUI();

    saveProject = new Button(Util.C.buttonSaveChanges());
    saveProject.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        doSave();
      }
    });

    initDescription();
    initProjectOptions();
    initcherryPickOptions();
    initAgreements();
    add(saveProject);
  }

  @Override
  protected void onLoad() {
    super.onLoad();
    for (final ApprovalType t :
        Gerrit.getConfig().getApprovalTypes().getApprovalTypes()) {
      final String footer = t.getCategory().getLabelName();
      if (!approvalsInFooter.containsKey(footer)) {
        final String title = Util.C.footerPrefix() + " " + footer;
        CheckBox checkBox = new CheckBox(title, true);
        approvalsInFooter.put(footer, checkBox);
        saveEnabler.listenTo(checkBox);
        cherryPickPanel.add(checkBox);
      }
    }
    Util.PROJECT_SVC.projectDetail(getProjectKey(),
        new ScreenLoadCallback<ProjectDetail>(this) {
          public void preDisplay(final ProjectDetail result) {
            enableForm(result.canModifyAgreements,
                result.canModifyDescription, result.canModifyMergeType);
            saveProject.setVisible(
                result.canModifyAgreements ||
                result.canModifyDescription ||
                result.canModifyMergeType);
            display(result);
          }
        });
  }

  private void enableForm(final boolean canModifyAgreements,
      final boolean canModifyDescription, final boolean canModifyMergeType) {
    submitType.setEnabled(canModifyMergeType);
    useContentMerge.setEnabled(canModifyMergeType);
    descTxt.setEnabled(canModifyDescription);
    useContributorAgreements.setEnabled(canModifyAgreements);
    useSignedOffBy.setEnabled(canModifyAgreements);
    requireChangeID.setEnabled(canModifyMergeType);
  }

  private void initDescription() {
    final VerticalPanel vp = new VerticalPanel();
    vp.add(new SmallHeading(Util.C.headingDescription()));

    descTxt = new NpTextArea();
    descTxt.setVisibleLines(6);
    descTxt.setCharacterWidth(60);
    vp.add(descTxt);

    add(vp);
    saveEnabler = new OnEditEnabler(saveProject);
    saveEnabler.listenTo(descTxt);
  }

  private void initProjectOptions() {
    projectOptionsPanel = new VerticalPanel();
    projectOptionsPanel.add(new SmallHeading(Util.C.headingProjectOptions()));

    submitType = new ListBox();
    for (final Project.SubmitType type : Project.SubmitType.values()) {
      submitType.addItem(Util.toLongString(type), type.name());
    }
    submitType.addChangeHandler(new ChangeHandler() {
      @Override
      public void onChange(ChangeEvent event) {
        setEnabledForUseContentMerge();
        setEnabledForCherryPick();
      }
    });
    saveEnabler.listenTo(submitType);
    projectOptionsPanel.add(submitType);

    useContentMerge = new CheckBox(Util.C.useContentMerge(), true);
    saveEnabler.listenTo(useContentMerge);
    projectOptionsPanel.add(useContentMerge);

    requireChangeID = new CheckBox(Util.C.requireChangeID(), true);
    saveEnabler.listenTo(requireChangeID);
    projectOptionsPanel.add(requireChangeID);

    allowTopicReview = new CheckBox(Util.C.allowTopicReview(), true);
    saveEnabler.listenTo(allowTopicReview);
    projectOptionsPanel.add(allowTopicReview);

    add(projectOptionsPanel);
  }

  /**
   * Enables the {@link #useContentMerge} checkbox if the selected submit type
   * allows the usage of content merge.
   * If the submit type (currently only 'Fast Forward Only') does not allow
   * content merge the useContentMerge checkbox gets disabled.
   */
  private void setEnabledForUseContentMerge() {
    if (SubmitType.FAST_FORWARD_ONLY.equals(Project.SubmitType
        .valueOf(submitType.getValue(submitType.getSelectedIndex())))) {
      useContentMerge.setEnabled(false);
      useContentMerge.setValue(false);
    } else {
      useContentMerge.setEnabled(submitType.isEnabled());
    }
  }

  private void setEnabledForCherryPick() {
    final boolean isCherryPickSubmitType =
      SubmitType.CHERRY_PICK.equals(Project.SubmitType.valueOf(
          submitType.getValue(submitType.getSelectedIndex())));
    enableCherryPickOptions(isCherryPickSubmitType);
  }

  private void enableCherryPickOptions(final boolean enable) {
    includeReviewedOn.setEnabled(enable);
    includeOnlyMaxApproval.setEnabled(enable);
    for (CheckBox checkBox : approvalsInFooter.values()) {
      checkBox.setEnabled(enable);
    }
  }

  private void initAgreements() {
    agreementsPanel = new VerticalPanel();
    agreementsPanel.add(new SmallHeading(Util.C.headingAgreements()));

    useContributorAgreements = new CheckBox(Util.C.useContributorAgreements());
    saveEnabler.listenTo(useContributorAgreements);
    agreementsPanel.add(useContributorAgreements);

    useSignedOffBy = new CheckBox(Util.C.useSignedOffBy(), true);
    saveEnabler.listenTo(useSignedOffBy);
    agreementsPanel.add(useSignedOffBy);

    add(agreementsPanel);
  }

  private void initcherryPickOptions() {
    cherryPickPanel = new VerticalPanel();
    cherryPickPanel.add(new SmallHeading(Util.C.cherryPickOptions()));

    includeOnlyMaxApproval = new CheckBox(Util.C.includeOnlyMaxApprovals(), true);
    saveEnabler.listenTo(includeOnlyMaxApproval);
    cherryPickPanel.add(includeOnlyMaxApproval);

    includeReviewedOn = new CheckBox(Util.C.includeReviewedOn(), true);
    saveEnabler.listenTo(includeReviewedOn);
    cherryPickPanel.add(includeReviewedOn);

    approvalsInFooter = new HashMap<String, CheckBox>();

    add(cherryPickPanel);
  }

  private void setSubmitType(final Project.SubmitType newSubmitType) {
    int index = -1;
    if (submitType != null) {
      for (int i = 0; i < submitType.getItemCount(); i++) {
        if (newSubmitType.name().equals(submitType.getValue(i))) {
          index = i;
          break;
        }
      }
      submitType.setSelectedIndex(index);
      setEnabledForUseContentMerge();
      setEnabledForCherryPick();
    }
  }

  void display(final ProjectDetail result) {
    project = result.project;

    final boolean isall =
        Gerrit.getConfig().getWildProject().equals(project.getNameKey());
    projectOptionsPanel.setVisible(!isall);
    agreementsPanel.setVisible(!isall);
    useContributorAgreements.setVisible(Gerrit.getConfig()
        .isUseContributorAgreements());

    descTxt.setText(project.getDescription());
    useContributorAgreements.setValue(project.isUseContributorAgreements());
    useSignedOffBy.setValue(project.isUseSignedOffBy());
    useContentMerge.setValue(project.isUseContentMerge());
    requireChangeID.setValue(project.isRequireChangeID());
    allowTopicReview.setValue(project.isAllowTopicReview());
    setSubmitType(project.getSubmitType());

    includeOnlyMaxApproval.setValue(project.isIncludeOnlyMaxApproval());
    includeReviewedOn.setValue(!project.isHideReviewedOn());
    cherryPickPanel.setVisible(!isall);

    Map<String, Boolean> hiddenFooters = project.getHiddenFooters();
    for (Entry<String, CheckBox> entry : approvalsInFooter.entrySet()) {
      final CheckBox checkBox = entry.getValue();
      if (hiddenFooters.containsKey(entry.getKey())) {
        checkBox.setValue(!hiddenFooters.get(entry.getKey()));
      } else {
        checkBox.setValue(true);
      }
    }

    saveProject.setEnabled(false);
  }

  private void doSave() {
    project.setDescription(descTxt.getText().trim());
    project.setUseContributorAgreements(useContributorAgreements.getValue());
    project.setUseSignedOffBy(useSignedOffBy.getValue());
    project.setUseContentMerge(useContentMerge.getValue());
    // The requireChangeId setting is needed when you use the topic review feature
    //
    project.setRequireChangeID(requireChangeID.getValue() || allowTopicReview.getValue());
    project.setAllowTopicReview(allowTopicReview.getValue());
    if (submitType.getSelectedIndex() >= 0) {
      project.setSubmitType(Project.SubmitType.valueOf(submitType
          .getValue(submitType.getSelectedIndex())));
    }
    project.setIncludeOnlyMaxApproval(includeOnlyMaxApproval.getValue());
    project.setHideReviewedOn(!includeReviewedOn.getValue());
    for (Entry<String, CheckBox> entry : approvalsInFooter.entrySet()) {
      project.addHiddenFooter(entry.getKey(), !entry.getValue().getValue());
    }

    enableForm(false, false, false);
    enableCherryPickOptions(false);

    Util.PROJECT_SVC.changeProjectSettings(project,
        new GerritCallback<ProjectDetail>() {
          public void onSuccess(final ProjectDetail result) {
            enableForm(result.canModifyAgreements,
                result.canModifyDescription, result.canModifyMergeType);
            display(result);
          }
        });
  }
}