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
.model
.taxon
.Classification
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonComparatorSearch
;
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
.model
.MessagingUtils
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
41 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
42 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
43 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
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
, ICdmEntitySessionEnabled
{
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 ICdmEntitySession cdmEntitySession
;
72 private String partNameCache
;
74 private IDataChangeBehavior dataChangeBehavior
;
81 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
85 protected IAdaptable
getInitialInput() {
87 TaxonComparatorSearch 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
113 root
= new Root(conversation
);
116 return new EmptyRoot();
121 public void init(IViewSite site
) throws PartInitException
{
133 if (CdmStore
.isActive() && conversation
== null) {
134 conversation
= CdmStore
.createConversation();
135 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
137 if (CdmStore
.isActive() && cdmEntitySession
== null) {
138 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
139 cdmEntitySession
.registerForDataStoreChanges(TaxonNavigator
.this);
141 CdmStore
.getLoginManager().addObserver(this);
145 * Refresh this navigators viewer
147 public void refresh() {
148 if(getConversationHolder() != null){
149 getConversationHolder().bind();
150 //FIXME : Need to make sure this is a stable fix (ticket 3822)
151 if(!getConversationHolder().isCompleted()){
152 getConversationHolder().commit();
155 if (cdmEntitySession
!= null) {
156 cdmEntitySession
.fireNotifications();
158 getCommonViewer().refresh();
162 * Refresh this navigators viewer
164 public void refresh(Set objects
) {
166 for(Object obj
: objects
) {
167 getCommonViewer().refresh(obj
);
172 * Removes all content
174 public void clear() {
175 getCommonViewer().setInput(new EmptyRoot());
184 * a {@link org.eclipse.ui.IMemento} object.
186 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
188 public void restore(IMemento memento
, IProgressMonitor monitor
) {
189 root
= new Root(conversation
);
190 if (memento
== null) {
191 getCommonViewer().setInput(root
);
195 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
196 IMemento
[] treePathMementos
= null;
198 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
200 if (treePathsMemento
!= null) {
201 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
202 mementoWork
= treePathMementos
.length
;
204 // begin the monitor with steps for all tree paths and steps for
206 // conversation s.o., refreshing the tree and setting the paths
207 IProgressMonitor subProgressMonitor
= NavigationUtil
208 .getSubProgressMonitor(monitor
, 1);
210 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
211 1 + mementoWork
+ 5);
212 subProgressMonitor
.subTask("Restoring Taxon Navigator");
213 subProgressMonitor
.worked(1);
215 conversation
= CdmStore
.createConversation();
216 subProgressMonitor
.worked(1);
217 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
218 subProgressMonitor
.worked(1);
219 getCommonViewer().setInput(root
);
220 subProgressMonitor
.worked(1);
221 getCommonViewer().refresh();
222 subProgressMonitor
.worked(1);
224 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
225 for (IMemento treePathMemento
: treePathMementos
) {
226 TreePath treePath
= createTreePathFromString(treePathMemento
228 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
229 treePaths
.add(treePath
);
230 subProgressMonitor
.worked(1);
234 if (treePaths
.size() > 0) {
235 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
236 treePaths
.toArray(new TreePath
[0]));
237 subProgressMonitor
.worked(1);
239 subProgressMonitor
.done();
246 private TreePath
createTreePathFromString(String string
) {
248 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
250 if (string
.length() == 0) {
254 for (String uuid
: string
.split(" ")) {
255 CdmBase cdmBaseObject
= CdmStore
.getService(
256 IClassificationService
.class).getTaxonNodeByUuid(
257 UUID
.fromString(uuid
));
258 if (cdmBaseObject
== null) {
259 // is this a tree uuid?
260 cdmBaseObject
= CdmStore
.getService(
261 IClassificationService
.class).load(
262 UUID
.fromString(uuid
));
264 if (cdmBaseObject
== null) {
268 pathList
.add(cdmBaseObject
);
270 return new TreePath(pathList
.toArray());
275 public void saveState(IMemento aMemento
) {
285 * a {@link org.eclipse.ui.IMemento} object.
286 * @param progressMonitor
287 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
289 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
290 if (memento
== null) {
293 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
296 super.saveState(memento
);
298 memento
= memento
.createChild(TREE_PATHS
);
299 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
301 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
303 for (TreePath treePath
: treePaths
) {
304 int pathLength
= treePath
.getSegmentCount();
306 for (int i
= 0; i
< pathLength
; i
++) {
307 Object segment
= treePath
.getSegment(i
);
308 if (segment
instanceof CdmBase
) {
309 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
312 MessagingUtils
.warn(getClass(),
313 "Non-taxon tree path segment " + segment
);
316 memento
.createChild(TREE_PATH
, path
.trim());
325 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
330 * getConversationHolder
333 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
337 public ConversationHolder
getConversationHolder() {
345 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
346 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
350 public void update(CdmDataChangeMap changeEvents
) {
351 if (dataChangeBehavior
== null) {
352 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
355 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
360 public String
getFrameToolTipText(Object element
) {
361 if (element
instanceof Root
) {
362 return "Taxonomic Tree";
364 return super.getFrameToolTipText(element
);
370 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
374 public void dispose() {
376 dataChangeBehavior
= null;
377 if (conversation
!= null) {
378 conversation
.unregisterForDataStoreChanges(this);
380 if(cdmEntitySession
!= null) {
381 cdmEntitySession
.dispose();
388 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
392 public void setFocus() {
393 // logger.warn("Setting focus to navigator");
395 if (getConversationHolder() != null) {
396 getConversationHolder().bind();
398 if(cdmEntitySession
!= null) {
399 cdmEntitySession
.bind();
407 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
408 * (eu.etaxonomy.cdm.model.common.CdmBase)
412 public boolean postOperation(CdmBase objectAffectedByOperation
) {
413 // nothing to do here
423 * a {@link org.eclipse.ui.IMemento} object.
425 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
427 public void save(IMemento memento
, IProgressMonitor monitor
) {
428 saveTreeState(memento
, monitor
);
429 if (conversation
!= null) {
430 conversation
.unregisterForDataStoreChanges(this);
437 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
438 NavigationUtil
.executeEditHandler();
439 // If the double click is passed up to the super-class it will
440 // expand/collapse trees.
441 // We do not want that
442 // super.handleDoubleClick(anEvent);
453 public boolean onComplete() {
460 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
464 public void showBusy(boolean busy
) {
465 super.showBusy(busy
);
466 getCommonViewer().getControl().setEnabled(!busy
);
468 partNameCache
= getPartName();
469 setPartName("Loading datasources");
471 if (partNameCache
!= null) {
472 setPartName(partNameCache
);
479 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
482 public void update(Observable o
, Object arg
) {
483 if(o
instanceof LoginManager
){
490 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
493 public ICdmEntitySession
getCdmEntitySession() {
494 return cdmEntitySession
;
498 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
501 public List
<Classification
> getRootEntities() {
503 return root
.getParentBeans();