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
.common
.ITreeNode
;
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
.navigation
.l10n
.Messages
;
56 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
57 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
58 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
59 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
60 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
61 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
64 * Taxonomic tree implementation using Common Navigator Framework.
71 public class TaxonNavigator
extends CommonNavigator
implements
72 IPostOperationEnabled
, IConversationEnabled
, Observer
,
73 ICdmEntitySessionEnabled
, ICdmChangeListener
{
75 private static final String RESTORING_TAXON_NAVIGATOR
= Messages
.TaxonNavigator_RESTORE
;
79 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
81 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
83 private static final String TREE_PATH
= "treepath"; //$NON-NLS-1$
85 private static final String TREE_PATHS
= "treepaths"; //$NON-NLS-1$
87 private ConversationHolder conversation
;
89 private ICdmEntitySession cdmEntitySession
;
91 private String partNameCache
;
93 private IDataChangeBehavior dataChangeBehavior
;
100 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
104 protected IAdaptable
getInitialInput() {
105 Comparator
<TaxonNode
> comparator
;
106 if (PreferencesUtil
.getSortNodesNaturally()){
107 comparator
= new TaxonNaturalComparator();
108 } else if (PreferencesUtil
.getSortNodesStrictlyAlphabetically()){
109 comparator
= new TaxonNodeByNameComparator();
111 comparator
= new TaxonNodeByRankAndNameComparator();
113 TaxonNodeNavigatorComparator viewerComparator
= new TaxonNodeNavigatorComparator(comparator
);
114 this.getCommonViewer().setComparator(viewerComparator
);
115 //this.getCommonViewer().setAutoExpandLevel(2);
116 setLinkingEnabled(true);
117 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
120 // public void selectionChanged(SelectionChangedEvent arg0) {
121 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
123 // Object firstElement = selection.getFirstElement();
125 // if (!(firstElement instanceof Classification)){
126 // //NavigationUtil.selectInNavigator(firstElement, null);
127 // NavigationUtil.openEditor(firstElement);
133 if (CdmStore
.isActive()) {
135 // TODO when closing and reopening the taxon navigator
136 // we do not preserve state. Closing the view, in contrary to
137 // closing the whole application
138 // should be handled by the state manager too
139 root
= new Root(conversation
);
143 return new EmptyRoot();
148 public void init(IViewSite site
) throws PartInitException
{
160 if (CdmStore
.isActive() && conversation
== null) {
161 conversation
= CdmStore
.createConversation();
162 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
164 if (CdmStore
.isActive()) {
165 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
166 CdmApplicationState
.getCurrentDataChangeService().register(this);
168 CdmStore
.getLoginManager().addObserver(this);
174 * Refresh this navigators viewer
176 public void refresh() {
177 if(getConversationHolder() != null){
178 getConversationHolder().bind();
179 //FIXME : Need to make sure this is a stable fix (ticket 3822)
180 if(!getConversationHolder().isCompleted()){
181 getConversationHolder().commit();
184 if(!getCommonViewer().getTree().isDisposed()){
185 getCommonViewer().refresh();
190 * Refresh this navigators viewer
192 public void refresh(Set
<?
> objects
) {
193 for(Object obj
: objects
) {
194 getCommonViewer().refresh(obj
);
199 * Removes all content
201 public void clear() {
202 getCommonViewer().setInput(new EmptyRoot());
211 * a {@link org.eclipse.ui.IMemento} object.
213 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
215 public void restore(IMemento memento
, IProgressMonitor monitor
) {
216 root
= new Root(conversation
);
217 if (memento
== null) {
218 getCommonViewer().setInput(root
);
222 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
223 IMemento
[] treePathMementos
= null;
225 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
227 if (treePathsMemento
!= null) {
228 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
229 mementoWork
= treePathMementos
.length
;
231 // begin the monitor with steps for all tree paths and steps for
233 // conversation s.o., refreshing the tree and setting the paths
234 IProgressMonitor subProgressMonitor
= NavigationUtil
235 .getSubProgressMonitor(monitor
, 1);
237 subProgressMonitor
.beginTask(RESTORING_TAXON_NAVIGATOR
,
238 1 + mementoWork
+ 5);
239 subProgressMonitor
.subTask(RESTORING_TAXON_NAVIGATOR
);
240 subProgressMonitor
.worked(1);
242 conversation
= CdmStore
.createConversation();
243 subProgressMonitor
.worked(1);
244 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
245 subProgressMonitor
.worked(1);
246 getCommonViewer().setInput(root
);
247 subProgressMonitor
.worked(1);
248 getCommonViewer().refresh();
249 subProgressMonitor
.worked(1);
251 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
252 for (IMemento treePathMemento
: treePathMementos
) {
253 TreePath treePath
= createTreePathFromString(treePathMemento
255 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
256 treePaths
.add(treePath
);
257 subProgressMonitor
.worked(1);
261 if (treePaths
.size() > 0) {
262 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
263 treePaths
.toArray(new TreePath
[0]));
264 subProgressMonitor
.worked(1);
266 subProgressMonitor
.done();
273 private TreePath
createTreePathFromString(String string
) {
275 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
277 if (string
.length() == 0) {
281 for (String uuid
: string
.split(" ")) { //$NON-NLS-1$
282 CdmBase cdmBaseObject
= CdmStore
.getService(
283 IClassificationService
.class).getTaxonNodeByUuid(
284 UUID
.fromString(uuid
));
285 if (cdmBaseObject
== null) {
286 // is this a tree uuid?
287 cdmBaseObject
= CdmStore
.getService(
288 IClassificationService
.class).load(
289 UUID
.fromString(uuid
));
291 if (cdmBaseObject
== null) {
295 pathList
.add(cdmBaseObject
);
297 return new TreePath(pathList
.toArray());
302 public void saveState(IMemento aMemento
) {
312 * a {@link org.eclipse.ui.IMemento} object.
313 * @param progressMonitor
314 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
316 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
317 if (memento
== null) {
320 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
323 super.saveState(memento
);
325 memento
= memento
.createChild(TREE_PATHS
);
326 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
328 monitor
.beginTask(Messages
.TaxonNavigator_SAVE_TASK
, treePaths
.length
);
330 for (TreePath treePath
: treePaths
) {
331 int pathLength
= treePath
.getSegmentCount();
332 String path
= ""; //$NON-NLS-1$
333 for (int i
= 0; i
< pathLength
; i
++) {
334 Object segment
= treePath
.getSegment(i
);
335 if (segment
instanceof CdmBase
) {
336 path
+= ((CdmBase
) segment
).getUuid().toString() + " "; //$NON-NLS-1$
339 MessagingUtils
.warn(getClass(),
340 "Non-taxon tree path segment " + segment
); //$NON-NLS-1$
343 memento
.createChild(TREE_PATH
, path
.trim());
352 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
357 * getConversationHolder
360 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
364 public ConversationHolder
getConversationHolder() {
372 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
373 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
377 public void update(CdmDataChangeMap changeEvents
) {
378 if (dataChangeBehavior
== null) {
379 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
382 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
387 public String
getFrameToolTipText(Object element
) {
388 if (element
instanceof Root
) {
389 return Messages
.TaxonNavigator_TAXON_TREE
;
391 return super.getFrameToolTipText(element
);
397 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
401 public void dispose() {
403 dataChangeBehavior
= null;
404 if (conversation
!= null) {
405 conversation
.unregisterForDataStoreChanges(this);
407 if(cdmEntitySession
!= null) {
408 cdmEntitySession
.dispose();
410 if(CdmApplicationState
.getCurrentDataChangeService() != null) {
411 CdmApplicationState
.getCurrentDataChangeService().unregister(this);
418 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
422 public void setFocus() {
423 // logger.warn("Setting focus to navigator");
425 if (getConversationHolder() != null) {
426 getConversationHolder().bind();
428 if(cdmEntitySession
!= null) {
429 cdmEntitySession
.bind();
437 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
438 * (eu.etaxonomy.cdm.model.common.CdmBase)
442 public boolean postOperation(CdmBase objectAffectedByOperation
) {
443 // nothing to do here
453 * a {@link org.eclipse.ui.IMemento} object.
455 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
457 public void save(IMemento memento
, IProgressMonitor monitor
) {
458 saveTreeState(memento
, monitor
);
459 if (conversation
!= null) {
460 conversation
.unregisterForDataStoreChanges(this);
467 protected void handleDoubleClick(DoubleClickEvent event
) {
468 ISelection selection
= event
.getSelection();
469 if(selection
instanceof IStructuredSelection
){
470 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
471 if(firstElement
instanceof ICdmBase
){
472 NavigationUtil
.openEditor((ICdmBase
) firstElement
);
475 // If the double click is passed up to the super-class it will
476 // expand/collapse trees.
477 // We do not want that
478 // super.handleDoubleClick(anEvent);
489 public boolean onComplete() {
496 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
500 public void showBusy(boolean busy
) {
501 super.showBusy(busy
);
502 getCommonViewer().getControl().setEnabled(!busy
);
504 partNameCache
= getPartName();
505 setPartName(Messages
.TaxonNavigator_LOAD_DATASOURCE
);
507 if (partNameCache
!= null) {
508 setPartName(partNameCache
);
515 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
518 public void update(Observable o
, Object arg
) {
519 if(o
instanceof LoginManager
){
526 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
529 public ICdmEntitySession
getCdmEntitySession() {
530 return cdmEntitySession
;
534 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
537 public List
<ITreeNode
> getRootEntities() {
539 return root
.getParentBeans();
545 * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
548 public void onChange(CdmChangeEvent event
) {
549 if(event
.getAction() == Action
.Delete
&& Classification
.class.equals(event
.getEntityType())) {
553 for(CdmBase cb
: event
.getChangedObjects()) {
554 if(cb
instanceof TaxonNode
) {
555 TaxonNode tn
= (TaxonNode
)cb
;
556 if(tn
.getTaxon() == null) {
557 getCommonViewer().refresh(tn
.getClassification());
559 getCommonViewer().refresh(cb
);
561 } else if (cb
instanceof Classification
) {
562 getCommonViewer().refresh();
569 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
572 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
573 Map
<Object
, List
<String
>> propertyPathsMap
= new HashMap
<Object
, List
<String
>>();
574 List
<String
> taxonNodePropertyPaths
= Arrays
.asList(new String
[] {
575 "taxon.name" //$NON-NLS-1$
577 propertyPathsMap
.put("childNodes", taxonNodePropertyPaths
); //$NON-NLS-1$
578 return propertyPathsMap
;