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
.IStructuredSelection
;
25 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
26 import org
.eclipse
.jface
.viewers
.TreePath
;
27 import org
.eclipse
.ui
.IMemento
;
28 import org
.eclipse
.ui
.IViewSite
;
29 import org
.eclipse
.ui
.PartInitException
;
30 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
31 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
33 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
34 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
35 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
36 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
37 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonComparatorSearch
;
39 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
40 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
41 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
42 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
43 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
44 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
45 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
48 * Taxonomic tree implementation using Common Navigator Framework.
55 public class TaxonNavigator
extends CommonNavigator
implements
56 IPostOperationEnabled
, IConversationEnabled
, Observer
{
60 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
62 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
64 private static final String TREE_PATH
= "treepath";
66 private static final String TREE_PATHS
= "treepaths";
68 private ConversationHolder conversation
;
70 private String partNameCache
;
72 private IDataChangeBehavior dataChangeBehavior
;
77 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
81 protected IAdaptable
getInitialInput() {
83 TaxonComparatorSearch comparator
= new TaxonComparatorSearch();
84 TaxonNodeComparator viewerComparator
= new TaxonNodeComparator(comparator
);
85 this.getCommonViewer().setComparator(viewerComparator
);
86 this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
89 public void selectionChanged(SelectionChangedEvent arg0
) {
90 IStructuredSelection selection
= (IStructuredSelection
) getCommonViewer().getSelection();
92 Object firstElement
= selection
.getFirstElement();
93 NavigationUtil
.executeEditHandler();
98 if (CdmStore
.isActive()) {
100 // TODO when closing and reopening the taxon navigator
101 // we do not preserve state. Closing the view, in contrary to
102 // closing the whole application
103 // should be handled by the state manager too
105 return new Root(conversation
);
107 return new EmptyRoot();
112 public void init(IViewSite site
) throws PartInitException
{
124 if (CdmStore
.isActive() && conversation
== null) {
125 conversation
= CdmStore
.createConversation();
126 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
128 CdmStore
.getLoginManager().addObserver(this);
133 * Refresh this navigators viewer
135 public void refresh() {
136 if(getConversationHolder() != null){
137 getConversationHolder().bind();
138 //FIXME : Need to make sure this is a stable fix (ticket 3822)
139 if(!getConversationHolder().isCompleted()){
140 getConversationHolder().commit();
143 getCommonViewer().refresh();
147 * Removes all content
149 public void clear() {
150 getCommonViewer().setInput(new EmptyRoot());
159 * a {@link org.eclipse.ui.IMemento} object.
161 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
163 public void restore(IMemento memento
, IProgressMonitor monitor
) {
164 if (memento
== null) {
165 getCommonViewer().setInput(new Root(conversation
));
169 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
170 IMemento
[] treePathMementos
= null;
172 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
174 if (treePathsMemento
!= null) {
175 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
176 mementoWork
= treePathMementos
.length
;
178 // begin the monitor with steps for all tree paths and steps for
180 // conversation s.o., refreshing the tree and setting the paths
181 IProgressMonitor subProgressMonitor
= NavigationUtil
182 .getSubProgressMonitor(monitor
, 1);
184 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
185 1 + mementoWork
+ 5);
186 subProgressMonitor
.subTask("Restoring Taxon Navigator");
187 subProgressMonitor
.worked(1);
189 conversation
= CdmStore
.createConversation();
190 subProgressMonitor
.worked(1);
191 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
192 subProgressMonitor
.worked(1);
193 getCommonViewer().setInput(new Root(conversation
));
194 subProgressMonitor
.worked(1);
195 getCommonViewer().refresh();
196 subProgressMonitor
.worked(1);
198 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
199 for (IMemento treePathMemento
: treePathMementos
) {
200 TreePath treePath
= createTreePathFromString(treePathMemento
202 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
203 treePaths
.add(treePath
);
204 subProgressMonitor
.worked(1);
208 if (treePaths
.size() > 0) {
209 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
210 treePaths
.toArray(new TreePath
[0]));
211 subProgressMonitor
.worked(1);
213 subProgressMonitor
.done();
220 private TreePath
createTreePathFromString(String string
) {
222 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
224 if (string
.length() == 0) {
228 for (String uuid
: string
.split(" ")) {
229 CdmBase cdmBaseObject
= CdmStore
.getService(
230 IClassificationService
.class).getTaxonNodeByUuid(
231 UUID
.fromString(uuid
));
232 if (cdmBaseObject
== null) {
233 // is this a tree uuid?
234 cdmBaseObject
= CdmStore
.getService(
235 IClassificationService
.class).load(
236 UUID
.fromString(uuid
));
238 if (cdmBaseObject
== null) {
242 pathList
.add(cdmBaseObject
);
244 return new TreePath(pathList
.toArray());
249 public void saveState(IMemento aMemento
) {
259 * a {@link org.eclipse.ui.IMemento} object.
260 * @param progressMonitor
261 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
263 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
264 if (memento
== null) {
267 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
270 super.saveState(memento
);
272 memento
= memento
.createChild(TREE_PATHS
);
273 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
275 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
277 for (TreePath treePath
: treePaths
) {
278 int pathLength
= treePath
.getSegmentCount();
280 for (int i
= 0; i
< pathLength
; i
++) {
281 Object segment
= treePath
.getSegment(i
);
282 if (segment
instanceof CdmBase
) {
283 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
286 NavigationUtil
.warn(getClass(),
287 "Non-taxon tree path segment " + segment
);
290 memento
.createChild(TREE_PATH
, path
.trim());
299 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
304 * getConversationHolder
307 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
311 public ConversationHolder
getConversationHolder() {
319 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
320 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
324 public void update(CdmDataChangeMap changeEvents
) {
325 if (dataChangeBehavior
== null) {
326 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
329 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
334 public String
getFrameToolTipText(Object element
) {
335 if (element
instanceof Root
) {
336 return "Taxonomic Tree";
338 return super.getFrameToolTipText(element
);
344 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
348 public void dispose() {
350 dataChangeBehavior
= null;
351 if (conversation
!= null) {
352 conversation
.unregisterForDataStoreChanges(this);
359 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
363 public void setFocus() {
364 // logger.warn("Setting focus to navigator");
366 if (getConversationHolder() != null) {
367 getConversationHolder().bind();
375 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
376 * (eu.etaxonomy.cdm.model.common.CdmBase)
380 public boolean postOperation(CdmBase objectAffectedByOperation
) {
381 // nothing to do here
391 * a {@link org.eclipse.ui.IMemento} object.
393 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
395 public void save(IMemento memento
, IProgressMonitor monitor
) {
396 saveTreeState(memento
, monitor
);
397 if (conversation
!= null) {
398 conversation
.unregisterForDataStoreChanges(this);
405 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
406 NavigationUtil
.executeEditHandler();
407 // If the double click is passed up to the super-class it will
408 // expand/collapse trees.
409 // We do not want that
410 // super.handleDoubleClick(anEvent);
421 public boolean onComplete() {
428 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
432 public void showBusy(boolean busy
) {
433 super.showBusy(busy
);
434 getCommonViewer().getControl().setEnabled(!busy
);
436 partNameCache
= getPartName();
437 setPartName("Loading datasources");
439 if (partNameCache
!= null) {
440 setPartName(partNameCache
);
447 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
450 public void update(Observable o
, Object arg
) {
451 if(o
instanceof LoginManager
){