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
.ui
.IMemento
;
26 import org
.eclipse
.ui
.IViewSite
;
27 import org
.eclipse
.ui
.PartInitException
;
28 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
30 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
31 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
33 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
34 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
35 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
36 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
37 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
38 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
40 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
43 * Taxonomic tree implementation using Common Navigator Framework.
50 public class TaxonNavigator
extends CommonNavigator
implements
51 IPostOperationEnabled
, IConversationEnabled
, Observer
{
55 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
57 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
59 private static final String TREE_PATH
= "treepath";
61 private static final String TREE_PATHS
= "treepaths";
63 private ConversationHolder conversation
;
65 private String partNameCache
;
67 private IDataChangeBehavior dataChangeBehavior
;
72 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
76 protected IAdaptable
getInitialInput() {
77 if (CdmStore
.isActive()) {
78 // TODO when closing and reopening the taxon navigator
79 // we do not preserve state. Closing the view, in contrary to
80 // closing the whole application
81 // should be handled by the state manager too
83 return new Root(conversation
);
85 return new EmptyRoot();
90 public void init(IViewSite site
) throws PartInitException
{
101 if (CdmStore
.isActive() && conversation
== null) {
102 conversation
= CdmStore
.createConversation();
103 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
105 CdmStore
.getLoginManager().addObserver(this);
109 * Refresh this navigators viewer
111 public void refresh() {
112 if(getConversationHolder() != null){
113 getConversationHolder().bind();
115 getCommonViewer().refresh();
119 * Removes all content
121 public void clear() {
122 getCommonViewer().setInput(new EmptyRoot());
131 * a {@link org.eclipse.ui.IMemento} object.
133 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
135 public void restore(IMemento memento
, IProgressMonitor monitor
) {
136 if (memento
== null) {
137 getCommonViewer().setInput(new Root(conversation
));
141 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
142 IMemento
[] treePathMementos
= null;
144 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
146 if (treePathsMemento
!= null) {
147 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
148 mementoWork
= treePathMementos
.length
;
150 // begin the monitor with steps for all tree paths and steps for
152 // conversation s.o., refreshing the tree and setting the paths
153 IProgressMonitor subProgressMonitor
= NavigationUtil
154 .getSubProgressMonitor(monitor
, 1);
156 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
157 1 + mementoWork
+ 5);
158 subProgressMonitor
.subTask("Restoring Taxon Navigator");
159 subProgressMonitor
.worked(1);
161 conversation
= CdmStore
.createConversation();
162 subProgressMonitor
.worked(1);
163 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
164 subProgressMonitor
.worked(1);
165 getCommonViewer().setInput(new Root(conversation
));
166 subProgressMonitor
.worked(1);
167 getCommonViewer().refresh();
168 subProgressMonitor
.worked(1);
170 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
171 for (IMemento treePathMemento
: treePathMementos
) {
172 TreePath treePath
= createTreePathFromString(treePathMemento
174 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
175 treePaths
.add(treePath
);
176 subProgressMonitor
.worked(1);
180 if (treePaths
.size() > 0) {
181 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
182 treePaths
.toArray(new TreePath
[0]));
183 subProgressMonitor
.worked(1);
185 subProgressMonitor
.done();
192 private TreePath
createTreePathFromString(String string
) {
194 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
196 if (string
.length() == 0) {
200 for (String uuid
: string
.split(" ")) {
201 CdmBase cdmBaseObject
= CdmStore
.getService(
202 IClassificationService
.class).getTaxonNodeByUuid(
203 UUID
.fromString(uuid
));
204 if (cdmBaseObject
== null) {
205 // is this a tree uuid?
206 cdmBaseObject
= CdmStore
.getService(
207 IClassificationService
.class).load(
208 UUID
.fromString(uuid
));
210 if (cdmBaseObject
== null) {
214 pathList
.add(cdmBaseObject
);
216 return new TreePath(pathList
.toArray());
221 public void saveState(IMemento aMemento
) {
231 * a {@link org.eclipse.ui.IMemento} object.
232 * @param progressMonitor
233 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
235 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
236 if (memento
== null) {
239 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
242 super.saveState(memento
);
244 memento
= memento
.createChild(TREE_PATHS
);
245 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
247 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
249 for (TreePath treePath
: treePaths
) {
250 int pathLength
= treePath
.getSegmentCount();
252 for (int i
= 0; i
< pathLength
; i
++) {
253 Object segment
= treePath
.getSegment(i
);
254 if (segment
instanceof CdmBase
) {
255 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
258 NavigationUtil
.warn(getClass(),
259 "Non-taxon tree path segment " + segment
);
262 memento
.createChild(TREE_PATH
, path
.trim());
271 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
276 * getConversationHolder
279 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
283 public ConversationHolder
getConversationHolder() {
291 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
292 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
296 public void update(CdmDataChangeMap changeEvents
) {
297 if (dataChangeBehavior
== null) {
298 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
301 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
306 public String
getFrameToolTipText(Object element
) {
307 if (element
instanceof Root
) {
308 return "Taxonomic Tree";
310 return super.getFrameToolTipText(element
);
316 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
320 public void dispose() {
322 dataChangeBehavior
= null;
323 if (conversation
!= null) {
324 conversation
.unregisterForDataStoreChanges(this);
331 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
335 public void setFocus() {
336 // logger.warn("Setting focus to navigator");
338 if (getConversationHolder() != null) {
339 getConversationHolder().bind();
347 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
348 * (eu.etaxonomy.cdm.model.common.CdmBase)
352 public boolean postOperation(CdmBase objectAffectedByOperation
) {
353 // nothing to do here
363 * a {@link org.eclipse.ui.IMemento} object.
365 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
367 public void save(IMemento memento
, IProgressMonitor monitor
) {
368 saveTreeState(memento
, monitor
);
369 if (conversation
!= null) {
370 conversation
.unregisterForDataStoreChanges(this);
377 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
378 NavigationUtil
.executeEditHandler();
379 // If the double click is passed up to the super-class it will
380 // expand/collapse trees.
381 // We do not want that
382 // super.handleDoubleClick(anEvent);
393 public boolean onComplete() {
400 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
404 public void showBusy(boolean busy
) {
405 super.showBusy(busy
);
406 getCommonViewer().getControl().setEnabled(!busy
);
408 partNameCache
= getPartName();
409 setPartName("Loading datasources");
411 if (partNameCache
!= null) {
412 setPartName(partNameCache
);
419 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
422 public void update(Observable o
, Object arg
) {
423 if(o
instanceof LoginManager
){