This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
TranslationHistoryPresenter.java
182 lines (161 loc) · 6.1 KB
/
TranslationHistoryPresenter.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
package org.zanata.webtrans.client.presenter;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;
import org.zanata.webtrans.client.events.CopyDataToEditorEvent;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.events.ReviewCommentEvent;
import org.zanata.webtrans.client.events.ReviewCommentEventHandler;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.rpc.AbstractAsyncCallback;
import org.zanata.webtrans.client.rpc.CachingDispatchAsync;
import org.zanata.webtrans.client.service.GetTransUnitActionContextHolder;
import org.zanata.webtrans.client.ui.TranslationHistoryDisplay;
import org.zanata.webtrans.shared.model.ComparableByDate;
import org.zanata.webtrans.shared.model.ReviewComment;
import org.zanata.webtrans.shared.model.TransHistoryItem;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.rpc.AddReviewCommentAction;
import org.zanata.webtrans.shared.rpc.AddReviewCommentResult;
import org.zanata.webtrans.shared.rpc.GetTranslationHistoryAction;
import org.zanata.webtrans.shared.rpc.GetTranslationHistoryResult;
import com.allen_sauer.gwt.log.client.Log;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.inject.Singleton;
/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Singleton
public class TranslationHistoryPresenter extends WidgetPresenter<TranslationHistoryDisplay>
implements TranslationHistoryDisplay.Listener, ReviewCommentEventHandler
{
private final TranslationHistoryDisplay display;
private final EventBus eventBus;
private final CachingDispatchAsync dispatcher;
private final WebTransMessages messages;
private final GetTransUnitActionContextHolder contextHolder;
private TargetContentsPresenter targetContentsPresenter;
private TransUnitId transUnitId;
private ComparingPair comparingPair = ComparingPair.empty();
@Inject
public TranslationHistoryPresenter(TranslationHistoryDisplay display, EventBus eventBus, CachingDispatchAsync dispatcher, WebTransMessages messages, GetTransUnitActionContextHolder contextHolder)
{
super(display, eventBus);
this.display = display;
this.eventBus = eventBus;
this.dispatcher = dispatcher;
this.messages = messages;
this.contextHolder = contextHolder;
display.setListener(this);
}
@Override
public void onShowReviewComment(ReviewCommentEvent event)
{
showTranslationHistory(event.getTransUnitId());
}
public void showTranslationHistory(final TransUnitId transUnitId)
{
this.transUnitId = transUnitId;
popupAndShowLoading(messages.translationHistory());
dispatcher.execute(new GetTranslationHistoryAction(transUnitId), new AsyncCallback<GetTranslationHistoryResult>()
{
@Override
public void onFailure(Throwable caught)
{
Log.error("failure getting translation history", caught);
eventBus.fireEvent(new NotificationEvent(NotificationEvent.Severity.Error, caught.getMessage()));
display.hide();
}
@Override
public void onSuccess(GetTranslationHistoryResult result)
{
Log.info("get back " + result.getHistoryItems().size() + " trans history for id:" + transUnitId);
displayEntries(result.getLatest(), result.getHistoryItems(), result.getReviewComments());
}
});
}
protected void popupAndShowLoading(String title)
{
comparingPair = ComparingPair.empty();
display.setTitle(title);
display.resetView();
display.center();
}
protected void displayEntries(TransHistoryItem latest, List<TransHistoryItem> otherEntries, List<ReviewComment> reviewComments)
{
List<ComparableByDate> all = Lists.newArrayList();
if (latest != null)
{
//add indicator for latest version
all.add(latest.setOptionalTag(messages.latest()));
List<String> newTargets = targetContentsPresenter.getNewTargets();
if (!Objects.equal(latest.getContents(), newTargets))
{
all.add(new TransHistoryItem(messages.unsaved(), newTargets, null, messages.you(), new Date()));
}
}
all.addAll(otherEntries);
all.addAll(reviewComments);
Collections.sort(all, Collections.reverseOrder());
display.setData(all);
}
@Override
public void addComment(String commentContent)
{
dispatcher.execute(new AddReviewCommentAction(transUnitId, commentContent,
contextHolder.getContext().getDocument().getId()), new AbstractAsyncCallback<AddReviewCommentResult>()
{
@Override
public void onSuccess(AddReviewCommentResult result)
{
display.addCommentToList(result.getComment());
display.clearInput();
}
});
}
@Override
public void copyIntoEditor(List<String> contents)
{
eventBus.fireEvent(new CopyDataToEditorEvent(contents));
}
@Override
public void compareClicked(TransHistoryItem item)
{
comparingPair = comparingPair.tryAddOrRemoveIfExists(item);
if (comparingPair.isFull())
{
display.showDiff(comparingPair.one(), comparingPair.two(), messages.translationHistoryComparison(
comparingPair.one().getVersionNum(), comparingPair.two().getVersionNum()));
}
else
{
display.disableComparison();
}
}
@Override
public boolean isItemInComparison(TransHistoryItem item)
{
return comparingPair.contains(item);
}
@Override
protected void onBind()
{
}
@Override
protected void onUnbind()
{
}
@Override
protected void onRevealDisplay()
{
}
public void setCurrentValueHolder(TargetContentsPresenter targetContentsPresenter)
{
this.targetContentsPresenter = targetContentsPresenter;
}
}