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
.navigation
.NavigationUtil
;
43 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
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() {
83 TaxonComparatorSearch comparator
= new TaxonComparatorSearch();
84 TaxonNodeComparator viewerComparator
= new TaxonNodeComparator(comparator
);
85 this.getCommonViewer().setComparator(viewerComparator
);
86 this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
89 public void selectionChanged(SelectionChangedEvent arg0
) {
90 IStructuredSelection selection
= (IStructuredSelection
) getCommonViewer().getSelection();
92 Object firstElement
= selection
.getFirstElement();
94 if (!(firstElement
instanceof Classification
)){
95 //NavigationUtil.selectInNavigator(firstElement, null);
96 NavigationUtil
.openEditor(firstElement
);
102 if (CdmStore
.isActive()) {
104 // TODO when closing and reopening the taxon navigator
105 // we do not preserve state. Closing the view, in contrary to
106 // closing the whole application
107 // should be handled by the state manager too
109 return new Root(conversation
);
111 return new EmptyRoot();
116 public void init(IViewSite site
) throws PartInitException
{
127 if (CdmStore
.isActive() && conversation
== null) {
128 conversation
= CdmStore
.createConversation();
129 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
131 CdmStore
.getLoginManager().addObserver(this);
135 * Refresh this navigators viewer
137 public void refresh() {
138 if(getConversationHolder() != null){
139 getConversationHolder().bind();
140 //FIXME : Need to make sure this is a stable fix (ticket 3822)
141 if(!getConversationHolder().isCompleted()){
142 getConversationHolder().commit();
145 getCommonViewer().refresh();
149 * Removes all content
151 public void clear() {
152 getCommonViewer().setInput(new EmptyRoot());
161 * a {@link org.eclipse.ui.IMemento} object.
163 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
165 public void restore(IMemento memento
, IProgressMonitor monitor
) {
166 if (memento
== null) {
167 getCommonViewer().setInput(new Root(conversation
));
171 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
172 IMemento
[] treePathMementos
= null;
174 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
176 if (treePathsMemento
!= null) {
177 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
178 mementoWork
= treePathMementos
.length
;
180 // begin the monitor with steps for all tree paths and steps for
182 // conversation s.o., refreshing the tree and setting the paths
183 IProgressMonitor subProgressMonitor
= NavigationUtil
184 .getSubProgressMonitor(monitor
, 1);
186 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
187 1 + mementoWork
+ 5);
188 subProgressMonitor
.subTask("Restoring Taxon Navigator");
189 subProgressMonitor
.worked(1);
191 conversation
= CdmStore
.createConversation();
192 subProgressMonitor
.worked(1);
193 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
194 subProgressMonitor
.worked(1);
195 getCommonViewer().setInput(new Root(conversation
));
196 subProgressMonitor
.worked(1);
197 getCommonViewer().refresh();
198 subProgressMonitor
.worked(1);
200 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
201 for (IMemento treePathMemento
: treePathMementos
) {
202 TreePath treePath
= createTreePathFromString(treePathMemento
204 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
205 treePaths
.add(treePath
);
206 subProgressMonitor
.worked(1);
210 if (treePaths
.size() > 0) {
211 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
212 treePaths
.toArray(new TreePath
[0]));
213 subProgressMonitor
.worked(1);
215 subProgressMonitor
.done();
222 private TreePath
createTreePathFromString(String string
) {
224 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
226 if (string
.length() == 0) {
230 for (String uuid
: string
.split(" ")) {
231 CdmBase cdmBaseObject
= CdmStore
.getService(
232 IClassificationService
.class).getTaxonNodeByUuid(
233 UUID
.fromString(uuid
));
234 if (cdmBaseObject
== null) {
235 // is this a tree uuid?
236 cdmBaseObject
= CdmStore
.getService(
237 IClassificationService
.class).load(
238 UUID
.fromString(uuid
));
240 if (cdmBaseObject
== null) {
244 pathList
.add(cdmBaseObject
);
246 return new TreePath(pathList
.toArray());
251 public void saveState(IMemento aMemento
) {
261 * a {@link org.eclipse.ui.IMemento} object.
262 * @param progressMonitor
263 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
265 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
266 if (memento
== null) {
269 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
272 super.saveState(memento
);
274 memento
= memento
.createChild(TREE_PATHS
);
275 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
277 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
279 for (TreePath treePath
: treePaths
) {
280 int pathLength
= treePath
.getSegmentCount();
282 for (int i
= 0; i
< pathLength
; i
++) {
283 Object segment
= treePath
.getSegment(i
);
284 if (segment
instanceof CdmBase
) {
285 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
288 NavigationUtil
.warn(getClass(),
289 "Non-taxon tree path segment " + segment
);
292 memento
.createChild(TREE_PATH
, path
.trim());
301 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
306 * getConversationHolder
309 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
313 public ConversationHolder
getConversationHolder() {
321 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
322 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
326 public void update(CdmDataChangeMap changeEvents
) {
327 if (dataChangeBehavior
== null) {
328 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
331 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
336 public String
getFrameToolTipText(Object element
) {
337 if (element
instanceof Root
) {
338 return "Taxonomic Tree";
340 return super.getFrameToolTipText(element
);
346 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
350 public void dispose() {
352 dataChangeBehavior
= null;
353 if (conversation
!= null) {
354 conversation
.unregisterForDataStoreChanges(this);
361 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
365 public void setFocus() {
366 // logger.warn("Setting focus to navigator");
368 if (getConversationHolder() != null) {
369 getConversationHolder().bind();
377 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
378 * (eu.etaxonomy.cdm.model.common.CdmBase)
382 public boolean postOperation(CdmBase objectAffectedByOperation
) {
383 // nothing to do here
393 * a {@link org.eclipse.ui.IMemento} object.
395 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
397 public void save(IMemento memento
, IProgressMonitor monitor
) {
398 saveTreeState(memento
, monitor
);
399 if (conversation
!= null) {
400 conversation
.unregisterForDataStoreChanges(this);
407 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
408 NavigationUtil
.executeEditHandler();
409 // If the double click is passed up to the super-class it will
410 // expand/collapse trees.
411 // We do not want that
412 // super.handleDoubleClick(anEvent);
423 public boolean onComplete() {
430 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
434 public void showBusy(boolean busy
) {
435 super.showBusy(busy
);
436 getCommonViewer().getControl().setEnabled(!busy
);
438 partNameCache
= getPartName();
439 setPartName("Loading datasources");
441 if (partNameCache
!= null) {
442 setPartName(partNameCache
);
449 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
452 public void update(Observable o
, Object arg
) {
453 if(o
instanceof LoginManager
){