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
.Arrays
;
15 import java
.util
.Comparator
;
16 import java
.util
.HashMap
;
17 import java
.util
.HashSet
;
18 import java
.util
.List
;
20 import java
.util
.Observable
;
21 import java
.util
.Observer
;
23 import java
.util
.UUID
;
25 import org
.eclipse
.core
.runtime
.IAdaptable
;
26 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
27 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.TreePath
;
30 import org
.eclipse
.ui
.IMemento
;
31 import org
.eclipse
.ui
.IViewSite
;
32 import org
.eclipse
.ui
.PartInitException
;
33 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
35 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
36 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
;
37 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
.Action
;
38 import eu
.etaxonomy
.cdm
.api
.application
.ICdmChangeListener
;
39 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
40 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
41 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
42 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNaturalComparator
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByRankAndNameComparator
;
47 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
48 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
49 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
50 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
51 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
52 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
53 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
54 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
55 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
56 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
57 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
58 import eu
.etaxonomy
.taxeditor
.view
.CdmViewerChooser
;
61 * Taxonomic tree implementation using Common Navigator Framework.
68 public class TaxonNavigator
extends CommonNavigator
implements
69 IPostOperationEnabled
, IConversationEnabled
, Observer
,
70 ICdmEntitySessionEnabled
, ICdmChangeListener
{
74 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
76 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
78 private static final String TREE_PATH
= "treepath";
80 private static final String TREE_PATHS
= "treepaths";
82 private ConversationHolder conversation
;
84 private ICdmEntitySession cdmEntitySession
;
86 private String partNameCache
;
88 private IDataChangeBehavior dataChangeBehavior
;
95 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
99 protected IAdaptable
getInitialInput() {
100 Comparator
<TaxonNode
> comparator
;
101 if (PreferencesUtil
.getSortNodesNaturally()){
102 comparator
= new TaxonNaturalComparator();
104 comparator
= new TaxonNodeByRankAndNameComparator();
106 TaxonNodeNavigatorComparator viewerComparator
= new TaxonNodeNavigatorComparator(comparator
);
107 this.getCommonViewer().setComparator(viewerComparator
);
108 setLinkingEnabled(true);
109 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
112 // public void selectionChanged(SelectionChangedEvent arg0) {
113 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
115 // Object firstElement = selection.getFirstElement();
117 // if (!(firstElement instanceof Classification)){
118 // //NavigationUtil.selectInNavigator(firstElement, null);
119 // NavigationUtil.openEditor(firstElement);
125 if (CdmStore
.isActive()) {
127 // TODO when closing and reopening the taxon navigator
128 // we do not preserve state. Closing the view, in contrary to
129 // closing the whole application
130 // should be handled by the state manager too
131 root
= new Root(conversation
);
134 return new EmptyRoot();
139 public void init(IViewSite site
) throws PartInitException
{
151 if (CdmStore
.isActive() && conversation
== null) {
152 conversation
= CdmStore
.createConversation();
153 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
155 if (CdmStore
.isActive()) {
156 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
157 CdmApplicationState
.getCurrentDataChangeService().register(this);
159 CdmStore
.getLoginManager().addObserver(this);
163 * Refresh this navigators viewer
165 public void refresh() {
166 if(getConversationHolder() != null){
167 getConversationHolder().bind();
168 //FIXME : Need to make sure this is a stable fix (ticket 3822)
169 if(!getConversationHolder().isCompleted()){
170 getConversationHolder().commit();
173 getCommonViewer().refresh();
177 * Refresh this navigators viewer
179 public void refresh(Set objects
) {
180 for(Object obj
: objects
) {
181 getCommonViewer().refresh(obj
);
186 * Removes all content
188 public void clear() {
189 getCommonViewer().setInput(new EmptyRoot());
198 * a {@link org.eclipse.ui.IMemento} object.
200 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
202 public void restore(IMemento memento
, IProgressMonitor monitor
) {
203 root
= new Root(conversation
);
204 if (memento
== null) {
205 getCommonViewer().setInput(root
);
209 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
210 IMemento
[] treePathMementos
= null;
212 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
214 if (treePathsMemento
!= null) {
215 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
216 mementoWork
= treePathMementos
.length
;
218 // begin the monitor with steps for all tree paths and steps for
220 // conversation s.o., refreshing the tree and setting the paths
221 IProgressMonitor subProgressMonitor
= NavigationUtil
222 .getSubProgressMonitor(monitor
, 1);
224 subProgressMonitor
.beginTask("Restoring Taxon Navigator",
225 1 + mementoWork
+ 5);
226 subProgressMonitor
.subTask("Restoring Taxon Navigator");
227 subProgressMonitor
.worked(1);
229 conversation
= CdmStore
.createConversation();
230 subProgressMonitor
.worked(1);
231 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
232 subProgressMonitor
.worked(1);
233 getCommonViewer().setInput(root
);
234 subProgressMonitor
.worked(1);
235 getCommonViewer().refresh();
236 subProgressMonitor
.worked(1);
238 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
239 for (IMemento treePathMemento
: treePathMementos
) {
240 TreePath treePath
= createTreePathFromString(treePathMemento
242 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
243 treePaths
.add(treePath
);
244 subProgressMonitor
.worked(1);
248 if (treePaths
.size() > 0) {
249 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
250 treePaths
.toArray(new TreePath
[0]));
251 subProgressMonitor
.worked(1);
253 subProgressMonitor
.done();
260 private TreePath
createTreePathFromString(String string
) {
262 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
264 if (string
.length() == 0) {
268 for (String uuid
: string
.split(" ")) {
269 CdmBase cdmBaseObject
= CdmStore
.getService(
270 IClassificationService
.class).getTaxonNodeByUuid(
271 UUID
.fromString(uuid
));
272 if (cdmBaseObject
== null) {
273 // is this a tree uuid?
274 cdmBaseObject
= CdmStore
.getService(
275 IClassificationService
.class).load(
276 UUID
.fromString(uuid
));
278 if (cdmBaseObject
== null) {
282 pathList
.add(cdmBaseObject
);
284 return new TreePath(pathList
.toArray());
289 public void saveState(IMemento aMemento
) {
299 * a {@link org.eclipse.ui.IMemento} object.
300 * @param progressMonitor
301 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
303 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
304 if (memento
== null) {
307 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
310 super.saveState(memento
);
312 memento
= memento
.createChild(TREE_PATHS
);
313 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
315 monitor
.beginTask("Saving Taxon Navigator State", treePaths
.length
);
317 for (TreePath treePath
: treePaths
) {
318 int pathLength
= treePath
.getSegmentCount();
320 for (int i
= 0; i
< pathLength
; i
++) {
321 Object segment
= treePath
.getSegment(i
);
322 if (segment
instanceof CdmBase
) {
323 path
+= ((CdmBase
) segment
).getUuid().toString() + " ";
326 MessagingUtils
.warn(getClass(),
327 "Non-taxon tree path segment " + segment
);
330 memento
.createChild(TREE_PATH
, path
.trim());
339 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
344 * getConversationHolder
347 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
351 public ConversationHolder
getConversationHolder() {
359 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
360 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
364 public void update(CdmDataChangeMap changeEvents
) {
365 if (dataChangeBehavior
== null) {
366 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
369 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
374 public String
getFrameToolTipText(Object element
) {
375 if (element
instanceof Root
) {
376 return "Taxonomic Tree";
378 return super.getFrameToolTipText(element
);
384 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
388 public void dispose() {
390 dataChangeBehavior
= null;
391 if (conversation
!= null) {
392 conversation
.unregisterForDataStoreChanges(this);
394 if(cdmEntitySession
!= null) {
395 cdmEntitySession
.dispose();
397 if(CdmApplicationState
.getCurrentDataChangeService() != null) {
398 CdmApplicationState
.getCurrentDataChangeService().unregister(this);
405 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
409 public void setFocus() {
410 // logger.warn("Setting focus to navigator");
412 if (getConversationHolder() != null) {
413 getConversationHolder().bind();
415 if(cdmEntitySession
!= null) {
416 cdmEntitySession
.bind();
424 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
425 * (eu.etaxonomy.cdm.model.common.CdmBase)
429 public boolean postOperation(CdmBase objectAffectedByOperation
) {
430 // nothing to do here
440 * a {@link org.eclipse.ui.IMemento} object.
442 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
444 public void save(IMemento memento
, IProgressMonitor monitor
) {
445 saveTreeState(memento
, monitor
);
446 if (conversation
!= null) {
447 conversation
.unregisterForDataStoreChanges(this);
454 protected void handleDoubleClick(DoubleClickEvent event
) {
455 if(event
.getSelection() instanceof IStructuredSelection
){
456 Object selectedObject
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
457 CdmViewerChooser chooser
= new CdmViewerChooser(getSite().getShell());
458 chooser
.chooseViewer(selectedObject
);
460 // If the double click is passed up to the super-class it will
461 // expand/collapse trees.
462 // We do not want that
463 // super.handleDoubleClick(anEvent);
474 public boolean onComplete() {
481 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
485 public void showBusy(boolean busy
) {
486 super.showBusy(busy
);
487 getCommonViewer().getControl().setEnabled(!busy
);
489 partNameCache
= getPartName();
490 setPartName("Loading datasources");
492 if (partNameCache
!= null) {
493 setPartName(partNameCache
);
500 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
503 public void update(Observable o
, Object arg
) {
504 if(o
instanceof LoginManager
){
511 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
514 public ICdmEntitySession
getCdmEntitySession() {
515 return cdmEntitySession
;
519 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
522 public List
<Classification
> getRootEntities() {
524 return root
.getParentBeans();
530 * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
533 public void onChange(CdmChangeEvent event
) {
534 if(event
.getAction() == Action
.Delete
&& Classification
.class.equals(event
.getEntityType())) {
538 for(CdmBase cb
: event
.getChangedObjects()) {
539 if(cb
instanceof TaxonNode
) {
540 TaxonNode tn
= (TaxonNode
)cb
;
541 if(tn
.getTaxon() == null) {
542 getCommonViewer().refresh(tn
.getClassification());
544 getCommonViewer().refresh(cb
);
546 } else if (cb
instanceof Classification
) {
547 getCommonViewer().refresh();
554 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
557 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
558 Map
<Object
, List
<String
>> propertyPathsMap
= new HashMap
<Object
, List
<String
>>();
559 List
<String
> taxonNodePropertyPaths
= Arrays
.asList(new String
[] {
562 propertyPathsMap
.put("childNodes", taxonNodePropertyPaths
);
563 return propertyPathsMap
;