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 getConversationHolder().commit();
116 getCommonViewer().refresh();
120 * Removes all content
122 public void clear() {
123 getCommonViewer().setInput(new EmptyRoot());
132 * a {@link org.eclipse.ui.IMemento} object.
134 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
136 public void restore(IMemento memento
, IProgressMonitor monitor
) {
137 if (memento
== null) {
138 getCommonViewer().setInput(new Root(conversation
));
142 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
143 IMemento
[] treePathMementos
= null;
145 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
147 if (treePathsMemento
!= null) {
148 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
149 mementoWork
= treePathMementos
.length
;
151 // begin the monitor with steps for all tree paths and steps for
153 // conversation s.o., refreshing the tree and setting the paths
154 IProgressMonitor subProgressMonitor
= NavigationUtil
155 .getSubProgressMonitor(monitor
, 1);
157 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
158 1 + mementoWork
+ 5);
159 subProgressMonitor
.subTask("Restoring Taxon Navigator");
160 subProgressMonitor
.worked(1);
162 conversation
= CdmStore
.createConversation();
163 subProgressMonitor
.worked(1);
164 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
165 subProgressMonitor
.worked(1);
166 getCommonViewer().setInput(new Root(conversation
));
167 subProgressMonitor
.worked(1);
168 getCommonViewer().refresh();
169 subProgressMonitor
.worked(1);
171 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
172 for (IMemento treePathMemento
: treePathMementos
) {
173 TreePath treePath
= createTreePathFromString(treePathMemento
175 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
176 treePaths
.add(treePath
);
177 subProgressMonitor
.worked(1);
181 if (treePaths
.size() > 0) {
182 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
183 treePaths
.toArray(new TreePath
[0]));
184 subProgressMonitor
.worked(1);
186 subProgressMonitor
.done();
193 private TreePath
createTreePathFromString(String string
) {
195 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
197 if (string
.length() == 0) {
201 for (String uuid
: string
.split(" ")) {
202 CdmBase cdmBaseObject
= CdmStore
.getService(
203 IClassificationService
.class).getTaxonNodeByUuid(
204 UUID
.fromString(uuid
));
205 if (cdmBaseObject
== null) {
206 // is this a tree uuid?
207 cdmBaseObject
= CdmStore
.getService(
208 IClassificationService
.class).load(
209 UUID
.fromString(uuid
));
211 if (cdmBaseObject
== null) {
215 pathList
.add(cdmBaseObject
);
217 return new TreePath(pathList
.toArray());
222 public void saveState(IMemento aMemento
) {
232 * a {@link org.eclipse.ui.IMemento} object.
233 * @param progressMonitor
234 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
236 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
237 if (memento
== null) {
240 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
243 super.saveState(memento
);
245 memento
= memento
.createChild(TREE_PATHS
);
246 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
248 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
250 for (TreePath treePath
: treePaths
) {
251 int pathLength
= treePath
.getSegmentCount();
253 for (int i
= 0; i
< pathLength
; i
++) {
254 Object segment
= treePath
.getSegment(i
);
255 if (segment
instanceof CdmBase
) {
256 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
259 NavigationUtil
.warn(getClass(),
260 "Non-taxon tree path segment " + segment
);
263 memento
.createChild(TREE_PATH
, path
.trim());
272 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
277 * getConversationHolder
280 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
284 public ConversationHolder
getConversationHolder() {
292 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
293 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
297 public void update(CdmDataChangeMap changeEvents
) {
298 if (dataChangeBehavior
== null) {
299 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
302 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
307 public String
getFrameToolTipText(Object element
) {
308 if (element
instanceof Root
) {
309 return "Taxonomic Tree";
311 return super.getFrameToolTipText(element
);
317 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
321 public void dispose() {
323 dataChangeBehavior
= null;
324 if (conversation
!= null) {
325 conversation
.unregisterForDataStoreChanges(this);
332 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
336 public void setFocus() {
337 // logger.warn("Setting focus to navigator");
339 if (getConversationHolder() != null) {
340 getConversationHolder().bind();
348 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
349 * (eu.etaxonomy.cdm.model.common.CdmBase)
353 public boolean postOperation(CdmBase objectAffectedByOperation
) {
354 // nothing to do here
364 * a {@link org.eclipse.ui.IMemento} object.
366 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
368 public void save(IMemento memento
, IProgressMonitor monitor
) {
369 saveTreeState(memento
, monitor
);
370 if (conversation
!= null) {
371 conversation
.unregisterForDataStoreChanges(this);
378 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
379 NavigationUtil
.executeEditHandler();
380 // If the double click is passed up to the super-class it will
381 // expand/collapse trees.
382 // We do not want that
383 // super.handleDoubleClick(anEvent);
394 public boolean onComplete() {
401 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
405 public void showBusy(boolean busy
) {
406 super.showBusy(busy
);
407 getCommonViewer().getControl().setEnabled(!busy
);
409 partNameCache
= getPartName();
410 setPartName("Loading datasources");
412 if (partNameCache
!= null) {
413 setPartName(partNameCache
);
420 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
423 public void update(Observable o
, Object arg
) {
424 if(o
instanceof LoginManager
){