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();
114 //FIXME : Need to make sure this is a stable fix (ticket 3822)
115 getConversationHolder().commit();
117 getCommonViewer().refresh();
121 * Removes all content
123 public void clear() {
124 getCommonViewer().setInput(new EmptyRoot());
133 * a {@link org.eclipse.ui.IMemento} object.
135 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
137 public void restore(IMemento memento
, IProgressMonitor monitor
) {
138 if (memento
== null) {
139 getCommonViewer().setInput(new Root(conversation
));
143 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
144 IMemento
[] treePathMementos
= null;
146 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
148 if (treePathsMemento
!= null) {
149 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
150 mementoWork
= treePathMementos
.length
;
152 // begin the monitor with steps for all tree paths and steps for
154 // conversation s.o., refreshing the tree and setting the paths
155 IProgressMonitor subProgressMonitor
= NavigationUtil
156 .getSubProgressMonitor(monitor
, 1);
158 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
159 1 + mementoWork
+ 5);
160 subProgressMonitor
.subTask("Restoring Taxon Navigator");
161 subProgressMonitor
.worked(1);
163 conversation
= CdmStore
.createConversation();
164 subProgressMonitor
.worked(1);
165 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
166 subProgressMonitor
.worked(1);
167 getCommonViewer().setInput(new Root(conversation
));
168 subProgressMonitor
.worked(1);
169 getCommonViewer().refresh();
170 subProgressMonitor
.worked(1);
172 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
173 for (IMemento treePathMemento
: treePathMementos
) {
174 TreePath treePath
= createTreePathFromString(treePathMemento
176 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
177 treePaths
.add(treePath
);
178 subProgressMonitor
.worked(1);
182 if (treePaths
.size() > 0) {
183 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
184 treePaths
.toArray(new TreePath
[0]));
185 subProgressMonitor
.worked(1);
187 subProgressMonitor
.done();
194 private TreePath
createTreePathFromString(String string
) {
196 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
198 if (string
.length() == 0) {
202 for (String uuid
: string
.split(" ")) {
203 CdmBase cdmBaseObject
= CdmStore
.getService(
204 IClassificationService
.class).getTaxonNodeByUuid(
205 UUID
.fromString(uuid
));
206 if (cdmBaseObject
== null) {
207 // is this a tree uuid?
208 cdmBaseObject
= CdmStore
.getService(
209 IClassificationService
.class).load(
210 UUID
.fromString(uuid
));
212 if (cdmBaseObject
== null) {
216 pathList
.add(cdmBaseObject
);
218 return new TreePath(pathList
.toArray());
223 public void saveState(IMemento aMemento
) {
233 * a {@link org.eclipse.ui.IMemento} object.
234 * @param progressMonitor
235 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
237 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
238 if (memento
== null) {
241 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
244 super.saveState(memento
);
246 memento
= memento
.createChild(TREE_PATHS
);
247 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
249 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
251 for (TreePath treePath
: treePaths
) {
252 int pathLength
= treePath
.getSegmentCount();
254 for (int i
= 0; i
< pathLength
; i
++) {
255 Object segment
= treePath
.getSegment(i
);
256 if (segment
instanceof CdmBase
) {
257 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
260 NavigationUtil
.warn(getClass(),
261 "Non-taxon tree path segment " + segment
);
264 memento
.createChild(TREE_PATH
, path
.trim());
273 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
278 * getConversationHolder
281 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
285 public ConversationHolder
getConversationHolder() {
293 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
294 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
298 public void update(CdmDataChangeMap changeEvents
) {
299 if (dataChangeBehavior
== null) {
300 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
303 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
308 public String
getFrameToolTipText(Object element
) {
309 if (element
instanceof Root
) {
310 return "Taxonomic Tree";
312 return super.getFrameToolTipText(element
);
318 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
322 public void dispose() {
324 dataChangeBehavior
= null;
325 if (conversation
!= null) {
326 conversation
.unregisterForDataStoreChanges(this);
333 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
337 public void setFocus() {
338 // logger.warn("Setting focus to navigator");
340 if (getConversationHolder() != null) {
341 getConversationHolder().bind();
349 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
350 * (eu.etaxonomy.cdm.model.common.CdmBase)
354 public boolean postOperation(CdmBase objectAffectedByOperation
) {
355 // nothing to do here
365 * a {@link org.eclipse.ui.IMemento} object.
367 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
369 public void save(IMemento memento
, IProgressMonitor monitor
) {
370 saveTreeState(memento
, monitor
);
371 if (conversation
!= null) {
372 conversation
.unregisterForDataStoreChanges(this);
379 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
380 NavigationUtil
.executeEditHandler();
381 // If the double click is passed up to the super-class it will
382 // expand/collapse trees.
383 // We do not want that
384 // super.handleDoubleClick(anEvent);
395 public boolean onComplete() {
402 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
406 public void showBusy(boolean busy
) {
407 super.showBusy(busy
);
408 getCommonViewer().getControl().setEnabled(!busy
);
410 partNameCache
= getPartName();
411 setPartName("Loading datasources");
413 if (partNameCache
!= null) {
414 setPartName(partNameCache
);
421 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
424 public void update(Observable o
, Object arg
) {
425 if(o
instanceof LoginManager
){