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
/
SearchResultsDocumentTable.java
493 lines (434 loc) · 17.4 KB
/
SearchResultsDocumentTable.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* Copyright 2012, 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.ui;
import java.util.Collection;
import java.util.List;
import org.zanata.common.ContentState;
import org.zanata.webtrans.client.presenter.PreviewState;
import org.zanata.webtrans.client.presenter.TransUnitReplaceInfo;
import org.zanata.webtrans.client.resources.WebTransMessages;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.cell.client.ActionCell.Delegate;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.ImageResourceRenderer;
import com.google.gwt.view.client.CellPreviewEvent;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.DefaultSelectionEventManager.BlacklistEventTranslator;
import com.google.gwt.view.client.DefaultSelectionEventManager.SelectAction;
/**
* Displays search results for a single document.
*
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a>
*
*/
public class SearchResultsDocumentTable extends CellTable<TransUnitReplaceInfo>
{
private static final int PREVIEW_COLUMN_INDEX = 4;
private static final int REPLACE_COLUMN_INDEX = 5;
private static CellTableResources cellTableResources;
private WebTransMessages messages;
private static SafeHtml spinner;
private static DefaultSelectionEventManager<TransUnitReplaceInfo> selectionManager = null;
private static String highlightString = null;
private static boolean requirePreview = true;
private CheckColumn checkboxColumn;
private TextColumn<TransUnitReplaceInfo> rowIndexColumn;
private Column<TransUnitReplaceInfo, List<String>> sourceColumn;
private Column<TransUnitReplaceInfo, TransUnitReplaceInfo> targetColumn;
private ActionColumn previewButtonColumn;
private ActionColumn replaceButtonColumn;
public static void setHighlightString(String highlightString)
{
SearchResultsDocumentTable.highlightString = highlightString;
}
public static void setRequirePreview(boolean required)
{
requirePreview = required;
}
public SearchResultsDocumentTable(Delegate<TransUnitReplaceInfo> previewDelegate,
Delegate<TransUnitReplaceInfo> replaceDelegate,
Delegate<TransUnitReplaceInfo> undoDelegate,
final SelectionModel<TransUnitReplaceInfo> selectionModel,
ValueChangeHandler<Boolean> selectAllHandler,
final WebTransMessages messages,
org.zanata.webtrans.client.resources.Resources resources)
{
super(15, getCellTableResources());
this.messages = messages;
if (spinner == null)
{
spinner = new ImageResourceRenderer().render(resources.spinner());
}
checkboxColumn = new CheckColumn(selectionModel);
rowIndexColumn = buildRowIndexColumn();
sourceColumn = buildSourceColumn();
targetColumn = buildTargetColumn();
previewButtonColumn = new ActionColumn(new PreviewActionCell(previewDelegate));
replaceButtonColumn = new ActionColumn(new ReplaceActionCell(replaceDelegate, undoDelegate));
setWidth("100%", true);
addColumn(checkboxColumn, "");
addColumn(rowIndexColumn, messages.rowIndex());
addColumn(sourceColumn, messages.source());
addColumn(targetColumn, messages.target());
addColumn(previewButtonColumn, messages.actions());
// allowing preview header to refer to this also
addColumn(replaceButtonColumn);
setColumnWidth(checkboxColumn, 50.0, Unit.PX);
setColumnWidth(rowIndexColumn, 70.0, Unit.PX);
setColumnWidth(sourceColumn, 50.0, Unit.PCT);
setColumnWidth(targetColumn, 50.0, Unit.PCT);
setColumnWidth(previewButtonColumn, 85.0, Unit.PX);
setColumnWidth(replaceButtonColumn, 85.0, Unit.PX);
sourceColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
targetColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
setSelectionModel(selectionModel, getSelectionManager());
addStyleName("projectWideSearchResultsDocumentBody");
}
// TODO add focus tracking field to allow current-document type interactions
// listeners may need to be attached in view
/**
* @return a column that displays the 1-based index of the text flow
*/
private static TextColumn<TransUnitReplaceInfo> buildRowIndexColumn()
{
return new TextColumn<TransUnitReplaceInfo>()
{
@Override
public String getValue(TransUnitReplaceInfo tu)
{
return Integer.toString(tu.getTransUnit().getRowIndex() + 1);
}
};
}
/**
* @return a column that displays the source contents for the text flow
*/
private static Column<TransUnitReplaceInfo, List<String>> buildSourceColumn()
{
return new Column<TransUnitReplaceInfo, List<String>>(new AbstractCell<List<String>>()
{
@Override
public void render(Context context, List<String> contents, SafeHtmlBuilder sb)
{
for (String source : notEmptyContents(contents))
{
HighlightingLabel label = new HighlightingLabel(source);
if (!Strings.isNullOrEmpty(highlightString))
{
label.highlightSearch(highlightString);
}
appendContent(sb, label.getElement().getString());
}
}
})
{
@Override
public List<String> getValue(TransUnitReplaceInfo info)
{
return info.getTransUnit().getSources();
}
};
}
/**
* @return a column that displays the target contents for the text flow
*/
private static Column<TransUnitReplaceInfo, TransUnitReplaceInfo> buildTargetColumn()
{
return new Column<TransUnitReplaceInfo, TransUnitReplaceInfo>(new AbstractCell<TransUnitReplaceInfo>()
{
@Override
public void render(Context context, TransUnitReplaceInfo info, SafeHtmlBuilder sb)
{
// TODO for replaced targets, highlight replacement term or show diff
List<String> contents = info.getTransUnit().getTargets();
if (info.getPreviewState() == PreviewState.Show)
{
for (int i = 0; i < contents.size(); i++)
{
DiffMatchPatchLabel label = new DiffMatchPatchLabel();
label.setOriginal(contents.get(i));
label.setText(info.getPreview().getContents().get(i));
appendContent(sb, label.getElement().getString());
}
}
else
{
for (String target : contents)
{
HighlightingLabel label = new HighlightingLabel(target);
if (!Strings.isNullOrEmpty(highlightString))
{
label.highlightSearch(highlightString);
}
appendContent(sb, label.getElement().getString());
}
}
}
})
{
@Override
public TransUnitReplaceInfo getValue(TransUnitReplaceInfo info)
{
return info;
}
@Override
public String getCellStyleNames(Context context, TransUnitReplaceInfo info)
{
String styleNames = Strings.nullToEmpty(super.getCellStyleNames(context, info));
if (info.getTransUnit().getStatus() == ContentState.Approved)
{
styleNames += " ApprovedStateDecoration";
}
else if (info.getTransUnit().getStatus() == ContentState.NeedReview)
{
styleNames += " FuzzyStateDecoration";
}
return styleNames;
}
};
}
private class CheckColumn extends Column<TransUnitReplaceInfo, Boolean>
{
private SelectionModel<TransUnitReplaceInfo> selectionModel;
public CheckColumn(SelectionModel<TransUnitReplaceInfo> selectionModel)
{
super(new CheckboxCell(true, false));
this.selectionModel = selectionModel;
}
@Override
public Boolean getValue(TransUnitReplaceInfo info)
{
return selectionModel.isSelected(info);
}
}
private class ActionColumn extends Column<TransUnitReplaceInfo, TransUnitReplaceInfo>
{
public ActionColumn(ActionCell<TransUnitReplaceInfo> actionCell)
{
super(actionCell);
this.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
this.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
this.setCellStyleNames("projectWideSearchResultsActionColumn");
}
@Override
public TransUnitReplaceInfo getValue(TransUnitReplaceInfo info)
{
return info;
}
}
private class ReplaceActionCell extends ActionCell<TransUnitReplaceInfo>
{
private Delegate<TransUnitReplaceInfo> undoDelegate;
private final SafeHtml disabledReplaceHtml;
private final SafeHtml replacingHtml;
private final SafeHtml undoHtml;
private final SafeHtml undoingHtml;
public ReplaceActionCell(Delegate<TransUnitReplaceInfo> replaceDelegate, Delegate<TransUnitReplaceInfo> undoDelegate) {
super(SafeHtmlUtils.fromString(messages.replace()), replaceDelegate);
this.undoDelegate = undoDelegate;
disabledReplaceHtml = buildButtonHtml("", messages.replace(), messages.previewRequiredBeforeReplace(), false);
replacingHtml = buildProcessingIndicator(messages.replacing());
undoingHtml = buildProcessingIndicator(messages.undoInProgress());
undoHtml = buildButtonHtml(messages.replaced(), messages.undo());
}
@Override
public void render(com.google.gwt.cell.client.Cell.Context context, TransUnitReplaceInfo value, SafeHtmlBuilder sb)
{
switch (value.getReplaceState())
{
case NotReplaced:
if (!requirePreview || value.getPreviewState() == PreviewState.Show || value.getPreviewState() == PreviewState.Hide)
{
super.render(context, value, sb);
}
else
{
sb.append(disabledReplaceHtml);
}
break;
case Replacing:
sb.append(replacingHtml);
break;
case Replaced:
sb.append(undoHtml);
break;
case Undoing:
sb.append(undoingHtml);
break;
}
}
@Override
protected void onEnterKeyDown(Context context, Element parent, TransUnitReplaceInfo value, NativeEvent event, ValueUpdater<TransUnitReplaceInfo> valueUpdater)
{
switch (value.getReplaceState())
{
case NotReplaced:
if (!requirePreview || value.getPreviewState() == PreviewState.Show || value.getPreviewState() == PreviewState.Hide)
{
super.onEnterKeyDown(context, parent, value, event, valueUpdater);
}
break;
case Replaced:
undoDelegate.execute(value);
break;
}
// else ignore (is processing)
};
}
private class PreviewActionCell extends ActionCell<TransUnitReplaceInfo>
{
private Delegate<TransUnitReplaceInfo> previewDelegate;
private final SafeHtml fetchingPreviewHtml;
private final SafeHtml hidePreviewHtml;
public PreviewActionCell(Delegate<TransUnitReplaceInfo> previewDelegate) {
super(SafeHtmlUtils.fromString(messages.fetchPreview()), previewDelegate);
this.previewDelegate = previewDelegate;
fetchingPreviewHtml = buildProcessingIndicator(messages.fetchingPreview());
hidePreviewHtml = buildButtonHtml("", messages.hidePreview());
}
@Override
public void render(com.google.gwt.cell.client.Cell.Context context, TransUnitReplaceInfo value, SafeHtmlBuilder sb)
{
switch (value.getPreviewState())
{
case NotFetched:
case Hide:
super.render(context, value, sb);
break;
case Fetching:
sb.append(fetchingPreviewHtml);
break;
case Show:
sb.append(hidePreviewHtml);
break;
case NotAllowed:
// empty cell
break;
}
}
@Override
protected void onEnterKeyDown(Context context, Element parent, TransUnitReplaceInfo value, NativeEvent event, ValueUpdater<TransUnitReplaceInfo> valueUpdater) {
// delegate is responsible for taking appropriate action
previewDelegate.execute(value);
};
}
private static SafeHtml buildButtonHtml(String message, String buttonLabel)
{
return buildButtonHtml(message, buttonLabel, "", true);
}
private static SafeHtml buildButtonHtml(String message, String buttonLabel, String title, boolean enabled)
{
SafeHtmlBuilder sb = new SafeHtmlBuilder()
.appendHtmlConstant(message)
.appendHtmlConstant("<button type=\"button\" tabindex=\"-1\"");
if (!enabled)
{
sb.appendHtmlConstant(" title=\"")
.appendEscaped(title)
.appendHtmlConstant("\" disabled=\"disabled\"");
}
return sb
.appendHtmlConstant(">")
.appendEscaped(buttonLabel)
.appendHtmlConstant("</button>")
.toSafeHtml();
}
private static SafeHtml buildProcessingIndicator(String message)
{
return new SafeHtmlBuilder()
.append(spinner)
.appendHtmlConstant("<br/>")
.appendHtmlConstant(message)
.toSafeHtml();
}
private static DefaultSelectionEventManager<TransUnitReplaceInfo> getSelectionManager()
{
if (selectionManager == null)
{
selectionManager = buildSelectionManager();
}
return selectionManager;
}
/**
* build a selection manager that toggles selection when the row is clicked,
* but suppresses selection when clicks are in the action button column to
* prevent undesired selections when using action buttons
*
* @return the selection manager
*/
private static DefaultSelectionEventManager<TransUnitReplaceInfo> buildSelectionManager()
{
BlacklistEventTranslator<TransUnitReplaceInfo> selectionEventTranslator = new BlacklistEventTranslator<TransUnitReplaceInfo>()
{
@Override
public SelectAction translateSelectionEvent(CellPreviewEvent<TransUnitReplaceInfo> event)
{
return isColumnBlacklisted(event.getColumn()) ? SelectAction.IGNORE : SelectAction.TOGGLE;
}
};
selectionEventTranslator.setColumnBlacklisted(PREVIEW_COLUMN_INDEX, true);
selectionEventTranslator.setColumnBlacklisted(REPLACE_COLUMN_INDEX, true);
return DefaultSelectionEventManager.<TransUnitReplaceInfo> createCustomManager(selectionEventTranslator);
}
private static CellTableResources getCellTableResources()
{
if (cellTableResources == null)
{
cellTableResources = GWT.create(CellTableResources.class);
}
return cellTableResources;
}
private static void appendContent(SafeHtmlBuilder sb, String content)
{
sb.appendHtmlConstant("<div class='translationContainer' style='border-bottom: dotted 1px grey;'>").appendHtmlConstant(content).appendHtmlConstant("</div>");
}
private static Collection<String> notEmptyContents(List<String> contents)
{
return Collections2.filter(contents, new Predicate<String>()
{
@Override
public boolean apply(String input)
{
return !Strings.isNullOrEmpty(input);
}
});
}
}