This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
AppView.java
419 lines (348 loc) · 14.2 KB
/
AppView.java
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
417
418
419
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.webtrans.client.view;
import org.zanata.rest.dto.stats.ContainerTranslationStatistics;
import org.zanata.util.CoverageIgnore;
import org.zanata.webtrans.client.Application;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.presenter.KeyShortcutPresenter;
import org.zanata.webtrans.client.presenter.MainView;
import org.zanata.webtrans.client.presenter.NotificationDetailListener;
import org.zanata.webtrans.client.presenter.SearchResultsPresenter;
import org.zanata.webtrans.client.presenter.TranslationPresenter;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.ui.Breadcrumb;
import org.zanata.webtrans.client.ui.HasTranslationStats.LabelFormat;
import org.zanata.webtrans.client.ui.NotificationDetailsBox;
import org.zanata.webtrans.client.ui.TransUnitCountBar;
import org.zanata.webtrans.client.ui.UnorderedListWidget;
import org.zanata.webtrans.shared.model.UserWorkspaceContext;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.dom.client.UListElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
public class AppView extends Composite implements AppDisplay,
NotificationDetailListener {
private Timer deactivateNotificationTimer = new Timer() {
@Override
public void run() {
deactivateNotification(notificationList);
}
};
interface AppViewUiBinder extends UiBinder<LayoutPanel, AppView> {
}
interface Styles extends CssResource {
String disableTab();
String selectedTab();
String highlightedTab();
}
private static AppViewUiBinder uiBinder = GWT.create(AppViewUiBinder.class);
private static NotificationTemplate notificationTemplate = GWT
.create(NotificationTemplate.class);
@UiField(provided = true)
TransUnitCountBar translationStatsBar;
@UiField
InlineLabel readOnlyLabel;
@UiField(provided = true)
Breadcrumb selectedDocumentSpan;
@UiField(provided = true)
Breadcrumb projectLink;
@UiField(provided = true)
Breadcrumb versionLink;
@UiField(provided = true)
Breadcrumb filesLink;
@UiField
LayoutPanel sideMenuContainer, rootContainer;
@UiField
HTMLPanel contentContainer;
@UiField
TabLayoutPanel content;
@UiField
Styles style;
@UiField
Anchor editorTab, searchAndReplaceTab, documentListTab, keyShortcuts;
@UiField(provided = true)
Anchor newEditorLink;
@UiField
UnorderedListWidget notifications;
@UiField
UListElement notificationList;
private final NotificationDetailsBox notificationDetailsBox;
private final WebTransMessages messages;
private Listener listener;
private final UserWorkspaceContext userWorkspaceContext;
@Inject
public AppView(WebTransMessages messages,
DocumentListDisplay documentListView,
SearchResultsPresenter.Display searchResultsView,
TranslationPresenter.Display translationView,
SideMenuDisplay sideMenuView,
KeyShortcutPresenter keyShortcutPresenter,
final UserWorkspaceContext userWorkspaceContext) {
// this must be initialized before uiBinder.createAndBindUi(), or an
// exception will be thrown at runtime
this.userWorkspaceContext = userWorkspaceContext;
this.messages = messages;
translationStatsBar =
new TransUnitCountBar(userWorkspaceContext, messages,
LabelFormat.PERCENT_COMPLETE_HRS, true,
userWorkspaceContext.getWorkspaceRestrictions()
.isProjectRequireReview());
translationStatsBar.setVisible(false); // hide until there is a value to
projectLink =
new Breadcrumb(true, false,
Application.getProjectHomeURL(userWorkspaceContext
.getWorkspaceContext().getWorkspaceId()));
versionLink =
new Breadcrumb(false, false,
Application.getVersionHomeURL(userWorkspaceContext
.getWorkspaceContext().getWorkspaceId()));
filesLink = new Breadcrumb(false, false, "");
// filesLink.setHref(Application.getVersionFilesURL(userWorkspaceContext.getWorkspaceContext().getWorkspaceId()));
selectedDocumentSpan = new Breadcrumb(false, true, "");
newEditorLink = new Anchor();
newEditorLink.setTarget("_blank");
newEditorLink.setText(messages.newEditorMessage());
newEditorLink.addStyleName("is-invisible");
initWidget(uiBinder.createAndBindUi(this));
readOnlyLabel.setTitle(messages.readOnly());
keyShortcuts.setTitle(messages.availableKeyShortcutsTitle());
sideMenuContainer.add(sideMenuView.asWidget());
searchAndReplaceTab.setTitle(messages.projectWideSearchAndReplace());
documentListTab.setTitle(messages.documentListTitle());
editorTab.setTitle(messages.editor());
content.add(documentListView.asWidget());
content.add(translationView.asWidget());
content.add(searchResultsView.asWidget());
notificationDetailsBox =
new NotificationDetailsBox(messages, keyShortcutPresenter);
notifications.ensureDebugId("notifications");
Window.enableScrolling(false);
}
@Override
public Widget asWidget() {
return this;
}
// Order of the tab
private final static int DOCUMENT_VIEW = 0;
private final static int EDITOR_VIEW = 1;
private final static int SEARCH_AND_REPLACE_VIEW = 2;
private final static int REVIEW_VIEW = 3;
@Override
public void showInMainView(MainView view) {
switch (view) {
case Documents:
content.selectTab(DOCUMENT_VIEW);
selectedDocumentSpan.setVisible(false);
newEditorLink.addStyleName("is-invisible");
setSelectedTab(documentListTab);
break;
case Search:
content.selectTab(SEARCH_AND_REPLACE_VIEW);
selectedDocumentSpan.setVisible(true);
newEditorLink.addStyleName("is-invisible");
setSelectedTab(searchAndReplaceTab);
break;
case Editor:
content.selectTab(EDITOR_VIEW);
selectedDocumentSpan.setVisible(true);
newEditorLink.removeStyleName("is-invisible");
setSelectedTab(editorTab);
}
}
private void setSelectedTab(Widget tab) {
editorTab.removeStyleName(style.selectedTab());
searchAndReplaceTab.removeStyleName(style.selectedTab());
documentListTab.removeStyleName(style.selectedTab());
tab.addStyleName(style.selectedTab());
}
@Override
public void setProjectLinkLabel(String text) {
projectLink.setText(text);
}
@Override
public void setVersionLinkLabel(String text) {
versionLink.setText(text);
}
@Override
public void setFilesLinkLabel(String text) {
filesLink.setText(text);
}
@Override
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
public void setDocumentLabel(String docPath, String docName) {
String selectedDocId = docPath + docName;
selectedDocumentSpan.setText(selectedDocId);
newEditorLink.setHref(Application.getNewEditorLink(
userWorkspaceContext.getWorkspaceContext().getWorkspaceId(),
selectedDocId.replace("/", ",")));
}
@Override
public void setStats(ContainerTranslationStatistics transStats,
boolean statsByWords) {
translationStatsBar.setStats(transStats, statsByWords);
translationStatsBar.setVisible(true);
}
@Override
public void setReadOnlyVisible(boolean visible) {
readOnlyLabel.setVisible(visible);
}
private final static double MIN_MENU_WIDTH = 2;
private final static double EXPENDED_MENU_RIGHT = 23;
private final static double MINIMISED_EDITOR_RIGHT = 21.5;
private final static int ANIMATE_DURATION = 100;
@Override
public void showSideMenu(boolean isShowing) {
rootContainer.forceLayout();
if (isShowing) {
rootContainer.setWidgetLeftRight(contentContainer, 0.0, Unit.EM,
MINIMISED_EDITOR_RIGHT, Unit.EM);
rootContainer.setWidgetRightWidth(sideMenuContainer, 0.0, Unit.PX,
EXPENDED_MENU_RIGHT, Unit.EM);
} else {
rootContainer.setWidgetLeftRight(contentContainer, 0.0, Unit.EM,
0.0, Unit.EM);
rootContainer.setWidgetRightWidth(sideMenuContainer, 0.0, Unit.EM,
MIN_MENU_WIDTH, Unit.EM);
}
rootContainer.animate(ANIMATE_DURATION);
}
@UiHandler("keyShortcuts")
public void onKeyShortcutsIconClick(ClickEvent event) {
listener.onKeyShortcutsClicked();
}
@Override
public void enableTab(MainView view, boolean enable) {
switch (view) {
case Search:
enableTab(searchAndReplaceTab, enable);
break;
case Documents:
enableTab(documentListTab, enable);
break;
case Editor:
enableTab(editorTab, enable);
break;
}
}
@UiHandler("filesLink")
public void onFilesLinkClick(ClickEvent event) {
listener.onDocumentListClicked();
}
@UiHandler("documentListTab")
public void onDocumentListTabClick(ClickEvent event) {
listener.onDocumentListClicked();
}
@UiHandler("editorTab")
public void onEditorTabClick(ClickEvent event) {
listener.onEditorClicked();
}
@UiHandler("searchAndReplaceTab")
public void onSearchAndReplaceTabTabClick(ClickEvent event) {
listener.onSearchAndReplaceClicked();
}
private void enableTab(Widget tab, boolean enable) {
if (enable) {
tab.removeStyleName(style.disableTab());
} else {
tab.addStyleName(style.disableTab());
}
}
@Override
public void setKeyboardShorcutColor(boolean aliasKeyListening) {
if (aliasKeyListening) {
keyShortcuts.addStyleName(style.highlightedTab());
} else {
keyShortcuts.removeStyleName(style.highlightedTab());
}
}
public void showNotification(NotificationEvent notification) {
notificationList.setInnerHTML(
createListItem(getMessageClass(notification.getSeverity()),
notification.getMessage()).asString());
activateNotification(notificationList);
if (notification.getSeverity() == NotificationEvent.Severity.Info || notification.getSeverity() ==
NotificationEvent.Severity.Warning) {
deactivateNotificationTimer.schedule(5000);
}
}
@Override
public void showNotificationDetail(NotificationEvent notificationEvent) {
notificationDetailsBox.setMessage(notificationEvent);
notificationDetailsBox.center();
}
@Override
public void closeMessage(NotificationEvent notificationEvent) {
notifications.clear();
}
private static SafeHtml createListItem(String messageStyle, String message) {
return notificationTemplate.listItem(messageStyle,
new SafeHtmlBuilder().appendEscaped(message).toSafeHtml());
}
private static String getMessageClass(NotificationEvent.Severity severity) {
switch (severity) {
case Warning:
return "message--warning";
case Error:
return "message--danger";
case Info:
return "message--highlight";
}
return "message--highlight";
}
// @formatter:off
@CoverageIgnore("JSNI")
private static native void activateNotification(Element element)/*-{
$wnd.zanata.messages.activate(element);
}-*/;
@CoverageIgnore("JSNI")
private static native void deactivateNotification(Element element)/*-{
$wnd.zanata.messages.deactivate(element);
}-*/;
// @formatter:on
public interface NotificationTemplate extends SafeHtmlTemplates {
@Template("<li class='{0} message--removable js-message-removable'>{1}" +
"<a href='#' class='message__remove js-message-remove'><i class='i i--remove'></i></a>" +
"</li>")
SafeHtml listItem(String messageStyle, SafeHtml message);
}
}