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
.HashSet
;
15 import java
.util
.List
;
16 import java
.util
.Observable
;
17 import java
.util
.Observer
;
19 import java
.util
.UUID
;
21 import org
.eclipse
.core
.runtime
.IAdaptable
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
24 import org
.eclipse
.jface
.viewers
.TreePath
;
25 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
26 import org
.eclipse
.ui
.IMemento
;
27 import org
.eclipse
.ui
.IViewSite
;
28 import org
.eclipse
.ui
.PartInitException
;
29 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
31 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
32 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonComparatorSearch
;
36 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
37 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
38 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
40 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
41 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
42 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
45 * Taxonomic tree implementation using Common Navigator Framework.
52 public class TaxonNavigator
extends CommonNavigator
implements
53 IPostOperationEnabled
, IConversationEnabled
, Observer
{
57 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
59 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
61 private static final String TREE_PATH
= "treepath";
63 private static final String TREE_PATHS
= "treepaths";
65 private ConversationHolder conversation
;
67 private String partNameCache
;
69 private IDataChangeBehavior dataChangeBehavior
;
74 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
78 protected IAdaptable
getInitialInput() {
79 TaxonComparatorSearch comparator
= new TaxonComparatorSearch();
80 TaxonNodeComparator viewerComparator
= new TaxonNodeComparator(comparator
);
81 this.getCommonViewer().setComparator(viewerComparator
);
82 if (CdmStore
.isActive()) {
83 // TODO when closing and reopening the taxon navigator
84 // we do not preserve state. Closing the view, in contrary to
85 // closing the whole application
86 // should be handled by the state manager too
88 return new Root(conversation
);
90 return new EmptyRoot();
95 public void init(IViewSite site
) throws PartInitException
{
107 if (CdmStore
.isActive() && conversation
== null) {
108 conversation
= CdmStore
.createConversation();
109 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
111 CdmStore
.getLoginManager().addObserver(this);
118 * Refresh this navigators viewer
120 public void refresh() {
121 if(getConversationHolder() != null){
122 getConversationHolder().bind();
123 //FIXME : Need to make sure this is a stable fix (ticket 3822)
124 getConversationHolder().commit();
126 getCommonViewer().refresh();
130 * Removes all content
132 public void clear() {
133 getCommonViewer().setInput(new EmptyRoot());
142 * a {@link org.eclipse.ui.IMemento} object.
144 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
146 public void restore(IMemento memento
, IProgressMonitor monitor
) {
147 if (memento
== null) {
148 getCommonViewer().setInput(new Root(conversation
));
152 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
153 IMemento
[] treePathMementos
= null;
155 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
157 if (treePathsMemento
!= null) {
158 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
159 mementoWork
= treePathMementos
.length
;
161 // begin the monitor with steps for all tree paths and steps for
163 // conversation s.o., refreshing the tree and setting the paths
164 IProgressMonitor subProgressMonitor
= NavigationUtil
165 .getSubProgressMonitor(monitor
, 1);
167 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
168 1 + mementoWork
+ 5);
169 subProgressMonitor
.subTask("Restoring Taxon Navigator");
170 subProgressMonitor
.worked(1);
172 conversation
= CdmStore
.createConversation();
173 subProgressMonitor
.worked(1);
174 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
175 subProgressMonitor
.worked(1);
176 getCommonViewer().setInput(new Root(conversation
));
177 subProgressMonitor
.worked(1);
178 getCommonViewer().refresh();
179 subProgressMonitor
.worked(1);
181 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
182 for (IMemento treePathMemento
: treePathMementos
) {
183 TreePath treePath
= createTreePathFromString(treePathMemento
185 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
186 treePaths
.add(treePath
);
187 subProgressMonitor
.worked(1);
191 if (treePaths
.size() > 0) {
192 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
193 treePaths
.toArray(new TreePath
[0]));
194 subProgressMonitor
.worked(1);
196 subProgressMonitor
.done();
203 private TreePath
createTreePathFromString(String string
) {
206 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
208 if (string
.length() == 0) {
212 for (String uuid
: string
.split(" ")) {
213 CdmBase cdmBaseObject
= CdmStore
.getService(
214 IClassificationService
.class).getTaxonNodeByUuid(
215 UUID
.fromString(uuid
));
216 if (cdmBaseObject
== null) {
217 // is this a tree uuid?
218 cdmBaseObject
= CdmStore
.getService(
219 IClassificationService
.class).load(
220 UUID
.fromString(uuid
));
222 if (cdmBaseObject
== null) {
226 pathList
.add(cdmBaseObject
);
228 return new TreePath(pathList
.toArray());
233 public void saveState(IMemento aMemento
) {
243 * a {@link org.eclipse.ui.IMemento} object.
244 * @param progressMonitor
245 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
247 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
248 if (memento
== null) {
251 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
254 super.saveState(memento
);
256 memento
= memento
.createChild(TREE_PATHS
);
257 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
259 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
261 for (TreePath treePath
: treePaths
) {
262 int pathLength
= treePath
.getSegmentCount();
264 for (int i
= 0; i
< pathLength
; i
++) {
265 Object segment
= treePath
.getSegment(i
);
266 if (segment
instanceof CdmBase
) {
267 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
270 NavigationUtil
.warn(getClass(),
271 "Non-taxon tree path segment " + segment
);
274 memento
.createChild(TREE_PATH
, path
.trim());
283 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
288 * getConversationHolder
291 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
295 public ConversationHolder
getConversationHolder() {
303 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
304 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
308 public void update(CdmDataChangeMap changeEvents
) {
309 if (dataChangeBehavior
== null) {
310 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
313 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
318 public String
getFrameToolTipText(Object element
) {
319 if (element
instanceof Root
) {
320 return "Taxonomic Tree";
322 return super.getFrameToolTipText(element
);
328 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
332 public void dispose() {
334 dataChangeBehavior
= null;
335 if (conversation
!= null) {
336 conversation
.unregisterForDataStoreChanges(this);
343 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
347 public void setFocus() {
348 // logger.warn("Setting focus to navigator");
350 if (getConversationHolder() != null) {
351 getConversationHolder().bind();
359 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
360 * (eu.etaxonomy.cdm.model.common.CdmBase)
364 public boolean postOperation(CdmBase objectAffectedByOperation
) {
365 // nothing to do here
375 * a {@link org.eclipse.ui.IMemento} object.
377 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
379 public void save(IMemento memento
, IProgressMonitor monitor
) {
380 saveTreeState(memento
, monitor
);
381 if (conversation
!= null) {
382 conversation
.unregisterForDataStoreChanges(this);
389 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
390 NavigationUtil
.executeEditHandler();
391 // If the double click is passed up to the super-class it will
392 // expand/collapse trees.
393 // We do not want that
394 // super.handleDoubleClick(anEvent);
405 public boolean onComplete() {
412 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
416 public void showBusy(boolean busy
) {
417 super.showBusy(busy
);
418 getCommonViewer().getControl().setEnabled(!busy
);
420 partNameCache
= getPartName();
421 setPartName("Loading datasources");
423 if (partNameCache
!= null) {
424 setPartName(partNameCache
);
431 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
434 public void update(Observable o
, Object arg
) {
435 if(o
instanceof LoginManager
){