-
Notifications
You must be signed in to change notification settings - Fork 51
/
TransFilterPresenter.java
235 lines (207 loc) · 9.82 KB
/
TransFilterPresenter.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
/*
* 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.presenter;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;
import org.zanata.webtrans.client.events.FilterViewEvent;
import org.zanata.webtrans.client.events.FilterViewEventHandler;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.events.UserConfigChangeEvent;
import org.zanata.webtrans.client.events.UserConfigChangeHandler;
import org.zanata.webtrans.client.history.History;
import org.zanata.webtrans.client.history.HistoryToken;
import org.zanata.webtrans.client.keys.ShortcutContext;
import org.zanata.webtrans.client.service.UserOptionsService;
import org.zanata.webtrans.client.view.TransFilterDisplay;
import org.zanata.webtrans.shared.rpc.EditorFilter;
import org.zanata.webtrans.shared.rpc.QueryParser;
import org.zanata.webtrans.shared.ui.UserConfigHolder;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.inject.Inject;
public class TransFilterPresenter extends WidgetPresenter<TransFilterDisplay>
implements TransFilterDisplay.Listener, UserConfigChangeHandler,
FilterViewEventHandler {
private static final String DATE_PATTERN = "yyyy-MM-dd";
private final History history;
private final UserOptionsService userOptionsService;
private final KeyShortcutPresenter keyShortcutPresenter;
@Inject
public TransFilterPresenter(final TransFilterDisplay display,
final EventBus eventBus, final History history,
UserOptionsService userOptionsService,
KeyShortcutPresenter keyShortcutPresenter) {
super(display, eventBus);
display.setListener(this);
this.history = history;
this.userOptionsService = userOptionsService;
this.keyShortcutPresenter = keyShortcutPresenter;
}
@Override
protected void onBind() {
registerHandler(eventBus.addHandler(FilterViewEvent.getType(), this));
registerHandler(eventBus.addHandler(UserConfigChangeEvent.TYPE, this));
display.setOptionsState(userOptionsService.getConfigHolder().getState());
}
public boolean isFocused() {
return display.isFocused();
}
@Override
public void searchTerm(String searchTerm) {
EditorFilter editorFilter = QueryParser.parse(searchTerm);
boolean invalidBefore =
invalidDateFormat(editorFilter.getLastModifiedBefore());
boolean invalidAfter =
invalidDateFormat(editorFilter.getLastModifiedAfter());
if (invalidBefore || invalidAfter) {
eventBus.fireEvent(new NotificationEvent(
NotificationEvent.Severity.Warning,
"Invalid date, expected format yyyy-mm-dd"));
display.selectPartialText(invalidBefore ? editorFilter
.getLastModifiedBefore() : editorFilter
.getLastModifiedAfter());
} else {
HistoryToken newToken = history.getHistoryToken();
populateHistoryTokenForEditorFilter(newToken, editorFilter);
history.newItem(newToken);
}
}
protected static boolean invalidDateFormat(String dateField) {
if (dateField == null) {
return false;
}
try {
DateTimeFormat.getFormat(DATE_PATTERN).parseStrict(
dateField);
} catch (IllegalArgumentException e) {
return true;
}
return false;
}
private static void populateHistoryTokenForEditorFilter(
HistoryToken newToken, EditorFilter editorFilter) {
newToken.setEditorTextSearch(editorFilter.getTextInContent());
newToken.setResId(editorFilter.getResId());
newToken.setChangedBefore(editorFilter.getLastModifiedBefore());
newToken.setChangedAfter(editorFilter.getLastModifiedAfter());
newToken.setLastModifiedBy(editorFilter.getLastModifiedByUser());
newToken.setSourceComment(editorFilter.getSourceComment());
newToken.setTargetComment(editorFilter.getTransComment());
newToken.setMsgContext(editorFilter.getMsgContext());
}
@Override
public void messageFilterOptionChanged(Boolean translatedChkValue,
Boolean fuzzyChkValue, Boolean untranslatedChkValue,
Boolean approvedChkValue, Boolean rejectedChkValue,
Boolean hasErrorChkValue, Boolean mtChkValue) {
UserConfigHolder configHolder = userOptionsService.getConfigHolder();
configHolder.setFilterByTranslated(translatedChkValue);
configHolder.setFilterByFuzzy(fuzzyChkValue);
configHolder.setFilterByUntranslated(untranslatedChkValue);
configHolder.setFilterByApproved(approvedChkValue);
configHolder.setFilterByRejected(rejectedChkValue);
configHolder.setFilterByHasError(hasErrorChkValue);
configHolder.setFilterByMT(mtChkValue);
pushFilterHistory(translatedChkValue, fuzzyChkValue,
untranslatedChkValue, approvedChkValue, rejectedChkValue,
hasErrorChkValue, mtChkValue, null);
}
@Override
public void onSearchFieldFocused(boolean focused) {
keyShortcutPresenter.setContextActive(ShortcutContext.Edit, !focused);
keyShortcutPresenter.setContextActive(ShortcutContext.Navigation,
!focused);
}
@Override
protected void onUnbind() {
}
@Override
public void onRevealDisplay() {
}
@Override
public void onUserConfigChanged(UserConfigChangeEvent event) {
if (event.getView() == MainView.Editor) {
UserConfigHolder.ConfigurationState configurationState =
userOptionsService.getConfigHolder().getState();
updateFilterStates(configurationState.isFilterByTranslated(),
configurationState.isFilterByFuzzy(),
configurationState.isFilterByUntranslated(),
configurationState.isFilterByApproved(),
configurationState.isFilterByRejected(),
configurationState.isFilterByHasError(),
configurationState.isFilterByMT());
pushFilterHistory(configurationState.isFilterByTranslated(),
configurationState.isFilterByFuzzy(),
configurationState.isFilterByUntranslated(),
configurationState.isFilterByApproved(),
configurationState.isFilterByRejected(),
configurationState.isFilterByHasError(),
configurationState.isFilterByMT(), null);
}
}
@Override
public void onFilterView(FilterViewEvent event) {
if (event.isCancelFilter()) {
updateFilterStates(event.isFilterTranslated(),
event.isFilterFuzzy(), event.isFilterUntranslated(),
event.isFilterApproved(), event.isFilterRejected(),
event.isFilterHasError(), event.isFilterMT());
pushFilterHistory(event.isFilterTranslated(),
event.isFilterFuzzy(), event.isFilterUntranslated(),
event.isFilterApproved(), event.isFilterRejected(),
event.isFilterHasError(), event.isFilterMT(),
event.getEditorFilter());
} else {
// this is fired from HistoryEventHandlerService
String searchTerm =
QueryParser.toQueryString(event.getEditorFilter());
display.setSearchTerm(searchTerm);
}
}
public void updateFilterStates(boolean filterByTranslated,
boolean filterByFuzzy, boolean filterByUntranslated,
boolean filterByApproved, boolean filterByRejected,
boolean filterByHasError, boolean filterByMT) {
display.setTranslatedFilter(filterByTranslated);
display.setNeedReviewFilter(filterByFuzzy);
display.setUntranslatedFilter(filterByUntranslated);
display.setApprovedFilter(filterByApproved);
display.setRejectedFilter(filterByRejected);
display.setHasErrorFilter(filterByHasError);
display.setMTFilter(filterByMT);
}
private void pushFilterHistory(boolean filterByTranslated,
boolean filterByFuzzy, boolean filterByUntranslated,
boolean filterByApproved, boolean filterByRejected,
boolean filterByHasError, boolean filterByMT, EditorFilter editorFilter) {
HistoryToken token = history.getHistoryToken();
token.setFilterTranslated(filterByTranslated);
token.setFilterFuzzy(filterByFuzzy);
token.setFilterUntranslated(filterByUntranslated);
token.setFilterApproved(filterByApproved);
token.setFilterRejected(filterByRejected);
token.setFilterHasError(filterByHasError);
token.setFilterMT(filterByMT);
if (editorFilter != null) {
populateHistoryTokenForEditorFilter(token, editorFilter);
}
history.newItem(token);
}
}