2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.Comparator
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
19 import java
.util
.Observable
;
20 import java
.util
.Observer
;
22 import java
.util
.UUID
;
24 import org
.eclipse
.core
.runtime
.IAdaptable
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
27 import org
.eclipse
.jface
.viewers
.ISelection
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.TreePath
;
30 import org
.eclipse
.ui
.IMemento
;
31 import org
.eclipse
.ui
.IViewSite
;
32 import org
.eclipse
.ui
.PartInitException
;
33 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
35 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
36 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
;
37 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
.Action
;
38 import eu
.etaxonomy
.cdm
.api
.application
.ICdmChangeListener
;
39 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
40 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
41 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
42 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
43 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNaturalComparator
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByNameComparator
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByRankAndNameComparator
;
49 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
50 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
51 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
52 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
53 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
54 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
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
;
63 * Taxonomic tree implementation using Common Navigator Framework.
70 public class TaxonNavigator
extends CommonNavigator
implements
71 IPostOperationEnabled
, IConversationEnabled
, Observer
,
72 ICdmEntitySessionEnabled
, ICdmChangeListener
{
74 private static final String RESTORING_TAXON_NAVIGATOR
= Messages
.TaxonNavigator_RESTORE
;
78 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
80 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
82 private static final String TREE_PATH
= "treepath"; //$NON-NLS-1$
84 private static final String TREE_PATHS
= "treepaths"; //$NON-NLS-1$
86 private ConversationHolder conversation
;
88 private ICdmEntitySession cdmEntitySession
;
90 private String partNameCache
;
92 private IDataChangeBehavior dataChangeBehavior
;
99 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
103 protected IAdaptable
getInitialInput() {
104 Comparator
<TaxonNode
> comparator
;
105 if (PreferencesUtil
.getSortNodesNaturally()){
106 comparator
= new TaxonNaturalComparator();
107 } else if (PreferencesUtil
.getSortNodesStrictlyAlphabetically()){
108 comparator
= new TaxonNodeByNameComparator();
110 comparator
= new TaxonNodeByRankAndNameComparator();
112 TaxonNodeNavigatorComparator viewerComparator
= new TaxonNodeNavigatorComparator(comparator
);
113 this.getCommonViewer().setComparator(viewerComparator
);
114 setLinkingEnabled(true);
115 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
118 // public void selectionChanged(SelectionChangedEvent arg0) {
119 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
121 // Object firstElement = selection.getFirstElement();
123 // if (!(firstElement instanceof Classification)){
124 // //NavigationUtil.selectInNavigator(firstElement, null);
125 // NavigationUtil.openEditor(firstElement);
131 if (CdmStore
.isActive()) {
133 // TODO when closing and reopening the taxon navigator
134 // we do not preserve state. Closing the view, in contrary to
135 // closing the whole application
136 // should be handled by the state manager too
137 root
= new Root(conversation
);
140 return new EmptyRoot();
145 public void init(IViewSite site
) throws PartInitException
{
157 if (CdmStore
.isActive() && conversation
== null) {
158 conversation
= CdmStore
.createConversation();
159 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
161 if (CdmStore
.isActive()) {
162 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
163 CdmApplicationState
.getCurrentDataChangeService().register(this);
165 CdmStore
.getLoginManager().addObserver(this);
169 * Refresh this navigators viewer
171 public void refresh() {
172 if(getConversationHolder() != null){
173 getConversationHolder().bind();
174 //FIXME : Need to make sure this is a stable fix (ticket 3822)
175 if(!getConversationHolder().isCompleted()){
176 getConversationHolder().commit();
179 if(!getCommonViewer().getTree().isDisposed()){
180 getCommonViewer().refresh();
185 * Refresh this navigators viewer
187 public void refresh(Set
<?
> objects
) {
188 for(Object obj
: objects
) {
189 getCommonViewer().refresh(obj
);
194 * Removes all content
196 public void clear() {
197 getCommonViewer().setInput(new EmptyRoot());
206 * a {@link org.eclipse.ui.IMemento} object.
208 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
210 public void restore(IMemento memento
, IProgressMonitor monitor
) {
211 root
= new Root(conversation
);
212 if (memento
== null) {
213 getCommonViewer().setInput(root
);
217 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
218 IMemento
[] treePathMementos
= null;
220 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
222 if (treePathsMemento
!= null) {
223 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
224 mementoWork
= treePathMementos
.length
;
226 // begin the monitor with steps for all tree paths and steps for
228 // conversation s.o., refreshing the tree and setting the paths
229 IProgressMonitor subProgressMonitor
= NavigationUtil
230 .getSubProgressMonitor(monitor
, 1);
232 subProgressMonitor
.beginTask(RESTORING_TAXON_NAVIGATOR
,
233 1 + mementoWork
+ 5);
234 subProgressMonitor
.subTask(RESTORING_TAXON_NAVIGATOR
);
235 subProgressMonitor
.worked(1);
237 conversation
= CdmStore
.createConversation();
238 subProgressMonitor
.worked(1);
239 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
240 subProgressMonitor
.worked(1);
241 getCommonViewer().setInput(root
);
242 subProgressMonitor
.worked(1);
243 getCommonViewer().refresh();
244 subProgressMonitor
.worked(1);
246 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
247 for (IMemento treePathMemento
: treePathMementos
) {
248 TreePath treePath
= createTreePathFromString(treePathMemento
250 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
251 treePaths
.add(treePath
);
252 subProgressMonitor
.worked(1);
256 if (treePaths
.size() > 0) {
257 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
258 treePaths
.toArray(new TreePath
[0]));
259 subProgressMonitor
.worked(1);
261 subProgressMonitor
.done();
268 private TreePath
createTreePathFromString(String string
) {
270 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
272 if (string
.length() == 0) {
276 for (String uuid
: string
.split(" ")) { //$NON-NLS-1$
277 CdmBase cdmBaseObject
= CdmStore
.getService(
278 IClassificationService
.class).getTaxonNodeByUuid(
279 UUID
.fromString(uuid
));
280 if (cdmBaseObject
== null) {
281 // is this a tree uuid?
282 cdmBaseObject
= CdmStore
.getService(
283 IClassificationService
.class).load(
284 UUID
.fromString(uuid
));
286 if (cdmBaseObject
== null) {
290 pathList
.add(cdmBaseObject
);
292 return new TreePath(pathList
.toArray());
297 public void saveState(IMemento aMemento
) {
307 * a {@link org.eclipse.ui.IMemento} object.
308 * @param progressMonitor
309 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
311 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
312 if (memento
== null) {
315 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
318 super.saveState(memento
);
320 memento
= memento
.createChild(TREE_PATHS
);
321 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
323 monitor
.beginTask(Messages
.TaxonNavigator_SAVE_TASK
, treePaths
.length
);
325 for (TreePath treePath
: treePaths
) {
326 int pathLength
= treePath
.getSegmentCount();
327 String path
= ""; //$NON-NLS-1$
328 for (int i
= 0; i
< pathLength
; i
++) {
329 Object segment
= treePath
.getSegment(i
);
330 if (segment
instanceof CdmBase
) {
331 path
+= ((CdmBase
) segment
).getUuid().toString() + " "; //$NON-NLS-1$
334 MessagingUtils
.warn(getClass(),
335 "Non-taxon tree path segment " + segment
); //$NON-NLS-1$
338 memento
.createChild(TREE_PATH
, path
.trim());
347 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
352 * getConversationHolder
355 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
359 public ConversationHolder
getConversationHolder() {
367 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
368 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
372 public void update(CdmDataChangeMap changeEvents
) {
373 if (dataChangeBehavior
== null) {
374 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
377 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
382 public String
getFrameToolTipText(Object element
) {
383 if (element
instanceof Root
) {
384 return Messages
.TaxonNavigator_TAXON_TREE
;
386 return super.getFrameToolTipText(element
);
392 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
396 public void dispose() {
398 dataChangeBehavior
= null;
399 if (conversation
!= null) {
400 conversation
.unregisterForDataStoreChanges(this);
402 if(cdmEntitySession
!= null) {
403 cdmEntitySession
.dispose();
405 if(CdmApplicationState
.getCurrentDataChangeService() != null) {
406 CdmApplicationState
.getCurrentDataChangeService().unregister(this);
413 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
417 public void setFocus() {
418 // logger.warn("Setting focus to navigator");
420 if (getConversationHolder() != null) {
421 getConversationHolder().bind();
423 if(cdmEntitySession
!= null) {
424 cdmEntitySession
.bind();
432 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
433 * (eu.etaxonomy.cdm.model.common.CdmBase)
437 public boolean postOperation(CdmBase objectAffectedByOperation
) {
438 // nothing to do here
448 * a {@link org.eclipse.ui.IMemento} object.
450 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
452 public void save(IMemento memento
, IProgressMonitor monitor
) {
453 saveTreeState(memento
, monitor
);
454 if (conversation
!= null) {
455 conversation
.unregisterForDataStoreChanges(this);
462 protected void handleDoubleClick(DoubleClickEvent event
) {
463 ISelection selection
= event
.getSelection();
464 if(selection
instanceof IStructuredSelection
){
465 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
466 if(firstElement
instanceof ICdmBase
){
467 NavigationUtil
.openEditor((ICdmBase
) firstElement
);
470 // If the double click is passed up to the super-class it will
471 // expand/collapse trees.
472 // We do not want that
473 // super.handleDoubleClick(anEvent);
484 public boolean onComplete() {
491 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
495 public void showBusy(boolean busy
) {
496 super.showBusy(busy
);
497 getCommonViewer().getControl().setEnabled(!busy
);
499 partNameCache
= getPartName();
500 setPartName(Messages
.TaxonNavigator_LOAD_DATASOURCE
);
502 if (partNameCache
!= null) {
503 setPartName(partNameCache
);
510 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
513 public void update(Observable o
, Object arg
) {
514 if(o
instanceof LoginManager
){
521 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
524 public ICdmEntitySession
getCdmEntitySession() {
525 return cdmEntitySession
;
529 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
532 public List
<Classification
> getRootEntities() {
534 return root
.getParentBeans();
540 * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
543 public void onChange(CdmChangeEvent event
) {
544 if(event
.getAction() == Action
.Delete
&& Classification
.class.equals(event
.getEntityType())) {
548 for(CdmBase cb
: event
.getChangedObjects()) {
549 if(cb
instanceof TaxonNode
) {
550 TaxonNode tn
= (TaxonNode
)cb
;
551 if(tn
.getTaxon() == null) {
552 getCommonViewer().refresh(tn
.getClassification());
554 getCommonViewer().refresh(cb
);
556 } else if (cb
instanceof Classification
) {
557 getCommonViewer().refresh();
564 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
567 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
568 Map
<Object
, List
<String
>> propertyPathsMap
= new HashMap
<Object
, List
<String
>>();
569 List
<String
> taxonNodePropertyPaths
= Arrays
.asList(new String
[] {
570 "taxon.name" //$NON-NLS-1$
572 propertyPathsMap
.put("childNodes", taxonNodePropertyPaths
); //$NON-NLS-1$
573 return propertyPathsMap
;