ea945e1041a8a5fcd966c4ba94a4c54566f3f969
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TaxonNavigator.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.navigation.navigator;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Comparator;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Observable;
21 import java.util.Observer;
22 import java.util.Set;
23 import java.util.UUID;
24
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.jface.viewers.DoubleClickEvent;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.TreePath;
31 import org.eclipse.ui.IMemento;
32 import org.eclipse.ui.IViewSite;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.navigator.CommonNavigator;
35
36 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
37 import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
38 import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
39 import eu.etaxonomy.cdm.api.application.ICdmChangeListener;
40 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
41 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
42 import eu.etaxonomy.cdm.api.service.IClassificationService;
43 import eu.etaxonomy.cdm.model.common.CdmBase;
44 import eu.etaxonomy.cdm.model.common.ICdmBase;
45 import eu.etaxonomy.cdm.model.taxon.Classification;
46 import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
47 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48 import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
49 import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator;
50 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
51 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
52 import eu.etaxonomy.taxeditor.model.IDataChangeBehavior;
53 import eu.etaxonomy.taxeditor.model.MessagingUtils;
54 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
55 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
56 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
57 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
58 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
59 import eu.etaxonomy.taxeditor.store.CdmStore;
60 import eu.etaxonomy.taxeditor.store.LoginManager;
61 import eu.etaxonomy.taxeditor.view.CdmViewerChooser;
62
63 /**
64 * Taxonomic tree implementation using Common Navigator Framework.
65 *
66 * @author p.ciardelli
67 * @author n.hoffmann
68 * @created 02.06.2009
69 * @version 1.0
70 */
71 public class TaxonNavigator extends CommonNavigator implements
72 IPostOperationEnabled, IConversationEnabled, Observer,
73 ICdmEntitySessionEnabled, ICdmChangeListener {
74
75 /**
76 * Constant
77 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
78 */
79 public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
80
81 private static final String TREE_PATH = "treepath";
82
83 private static final String TREE_PATHS = "treepaths";
84
85 private ConversationHolder conversation;
86
87 private ICdmEntitySession cdmEntitySession;
88
89 private String partNameCache;
90
91 private IDataChangeBehavior dataChangeBehavior;
92
93 private Root root;
94
95 /*
96 * (non-Javadoc)
97 *
98 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
99 */
100 /** {@inheritDoc} */
101 @Override
102 protected IAdaptable getInitialInput() {
103 Comparator<TaxonNode> comparator;
104 if (PreferencesUtil.getSortNodesNaturally()){
105 comparator = new TaxonNaturalComparator();
106 } else if (PreferencesUtil.getSortNodesStrictlyAlphabetically()){
107 comparator = new TaxonNodeByNameComparator();
108 }else {
109 comparator = new TaxonNodeByRankAndNameComparator();
110 }
111 TaxonNodeNavigatorComparator viewerComparator = new TaxonNodeNavigatorComparator(comparator);
112 this.getCommonViewer().setComparator(viewerComparator);
113 setLinkingEnabled(true);
114 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
115 //
116 // @Override
117 // public void selectionChanged(SelectionChangedEvent arg0) {
118 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
119 //
120 // Object firstElement = selection.getFirstElement();
121 // //
122 // if (!(firstElement instanceof Classification)){
123 // //NavigationUtil.selectInNavigator(firstElement, null);
124 // NavigationUtil.openEditor(firstElement);
125 // }
126 //
127 // }
128 // } );
129
130 if (CdmStore.isActive()) {
131
132 // TODO when closing and reopening the taxon navigator
133 // we do not preserve state. Closing the view, in contrary to
134 // closing the whole application
135 // should be handled by the state manager too
136 root = new Root(conversation);
137 return root;
138 }
139 return new EmptyRoot();
140 }
141
142 /** {@inheritDoc} */
143 @Override
144 public void init(IViewSite site) throws PartInitException {
145 super.init(site);
146 init();
147 }
148
149 /**
150 * <p>
151 * init
152 * </p>
153 */
154 public void init() {
155
156 if (CdmStore.isActive() && conversation == null) {
157 conversation = CdmStore.createConversation();
158 conversation.registerForDataStoreChanges(TaxonNavigator.this);
159 }
160 if (CdmStore.isActive()) {
161 cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
162 CdmApplicationState.getCurrentDataChangeService().register(this);
163 }
164 CdmStore.getLoginManager().addObserver(this);
165 }
166
167 /**
168 * Refresh this navigators viewer
169 */
170 public void refresh() {
171 if(getConversationHolder() != null){
172 getConversationHolder().bind();
173 //FIXME : Need to make sure this is a stable fix (ticket 3822)
174 if(!getConversationHolder().isCompleted()){
175 getConversationHolder().commit();
176 }
177 }
178 getCommonViewer().refresh();
179 }
180
181 /**
182 * Refresh this navigators viewer
183 */
184 public void refresh(Set objects) {
185 for(Object obj : objects) {
186 getCommonViewer().refresh(obj);
187 }
188 }
189
190 /**
191 * Removes all content
192 */
193 public void clear() {
194 getCommonViewer().setInput(new EmptyRoot());
195 }
196
197 /**
198 * <p>
199 * restore
200 * </p>
201 *
202 * @param memento
203 * a {@link org.eclipse.ui.IMemento} object.
204 * @param monitor
205 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
206 */
207 public void restore(IMemento memento, IProgressMonitor monitor) {
208 root = new Root(conversation);
209 if (memento == null) {
210 getCommonViewer().setInput(root);
211 return;
212 }
213 int mementoWork = 0;
214 Set<TreePath> treePaths = new HashSet<TreePath>();
215 IMemento[] treePathMementos = null;
216
217 IMemento treePathsMemento = memento.getChild(TREE_PATHS);
218
219 if (treePathsMemento != null) {
220 treePathMementos = treePathsMemento.getChildren(TREE_PATH);
221 mementoWork = treePathMementos.length;
222 }
223 // begin the monitor with steps for all tree paths and steps for
224 // creating
225 // conversation s.o., refreshing the tree and setting the paths
226 IProgressMonitor subProgressMonitor = NavigationUtil
227 .getSubProgressMonitor(monitor, 1);
228
229 subProgressMonitor.beginTask("Restoring Taxon Navigator",
230 1 + mementoWork + 5);
231 subProgressMonitor.subTask("Restoring Taxon Navigator");
232 subProgressMonitor.worked(1);
233
234 conversation = CdmStore.createConversation();
235 subProgressMonitor.worked(1);
236 conversation.registerForDataStoreChanges(TaxonNavigator.this);
237 subProgressMonitor.worked(1);
238 getCommonViewer().setInput(root);
239 subProgressMonitor.worked(1);
240 getCommonViewer().refresh();
241 subProgressMonitor.worked(1);
242
243 if (treePathMementos != null && treePathMementos.length > 0) {
244 for (IMemento treePathMemento : treePathMementos) {
245 TreePath treePath = createTreePathFromString(treePathMemento
246 .getID());
247 if (!subProgressMonitor.isCanceled() && treePath != null) {
248 treePaths.add(treePath);
249 subProgressMonitor.worked(1);
250 }
251 }
252 }
253 if (treePaths.size() > 0) {
254 TaxonNavigator.this.getCommonViewer().setExpandedTreePaths(
255 treePaths.toArray(new TreePath[0]));
256 subProgressMonitor.worked(1);
257 }
258 subProgressMonitor.done();
259 }
260
261 /**
262 * @param string
263 * @return
264 */
265 private TreePath createTreePathFromString(String string) {
266
267 List<CdmBase> pathList = new ArrayList<CdmBase>();
268
269 if (string.length() == 0) {
270 return null;
271 }
272
273 for (String uuid : string.split(" ")) {
274 CdmBase cdmBaseObject = CdmStore.getService(
275 IClassificationService.class).getTaxonNodeByUuid(
276 UUID.fromString(uuid));
277 if (cdmBaseObject == null) {
278 // is this a tree uuid?
279 cdmBaseObject = CdmStore.getService(
280 IClassificationService.class).load(
281 UUID.fromString(uuid));
282
283 if (cdmBaseObject == null) {
284 return null;
285 }
286 }
287 pathList.add(cdmBaseObject);
288 }
289 return new TreePath(pathList.toArray());
290 }
291
292 /** {@inheritDoc} */
293 @Override
294 public void saveState(IMemento aMemento) {
295 //
296 }
297
298 /**
299 * <p>
300 * saveTreeState
301 * </p>
302 *
303 * @param memento
304 * a {@link org.eclipse.ui.IMemento} object.
305 * @param progressMonitor
306 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
307 */
308 public void saveTreeState(IMemento memento, IProgressMonitor progressMonitor) {
309 if (memento == null) {
310 return;
311 }
312 IProgressMonitor monitor = NavigationUtil.getSubProgressMonitor(
313 progressMonitor, 1);
314
315 super.saveState(memento);
316
317 memento = memento.createChild(TREE_PATHS);
318 TreePath[] treePaths = this.getCommonViewer().getExpandedTreePaths();
319
320 monitor.beginTask("Saving Taxon Navigator State", treePaths.length);
321
322 for (TreePath treePath : treePaths) {
323 int pathLength = treePath.getSegmentCount();
324 String path = "";
325 for (int i = 0; i < pathLength; i++) {
326 Object segment = treePath.getSegment(i);
327 if (segment instanceof CdmBase) {
328 path += ((CdmBase) segment).getUuid().toString() + " ";
329 monitor.worked(1);
330 } else {
331 MessagingUtils.warn(getClass(),
332 "Non-taxon tree path segment " + segment);
333 }
334 }
335 memento.createChild(TREE_PATH, path.trim());
336 }
337 monitor.done();
338 }
339
340 /*
341 * (non-Javadoc)
342 *
343 * @see
344 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
345 * ()
346 */
347 /**
348 * <p>
349 * getConversationHolder
350 * </p>
351 *
352 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
353 * object.
354 */
355 @Override
356 public ConversationHolder getConversationHolder() {
357 return conversation;
358 }
359
360 /*
361 * (non-Javadoc)
362 *
363 * @see
364 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
365 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
366 */
367 /** {@inheritDoc} */
368 @Override
369 public void update(CdmDataChangeMap changeEvents) {
370 if (dataChangeBehavior == null) {
371 dataChangeBehavior = new TaxonNavigatorDataChangeBehavior(this);
372 }
373
374 DataChangeBridge.handleDataChange(changeEvents, dataChangeBehavior);
375 }
376
377 /** {@inheritDoc} */
378 @Override
379 public String getFrameToolTipText(Object element) {
380 if (element instanceof Root) {
381 return "Taxonomic Tree";
382 }
383 return super.getFrameToolTipText(element);
384 }
385
386 /*
387 * (non-Javadoc)
388 *
389 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
390 */
391 /** {@inheritDoc} */
392 @Override
393 public void dispose() {
394 super.dispose();
395 dataChangeBehavior = null;
396 if (conversation != null) {
397 conversation.unregisterForDataStoreChanges(this);
398 }
399 if(cdmEntitySession != null) {
400 cdmEntitySession.dispose();
401 }
402 if(CdmApplicationState.getCurrentDataChangeService() != null) {
403 CdmApplicationState.getCurrentDataChangeService().unregister(this);
404 }
405 }
406
407 /*
408 * (non-Javadoc)
409 *
410 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
411 */
412 /** {@inheritDoc} */
413 @Override
414 public void setFocus() {
415 // logger.warn("Setting focus to navigator");
416 super.setFocus();
417 if (getConversationHolder() != null) {
418 getConversationHolder().bind();
419 }
420 if(cdmEntitySession != null) {
421 cdmEntitySession.bind();
422 }
423 }
424
425 /*
426 * (non-Javadoc)
427 *
428 * @see
429 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
430 * (eu.etaxonomy.cdm.model.common.CdmBase)
431 */
432 /** {@inheritDoc} */
433 @Override
434 public boolean postOperation(CdmBase objectAffectedByOperation) {
435 // nothing to do here
436 return true;
437 }
438
439 /**
440 * <p>
441 * save
442 * </p>
443 *
444 * @param memento
445 * a {@link org.eclipse.ui.IMemento} object.
446 * @param monitor
447 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
448 */
449 public void save(IMemento memento, IProgressMonitor monitor) {
450 saveTreeState(memento, monitor);
451 if (conversation != null) {
452 conversation.unregisterForDataStoreChanges(this);
453 conversation = null;
454 }
455 }
456
457 /** {@inheritDoc} */
458 @Override
459 protected void handleDoubleClick(DoubleClickEvent event) {
460 ISelection selection = event.getSelection();
461 if(selection instanceof IStructuredSelection){
462 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
463 if(firstElement instanceof ICdmBase){
464 NavigationUtil.openEditor((ICdmBase) firstElement);
465 }
466 }
467 // If the double click is passed up to the super-class it will
468 // expand/collapse trees.
469 // We do not want that
470 // super.handleDoubleClick(anEvent);
471 }
472
473 /**
474 * <p>
475 * onComplete
476 * </p>
477 *
478 * @return a boolean.
479 */
480 @Override
481 public boolean onComplete() {
482 return true;
483 }
484
485 /*
486 * (non-Javadoc)
487 *
488 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
489 */
490 /** {@inheritDoc} */
491 @Override
492 public void showBusy(boolean busy) {
493 super.showBusy(busy);
494 getCommonViewer().getControl().setEnabled(!busy);
495 if (busy) {
496 partNameCache = getPartName();
497 setPartName("Loading datasources");
498 } else {
499 if (partNameCache != null) {
500 setPartName(partNameCache);
501 }
502 }
503 }
504
505
506 /* (non-Javadoc)
507 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
508 */
509 @Override
510 public void update(Observable o, Object arg) {
511 if(o instanceof LoginManager){
512 refresh();
513 }
514
515 }
516
517 /* (non-Javadoc)
518 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
519 */
520 @Override
521 public ICdmEntitySession getCdmEntitySession() {
522 return cdmEntitySession;
523 }
524
525 /* (non-Javadoc)
526 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
527 */
528 @Override
529 public List<Classification> getRootEntities() {
530 if(root != null) {
531 return root.getParentBeans();
532 }
533 return null;
534 }
535
536 /* (non-Javadoc)
537 * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
538 */
539 @Override
540 public void onChange(CdmChangeEvent event) {
541 if(event.getAction() == Action.Delete && Classification.class.equals(event.getEntityType())) {
542 refresh();
543 return;
544 }
545 for(CdmBase cb : event.getChangedObjects()) {
546 if(cb instanceof TaxonNode) {
547 TaxonNode tn = (TaxonNode)cb;
548 if(tn.getTaxon() == null) {
549 getCommonViewer().refresh(tn.getClassification());
550 } else {
551 getCommonViewer().refresh(cb);
552 }
553 } else if (cb instanceof Classification) {
554 getCommonViewer().refresh();
555 }
556 }
557
558 }
559
560 /* (non-Javadoc)
561 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
562 */
563 @Override
564 public Map<Object, List<String>> getPropertyPathsMap() {
565 Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
566 List<String> taxonNodePropertyPaths = Arrays.asList(new String[] {
567 "taxon.name"
568 });
569 propertyPathsMap.put("childNodes", taxonNodePropertyPaths);
570 return propertyPathsMap;
571 }
572 }