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
/
SearchResultsPresenter.java
625 lines (535 loc) · 22.4 KB
/
SearchResultsPresenter.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
* 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.presenter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.events.NotificationEvent.Severity;
import org.zanata.webtrans.client.events.TransUnitUpdatedEvent;
import org.zanata.webtrans.client.events.TransUnitUpdatedEventHandler;
import org.zanata.webtrans.client.history.History;
import org.zanata.webtrans.client.history.HistoryToken;
import org.zanata.webtrans.client.rpc.CachingDispatchAsync;
import org.zanata.webtrans.shared.model.TransUnit;
import org.zanata.webtrans.shared.model.TransUnitUpdateInfo;
import org.zanata.webtrans.shared.rpc.GetProjectTransUnitLists;
import org.zanata.webtrans.shared.rpc.GetProjectTransUnitListsResult;
import org.zanata.webtrans.shared.rpc.ReplaceText;
import org.zanata.webtrans.shared.rpc.UpdateTransUnitResult;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.cell.client.ActionCell.Delegate;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionModel;
import com.google.inject.Inject;
/**
* View for project-wide search and replace within textflow targets
*
* @author David Mason, damason@redhat.com
*/
public class SearchResultsPresenter extends WidgetPresenter<SearchResultsPresenter.Display>
{
public interface Display extends WidgetDisplay
{
HasText getTestLabel();
/**
* Set the string that will be highlighted in target content.
* Set to null or empty string to disable highlight
*
* @param highlightString
*/
void setHighlightString(String highlightString);
HasValue<String> getFilterTextBox();
HasValue<String> getReplacementTextBox();
HasValue<Boolean> getCaseSensitiveChk();
HasChangeHandlers getSearchFieldSelector();
String getSelectedSearchField();
HasClickHandlers getReplaceAllButton();
void clearAll();
HasClickHandlers addDocumentLabel(String docName);
HasData<TransUnitReplaceInfo> addTUTable(Delegate<TransUnitReplaceInfo> replaceDelegate, Delegate<TransUnitReplaceInfo> undoDelegate, SelectionModel<TransUnitReplaceInfo> selectionModel, ValueChangeHandler<Boolean> selectAllHandler);
}
private final CachingDispatchAsync dispatcher;
private final History history;
private AsyncCallback<GetProjectTransUnitListsResult> projectSearchCallback;
private Delegate<TransUnitReplaceInfo> replaceButtonDelegate;
private Delegate<TransUnitReplaceInfo> undoButtonDelegate;
private Comparator<TransUnitReplaceInfo> tuInfoComparator;
/**
* Model objects for tables in display. Changes to these are reflected in the
* view.
*/
private Map<Long, ListDataProvider<TransUnitReplaceInfo>> documentDataProviders;
/**
* Selection model objects for tables in display. Used to determine which
* transunits are selected
*/
private Map<Long, MultiSelectionModel<TransUnitReplaceInfo>> documentSelectionModels;
/**
* most recent history state that was responded to
*/
private HistoryToken currentHistoryState = null;
@Inject
public SearchResultsPresenter(Display display, EventBus eventBus, CachingDispatchAsync dispatcher, History history)
{
super(display, eventBus);
this.history = history;
this.dispatcher = dispatcher;
}
@Override
protected void onBind()
{
projectSearchCallback = buildProjectSearchCallback();
replaceButtonDelegate = buildReplaceButtonDelegate();
undoButtonDelegate = buildUndoButtonDelegate();
documentDataProviders = new HashMap<Long, ListDataProvider<TransUnitReplaceInfo>>();
documentSelectionModels = new HashMap<Long, MultiSelectionModel<TransUnitReplaceInfo>>();
tuInfoComparator = buildTransUnitReplaceInfoComparator();
registerHandler(display.getFilterTextBox().addValueChangeHandler(new ValueChangeHandler<String>()
{
@Override
public void onValueChange(ValueChangeEvent<String> event)
{
HistoryToken token = HistoryToken.fromTokenString(history.getToken());
if (!event.getValue().equals(token.getProjectSearchText()))
{
token.setProjectSearchText(event.getValue());
history.newItem(token.toTokenString());
}
}
}));
registerHandler(display.getReplacementTextBox().addValueChangeHandler(new ValueChangeHandler<String>()
{
@Override
public void onValueChange(ValueChangeEvent<String> event)
{
HistoryToken token = HistoryToken.fromTokenString(history.getToken());
if (!event.getValue().equals(token.getProjectSearchReplacement()))
{
token.setProjectSearchReplacement(event.getValue());
history.newItem(token.toTokenString());
}
}
}));
registerHandler(display.getCaseSensitiveChk().addValueChangeHandler(new ValueChangeHandler<Boolean>()
{
@Override
public void onValueChange(ValueChangeEvent<Boolean> event)
{
HistoryToken token = HistoryToken.fromTokenString(history.getToken());
if (event.getValue() != token.getProjectSearchCaseSensitive())
{
token.setProjectSearchCaseSensitive(event.getValue());
history.newItem(token.toTokenString());
}
}
}));
registerHandler(display.getSearchFieldSelector().addChangeHandler(new ChangeHandler()
{
@Override
public void onChange(ChangeEvent event)
{
HistoryToken token = HistoryToken.fromTokenString(history.getToken());
String selected = display.getSelectedSearchField();
boolean searchSource = selected.equals("source") || selected.equals("both");
boolean searchTarget = selected.equals("target") || selected.equals("both");
token.setProjectSearchInSource(searchSource);
token.setProjectSearchInTarget(searchTarget);
history.newItem(token.toTokenString());
}
}));
registerHandler(display.getReplaceAllButton().addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
List<TransUnit> selected = new ArrayList<TransUnit>();
for (MultiSelectionModel<TransUnitReplaceInfo> sm : documentSelectionModels.values())
{
for (TransUnitReplaceInfo info : sm.getSelectedSet())
{
selected.add(info.getTransUnit());
}
}
if (selected.isEmpty())
{
eventBus.fireEvent(new NotificationEvent(Severity.Warning, "Nothing selected"));
}
else
{
fireReplaceTextEvent(selected);
}
}
}));
history.addValueChangeHandler(new ValueChangeHandler<String>()
{
@Override
public void onValueChange(ValueChangeEvent<String> event)
{
if (currentHistoryState == null)
currentHistoryState = new HistoryToken(); // default values
HistoryToken token = HistoryToken.fromTokenString(event.getValue());
boolean caseSensitivityChanged = token.getProjectSearchCaseSensitive() != currentHistoryState.getProjectSearchCaseSensitive();
boolean searchTextChanged = !token.getProjectSearchText().equals(currentHistoryState.getProjectSearchText());
boolean searchFieldsChanged = token.isProjectSearchInSource() != currentHistoryState.isProjectSearchInSource();
searchFieldsChanged |= token.isProjectSearchInTarget() != currentHistoryState.isProjectSearchInTarget();
if (caseSensitivityChanged || searchTextChanged || searchFieldsChanged)
{
display.setHighlightString(token.getProjectSearchText());
display.getFilterTextBox().setValue(token.getProjectSearchText(), true);
display.getCaseSensitiveChk().setValue(token.getProjectSearchCaseSensitive(), false);
documentDataProviders.clear();
documentSelectionModels.clear();
display.clearAll();
if (!token.getProjectSearchText().isEmpty())
{
//TODO show loading indicator
dispatcher.execute(new GetProjectTransUnitLists(token.getProjectSearchText(), token.isProjectSearchInSource(), token.isProjectSearchInTarget(), token.getProjectSearchCaseSensitive()), projectSearchCallback);
}
}
if (!token.getProjectSearchReplacement().equals(currentHistoryState.getProjectSearchReplacement()))
{
display.getReplacementTextBox().setValue(token.getProjectSearchReplacement(), true);
}
currentHistoryState = token;
}
});
registerHandler(eventBus.addHandler(TransUnitUpdatedEvent.getType(), new TransUnitUpdatedEventHandler() {
@Override
public void onTransUnitUpdated(final TransUnitUpdatedEvent event)
{
TransUnitUpdateInfo updateInfo = event.getUpdateInfo();
TransUnitReplaceInfo replaceInfo = getReplaceInfoForUpdatedTU(updateInfo);
if (replaceInfo == null)
{
Log.debug("no matching TU in document for TU update, id: " + updateInfo.getTransUnit().getId().getId());
return;
}
Log.debug("found matching TU for TU update, id: " + updateInfo.getTransUnit().getId().getId());
MultiSelectionModel<TransUnitReplaceInfo> selectionModel = documentSelectionModels.get(updateInfo.getDocumentId().getId());
if (selectionModel == null)
{
Log.error("missing selection model for document, id: " + updateInfo.getDocumentId().getId());
}
else
{
// no need to do this as replaceInfo only has properties changed
// safe to remove if desired behaviour is keeping selected
selectionModel.setSelected(replaceInfo, false);
}
if (replaceInfo.isUndoable() && replaceInfo.getTransUnit().getVerNum() != updateInfo.getTransUnit().getVerNum())
{
// can't undo after new update
replaceInfo.setUndoable(false);
replaceInfo.setReplaceable(true);
replaceInfo.setReplacing(false);
replaceInfo.setReplaceInfo(null);
}
replaceInfo.setTransUnit(updateInfo.getTransUnit());
// force table refresh as property changes are not detected
ListDataProvider<TransUnitReplaceInfo> dataProvider = documentDataProviders.get(updateInfo.getDocumentId().getId());
if (dataProvider != null)
{
dataProvider.refresh();
}
}
}));
}
private void showDocInEditor(String doc)
{
HistoryToken token = HistoryToken.fromTokenString(history.getToken());
token.setDocumentPath(doc);
token.setView(MainView.Editor);
history.newItem(token.toTokenString());
}
@Override
protected void onUnbind()
{
}
@Override
public void onRevealDisplay()
{
}
private AsyncCallback<GetProjectTransUnitListsResult> buildProjectSearchCallback()
{
return new AsyncCallback<GetProjectTransUnitListsResult>() {
@Override
public void onFailure(Throwable caught)
{
Log.error("[SearchResultsPresenter] failed project-wide search request: " + caught.getMessage());
eventBus.fireEvent(new NotificationEvent(Severity.Error, "Project-wide search failed"));
display.clearAll();
display.getTestLabel().setText("Project TU search failed");
}
@Override
public void onSuccess(GetProjectTransUnitListsResult result)
{
display.getTestLabel().setText("Project TU search returned documents: " + result.getDocumentIds().size());
//TODO extract clearAllData function for these 3 lines
documentDataProviders.clear();
documentSelectionModels.clear();
display.clearAll();
for (Long docId : result.getDocumentIds())
{
final String doc = result.getDocPath(docId);
HasClickHandlers docLabel = display.addDocumentLabel(doc);
docLabel.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event)
{
showDocInEditor(doc);
}
});
final MultiSelectionModel<TransUnitReplaceInfo> selectionModel = new MultiSelectionModel<TransUnitReplaceInfo>();
final ListDataProvider<TransUnitReplaceInfo> dataProvider = new ListDataProvider<TransUnitReplaceInfo>();
// TODO set selected value for header based on selection of rows?
HasData<TransUnitReplaceInfo> table = display.addTUTable(replaceButtonDelegate, undoButtonDelegate, selectionModel, new ValueChangeHandler<Boolean>()
{
@Override
public void onValueChange(ValueChangeEvent<Boolean> event)
{
if (event.getValue())
{
for (TransUnitReplaceInfo info : dataProvider.getList())
{
selectionModel.setSelected(info, true);
}
}
else
{
selectionModel.clear();
}
}
});
dataProvider.addDataDisplay(table);
List<TransUnitReplaceInfo> data = dataProvider.getList();
for (TransUnit tu : result.getUnits(docId))
{
data.add(new TransUnitReplaceInfo(tu));
}
Collections.sort(data, tuInfoComparator);
documentDataProviders.put(docId, dataProvider);
documentSelectionModels.put(docId, selectionModel);
}
}
};
}
private Delegate<TransUnitReplaceInfo> buildReplaceButtonDelegate()
{
return new Delegate<TransUnitReplaceInfo>() {
@Override
public void execute(TransUnitReplaceInfo tu)
{
fireReplaceTextEvent(Collections.singletonList(tu.getTransUnit()));
}
};
}
private Delegate<TransUnitReplaceInfo> buildUndoButtonDelegate()
{
return new Delegate<TransUnitReplaceInfo>() {
@Override
public void execute(TransUnitReplaceInfo info)
{
eventBus.fireEvent(new NotificationEvent(Severity.Error, "Undo not implemented"));
// FIXME fire this when it exists
// fireUndoUpdateEvent(Collections.singletonList(tu));
}
};
}
/**
* @param toReplace list of TransUnits to replace
*/
private void fireReplaceTextEvent(List<TransUnit> toReplace)
{
// TODO set info to replacing... before calling this event
String searchText = currentHistoryState.getProjectSearchText();
String replacement = currentHistoryState.getProjectSearchReplacement();
boolean caseSensitive = currentHistoryState.getProjectSearchCaseSensitive();
ReplaceText action = new ReplaceText(toReplace, searchText, replacement, caseSensitive);
dispatcher.execute(action, new AsyncCallback<UpdateTransUnitResult>()
{
@Override
public void onFailure(Throwable e)
{
Log.error("[SearchResultsPresenter] Replace text failure " + e, e);
// TODO use localised string
eventBus.fireEvent(new NotificationEvent(Severity.Error, "Replace text failed"));
// TODO consider whether possible/desired to change TU state from 'replacing'
}
@Override
public void onSuccess(UpdateTransUnitResult result)
{
Set<Long> updatedDocs = new HashSet<Long>();
for (TransUnitUpdateInfo updateInfo : result.getUpdateInfoList())
{
if (updateInfo.isSuccess())
{
TransUnitReplaceInfo replaceInfo = getReplaceInfoForUpdatedTU(updateInfo);
if (replaceInfo != null)
{
replaceInfo.setReplaceInfo(updateInfo);
replaceInfo.setUndoable(true);
replaceInfo.setReplaceable(false);
replaceInfo.setReplacing(false);
//this should be done when the TU update event comes in anyway
//may want to remove this
replaceInfo.setTransUnit(updateInfo.getTransUnit());
}
updatedDocs.add(updateInfo.getDocumentId().getId());
}
// individual failure behaviour not yet defined
}
// force table refresh as property changes are not detected
for (Long docId : updatedDocs)
{
ListDataProvider<TransUnitReplaceInfo> dataProvider = documentDataProviders.get(docId);
if (dataProvider != null)
{
dataProvider.refresh();
}
}
// TODO use localised string
eventBus.fireEvent(new NotificationEvent(Severity.Info, "Successfully replaced text"));
}
});
}
public class TransUnitReplaceInfo
{
private TransUnit tu;
private TransUnitUpdateInfo replaceInfo = null;
private boolean replaceable;
private boolean replacing;
private boolean undoable;
public TransUnitReplaceInfo(TransUnit tu)
{
this.tu = tu;
replaceInfo = null;
replaceable = true;
replacing = false;
undoable = false;
}
public TransUnit getTransUnit()
{
return tu;
}
public void setTransUnit(TransUnit tu)
{
this.tu = tu;
}
public TransUnitUpdateInfo getReplaceInfo()
{
return replaceInfo;
}
public void setReplaceInfo(TransUnitUpdateInfo replaceInfo)
{
this.replaceInfo = replaceInfo;
}
public boolean isReplaceable()
{
return replaceable;
}
public void setReplaceable(boolean canReplace)
{
this.replaceable = canReplace;
}
public boolean isReplacing()
{
return replacing;
}
public void setReplacing(boolean isReplacing)
{
this.replacing = isReplacing;
}
public boolean isUndoable()
{
return undoable;
}
public void setUndoable(boolean undoable)
{
this.undoable = undoable;
}
}
private Comparator<TransUnitReplaceInfo> buildTransUnitReplaceInfoComparator()
{
return new Comparator<SearchResultsPresenter.TransUnitReplaceInfo>()
{
@Override
public int compare(TransUnitReplaceInfo o1, TransUnitReplaceInfo o2)
{
if (o1 == o2)
{
return 0;
}
if (o1 != null)
{
return (o2 != null ? Integer.valueOf(o1.getTransUnit().getRowIndex()).compareTo(o2.getTransUnit().getRowIndex()) : 1);
}
return -1;
}
};
}
/**
* @param updateInfo
* @param replaceInfo
* @return the replace info for the updated {@link TransUnit}, or null if the
* current search results do not contain the TransUnit
*/
private TransUnitReplaceInfo getReplaceInfoForUpdatedTU(TransUnitUpdateInfo updateInfo)
{
ListDataProvider<TransUnitReplaceInfo> dataProvider = documentDataProviders.get(updateInfo.getDocumentId().getId());
if (dataProvider == null)
{
Log.debug("document '" + updateInfo.getDocumentId().getId() + "' not found for TU update, id: " + updateInfo.getTransUnit().getId().getId());
return null;
}
List<TransUnitReplaceInfo> replaceInfoList = dataProvider.getList();
//TransUnit does not appear to have .equals(o), so list items are manually compared
for (TransUnitReplaceInfo info : replaceInfoList)
{
if (info.getTransUnit().getId().getId() == updateInfo.getTransUnit().getId().getId())
{
return info;
}
}
return null;
}
}