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
.ISelectionChangedListener
;
25 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
26 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
27 import org
.eclipse
.jface
.viewers
.TreePath
;
28 import org
.eclipse
.ui
.IMemento
;
29 import org
.eclipse
.ui
.IViewSite
;
30 import org
.eclipse
.ui
.PartInitException
;
31 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
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
.taxon
.Classification
;
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
.model
.MessagingUtils
;
43 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
44 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
49 * Taxonomic tree implementation using Common Navigator Framework.
56 public class TaxonNavigator
extends CommonNavigator
implements
57 IPostOperationEnabled
, IConversationEnabled
, Observer
{
61 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
63 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
65 private static final String TREE_PATH
= "treepath";
67 private static final String TREE_PATHS
= "treepaths";
69 private ConversationHolder conversation
;
71 private String partNameCache
;
73 private IDataChangeBehavior dataChangeBehavior
;
78 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
82 protected IAdaptable
getInitialInput() {
84 TaxonComparatorSearch comparator
= new TaxonComparatorSearch();
85 TaxonNodeComparator viewerComparator
= new TaxonNodeComparator(comparator
);
86 this.getCommonViewer().setComparator(viewerComparator
);
87 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
90 // public void selectionChanged(SelectionChangedEvent arg0) {
91 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
93 // Object firstElement = selection.getFirstElement();
95 // if (!(firstElement instanceof Classification)){
96 // //NavigationUtil.selectInNavigator(firstElement, null);
97 // NavigationUtil.openEditor(firstElement);
103 if (CdmStore
.isActive()) {
105 // TODO when closing and reopening the taxon navigator
106 // we do not preserve state. Closing the view, in contrary to
107 // closing the whole application
108 // should be handled by the state manager too
110 return new Root(conversation
);
112 return new EmptyRoot();
117 public void init(IViewSite site
) throws PartInitException
{
128 if (CdmStore
.isActive() && conversation
== null) {
129 conversation
= CdmStore
.createConversation();
130 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
132 CdmStore
.getLoginManager().addObserver(this);
136 * Refresh this navigators viewer
138 public void refresh() {
139 if(getConversationHolder() != null){
140 getConversationHolder().bind();
141 //FIXME : Need to make sure this is a stable fix (ticket 3822)
142 if(!getConversationHolder().isCompleted()){
143 getConversationHolder().commit();
146 getCommonViewer().refresh();
150 * Removes all content
152 public void clear() {
153 getCommonViewer().setInput(new EmptyRoot());
162 * a {@link org.eclipse.ui.IMemento} object.
164 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
166 public void restore(IMemento memento
, IProgressMonitor monitor
) {
167 if (memento
== null) {
168 getCommonViewer().setInput(new Root(conversation
));
172 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
173 IMemento
[] treePathMementos
= null;
175 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
177 if (treePathsMemento
!= null) {
178 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
179 mementoWork
= treePathMementos
.length
;
181 // begin the monitor with steps for all tree paths and steps for
183 // conversation s.o., refreshing the tree and setting the paths
184 IProgressMonitor subProgressMonitor
= NavigationUtil
185 .getSubProgressMonitor(monitor
, 1);
187 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
188 1 + mementoWork
+ 5);
189 subProgressMonitor
.subTask("Restoring Taxon Navigator");
190 subProgressMonitor
.worked(1);
192 conversation
= CdmStore
.createConversation();
193 subProgressMonitor
.worked(1);
194 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
195 subProgressMonitor
.worked(1);
196 getCommonViewer().setInput(new Root(conversation
));
197 subProgressMonitor
.worked(1);
198 getCommonViewer().refresh();
199 subProgressMonitor
.worked(1);
201 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
202 for (IMemento treePathMemento
: treePathMementos
) {
203 TreePath treePath
= createTreePathFromString(treePathMemento
205 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
206 treePaths
.add(treePath
);
207 subProgressMonitor
.worked(1);
211 if (treePaths
.size() > 0) {
212 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
213 treePaths
.toArray(new TreePath
[0]));
214 subProgressMonitor
.worked(1);
216 subProgressMonitor
.done();
223 private TreePath
createTreePathFromString(String string
) {
225 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
227 if (string
.length() == 0) {
231 for (String uuid
: string
.split(" ")) {
232 CdmBase cdmBaseObject
= CdmStore
.getService(
233 IClassificationService
.class).getTaxonNodeByUuid(
234 UUID
.fromString(uuid
));
235 if (cdmBaseObject
== null) {
236 // is this a tree uuid?
237 cdmBaseObject
= CdmStore
.getService(
238 IClassificationService
.class).load(
239 UUID
.fromString(uuid
));
241 if (cdmBaseObject
== null) {
245 pathList
.add(cdmBaseObject
);
247 return new TreePath(pathList
.toArray());
252 public void saveState(IMemento aMemento
) {
262 * a {@link org.eclipse.ui.IMemento} object.
263 * @param progressMonitor
264 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
266 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
267 if (memento
== null) {
270 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
273 super.saveState(memento
);
275 memento
= memento
.createChild(TREE_PATHS
);
276 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
278 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
280 for (TreePath treePath
: treePaths
) {
281 int pathLength
= treePath
.getSegmentCount();
283 for (int i
= 0; i
< pathLength
; i
++) {
284 Object segment
= treePath
.getSegment(i
);
285 if (segment
instanceof CdmBase
) {
286 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
289 MessagingUtils
.warn(getClass(),
290 "Non-taxon tree path segment " + segment
);
293 memento
.createChild(TREE_PATH
, path
.trim());
302 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
307 * getConversationHolder
310 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
314 public ConversationHolder
getConversationHolder() {
322 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
323 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
327 public void update(CdmDataChangeMap changeEvents
) {
328 if (dataChangeBehavior
== null) {
329 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
332 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
337 public String
getFrameToolTipText(Object element
) {
338 if (element
instanceof Root
) {
339 return "Taxonomic Tree";
341 return super.getFrameToolTipText(element
);
347 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
351 public void dispose() {
353 dataChangeBehavior
= null;
354 if (conversation
!= null) {
355 conversation
.unregisterForDataStoreChanges(this);
362 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
366 public void setFocus() {
367 // logger.warn("Setting focus to navigator");
369 if (getConversationHolder() != null) {
370 getConversationHolder().bind();
378 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
379 * (eu.etaxonomy.cdm.model.common.CdmBase)
383 public boolean postOperation(CdmBase objectAffectedByOperation
) {
384 // nothing to do here
394 * a {@link org.eclipse.ui.IMemento} object.
396 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
398 public void save(IMemento memento
, IProgressMonitor monitor
) {
399 saveTreeState(memento
, monitor
);
400 if (conversation
!= null) {
401 conversation
.unregisterForDataStoreChanges(this);
408 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
409 NavigationUtil
.executeEditHandler();
410 // If the double click is passed up to the super-class it will
411 // expand/collapse trees.
412 // We do not want that
413 // super.handleDoubleClick(anEvent);
424 public boolean onComplete() {
431 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
435 public void showBusy(boolean busy
) {
436 super.showBusy(busy
);
437 getCommonViewer().getControl().setEnabled(!busy
);
439 partNameCache
= getPartName();
440 setPartName("Loading datasources");
442 if (partNameCache
!= null) {
443 setPartName(partNameCache
);
450 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
453 public void update(Observable o
, Object arg
) {
454 if(o
instanceof LoginManager
){