3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
;
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
;
20 import java
.util
.Observable
;
21 import java
.util
.Observer
;
23 import java
.util
.UUID
;
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
.TreePath
;
29 import org
.eclipse
.ui
.IMemento
;
30 import org
.eclipse
.ui
.IViewSite
;
31 import org
.eclipse
.ui
.PartInitException
;
32 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
34 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
35 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
;
36 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
.Action
;
37 import eu
.etaxonomy
.cdm
.api
.application
.ICdmChangeListener
;
38 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
39 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
40 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
41 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNaturalComparator
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByRankAndNameComparator
;
46 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
47 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
48 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
49 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
50 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
51 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
52 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
53 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
54 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
55 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
56 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
59 * Taxonomic tree implementation using Common Navigator Framework.
66 public class TaxonNavigator
extends CommonNavigator
implements
67 IPostOperationEnabled
, IConversationEnabled
, Observer
,
68 ICdmEntitySessionEnabled
, ICdmChangeListener
{
72 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
74 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
76 private static final String TREE_PATH
= "treepath";
78 private static final String TREE_PATHS
= "treepaths";
80 private ConversationHolder conversation
;
82 private ICdmEntitySession cdmEntitySession
;
84 private String partNameCache
;
86 private IDataChangeBehavior dataChangeBehavior
;
93 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
97 protected IAdaptable
getInitialInput() {
98 Comparator
<TaxonNode
> comparator
;
99 if (PreferencesUtil
.getSortNodesNaturally()){
100 comparator
= new TaxonNaturalComparator();
102 comparator
= new TaxonNodeByRankAndNameComparator();
104 TaxonNodeNavigatorComparator viewerComparator
= new TaxonNodeNavigatorComparator(comparator
);
105 this.getCommonViewer().setComparator(viewerComparator
);
106 setLinkingEnabled(true);
107 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
110 // public void selectionChanged(SelectionChangedEvent arg0) {
111 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
113 // Object firstElement = selection.getFirstElement();
115 // if (!(firstElement instanceof Classification)){
116 // //NavigationUtil.selectInNavigator(firstElement, null);
117 // NavigationUtil.openEditor(firstElement);
123 if (CdmStore
.isActive()) {
125 // TODO when closing and reopening the taxon navigator
126 // we do not preserve state. Closing the view, in contrary to
127 // closing the whole application
128 // should be handled by the state manager too
129 root
= new Root(conversation
);
132 return new EmptyRoot();
137 public void init(IViewSite site
) throws PartInitException
{
149 if (CdmStore
.isActive() && conversation
== null) {
150 conversation
= CdmStore
.createConversation();
151 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
153 if (CdmStore
.isActive()) {
154 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
155 CdmApplicationState
.getCurrentDataChangeService().register(this);
157 CdmStore
.getLoginManager().addObserver(this);
161 * Refresh this navigators viewer
163 public void refresh() {
164 if(getConversationHolder() != null){
165 getConversationHolder().bind();
166 //FIXME : Need to make sure this is a stable fix (ticket 3822)
167 if(!getConversationHolder().isCompleted()){
168 getConversationHolder().commit();
171 getCommonViewer().refresh();
175 * Refresh this navigators viewer
177 public void refresh(Set objects
) {
178 for(Object obj
: objects
) {
179 getCommonViewer().refresh(obj
);
184 * Removes all content
186 public void clear() {
187 getCommonViewer().setInput(new EmptyRoot());
196 * a {@link org.eclipse.ui.IMemento} object.
198 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
200 public void restore(IMemento memento
, IProgressMonitor monitor
) {
201 root
= new Root(conversation
);
202 if (memento
== null) {
203 getCommonViewer().setInput(root
);
207 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
208 IMemento
[] treePathMementos
= null;
210 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
212 if (treePathsMemento
!= null) {
213 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
214 mementoWork
= treePathMementos
.length
;
216 // begin the monitor with steps for all tree paths and steps for
218 // conversation s.o., refreshing the tree and setting the paths
219 IProgressMonitor subProgressMonitor
= NavigationUtil
220 .getSubProgressMonitor(monitor
, 1);
222 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
223 1 + mementoWork
+ 5);
224 subProgressMonitor
.subTask("Restoring Taxon Navigator");
225 subProgressMonitor
.worked(1);
227 conversation
= CdmStore
.createConversation();
228 subProgressMonitor
.worked(1);
229 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
230 subProgressMonitor
.worked(1);
231 getCommonViewer().setInput(root
);
232 subProgressMonitor
.worked(1);
233 getCommonViewer().refresh();
234 subProgressMonitor
.worked(1);
236 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
237 for (IMemento treePathMemento
: treePathMementos
) {
238 TreePath treePath
= createTreePathFromString(treePathMemento
240 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
241 treePaths
.add(treePath
);
242 subProgressMonitor
.worked(1);
246 if (treePaths
.size() > 0) {
247 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
248 treePaths
.toArray(new TreePath
[0]));
249 subProgressMonitor
.worked(1);
251 subProgressMonitor
.done();
258 private TreePath
createTreePathFromString(String string
) {
260 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
262 if (string
.length() == 0) {
266 for (String uuid
: string
.split(" ")) {
267 CdmBase cdmBaseObject
= CdmStore
.getService(
268 IClassificationService
.class).getTaxonNodeByUuid(
269 UUID
.fromString(uuid
));
270 if (cdmBaseObject
== null) {
271 // is this a tree uuid?
272 cdmBaseObject
= CdmStore
.getService(
273 IClassificationService
.class).load(
274 UUID
.fromString(uuid
));
276 if (cdmBaseObject
== null) {
280 pathList
.add(cdmBaseObject
);
282 return new TreePath(pathList
.toArray());
287 public void saveState(IMemento aMemento
) {
297 * a {@link org.eclipse.ui.IMemento} object.
298 * @param progressMonitor
299 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
301 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
302 if (memento
== null) {
305 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
308 super.saveState(memento
);
310 memento
= memento
.createChild(TREE_PATHS
);
311 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
313 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
315 for (TreePath treePath
: treePaths
) {
316 int pathLength
= treePath
.getSegmentCount();
318 for (int i
= 0; i
< pathLength
; i
++) {
319 Object segment
= treePath
.getSegment(i
);
320 if (segment
instanceof CdmBase
) {
321 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
324 MessagingUtils
.warn(getClass(),
325 "Non-taxon tree path segment " + segment
);
328 memento
.createChild(TREE_PATH
, path
.trim());
337 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
342 * getConversationHolder
345 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
349 public ConversationHolder
getConversationHolder() {
357 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
358 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
362 public void update(CdmDataChangeMap changeEvents
) {
363 if (dataChangeBehavior
== null) {
364 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
367 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
372 public String
getFrameToolTipText(Object element
) {
373 if (element
instanceof Root
) {
374 return "Taxonomic Tree";
376 return super.getFrameToolTipText(element
);
382 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
386 public void dispose() {
388 dataChangeBehavior
= null;
389 if (conversation
!= null) {
390 conversation
.unregisterForDataStoreChanges(this);
392 if(cdmEntitySession
!= null) {
393 cdmEntitySession
.dispose();
395 if(CdmApplicationState
.getCurrentDataChangeService() != null) {
396 CdmApplicationState
.getCurrentDataChangeService().unregister(this);
403 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
407 public void setFocus() {
408 // logger.warn("Setting focus to navigator");
410 if (getConversationHolder() != null) {
411 getConversationHolder().bind();
413 if(cdmEntitySession
!= null) {
414 cdmEntitySession
.bind();
422 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
423 * (eu.etaxonomy.cdm.model.common.CdmBase)
427 public boolean postOperation(CdmBase objectAffectedByOperation
) {
428 // nothing to do here
438 * a {@link org.eclipse.ui.IMemento} object.
440 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
442 public void save(IMemento memento
, IProgressMonitor monitor
) {
443 saveTreeState(memento
, monitor
);
444 if (conversation
!= null) {
445 conversation
.unregisterForDataStoreChanges(this);
452 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
453 NavigationUtil
.executeEditHandler();
454 // If the double click is passed up to the super-class it will
455 // expand/collapse trees.
456 // We do not want that
457 // super.handleDoubleClick(anEvent);
468 public boolean onComplete() {
475 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
479 public void showBusy(boolean busy
) {
480 super.showBusy(busy
);
481 getCommonViewer().getControl().setEnabled(!busy
);
483 partNameCache
= getPartName();
484 setPartName("Loading datasources");
486 if (partNameCache
!= null) {
487 setPartName(partNameCache
);
494 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
497 public void update(Observable o
, Object arg
) {
498 if(o
instanceof LoginManager
){
505 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
508 public ICdmEntitySession
getCdmEntitySession() {
509 return cdmEntitySession
;
513 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
516 public List
<Classification
> getRootEntities() {
518 return root
.getParentBeans();
524 * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
527 public void onChange(CdmChangeEvent event
) {
528 if(event
.getAction() == Action
.Delete
&& Classification
.class.equals(event
.getEntityType())) {
532 for(CdmBase cb
: event
.getChangedObjects()) {
533 if(cb
instanceof TaxonNode
) {
534 TaxonNode tn
= (TaxonNode
)cb
;
535 if(tn
.getTaxon() == null) {
536 getCommonViewer().refresh(tn
.getClassification());
538 getCommonViewer().refresh(cb
);
540 } else if (cb
instanceof Classification
) {
541 getCommonViewer().refresh();
548 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
551 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
552 Map
<Object
, List
<String
>> propertyPathsMap
= new HashMap
<Object
, List
<String
>>();
553 List
<String
> taxonNodePropertyPaths
= Arrays
.asList(new String
[] {
556 propertyPathsMap
.put("childNodes", taxonNodePropertyPaths
);
557 return propertyPathsMap
;