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
);
82 if (CdmStore
.isActive()) {
84 // TODO when closing and reopening the taxon navigator
85 // we do not preserve state. Closing the view, in contrary to
86 // closing the whole application
87 // should be handled by the state manager too
89 return new Root(conversation
);
91 return new EmptyRoot();
96 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);
115 * Refresh this navigators viewer
117 public void refresh() {
118 if(getConversationHolder() != null){
119 getConversationHolder().bind();
120 //FIXME : Need to make sure this is a stable fix (ticket 3822)
121 if(!getConversationHolder().isCompleted()){
122 getConversationHolder().commit();
125 getCommonViewer().refresh();
129 * Removes all content
131 public void clear() {
132 getCommonViewer().setInput(new EmptyRoot());
141 * a {@link org.eclipse.ui.IMemento} object.
143 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
145 public void restore(IMemento memento
, IProgressMonitor monitor
) {
146 if (memento
== null) {
147 getCommonViewer().setInput(new Root(conversation
));
151 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
152 IMemento
[] treePathMementos
= null;
154 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
156 if (treePathsMemento
!= null) {
157 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
158 mementoWork
= treePathMementos
.length
;
160 // begin the monitor with steps for all tree paths and steps for
162 // conversation s.o., refreshing the tree and setting the paths
163 IProgressMonitor subProgressMonitor
= NavigationUtil
164 .getSubProgressMonitor(monitor
, 1);
166 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
167 1 + mementoWork
+ 5);
168 subProgressMonitor
.subTask("Restoring Taxon Navigator");
169 subProgressMonitor
.worked(1);
171 conversation
= CdmStore
.createConversation();
172 subProgressMonitor
.worked(1);
173 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
174 subProgressMonitor
.worked(1);
175 getCommonViewer().setInput(new Root(conversation
));
176 subProgressMonitor
.worked(1);
177 getCommonViewer().refresh();
178 subProgressMonitor
.worked(1);
180 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
181 for (IMemento treePathMemento
: treePathMementos
) {
182 TreePath treePath
= createTreePathFromString(treePathMemento
184 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
185 treePaths
.add(treePath
);
186 subProgressMonitor
.worked(1);
190 if (treePaths
.size() > 0) {
191 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
192 treePaths
.toArray(new TreePath
[0]));
193 subProgressMonitor
.worked(1);
195 subProgressMonitor
.done();
202 private TreePath
createTreePathFromString(String string
) {
204 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
206 if (string
.length() == 0) {
210 for (String uuid
: string
.split(" ")) {
211 CdmBase cdmBaseObject
= CdmStore
.getService(
212 IClassificationService
.class).getTaxonNodeByUuid(
213 UUID
.fromString(uuid
));
214 if (cdmBaseObject
== null) {
215 // is this a tree uuid?
216 cdmBaseObject
= CdmStore
.getService(
217 IClassificationService
.class).load(
218 UUID
.fromString(uuid
));
220 if (cdmBaseObject
== null) {
224 pathList
.add(cdmBaseObject
);
226 return new TreePath(pathList
.toArray());
231 public void saveState(IMemento aMemento
) {
241 * a {@link org.eclipse.ui.IMemento} object.
242 * @param progressMonitor
243 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
245 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
246 if (memento
== null) {
249 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
252 super.saveState(memento
);
254 memento
= memento
.createChild(TREE_PATHS
);
255 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
257 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
259 for (TreePath treePath
: treePaths
) {
260 int pathLength
= treePath
.getSegmentCount();
262 for (int i
= 0; i
< pathLength
; i
++) {
263 Object segment
= treePath
.getSegment(i
);
264 if (segment
instanceof CdmBase
) {
265 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
268 NavigationUtil
.warn(getClass(),
269 "Non-taxon tree path segment " + segment
);
272 memento
.createChild(TREE_PATH
, path
.trim());
281 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
286 * getConversationHolder
289 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
293 public ConversationHolder
getConversationHolder() {
301 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
302 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
306 public void update(CdmDataChangeMap changeEvents
) {
307 if (dataChangeBehavior
== null) {
308 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
311 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
316 public String
getFrameToolTipText(Object element
) {
317 if (element
instanceof Root
) {
318 return "Taxonomic Tree";
320 return super.getFrameToolTipText(element
);
326 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
330 public void dispose() {
332 dataChangeBehavior
= null;
333 if (conversation
!= null) {
334 conversation
.unregisterForDataStoreChanges(this);
341 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
345 public void setFocus() {
346 // logger.warn("Setting focus to navigator");
348 if (getConversationHolder() != null) {
349 getConversationHolder().bind();
357 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
358 * (eu.etaxonomy.cdm.model.common.CdmBase)
362 public boolean postOperation(CdmBase objectAffectedByOperation
) {
363 // nothing to do here
373 * a {@link org.eclipse.ui.IMemento} object.
375 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
377 public void save(IMemento memento
, IProgressMonitor monitor
) {
378 saveTreeState(memento
, monitor
);
379 if (conversation
!= null) {
380 conversation
.unregisterForDataStoreChanges(this);
387 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
388 NavigationUtil
.executeEditHandler();
389 // If the double click is passed up to the super-class it will
390 // expand/collapse trees.
391 // We do not want that
392 // super.handleDoubleClick(anEvent);
403 public boolean onComplete() {
410 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
414 public void showBusy(boolean busy
) {
415 super.showBusy(busy
);
416 getCommonViewer().getControl().setEnabled(!busy
);
418 partNameCache
= getPartName();
419 setPartName("Loading datasources");
421 if (partNameCache
!= null) {
422 setPartName(partNameCache
);
429 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
432 public void update(Observable o
, Object arg
) {
433 if(o
instanceof LoginManager
){