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
;
17 import java
.util
.UUID
;
19 import org
.eclipse
.core
.commands
.Command
;
20 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
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
.commands
.ICommandService
;
29 import org
.eclipse
.ui
.handlers
.IHandlerService
;
30 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
32 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
33 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
34 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
35 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
36 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
37 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
38 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
40 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
41 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
44 * Taxonomic tree implementation using Common Navigator Framework.
51 public class TaxonNavigator
extends CommonNavigator
implements IPostOperationEnabled
, IConversationEnabled
{
53 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
54 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
56 /** Constant <code>OPEN_COMMAND_ID="eu.etaxonomy.taxeditor.navigation.comma"{trunked}</code> */
57 public static final String OPEN_COMMAND_ID
= "eu.etaxonomy.taxeditor.navigation.command.editSelection";
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
;
70 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
74 protected IAdaptable
getInitialInput() {
75 if(CdmStore
.isActive()){
76 // TODO when closing and reopening the taxon navigator
77 // we do not preserve state. Closing the view, in contrary to closing the whole application
78 // should be handled by the state manager too
80 return new Root(conversation
);
82 return new EmptyRoot();
86 public void init(IViewSite site
)
87 throws PartInitException
{
96 if(CdmStore
.isActive() && conversation
== null){
97 conversation
= CdmStore
.createConversation();
98 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
103 * Refresh this navigators viewer
105 public void refresh(){
106 getConversationHolder().bind();
107 getCommonViewer().refresh();
111 * Removes all content
114 getCommonViewer().setInput(new EmptyRoot());
120 * @param memento a {@link org.eclipse.ui.IMemento} object.
121 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
123 public void restore(IMemento memento
, IProgressMonitor monitor
) {
124 if (memento
== null) {
125 getCommonViewer().setInput(new Root(conversation
));
129 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
130 IMemento
[] treePathMementos
= null;
132 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
134 if (treePathsMemento
!= null) {
135 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
136 mementoWork
= treePathMementos
.length
;
138 // begin the monitor with steps for all tree paths and steps for creating
139 // conversation s.o., refreshing the tree and setting the paths
140 IProgressMonitor subProgressMonitor
= NavigationUtil
.getSubProgressMonitor(monitor
, 1);
142 subProgressMonitor
.beginTask("Restoring Taxon Navigator", 1 + mementoWork
+ 5);
143 subProgressMonitor
.subTask("Restoring Taxon Navigator");
144 subProgressMonitor
.worked(1);
146 conversation
= CdmStore
.createConversation();
147 subProgressMonitor
.worked(1);
148 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
149 subProgressMonitor
.worked(1);
150 getCommonViewer().setInput(new Root(conversation
));
151 subProgressMonitor
.worked(1);
152 getCommonViewer().refresh();
153 subProgressMonitor
.worked(1);
157 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
158 for (IMemento treePathMemento
: treePathMementos
) {
159 TreePath treePath
= createTreePathFromString(treePathMemento
.getID());
160 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
161 treePaths
.add(treePath
);
162 subProgressMonitor
.worked(1);
166 if (treePaths
.size() > 0) {
167 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(treePaths
.toArray(new TreePath
[0]));
168 subProgressMonitor
.worked(1);
170 subProgressMonitor
.done();
177 private TreePath
createTreePathFromString(String string
) {
179 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
181 if(string
.length() == 0) return null;
183 for (String uuid
: string
.split(" ")) {
184 CdmBase cdmBaseObject
= CdmStore
.getService(IClassificationService
.class).getTaxonNodeByUuid(UUID
.fromString(uuid
));
185 if (cdmBaseObject
== null) {
186 // is this a tree uuid?
187 cdmBaseObject
= CdmStore
.getService(IClassificationService
.class).getClassificationByUuid(UUID
.fromString(uuid
));
189 if(cdmBaseObject
== null) return null;
191 pathList
.add(cdmBaseObject
);
193 return new TreePath(pathList
.toArray());
199 public void saveState(IMemento aMemento
) {
204 * <p>saveTreeState</p>
206 * @param memento a {@link org.eclipse.ui.IMemento} object.
207 * @param progressMonitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
209 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
210 if (memento
== null) {
213 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(progressMonitor
, 1);
215 super.saveState(memento
);
217 memento
= memento
.createChild(TREE_PATHS
);
218 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
220 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
222 for (TreePath treePath
: treePaths
) {
223 int pathLength
= treePath
.getSegmentCount();
225 for (int i
= 0; i
< pathLength
; i
++) {
226 Object segment
= treePath
.getSegment(i
);
227 if (segment
instanceof CdmBase
) {
228 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
231 NavigationUtil
.warn(getClass(), "Non-taxon tree path segment " + segment
);
234 memento
.createChild(TREE_PATH
, path
.trim());
240 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
243 * <p>getConversationHolder</p>
245 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
247 public ConversationHolder
getConversationHolder() {
252 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
255 public void update(CdmDataChangeMap changeEvents
) {
256 if(dataChangeBehavior
== null){
257 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
260 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
264 public String
getFrameToolTipText(Object element
) {
265 if (element
instanceof Root
) {
266 return "Taxonomic Tree";
268 return super.getFrameToolTipText(element
);
272 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
276 public void dispose() {
278 dataChangeBehavior
= null;
279 if(conversation
!= null){
280 conversation
.unregisterForDataStoreChanges(this);
285 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
289 public void setFocus() {
290 //logger.warn("Setting focus to navigator");
292 if(getConversationHolder() != null){
293 getConversationHolder().bind();
298 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
301 public boolean postOperation(CdmBase objectAffectedByOperation
) {
302 // nothing to do here
309 * @param memento a {@link org.eclipse.ui.IMemento} object.
310 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
312 public void save(IMemento memento
, IProgressMonitor monitor
) {
313 saveTreeState(memento
, monitor
);
314 if(conversation
!= null){
315 conversation
.unregisterForDataStoreChanges(this);
322 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
324 ICommandService commandService
= (ICommandService
)getSite().getService(ICommandService
.class);
326 Command command
= commandService
.getCommand(OPEN_COMMAND_ID
);
327 if(command
.isEnabled()) {
328 IHandlerService handlerService
= (IHandlerService
)getSite().getService(IHandlerService
.class);
330 handlerService
.executeCommand(OPEN_COMMAND_ID
, null);
331 } catch (NotDefinedException e
) {
332 throw new RuntimeException("Could not find open command: " + OPEN_COMMAND_ID
);
333 } catch (Exception e
) {
334 NavigationUtil
.error(getClass(), "An exception occured while trying to open a selection", e
);
337 // If the double click is passed up to the super-class it will expand/collapse trees.
338 // We do not want that
339 //super.handleDoubleClick(anEvent);
347 public boolean onComplete() {
352 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
356 public void showBusy(boolean busy
) {
357 super.showBusy(busy
);
358 getCommonViewer().getControl().setEnabled(!busy
);
360 partNameCache
= getPartName();
361 setPartName("Loading datasources");
363 if(partNameCache
!= null){
364 setPartName(partNameCache
);