summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeSetComplexDisclosurePanel.java
blob: d70e70233feb718dec4624a985912bbedfe8309d (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (C) 2011 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.changes;

import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.changes.ChangeTable.ApprovalViewType;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.ui.Hyperlink;
import com.google.gerrit.common.data.ChangeInfo;
import com.google.gerrit.common.data.ChangeSetDetail;
import com.google.gerrit.common.data.TopicDetail;
import com.google.gerrit.reviewdb.AbstractEntity.Status;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.ChangeSet;
import com.google.gerrit.reviewdb.ChangeSetInfo;
import com.google.gerrit.reviewdb.TopicMessage;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.Panel;

import java.util.ArrayList;
import java.util.List;

class ChangeSetComplexDisclosurePanel extends CommonComplexDisclosurePanel {
  private static final int R_AUTHOR = 0;
  private static final int R_DOWNLOAD = 1;
  private static final int R_CNT = 2;

  private final TopicScreen topicScreen;
  private final TopicDetail topicDetail;
  private final ChangeSet changeSet;
  private final FlowPanel body;

  private Panel actionsPanel;
  private ChangeTable changeTable;
  protected Hyperlink prev;
  protected Hyperlink next;

  private ChangeSet.Id diffBaseId;
  Button stageChangeSetButton;
  Button submitChangeSetButton;

  /**
   * Creates a closed complex disclosure panel for a change set.
   * The change set details are loaded when the complex disclosure panel is opened.
   */
  ChangeSetComplexDisclosurePanel(final TopicScreen parent, final TopicDetail detail,
      final ChangeSet cs) {
    this(parent, detail, cs, false);
    addOpenHandler(this);
  }

  /**
   * Creates an open complex disclosure panel for a patch set.
   */
  ChangeSetComplexDisclosurePanel(final TopicScreen parent, final TopicDetail detail,
      final ChangeSetDetail csd) {
    this(parent, detail, csd.getChangeSet(), true);
    ensureLoaded(csd);
  }

  private ChangeSetComplexDisclosurePanel(final TopicScreen parent, final TopicDetail detail,
      final ChangeSet cs, boolean isOpen) {
    super(Util.TM.changeSetHeader(cs.getChangeSetId()), isOpen);
    topicScreen = parent;
    topicDetail = detail;
    changeSet = cs;
    body = new FlowPanel();
    setContent(body);

    // TODO Gitweb support
  }

  public void setDiffBaseId(ChangeSet.Id diffBaseId) {
    this.diffBaseId = diffBaseId;
  }

  /**
   * Display the table showing the Author and Download links,
   * followed by the action buttons.
   */
  public void ensureLoaded(final ChangeSetDetail detail) {
    infoTable = new Grid(R_CNT, 2);
    infoTable.setStyleName(Gerrit.RESOURCES.css().infoBlock());
    infoTable.addStyleName(Gerrit.RESOURCES.css().patchSetInfoBlock());

    initRow(R_AUTHOR, Util.TC.changeSetInfoAuthor());
    initRow(R_DOWNLOAD, Util.TC.changeSetInfoDownload());

    final CellFormatter itfmt = infoTable.getCellFormatter();
    itfmt.addStyleName(0, 0, Gerrit.RESOURCES.css().topmost());
    itfmt.addStyleName(0, 1, Gerrit.RESOURCES.css().topmost());
    itfmt.addStyleName(R_CNT - 1, 0, Gerrit.RESOURCES.css().bottomheader());
    itfmt.addStyleName(R_AUTHOR, 1, Gerrit.RESOURCES.css().useridentity());
    itfmt.addStyleName(R_DOWNLOAD, 1, Gerrit.RESOURCES.css()
        .downloadLinkListCell());

    final ChangeSetInfo info = detail.getInfo();
    displayUserIdentity(R_AUTHOR, info.getAuthor());
    displayDownload(topicDetail.getTopic().getProject(), topicDetail.isAllowsAnonymous(),
        changeSet.getRefName(), topicDetail.getTopic().getTopicId(),
        changeSet.getChangeSetId(), R_DOWNLOAD);

    body.add(infoTable);

    displayChangeTable(detail);
    body.add(changeTable);

    if (!changeSet.getId().equals(diffBaseId)) {
      actionsPanel = new FlowPanel();
      actionsPanel.setStyleName(Gerrit.RESOURCES.css().patchSetActions());
      body.add(actionsPanel);
      if (Gerrit.isSignedIn()) {
        if (topicDetail.isCurrentChangeSet(detail)) {
          populateActions(detail);
        }
        // TODO populateDiffAllActions(detail);
        // The diff will have to access the ChangeSets in order to
        // the PatchSet.Id of the most recent Change, and also the
        // older change in the diffBaseId
      }
    }
  }

  private void displayChangeTable(ChangeSetDetail detail) {
    prev = new Hyperlink(Util.TC.changeSetChangeListPrev(), true, "");
    prev.setVisible(false);

    next = new Hyperlink(Util.TC.changeSetChangeListNext(), true, "");
    next.setVisible(false);

    List<Change> currChanges = detail.getChanges();
    List<ChangeInfo> cil = new ArrayList<ChangeInfo>();

    for (Change ch : currChanges) cil.add(new ChangeInfo(ch));

    changeTable = new ChangeTable(true);

    changeTable.setAccountInfoCache(topicDetail.getAccounts());
    ChangeTable.Section section = new ChangeTable.Section(null, ApprovalViewType.STRONGEST, null);
    if (!cil.isEmpty()) {
      changeTable.addSection(section);
      section.display(cil);
      changeTable.finishDisplay();
    }
    changeTable.setRegisterKeys(true);
  }

  private void populateActions(final ChangeSetDetail detail) {
    final boolean isOpen = topicDetail.getTopic().getStatus().isOpen();
    final boolean isNew = topicDetail.getTopic().getStatus() == Status.NEW;

    if (isOpen && isNew && topicDetail.canStage()) {

      stageChangeSetButton =
        new Button(Util.TM
            .stageChangeSet(detail.getChangeSet().getChangeSetId()));
      stageChangeSetButton.setEnabled(false);
      stageChangeSetButton.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(final ClickEvent event) {
          stageChangeSetButton.setEnabled(false);
          Util.T_MANAGE_SVC.stage(changeSet.getId(),
              new GerritCallback<TopicDetail>() {
                @Override
                public void onSuccess(TopicDetail result) {
                  onSubmitResult(result);
                }

                @Override
                public void onFailure(Throwable caught) {
                  super.onFailure(caught);
                }
          });
        }
      });
      actionsPanel.add(stageChangeSetButton);
    }

    if (isOpen && topicDetail.canSubmit()) {
      submitChangeSetButton =
          new Button(Util.TM
              .submitChangeSet(detail.getChangeSet().getChangeSetId()));
      submitChangeSetButton.setEnabled(false);
      submitChangeSetButton.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(final ClickEvent event) {
          submitChangeSetButton.setEnabled(false);
          Util.T_MANAGE_SVC.submit(changeSet.getId(),
              new GerritCallback<TopicDetail>() {
                public void onSuccess(TopicDetail result) {
                  onSubmitResult(result);
                }

                @Override
                public void onFailure(Throwable caught) {
                  super.onFailure(caught);
                }
              });
        }
      });
      actionsPanel.add(submitChangeSetButton);
    }

    if (topicDetail.canRevert()) {
      final Button b = new Button(Util.TC.buttonRevertTopicBegin());
      b.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(final ClickEvent event) {
          b.setEnabled(false);
          new CommentedChangeActionDialog<TopicDetail>(changeSet.getId(), createCommentedCallback(b),
              Util.TC.revertTopicTitle(), Util.TC.headingRevertMessage(),
              Util.TC.buttonRevertTopicSend(), Util.TC.buttonRevertTopicCancel(),
              Gerrit.RESOURCES.css().revertChangeDialog(), Gerrit.RESOURCES.css().revertMessage(),
              Util.TM.revertTopicDefaultMessage(topicDetail.getTopic().getTopic(),
                  topicDetail.getTopic().getId().get(), changeSet.getId().get())) {
                public void onSend() {
                  Util.T_MANAGE_SVC.revertTopic(getChangeSetId() , getMessageText(), createCallback());
                }
              }.center();
        }
      });
      actionsPanel.add(b);
    }

    if (topicDetail.canAbandon()) {
      final Button b = new Button(Util.TC.buttonAbandonTopicBegin());
      b.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(final ClickEvent event) {
          b.setEnabled(false);
          new CommentedChangeActionDialog<TopicDetail>(changeSet.getId(), createCommentedCallback(b),
              Util.TC.abandonTopicTitle(), Util.TC.headingAbandonMessage(),
              Util.TC.buttonAbandonTopicSend(), Util.TC.buttonAbandonTopicCancel(),
              Gerrit.RESOURCES.css().abandonChangeDialog(), Gerrit.RESOURCES.css().abandonMessage()) {
                public void onSend() {
                  Util.T_MANAGE_SVC.abandonTopic(getChangeSetId() , getMessageText(), createCallback());
                }
              }.center();
        }
      });
      actionsPanel.add(b);
    }

    if (topicDetail.canUnstage()) {
      final Button b = new Button(Util.C.buttonUnstagingChange());
      b.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
          b.setEnabled(false);
          Util.T_MANAGE_SVC.unstage(changeSet.getId(),
              new GerritCallback<TopicDetail>() {
                @Override
                public void onSuccess(TopicDetail result) {
                  topicScreen.update(result);
                }
          });
        }
      });
      actionsPanel.add(b);
    }

    if (topicDetail.canRestore()) {
      final Button b = new Button(Util.TC.buttonRestoreTopicBegin());
      b.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(final ClickEvent event) {
          b.setEnabled(false);
          new CommentedChangeActionDialog<TopicDetail>(changeSet.getId(), createCommentedCallback(b),
              Util.TC.restoreTopicTitle(), Util.TC.headingRestoreMessage(),
              Util.TC.buttonRestoreTopicSend(), Util.TC.buttonRestoreTopicCancel(),
              Gerrit.RESOURCES.css().abandonChangeDialog(), Gerrit.RESOURCES.css().abandonMessage()) {
                public void onSend() {
                  Util.T_MANAGE_SVC.restoreTopic(getChangeSetId(), getMessageText(), createCallback());
                }
              }.center();
        }
      });
      actionsPanel.add(b);
    }
  }

  public void refresh() {
    Util.T_DETAIL_SVC.changeSetDetail(changeSet.getId(),
        new GerritCallback<ChangeSetDetail>() {
          @Override
          public void onSuccess(ChangeSetDetail result) {

            if (changeSet.getId().equals(diffBaseId)) {
              changeTable.setVisible(false);
              actionsPanel.setVisible(false);
            } else {

              if (changeTable != null) {
                changeTable.removeFromParent();
              }
              displayChangeTable(result);
              body.add(changeTable);
            }
          }
        });
  }

  @Override
  public void onOpen(final OpenEvent<DisclosurePanel> event) {
    if (infoTable == null) {
      Util.T_DETAIL_SVC.changeSetDetail(changeSet.getId(),
          new GerritCallback<ChangeSetDetail>() {
            public void onSuccess(final ChangeSetDetail result) {
              ensureLoaded(result);
            }
          });
    }
  }

  private void onSubmitResult(final TopicDetail result) {
    if (result.getTopic().getStatus() == Status.NEW) {
      // The submit failed. Try to locate the message and display
      // it to the user, it should be the last one created by Gerrit.
      //
      TopicMessage msg = null;
      if (result.getMessages() != null && result.getMessages().size() > 0) {
        for (int i = result.getMessages().size() - 1; i >= 0; i--) {
          if (result.getMessages().get(i).getAuthor() == null) {
            msg = result.getMessages().get(i);
            break;
          }
        }
      }

      if (msg != null) {
        new SubmitFailureDialog(result, msg).center();
      }
    }
    topicScreen.update(result);
  }

  public ChangeSet getChangeSet() {
    return changeSet;
  }

  /** Activates / Deactivates the key navigation and the highlighting of the current row for the change table */
  public void setActive(boolean active) {
    if (changeTable != null) {
      changeTable.setRegisterKeys(active);
    }
  }

  private AsyncCallback<TopicDetail> createCommentedCallback(final Button b) {
    return new AsyncCallback<TopicDetail>() {
      public void onSuccess(TopicDetail result) {
        topicScreen.update(result);
      }

      public void onFailure(Throwable caught) {
        b.setEnabled(true);
      }
    };
  }

  /**
   * topicDetail callback that updates status for the
   * submitChangeSetButton and stageChangeSetButton
   */
  public void topicDetailCallback() {
    Util.T_DETAIL_SVC.topicDetail(topicDetail.getTopic().getId(), new GerritCallback<TopicDetail>() {
      public void onSuccess(TopicDetail result) {

            if (submitChangeSetButton != null) {
              if (changeTable.getChangeSetIsReviewed()) {
                submitChangeSetButton.setEnabled(true);
              } else {
                submitChangeSetButton.setEnabled(false);
              }
            }

            if (stageChangeSetButton != null) {
              if (changeTable.getChangeSetIsReviewed()) {
                stageChangeSetButton.setEnabled(true);
              } else {
                stageChangeSetButton.setEnabled(false);
              }
            }

          }

          public void onFailure(Throwable caught) {
            if (submitChangeSetButton != null) {
              submitChangeSetButton.setEnabled(false);
            }
            if (stageChangeSetButton != null) {
              stageChangeSetButton.setEnabled(false);
            }
          }
        });
  }
}