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
.application
.CdmApplicationState
;
32 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
;
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
.model
.taxon
.TaxonNaturalComparator
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
41 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
42 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
43 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
44 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
45 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
46 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
47 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
48 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
49 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
50 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
51 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
54 * Taxonomic tree implementation using Common Navigator Framework.
61 public class TaxonNavigator
extends CommonNavigator
implements
62 IPostOperationEnabled
, IConversationEnabled
, Observer
, ICdmEntitySessionEnabled
{
66 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
68 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
70 private static final String TREE_PATH
= "treepath";
72 private static final String TREE_PATHS
= "treepaths";
74 private ConversationHolder conversation
;
76 private ICdmEntitySession cdmEntitySession
;
78 private String partNameCache
;
80 private IDataChangeBehavior dataChangeBehavior
;
87 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
91 protected IAdaptable
getInitialInput() {
92 Comparator comparator
;
93 if (PreferencesUtil
.getSortNodesNaturally()){
94 comparator
= new TaxonNaturalComparator();
96 comparator
= new TaxonComparatorSearch();
98 TaxonNodeComparator viewerComparator
= new TaxonNodeComparator(comparator
);
99 this.getCommonViewer().setComparator(viewerComparator
);
100 setLinkingEnabled(true);
101 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
104 // public void selectionChanged(SelectionChangedEvent arg0) {
105 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
107 // Object firstElement = selection.getFirstElement();
109 // if (!(firstElement instanceof Classification)){
110 // //NavigationUtil.selectInNavigator(firstElement, null);
111 // NavigationUtil.openEditor(firstElement);
117 if (CdmStore
.isActive()) {
119 // TODO when closing and reopening the taxon navigator
120 // we do not preserve state. Closing the view, in contrary to
121 // closing the whole application
122 // should be handled by the state manager too
123 root
= new Root(conversation
);
126 return new EmptyRoot();
131 public void init(IViewSite site
) throws PartInitException
{
143 if (CdmStore
.isActive() && conversation
== null) {
144 conversation
= CdmStore
.createConversation();
145 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
147 if (CdmStore
.isActive() && cdmEntitySession
== null) {
148 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
149 CdmApplicationState
.getCurrentDataChangeService().register(this);
151 CdmStore
.getLoginManager().addObserver(this);
155 * Refresh this navigators viewer
157 public void refresh() {
158 if(getConversationHolder() != null){
159 getConversationHolder().bind();
160 //FIXME : Need to make sure this is a stable fix (ticket 3822)
161 if(!getConversationHolder().isCompleted()){
162 getConversationHolder().commit();
165 if (cdmEntitySession
!= null) {
166 cdmEntitySession
.fireNotifications();
168 getCommonViewer().refresh();
172 * Refresh this navigators viewer
174 public void refresh(Set objects
) {
175 for(Object obj
: objects
) {
176 getCommonViewer().refresh(obj
);
181 * Removes all content
183 public void clear() {
184 getCommonViewer().setInput(new EmptyRoot());
193 * a {@link org.eclipse.ui.IMemento} object.
195 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
197 public void restore(IMemento memento
, IProgressMonitor monitor
) {
198 root
= new Root(conversation
);
199 if (memento
== null) {
200 getCommonViewer().setInput(root
);
204 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
205 IMemento
[] treePathMementos
= null;
207 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
209 if (treePathsMemento
!= null) {
210 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
211 mementoWork
= treePathMementos
.length
;
213 // begin the monitor with steps for all tree paths and steps for
215 // conversation s.o., refreshing the tree and setting the paths
216 IProgressMonitor subProgressMonitor
= NavigationUtil
217 .getSubProgressMonitor(monitor
, 1);
219 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
220 1 + mementoWork
+ 5);
221 subProgressMonitor
.subTask("Restoring Taxon Navigator");
222 subProgressMonitor
.worked(1);
224 conversation
= CdmStore
.createConversation();
225 subProgressMonitor
.worked(1);
226 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
227 subProgressMonitor
.worked(1);
228 getCommonViewer().setInput(root
);
229 subProgressMonitor
.worked(1);
230 getCommonViewer().refresh();
231 subProgressMonitor
.worked(1);
233 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
234 for (IMemento treePathMemento
: treePathMementos
) {
235 TreePath treePath
= createTreePathFromString(treePathMemento
237 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
238 treePaths
.add(treePath
);
239 subProgressMonitor
.worked(1);
243 if (treePaths
.size() > 0) {
244 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
245 treePaths
.toArray(new TreePath
[0]));
246 subProgressMonitor
.worked(1);
248 subProgressMonitor
.done();
255 private TreePath
createTreePathFromString(String string
) {
257 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
259 if (string
.length() == 0) {
263 for (String uuid
: string
.split(" ")) {
264 CdmBase cdmBaseObject
= CdmStore
.getService(
265 IClassificationService
.class).getTaxonNodeByUuid(
266 UUID
.fromString(uuid
));
267 if (cdmBaseObject
== null) {
268 // is this a tree uuid?
269 cdmBaseObject
= CdmStore
.getService(
270 IClassificationService
.class).load(
271 UUID
.fromString(uuid
));
273 if (cdmBaseObject
== null) {
277 pathList
.add(cdmBaseObject
);
279 return new TreePath(pathList
.toArray());
284 public void saveState(IMemento aMemento
) {
294 * a {@link org.eclipse.ui.IMemento} object.
295 * @param progressMonitor
296 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
298 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
299 if (memento
== null) {
302 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
305 super.saveState(memento
);
307 memento
= memento
.createChild(TREE_PATHS
);
308 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
310 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
312 for (TreePath treePath
: treePaths
) {
313 int pathLength
= treePath
.getSegmentCount();
315 for (int i
= 0; i
< pathLength
; i
++) {
316 Object segment
= treePath
.getSegment(i
);
317 if (segment
instanceof CdmBase
) {
318 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
321 MessagingUtils
.warn(getClass(),
322 "Non-taxon tree path segment " + segment
);
325 memento
.createChild(TREE_PATH
, path
.trim());
334 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
339 * getConversationHolder
342 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
346 public ConversationHolder
getConversationHolder() {
354 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
355 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
359 public void update(CdmDataChangeMap changeEvents
) {
360 if (dataChangeBehavior
== null) {
361 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
364 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
369 public String
getFrameToolTipText(Object element
) {
370 if (element
instanceof Root
) {
371 return "Taxonomic Tree";
373 return super.getFrameToolTipText(element
);
379 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
383 public void dispose() {
385 dataChangeBehavior
= null;
386 if (conversation
!= null) {
387 conversation
.unregisterForDataStoreChanges(this);
389 if(cdmEntitySession
!= null) {
390 cdmEntitySession
.dispose();
397 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
401 public void setFocus() {
402 // logger.warn("Setting focus to navigator");
404 if (getConversationHolder() != null) {
405 getConversationHolder().bind();
407 if(cdmEntitySession
!= null) {
408 cdmEntitySession
.bind();
416 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
417 * (eu.etaxonomy.cdm.model.common.CdmBase)
421 public boolean postOperation(CdmBase objectAffectedByOperation
) {
422 // nothing to do here
432 * a {@link org.eclipse.ui.IMemento} object.
434 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
436 public void save(IMemento memento
, IProgressMonitor monitor
) {
437 saveTreeState(memento
, monitor
);
438 if (conversation
!= null) {
439 conversation
.unregisterForDataStoreChanges(this);
446 protected void handleDoubleClick(DoubleClickEvent anEvent
) {
447 NavigationUtil
.executeEditHandler();
448 // If the double click is passed up to the super-class it will
449 // expand/collapse trees.
450 // We do not want that
451 // super.handleDoubleClick(anEvent);
462 public boolean onComplete() {
469 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
473 public void showBusy(boolean busy
) {
474 super.showBusy(busy
);
475 getCommonViewer().getControl().setEnabled(!busy
);
477 partNameCache
= getPartName();
478 setPartName("Loading datasources");
480 if (partNameCache
!= null) {
481 setPartName(partNameCache
);
488 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
491 public void update(Observable o
, Object arg
) {
492 if(o
instanceof LoginManager
){
499 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
502 public ICdmEntitySession
getCdmEntitySession() {
503 return cdmEntitySession
;
507 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
510 public List
<Classification
> getRootEntities() {
512 return root
.getParentBeans();
518 * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
521 public void onChange(CdmChangeEvent event
) {
522 for(CdmBase cb
: event
.getChangedObjects()) {
523 if(cb
instanceof TaxonNode
) {
524 getCommonViewer().refresh(cb
);