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
.Comparator
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
17 import java
.util
.Observable
;
18 import java
.util
.Observer
;
20 import java
.util
.UUID
;
22 import org
.eclipse
.core
.runtime
.IAdaptable
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
25 import org
.eclipse
.jface
.viewers
.TreePath
;
26 import org
.eclipse
.ui
.IMemento
;
27 import org
.eclipse
.ui
.IViewSite
;
28 import org
.eclipse
.ui
.PartInitException
;
29 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
31 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
32 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonComparatorSearch
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNaturalComparator
;
37 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
38 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
39 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
40 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
41 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
42 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
43 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
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() {
82 Comparator comparator
;
83 if (PreferencesUtil
.getSortNodesNaturally()){
84 comparator
= new TaxonNaturalComparator();
86 comparator
= new TaxonComparatorSearch();
88 TaxonNodeComparator viewerComparator
= new TaxonNodeComparator(comparator
);
89 this.getCommonViewer().setComparator(viewerComparator
);
90 setLinkingEnabled(true);
91 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
94 // public void selectionChanged(SelectionChangedEvent arg0) {
95 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
97 // Object firstElement = selection.getFirstElement();
99 // if (!(firstElement instanceof Classification)){
100 // //NavigationUtil.selectInNavigator(firstElement, null);
101 // NavigationUtil.openEditor(firstElement);
107 if (CdmStore
.isActive()) {
109 // TODO when closing and reopening the taxon navigator
110 // we do not preserve state. Closing the view, in contrary to
111 // closing the whole application
112 // should be handled by the state manager too
114 return new Root(conversation
);
116 return new EmptyRoot();
121 public void init(IViewSite site
) throws PartInitException
{
132 if (CdmStore
.isActive() && conversation
== null) {
133 conversation
= CdmStore
.createConversation();
134 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
136 CdmStore
.getLoginManager().addObserver(this);
140 * Refresh this navigators viewer
142 public void refresh() {
143 if(getConversationHolder() != null){
144 getConversationHolder().bind();
145 //FIXME : Need to make sure this is a stable fix (ticket 3822)
146 if(!getConversationHolder().isCompleted()){
147 getConversationHolder().commit();
150 getCommonViewer().refresh();
154 * Removes all content
156 public void clear() {
157 getCommonViewer().setInput(new EmptyRoot());
166 * a {@link org.eclipse.ui.IMemento} object.
168 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
170 public void restore(IMemento memento
, IProgressMonitor monitor
) {
171 if (memento
== null) {
172 getCommonViewer().setInput(new Root(conversation
));
176 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
177 IMemento
[] treePathMementos
= null;
179 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
181 if (treePathsMemento
!= null) {
182 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
183 mementoWork
= treePathMementos
.length
;
185 // begin the monitor with steps for all tree paths and steps for
187 // conversation s.o., refreshing the tree and setting the paths
188 IProgressMonitor subProgressMonitor
= NavigationUtil
189 .getSubProgressMonitor(monitor
, 1);
191 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
192 1 + mementoWork
+ 5);
193 subProgressMonitor
.subTask("Restoring Taxon Navigator");
194 subProgressMonitor
.worked(1);
196 conversation
= CdmStore
.createConversation();
197 subProgressMonitor
.worked(1);
198 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
199 subProgressMonitor
.worked(1);
200 getCommonViewer().setInput(new Root(conversation
));
201 subProgressMonitor
.worked(1);
202 getCommonViewer().refresh();
203 subProgressMonitor
.worked(1);
205 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
206 for (IMemento treePathMemento
: treePathMementos
) {
207 TreePath treePath
= createTreePathFromString(treePathMemento
209 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
210 treePaths
.add(treePath
);
211 subProgressMonitor
.worked(1);
215 if (treePaths
.size() > 0) {
216 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
217 treePaths
.toArray(new TreePath
[0]));
218 subProgressMonitor
.worked(1);
220 subProgressMonitor
.done();
227 private TreePath
createTreePathFromString(String string
) {
229 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
231 if (string
.length() == 0) {
235 for (String uuid
: string
.split(" ")) {
236 CdmBase cdmBaseObject
= CdmStore
.getService(
237 IClassificationService
.class).getTaxonNodeByUuid(
238 UUID
.fromString(uuid
));
239 if (cdmBaseObject
== null) {
240 // is this a tree uuid?
241 cdmBaseObject
= CdmStore
.getService(
242 IClassificationService
.class).load(
243 UUID
.fromString(uuid
));
245 if (cdmBaseObject
== null) {
249 pathList
.add(cdmBaseObject
);
251 return new TreePath(pathList
.toArray());
256 public void saveState(IMemento aMemento
) {
266 * a {@link org.eclipse.ui.IMemento} object.
267 * @param progressMonitor
268 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
270 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
271 if (memento
== null) {
274 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
277 super.saveState(memento
);
279 memento
= memento
.createChild(TREE_PATHS
);
280 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
282 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
284 for (TreePath treePath
: treePaths
) {
285 int pathLength
= treePath
.getSegmentCount();
287 for (int i
= 0; i
< pathLength
; i
++) {
288 Object segment
= treePath
.getSegment(i
);
289 if (segment
instanceof CdmBase
) {
290 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
293 MessagingUtils
.warn(getClass(),
294 "Non-taxon tree path segment " + segment
);
297 memento
.createChild(TREE_PATH
, path
.trim());
306 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
311 * getConversationHolder
314 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
318 public ConversationHolder
getConversationHolder() {
326 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
327 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
331 public void update(CdmDataChangeMap changeEvents
) {
332 if (dataChangeBehavior
== null) {
333 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
336 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
341 public String
getFrameToolTipText(Object element
) {
342 if (element
instanceof Root
) {
343 return "Taxonomic Tree";
345 return super.getFrameToolTipText(element
);
351 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
355 public void dispose() {
357 dataChangeBehavior
= null;
358 if (conversation
!= null) {
359 conversation
.unregisterForDataStoreChanges(this);
366 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
370 public void setFocus() {
371 // logger.warn("Setting focus to navigator");
373 if (getConversationHolder() != null) {
374 getConversationHolder().bind();
382 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
383 * (eu.etaxonomy.cdm.model.common.CdmBase)
387 public boolean postOperation(CdmBase objectAffectedByOperation
) {
388 // nothing to do here
398 * a {@link org.eclipse.ui.IMemento} object.
400 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
402 public void save(IMemento memento
, IProgressMonitor monitor
) {
403 saveTreeState(memento
, monitor
);
404 if (conversation
!= null) {
405 conversation
.unregisterForDataStoreChanges(this);
412 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
413 NavigationUtil
.executeEditHandler();
414 // If the double click is passed up to the super-class it will
415 // expand/collapse trees.
416 // We do not want that
417 // super.handleDoubleClick(anEvent);
428 public boolean onComplete() {
435 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
439 public void showBusy(boolean busy
) {
440 super.showBusy(busy
);
441 getCommonViewer().getControl().setEnabled(!busy
);
443 partNameCache
= getPartName();
444 setPartName("Loading datasources");
446 if (partNameCache
!= null) {
447 setPartName(partNameCache
);
454 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
457 public void update(Observable o
, Object arg
) {
458 if(o
instanceof LoginManager
){