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