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