This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
TargetContentsView.java
335 lines (289 loc) · 8.95 KB
/
TargetContentsView.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
/*
* 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 java.util.ArrayList;
import java.util.List;
import org.zanata.common.ContentState;
import org.zanata.webtrans.client.events.ReviewCommentEvent;
import org.zanata.webtrans.client.ui.Editor;
import org.zanata.webtrans.client.ui.EditorButtonsWidget;
import org.zanata.webtrans.client.ui.ToggleEditor;
import org.zanata.webtrans.client.ui.UndoLink;
import org.zanata.webtrans.client.ui.ValidationMessagePanelView;
import org.zanata.webtrans.client.util.ContentStateToStyleUtil;
import org.zanata.webtrans.shared.model.TransUnit;
import org.zanata.webtrans.shared.model.TransUnitId;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
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.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Provider;
import net.customware.gwt.presenter.client.EventBus;
public class TargetContentsView extends Composite implements TargetContentsDisplay
{
private static final int COLUMNS = 1;
private static Binder binder = GWT.create(Binder.class);
private final EventBus eventBus;
@UiField
Grid editorGrid;
@UiField(provided = true)
ValidationMessagePanelView validationPanel;
@UiField
Styles style;
@UiField
Label savingIndicator;
@UiField(provided = true)
EditorButtonsWidget buttons;
@UiField
Label commentIndicator;
private HorizontalPanel rootPanel;
private ArrayList<ToggleEditor> editors;
private Listener listener;
private EditingState editingState = EditingState.SAVED;
private TransUnit cachedValue;
@Inject
public TargetContentsView(Provider<ValidationMessagePanelView> validationMessagePanelViewProvider, EventBus eventBus)
{
this.eventBus = eventBus;
buttons = new EditorButtonsWidget(eventBus);
validationPanel = validationMessagePanelViewProvider.get();
rootPanel = binder.createAndBindUi(this);
editorGrid.addStyleName("TableEditorCell-Target-Table");
editorGrid.ensureDebugId("target-contents-grid");
editorGrid.setWidth("100%");
editors = Lists.newArrayList();
}
@UiHandler("commentIndicator")
public void commentIndicatorClicked(ClickEvent event)
{
eventBus.fireEvent(new ReviewCommentEvent(getId()));
}
@Override
public void showButtons(boolean displayButtons)
{
buttons.setVisible(displayButtons);
for (ToggleEditor editor : editors)
{
editor.showCopySourceButton(displayButtons);
}
}
@Override
public void focusEditor(int currentEditorIndex)
{
if (currentEditorIndex >= 0 && currentEditorIndex < editors.size())
{
editors.get(currentEditorIndex).setFocus();
}
}
@Override
public void addUndo(final UndoLink undoLink)
{
buttons.addUndo(undoLink);
}
@Override
public void setValueAndCreateNewEditors(TransUnit transUnit)
{
setCachedTU(transUnit);
updateCommentIndicator(transUnit.getCommentsCount());
editors.clear();
List<String> cachedTargets = cachedValue.getTargets();
if (cachedTargets == null)
{
cachedTargets = Lists.newArrayList("");
}
editorGrid.resize(cachedTargets.size(), COLUMNS);
int rowIndex = 0;
for (String target : cachedTargets)
{
Editor editor = new Editor(target, rowIndex, listener, transUnit.getId());
editor.setEnableSpellCheck(listener.getConfigState().isSpellCheckEnabled());
editorGrid.setWidget(rowIndex, 0, editor);
editors.add(editor);
rowIndex++;
}
editingState = EditingState.SAVED;
}
private static String resolveStyleName(ContentState status)
{
return ContentStateToStyleUtil.stateToStyle(status, "TableEditorRow ");
}
@Override
public void updateCommentIndicator(int commentsCount)
{
commentIndicator.setVisible(commentsCount > 0);
commentIndicator.setText(String.valueOf(commentsCount));
}
@Override
public void setState(EditingState editingState)
{
if (this.editingState == editingState)
{
return;
}
this.editingState = editingState;
if (editingState == EditingState.UNSAVED)
{
editorGrid.setStyleName(style.unsaved());
savingIndicator.setVisible(false);
}
else if (editingState == EditingState.SAVING)
{
savingIndicator.setVisible(true);
}
else
{
editorGrid.setStyleName(resolveStyleName(cachedValue.getStatus()));
savingIndicator.setVisible(false);
}
}
@Override
public EditingState getEditingState()
{
return editingState;
}
@Override
public void updateCachedTargetsAndVersion(List<String> targets, Integer verNum, ContentState status)
{
setCachedTU(TransUnit.Builder.from(cachedValue).setTargets(targets).setVerNum(verNum).setStatus(status).build());
}
private void setCachedTU(TransUnit newTransUnit)
{
cachedValue = newTransUnit;
editorGrid.setStyleName(resolveStyleName(cachedValue.getStatus()));
buttons.setIdAndState(cachedValue.getId(), cachedValue.getStatus());
}
@Override
public void setEnableSpellCheck(boolean spellCheckEnabled)
{
for (ToggleEditor editor : editors)
{
editor.setEnableSpellCheck(spellCheckEnabled);
}
}
@Override
public void highlightSearch(String findMessage)
{
for (ToggleEditor editor : editors)
{
editor.highlightSearch(findMessage);
}
}
@Override
public ArrayList<String> getNewTargets()
{
ArrayList<String> result = Lists.newArrayList();
for (ToggleEditor editor : editors)
{
result.add(editor.getText());
}
return result;
}
@Override
public List<String> getCachedTargets()
{
return cachedValue.getTargets();
}
@Override
public TransUnitId getId()
{
return cachedValue.getId();
}
@Override
public ArrayList<ToggleEditor> getEditors()
{
return editors;
}
@Override
public void setListener(Listener listener)
{
this.listener = listener;
buttons.setListener(listener);
}
@Override
public void revertEditorContents()
{
List<String> cachedTargets = cachedValue.getTargets();
for (int i = 0; i < cachedTargets.size(); i++)
{
String target = cachedTargets.get(i);
editors.get(i).setTextAndValidate(target);
}
editorGrid.setStyleName(resolveStyleName(cachedValue.getStatus()));
}
@Override
public void refresh()
{
for (ToggleEditor editor : editors)
{
editor.refresh();
}
}
@Override
public Integer getVerNum()
{
return cachedValue.getVerNum();
}
@Override
public void setToMode(ToggleEditor.ViewMode viewMode)
{
for (ToggleEditor editor : editors)
{
editor.setViewMode(viewMode);
}
validationPanel.setVisible(viewMode == ToggleEditor.ViewMode.EDIT);
}
@Override
public void updateValidationWarning(List<String> errors)
{
validationPanel.updateValidationWarning(errors);
}
@Override
public Widget asWidget()
{
return rootPanel;
}
@Override
public String toString()
{
return Objects.toStringHelper(this).add("editors", editors).toString();
}
interface Styles extends CssResource
{
String targetContentsCell();
String editorGridWrapper();
String unsaved();
String saving();
String commentIndicator();
}
interface Binder extends UiBinder<HorizontalPanel, TargetContentsView>
{
}
}