summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/CommentPanel.java
blob: 8f8c7eac18f8ed5049c7e0d1c0a9425e372b5e18 (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
// Copyright (C) 2009 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.ui;

import com.google.gerrit.client.FormatUtil;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.account.AccountInfo;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.HasBlurHandlers;
import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
import com.google.gwt.event.dom.client.HasFocusHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwtexpui.safehtml.client.SafeHtml;
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;

import java.util.Date;

public class CommentPanel extends Composite implements HasDoubleClickHandlers,
    HasFocusHandlers, FocusHandler, HasBlurHandlers, BlurHandler {
  private static final int SUMMARY_LENGTH = 75;
  private final HandlerManager handlerManager = new HandlerManager(this);
  private final FlexTable header;
  private final InlineLabel messageSummary;
  private final FlowPanel content;
  private final DoubleClickHTML messageText;
  private FlowPanel buttons;
  private boolean recent;

  public CommentPanel(final AccountInfo author, final Date when, String message) {
    this();

    setMessageText(message);
    setAuthorNameText(FormatUtil.name(author));
    setDateText(FormatUtil.shortFormatDayTime(when));

    final CellFormatter fmt = header.getCellFormatter();
    fmt.getElement(0, 0).setTitle(FormatUtil.nameEmail(author));
    fmt.getElement(0, 2).setTitle(FormatUtil.mediumFormat(when));
  }

  protected CommentPanel() {
    final FlowPanel body = new FlowPanel();
    initWidget(body);
    setStyleName(Gerrit.RESOURCES.css().commentPanel());

    messageSummary = new InlineLabel();
    messageSummary.setStyleName(Gerrit.RESOURCES.css().commentPanelSummary());

    header = new FlexTable();
    header.setStyleName(Gerrit.RESOURCES.css().commentPanelHeader());
    header.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        setOpen(!isOpen());
      }
    });
    header.setText(0, 0, "");
    header.setWidget(0, 1, messageSummary);
    header.setText(0, 2, "");
    final CellFormatter fmt = header.getCellFormatter();
    fmt.setStyleName(0, 0, Gerrit.RESOURCES.css().commentPanelAuthorCell());
    fmt.setStyleName(0, 1, Gerrit.RESOURCES.css().commentPanelSummaryCell());
    fmt.setStyleName(0, 2, Gerrit.RESOURCES.css().commentPanelDateCell());
    fmt.setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);
    body.add(header);

    content = new FlowPanel();
    content.setStyleName(Gerrit.RESOURCES.css().commentPanelContent());
    content.setVisible(false);
    body.add(content);

    messageText = new DoubleClickHTML();
    messageText.setStyleName(Gerrit.RESOURCES.css().commentPanelMessage());
    content.add(messageText);
  }

  @Override
  public HandlerRegistration addDoubleClickHandler(DoubleClickHandler handler) {
    return messageText.addDoubleClickHandler(handler);
  }

  protected void setMessageText(String message) {
    if (message == null) {
      message = "";
    } else {
      message = message.trim();
    }

    messageSummary.setText(summarize(message));
    SafeHtml buf = new SafeHtmlBuilder().append(message).wikify();
    buf = CommentLinkProcessor.apply(buf);
    SafeHtml.set(messageText, buf);
  }

  public void setAuthorNameText(final String nameText) {
    header.setText(0, 0, nameText);
  }

  protected void setDateText(final String dateText) {
    header.setText(0, 2, dateText);
  }

  protected void setMessageTextVisible(final boolean show) {
    messageText.setVisible(show);
  }

  protected void addContent(final Widget w) {
    if (buttons != null) {
      content.insert(w, content.getWidgetIndex(buttons));
    } else {
      content.add(w);
    }
  }

  /**
   * Registers a {@link FocusHandler} for this comment panel.
   * The comment panel is considered as being focused whenever any button in the
   * comment panel gets focused.
   *
   * @param handler the focus handler to be registered
   */
  @Override
  public HandlerRegistration addFocusHandler(final FocusHandler handler) {
    return handlerManager.addHandler(FocusEvent.getType(), handler);
  }

  /**
   * Registers a {@link BlurHandler} for this comment panel.
   * The comment panel is considered as being blurred whenever any button in the
   * comment panel gets blurred.
   *
   * @param handler the blur handler to be registered
   */
  @Override
  public HandlerRegistration addBlurHandler(final BlurHandler handler) {
    return handlerManager.addHandler(BlurEvent.getType(), handler);
  }

  protected void addButton(final Button button) {
    // register focus and blur handler for each button, so that we can fire
    // focus and blur events for the comment panel
    button.addFocusHandler(this);
    button.addBlurHandler(this);
    getButtonPanel().add(button);
  }

  private Panel getButtonPanel() {
    if (buttons == null) {
      buttons = new FlowPanel();
      buttons.setStyleName(Gerrit.RESOURCES.css().commentPanelButtons());
      content.add(buttons);
    }
    return buttons;
  }

  @Override
  public void onFocus(final FocusEvent event) {
    // a button was focused -> fire focus event for the comment panel
    handlerManager.fireEvent(event);
  }

  @Override
  public void onBlur(final BlurEvent event) {
    // a button was blurred -> fire blur event for the comment panel
    handlerManager.fireEvent(event);
  }

  public void enableButtons(final boolean on) {
    for (Widget w : getButtonPanel()) {
      if (w instanceof Button) {
        ((Button) w).setEnabled(on);
      }
    }
  }

  private static String summarize(final String message) {
    if (message.length() < SUMMARY_LENGTH) {
      return message;
    }

    int p = 0;
    final StringBuilder r = new StringBuilder();
    while (r.length() < SUMMARY_LENGTH) {
      final int e = message.indexOf(' ', p);
      if (e < 0) {
        break;
      }

      final String word = message.substring(p, e).trim();
      if (SUMMARY_LENGTH <= r.length() + word.length() + 1) {
        break;
      }
      if (r.length() > 0) {
        r.append(' ');
      }
      r.append(word);
      p = e + 1;
    }
    r.append(" \u2026");
    return r.toString();
  }

  public boolean isOpen() {
    return content.isVisible();
  }

  public void setOpen(final boolean open) {
    messageSummary.setVisible(!open);
    content.setVisible(open);
  }

  public boolean isRecent() {
    return recent;
  }

  public void setRecent(final boolean r) {
    recent = r;
  }

  private static class DoubleClickHTML extends HTML implements
      HasDoubleClickHandlers {
    public HandlerRegistration addDoubleClickHandler(DoubleClickHandler handler) {
      return addDomHandler(handler, DoubleClickEvent.getType());
    }
  }
}