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