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
.model
.taxon
.TaxonComparatorSearch
;
35 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
36 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
37 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
39 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
40 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
41 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
44 * Taxonomic tree implementation using Common Navigator Framework.
51 public class TaxonNavigator
extends CommonNavigator
implements
52 IPostOperationEnabled
, IConversationEnabled
, Observer
{
56 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
58 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
60 private static final String TREE_PATH
= "treepath";
62 private static final String TREE_PATHS
= "treepaths";
64 private ConversationHolder conversation
;
66 private String partNameCache
;
68 private IDataChangeBehavior dataChangeBehavior
;
73 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
77 protected IAdaptable
getInitialInput() {
79 TaxonComparatorSearch comparator
= new TaxonComparatorSearch();
80 TaxonNodeComparator viewerComparator
= new TaxonNodeComparator(comparator
);
81 this.getCommonViewer().setComparator(viewerComparator
);
83 if (CdmStore
.isActive()) {
85 // TODO when closing and reopening the taxon navigator
86 // we do not preserve state. Closing the view, in contrary to
87 // closing the whole application
88 // should be handled by the state manager too
90 return new Root(conversation
);
92 return new EmptyRoot();
97 public void init(IViewSite site
) throws PartInitException
{
108 if (CdmStore
.isActive() && conversation
== null) {
109 conversation
= CdmStore
.createConversation();
110 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
112 CdmStore
.getLoginManager().addObserver(this);
116 * Refresh this navigators viewer
118 public void refresh() {
119 if(getConversationHolder() != null){
120 getConversationHolder().bind();
121 //FIXME : Need to make sure this is a stable fix (ticket 3822)
122 if(!getConversationHolder().isCompleted()){
123 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
) {
205 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
207 if (string
.length() == 0) {
211 for (String uuid
: string
.split(" ")) {
212 CdmBase cdmBaseObject
= CdmStore
.getService(
213 IClassificationService
.class).getTaxonNodeByUuid(
214 UUID
.fromString(uuid
));
215 if (cdmBaseObject
== null) {
216 // is this a tree uuid?
217 cdmBaseObject
= CdmStore
.getService(
218 IClassificationService
.class).load(
219 UUID
.fromString(uuid
));
221 if (cdmBaseObject
== null) {
225 pathList
.add(cdmBaseObject
);
227 return new TreePath(pathList
.toArray());
232 public void saveState(IMemento aMemento
) {
242 * a {@link org.eclipse.ui.IMemento} object.
243 * @param progressMonitor
244 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
246 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
247 if (memento
== null) {
250 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
253 super.saveState(memento
);
255 memento
= memento
.createChild(TREE_PATHS
);
256 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
258 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
260 for (TreePath treePath
: treePaths
) {
261 int pathLength
= treePath
.getSegmentCount();
263 for (int i
= 0; i
< pathLength
; i
++) {
264 Object segment
= treePath
.getSegment(i
);
265 if (segment
instanceof CdmBase
) {
266 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
269 NavigationUtil
.warn(getClass(),
270 "Non-taxon tree path segment " + segment
);
273 memento
.createChild(TREE_PATH
, path
.trim());
282 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
287 * getConversationHolder
290 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
294 public ConversationHolder
getConversationHolder() {
302 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
303 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
307 public void update(CdmDataChangeMap changeEvents
) {
308 if (dataChangeBehavior
== null) {
309 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
312 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
317 public String
getFrameToolTipText(Object element
) {
318 if (element
instanceof Root
) {
319 return "Taxonomic Tree";
321 return super.getFrameToolTipText(element
);
327 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
331 public void dispose() {
333 dataChangeBehavior
= null;
334 if (conversation
!= null) {
335 conversation
.unregisterForDataStoreChanges(this);
342 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
346 public void setFocus() {
347 // logger.warn("Setting focus to navigator");
349 if (getConversationHolder() != null) {
350 getConversationHolder().bind();
358 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
359 * (eu.etaxonomy.cdm.model.common.CdmBase)
363 public boolean postOperation(CdmBase objectAffectedByOperation
) {
364 // nothing to do here
374 * a {@link org.eclipse.ui.IMemento} object.
376 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
378 public void save(IMemento memento
, IProgressMonitor monitor
) {
379 saveTreeState(memento
, monitor
);
380 if (conversation
!= null) {
381 conversation
.unregisterForDataStoreChanges(this);
388 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
389 NavigationUtil
.executeEditHandler();
390 // If the double click is passed up to the super-class it will
391 // expand/collapse trees.
392 // We do not want that
393 // super.handleDoubleClick(anEvent);
404 public boolean onComplete() {
411 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
415 public void showBusy(boolean busy
) {
416 super.showBusy(busy
);
417 getCommonViewer().getControl().setEnabled(!busy
);
419 partNameCache
= getPartName();
420 setPartName("Loading datasources");
422 if (partNameCache
!= null) {
423 setPartName(partNameCache
);
430 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
433 public void update(Observable o
, Object arg
) {
434 if(o
instanceof LoginManager
){