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