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