1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.navigation.key.polytomous.e4;
|
5
|
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
|
9
|
import javax.annotation.PostConstruct;
|
10
|
import javax.annotation.PreDestroy;
|
11
|
import javax.inject.Inject;
|
12
|
|
13
|
import org.eclipse.core.commands.Command;
|
14
|
import org.eclipse.core.commands.ParameterizedCommand;
|
15
|
import org.eclipse.core.runtime.IProgressMonitor;
|
16
|
import org.eclipse.e4.core.commands.ECommandService;
|
17
|
import org.eclipse.e4.core.commands.EHandlerService;
|
18
|
import org.eclipse.e4.ui.di.Focus;
|
19
|
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
|
20
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
21
|
import org.eclipse.e4.ui.services.EMenuService;
|
22
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
23
|
import org.eclipse.jface.viewers.ColumnLabelProvider;
|
24
|
import org.eclipse.jface.viewers.DoubleClickEvent;
|
25
|
import org.eclipse.jface.viewers.IDoubleClickListener;
|
26
|
import org.eclipse.jface.viewers.ISelection;
|
27
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
28
|
import org.eclipse.jface.viewers.ListViewer;
|
29
|
import org.eclipse.jface.viewers.Viewer;
|
30
|
import org.eclipse.jface.viewers.ViewerFilter;
|
31
|
import org.eclipse.swt.SWT;
|
32
|
import org.eclipse.swt.events.ModifyEvent;
|
33
|
import org.eclipse.swt.events.ModifyListener;
|
34
|
import org.eclipse.swt.layout.GridData;
|
35
|
import org.eclipse.swt.layout.GridLayout;
|
36
|
import org.eclipse.swt.widgets.Composite;
|
37
|
import org.eclipse.swt.widgets.Display;
|
38
|
import org.eclipse.swt.widgets.Event;
|
39
|
import org.eclipse.swt.widgets.Text;
|
40
|
import org.eclipse.ui.IMemento;
|
41
|
|
42
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
43
|
import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
|
44
|
import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
|
45
|
import eu.etaxonomy.cdm.api.application.ICdmChangeListener;
|
46
|
import eu.etaxonomy.cdm.api.application.ICdmDataChangeService;
|
47
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
48
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
49
|
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
|
50
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
51
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
52
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
53
|
import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
|
54
|
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
|
55
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
56
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
57
|
import eu.etaxonomy.taxeditor.navigation.key.polytomous.PolytomousKeyContentProvider;
|
58
|
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
|
59
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
60
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
61
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
62
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
63
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
64
|
|
65
|
/**
|
66
|
*
|
67
|
* @author pplitzner
|
68
|
* @since Sep 28, 2017
|
69
|
*
|
70
|
*/
|
71
|
public class PolytomousKeyViewPartE4 implements
|
72
|
IConversationEnabled, ICdmEntitySessionEnabled, IPostOperationEnabled,
|
73
|
ICdmChangeListener {
|
74
|
|
75
|
private class FilterModifyListener implements ModifyListener{
|
76
|
@Override
|
77
|
public void modifyText(ModifyEvent e) {
|
78
|
ViewerFilter filter = new ViewerFilter(){
|
79
|
|
80
|
@Override
|
81
|
public boolean select(Viewer viewer, Object parentElement,
|
82
|
Object element) {
|
83
|
|
84
|
if(element instanceof PolytomousKey){
|
85
|
PolytomousKey key = (PolytomousKey) element;
|
86
|
if(key.getTitleCache().contains(text_filter.getText())){
|
87
|
return true;
|
88
|
}
|
89
|
}
|
90
|
|
91
|
return false;
|
92
|
}
|
93
|
|
94
|
};
|
95
|
|
96
|
viewer.setFilters(new ViewerFilter[]{filter});
|
97
|
}
|
98
|
}
|
99
|
|
100
|
|
101
|
public static final String ID = "eu.etaxonomy.taxeditor.navigation.key.polytomous.polytomousKeyViewPart"; //$NON-NLS-1$
|
102
|
|
103
|
public static final String OPEN_COMMAND_ID = "eu.etaxonomy.taxeditor.navigation.key.polytomous.editNodes"; //$NON-NLS-1$
|
104
|
|
105
|
private class ContextListener extends ContextListenerAdapter{
|
106
|
@Override
|
107
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
108
|
monitor.subTask(Messages.PolytomousKeyViewPart_SHUTDOWN);
|
109
|
if(!viewer.getControl().isDisposed()){
|
110
|
viewer.setInput(null);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
116
|
monitor.subTask(Messages.PolytomousKeyViewPart_INIT);
|
117
|
setInput();
|
118
|
}
|
119
|
}
|
120
|
|
121
|
private ListViewer viewer;
|
122
|
private ConversationHolder conversation;
|
123
|
private IContextListener contextListener;
|
124
|
private final CdmFormFactory formFactory;
|
125
|
private Text text_filter;
|
126
|
private ICdmEntitySession cdmEntitySession;
|
127
|
|
128
|
private PolytomousKeyViewPartDataChangeBehaviorE4 dataChangeBehavior;
|
129
|
|
130
|
|
131
|
@Inject
|
132
|
private EHandlerService handlerService;
|
133
|
|
134
|
@Inject
|
135
|
private ECommandService commandService;
|
136
|
|
137
|
@Inject
|
138
|
private ESelectionService selService;
|
139
|
|
140
|
@Inject
|
141
|
private MDirtyable dirty;
|
142
|
|
143
|
private ISelectionChangedListener selectionChangedListener;
|
144
|
|
145
|
@Inject
|
146
|
private MPart thisPart;
|
147
|
|
148
|
public PolytomousKeyViewPartE4(){
|
149
|
formFactory = new CdmFormFactory(Display.getDefault());
|
150
|
}
|
151
|
|
152
|
@PostConstruct
|
153
|
public void createPartControl(Composite parent, EMenuService menuService) {
|
154
|
|
155
|
Composite container = new Composite(parent, SWT.NONE);
|
156
|
container.setLayout(new GridLayout());
|
157
|
|
158
|
contextListener = new ContextListener();
|
159
|
CdmStore.getContextManager().addContextListener(contextListener);
|
160
|
|
161
|
text_filter = formFactory.createText(container, ""); //$NON-NLS-1$
|
162
|
text_filter.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
|
163
|
text_filter.addModifyListener(new FilterModifyListener());
|
164
|
|
165
|
viewer = new ListViewer(container, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
|
166
|
viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
167
|
|
168
|
viewer.setContentProvider(new PolytomousKeyContentProvider());
|
169
|
viewer.setLabelProvider(new ColumnLabelProvider());
|
170
|
|
171
|
viewer.addDoubleClickListener(new IDoubleClickListener() {
|
172
|
|
173
|
@Override
|
174
|
public void doubleClick(DoubleClickEvent event) {
|
175
|
openSelectedKeyNodes();
|
176
|
}
|
177
|
});
|
178
|
|
179
|
|
180
|
|
181
|
//propagate selection
|
182
|
selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
|
183
|
viewer.addSelectionChangedListener(selectionChangedListener);
|
184
|
|
185
|
//create context menu
|
186
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.navigation.popupmenu.polytomouskeyview");
|
187
|
|
188
|
if(CdmStore.isActive()){
|
189
|
setInput();
|
190
|
}
|
191
|
}
|
192
|
|
193
|
public void openSelectedKeyNodes() {
|
194
|
Command command = commandService.getCommand(OPEN_COMMAND_ID);
|
195
|
ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, null);
|
196
|
if(command.isEnabled()) {
|
197
|
try {
|
198
|
handlerService.executeHandler(parameterizedCommand);
|
199
|
} catch (Exception e) {
|
200
|
MessagingUtils.error(getClass(), Messages.PolytomousKeyViewPart_EXCEPTION, e);
|
201
|
}
|
202
|
}
|
203
|
}
|
204
|
|
205
|
public void setViewerSelection(ISelection selection, boolean reveal) {
|
206
|
viewer.setSelection(selection,reveal);
|
207
|
viewer.getList().notifyListeners(SWT.Selection,new Event());
|
208
|
}
|
209
|
|
210
|
private void setInput() {
|
211
|
conversation = CdmStore.createConversation();
|
212
|
conversation.registerForDataStoreChanges(this);
|
213
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
214
|
CdmApplicationState.getCurrentDataChangeService().register(this);
|
215
|
|
216
|
List<PolytomousKey> input = CdmStore.getService(IPolytomousKeyService.class).list(PolytomousKey.class, null, null, null, null);
|
217
|
if(!viewer.getControl().isDisposed()){
|
218
|
viewer.setInput(input);
|
219
|
}
|
220
|
}
|
221
|
|
222
|
@Focus
|
223
|
public void setFocus() {
|
224
|
if(cdmEntitySession != null) {
|
225
|
cdmEntitySession.bind();
|
226
|
}
|
227
|
if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()) {
|
228
|
viewer.getControl().setFocus();
|
229
|
}
|
230
|
}
|
231
|
|
232
|
@Override
|
233
|
public void update(CdmDataChangeMap changeEvents) {
|
234
|
if (dataChangeBehavior == null) {
|
235
|
dataChangeBehavior = new PolytomousKeyViewPartDataChangeBehaviorE4(this);
|
236
|
}
|
237
|
|
238
|
DataChangeBridge.handleDataChange(changeEvents, dataChangeBehavior);
|
239
|
}
|
240
|
|
241
|
@Override
|
242
|
public ConversationHolder getConversationHolder() {
|
243
|
return conversation;
|
244
|
}
|
245
|
|
246
|
@PreDestroy
|
247
|
public void dispose() {
|
248
|
if(conversation!=null){
|
249
|
conversation.unregisterForDataStoreChanges(this);
|
250
|
conversation.close();
|
251
|
CdmStore.getContextManager().removeContextListener(contextListener);
|
252
|
}
|
253
|
if(cdmEntitySession != null) {
|
254
|
cdmEntitySession.dispose();
|
255
|
}
|
256
|
ICdmDataChangeService currentDataChangeService = CdmApplicationState.getCurrentDataChangeService();
|
257
|
if(currentDataChangeService!=null){
|
258
|
currentDataChangeService.unregister(this);
|
259
|
}
|
260
|
}
|
261
|
|
262
|
@Override
|
263
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
264
|
getConversationHolder().bind();
|
265
|
getConversationHolder().commit(true);
|
266
|
viewer.refresh();
|
267
|
return true;
|
268
|
}
|
269
|
|
270
|
@Override
|
271
|
public boolean onComplete() {
|
272
|
return true;
|
273
|
}
|
274
|
|
275
|
public void refresh() {
|
276
|
getConversationHolder().bind();
|
277
|
//FIXME : Need to make sure this is a stable fix (ticket 3822)
|
278
|
getConversationHolder().commit();
|
279
|
List<PolytomousKey> input = CdmStore.getService(IPolytomousKeyService.class).list(PolytomousKey.class, null, null, null, null);
|
280
|
if(!viewer.getControl().isDisposed()){
|
281
|
viewer.setInput(input);
|
282
|
}
|
283
|
}
|
284
|
|
285
|
public List<PolytomousKey> getKeys() {
|
286
|
return (List<PolytomousKey>)viewer.getInput();
|
287
|
}
|
288
|
|
289
|
@Override
|
290
|
public ICdmEntitySession getCdmEntitySession() {
|
291
|
return cdmEntitySession;
|
292
|
}
|
293
|
|
294
|
@Override
|
295
|
public List<PolytomousKey> getRootEntities() {
|
296
|
return getKeys();
|
297
|
}
|
298
|
|
299
|
@Override
|
300
|
public void onChange(CdmChangeEvent event) {
|
301
|
if(event.getAction() == Action.Delete && PolytomousKey.class.equals(event.getEntityType())) {
|
302
|
refresh();
|
303
|
}
|
304
|
|
305
|
}
|
306
|
|
307
|
@Override
|
308
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
309
|
return null;
|
310
|
}
|
311
|
|
312
|
}
|