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