2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.Comparator
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
19 import java
.util
.Observable
;
20 import java
.util
.Observer
;
22 import java
.util
.UUID
;
24 import org
.eclipse
.core
.runtime
.IAdaptable
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
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
.application
.CdmApplicationState
;
34 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
;
35 import eu
.etaxonomy
.cdm
.api
.application
.ICdmChangeListener
;
36 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
37 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
39 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNaturalComparator
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByNameComparator
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByRankAndNameComparator
;
45 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
46 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
47 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
48 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
49 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
50 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
51 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
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
;
60 * Taxonomic tree implementation using Common Navigator Framework.
67 public class TaxonNavigator
extends CommonNavigator
implements
68 IPostOperationEnabled
, IConversationEnabled
, Observer
,
69 ICdmEntitySessionEnabled
, ICdmChangeListener
{
71 private static final String RESTORING_TAXON_NAVIGATOR
= Messages
.TaxonNavigator_RESTORE
;
75 * <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code>
77 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator"; //$NON-NLS-1$
79 private static final String TREE_PATH
= "treepath"; //$NON-NLS-1$
81 private static final String TREE_PATHS
= "treepaths"; //$NON-NLS-1$
83 private ConversationHolder conversation
;
85 private ICdmEntitySession cdmEntitySession
;
87 private String partNameCache
;
89 private IDataChangeBehavior dataChangeBehavior
;
96 * @see org.eclipse.ui.navigator.CommonNavigator#getInitialInput()
100 protected IAdaptable
getInitialInput() {
101 Comparator
<TaxonNode
> comparator
;
102 if (PreferencesUtil
.getSortNodesNaturally()){
103 comparator
= new TaxonNaturalComparator();
104 } else if (PreferencesUtil
.getSortNodesStrictlyAlphabetically()){
105 comparator
= new TaxonNodeByNameComparator();
107 comparator
= new TaxonNodeByRankAndNameComparator();
109 // TaxonNodeNavigatorComparator viewerComparator = new TaxonNodeNavigatorComparator(comparator);
110 // this.getCommonViewer().setComparator(viewerComparator);
111 // //this.getCommonViewer().setAutoExpandLevel(2);
112 setLinkingEnabled(true);
113 // this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
116 // public void selectionChanged(SelectionChangedEvent arg0) {
117 // IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
119 // Object firstElement = selection.getFirstElement();
121 // if (!(firstElement instanceof Classification)){
122 // //NavigationUtil.selectInNavigator(firstElement, null);
123 // NavigationUtil.openEditor(firstElement);
129 if (CdmStore
.isActive()) {
131 // TODO when closing and reopening the taxon navigator
132 // we do not preserve state. Closing the view, in contrary to
133 // closing the whole application
134 // should be handled by the state manager too
135 root
= new Root(conversation
);
139 return new EmptyRoot();
144 public void init(IViewSite site
) throws PartInitException
{
156 if (CdmStore
.isActive() && conversation
== null) {
157 conversation
= CdmStore
.createConversation();
158 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
160 if (CdmStore
.isActive()) {
161 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
162 CdmApplicationState
.getCurrentDataChangeService().register(this);
164 CdmStore
.getLoginManager().addObserver(this);
170 * Refresh this navigators viewer
172 public void refresh() {
173 if(getConversationHolder() != null){
174 getConversationHolder().bind();
175 //FIXME : Need to make sure this is a stable fix (ticket 3822)
176 if(!getConversationHolder().isCompleted()){
177 getConversationHolder().commit();
180 if(!getCommonViewer().getTree().isDisposed()){
181 getCommonViewer().refresh();
186 * Refresh this navigators viewer
188 public void refresh(Set
<?
> objects
) {
189 for(Object obj
: objects
) {
190 getCommonViewer().refresh(obj
);
195 * Removes all content
197 public void clear() {
198 getCommonViewer().setInput(new EmptyRoot());
207 * a {@link org.eclipse.ui.IMemento} object.
209 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
211 public void restore(IMemento memento
, IProgressMonitor monitor
) {
212 root
= new Root(conversation
);
213 if (memento
== null) {
214 getCommonViewer().setInput(root
);
218 Set
<TreePath
> treePaths
= new HashSet
<TreePath
>();
219 IMemento
[] treePathMementos
= null;
221 IMemento treePathsMemento
= memento
.getChild(TREE_PATHS
);
223 if (treePathsMemento
!= null) {
224 treePathMementos
= treePathsMemento
.getChildren(TREE_PATH
);
225 mementoWork
= treePathMementos
.length
;
227 // begin the monitor with steps for all tree paths and steps for
229 // conversation s.o., refreshing the tree and setting the paths
230 IProgressMonitor subProgressMonitor
= NavigationUtil
231 .getSubProgressMonitor(monitor
, 1);
233 subProgressMonitor
.beginTask(RESTORING_TAXON_NAVIGATOR
,
234 1 + mementoWork
+ 5);
235 subProgressMonitor
.subTask(RESTORING_TAXON_NAVIGATOR
);
236 subProgressMonitor
.worked(1);
238 conversation
= CdmStore
.createConversation();
239 subProgressMonitor
.worked(1);
240 conversation
.registerForDataStoreChanges(TaxonNavigator
.this);
241 subProgressMonitor
.worked(1);
242 getCommonViewer().setInput(root
);
243 subProgressMonitor
.worked(1);
244 getCommonViewer().refresh();
245 subProgressMonitor
.worked(1);
247 if (treePathMementos
!= null && treePathMementos
.length
> 0) {
248 for (IMemento treePathMemento
: treePathMementos
) {
249 TreePath treePath
= createTreePathFromString(treePathMemento
251 if (!subProgressMonitor
.isCanceled() && treePath
!= null) {
252 treePaths
.add(treePath
);
253 subProgressMonitor
.worked(1);
257 if (treePaths
.size() > 0) {
258 TaxonNavigator
.this.getCommonViewer().setExpandedTreePaths(
259 treePaths
.toArray(new TreePath
[0]));
260 subProgressMonitor
.worked(1);
262 subProgressMonitor
.done();
269 private TreePath
createTreePathFromString(String string
) {
271 List
<CdmBase
> pathList
= new ArrayList
<CdmBase
>();
273 if (string
.length() == 0) {
277 for (String uuid
: string
.split(" ")) { //$NON-NLS-1$
278 CdmBase cdmBaseObject
= CdmStore
.getService(
279 IClassificationService
.class).getTaxonNodeByUuid(
280 UUID
.fromString(uuid
));
281 if (cdmBaseObject
== null) {
282 // is this a tree uuid?
283 cdmBaseObject
= CdmStore
.getService(
284 IClassificationService
.class).load(
285 UUID
.fromString(uuid
));
287 if (cdmBaseObject
== null) {
291 pathList
.add(cdmBaseObject
);
293 return new TreePath(pathList
.toArray());
298 public void saveState(IMemento aMemento
) {
308 * a {@link org.eclipse.ui.IMemento} object.
309 * @param progressMonitor
310 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
312 public void saveTreeState(IMemento memento
, IProgressMonitor progressMonitor
) {
313 if (memento
== null) {
316 IProgressMonitor monitor
= NavigationUtil
.getSubProgressMonitor(
319 super.saveState(memento
);
321 memento
= memento
.createChild(TREE_PATHS
);
322 TreePath
[] treePaths
= this.getCommonViewer().getExpandedTreePaths();
324 monitor
.beginTask(Messages
.TaxonNavigator_SAVE_TASK
, treePaths
.length
);
326 for (TreePath treePath
: treePaths
) {
327 int pathLength
= treePath
.getSegmentCount();
328 String path
= ""; //$NON-NLS-1$
329 for (int i
= 0; i
< pathLength
; i
++) {
330 Object segment
= treePath
.getSegment(i
);
331 if (segment
instanceof CdmBase
) {
332 path
+= ((CdmBase
) segment
).getUuid().toString() + " "; //$NON-NLS-1$
335 MessagingUtils
.warn(getClass(),
336 "Non-taxon tree path segment " + segment
); //$NON-NLS-1$
339 memento
.createChild(TREE_PATH
, path
.trim());
348 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
353 * getConversationHolder
356 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
360 public ConversationHolder
getConversationHolder() {
368 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update
369 * (eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
373 public void update(CdmDataChangeMap changeEvents
) {
374 if (dataChangeBehavior
== null) {
375 dataChangeBehavior
= new TaxonNavigatorDataChangeBehavior(this);
378 DataChangeBridge
.handleDataChange(changeEvents
, dataChangeBehavior
);
383 public String
getFrameToolTipText(Object element
) {
384 if (element
instanceof Root
) {
385 return Messages
.TaxonNavigator_TAXON_TREE
;
387 return super.getFrameToolTipText(element
);
393 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
397 public void dispose() {
399 dataChangeBehavior
= null;
400 if (conversation
!= null) {
401 conversation
.unregisterForDataStoreChanges(this);
403 if(cdmEntitySession
!= null) {
404 cdmEntitySession
.dispose();
406 if(CdmApplicationState
.getCurrentDataChangeService() != null) {
407 CdmApplicationState
.getCurrentDataChangeService().unregister(this);
414 * @see org.eclipse.ui.navigator.CommonNavigator#setFocus()
418 public void setFocus() {
419 // logger.warn("Setting focus to navigator");
421 if (getConversationHolder() != null) {
422 getConversationHolder().bind();
424 if(cdmEntitySession
!= null) {
425 cdmEntitySession
.bind();
433 * eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation
434 * (eu.etaxonomy.cdm.model.common.CdmBase)
438 public boolean postOperation(CdmBase objectAffectedByOperation
) {
439 // nothing to do here
449 * a {@link org.eclipse.ui.IMemento} object.
451 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
453 public void save(IMemento memento
, IProgressMonitor monitor
) {
454 saveTreeState(memento
, monitor
);
455 if (conversation
!= null) {
456 conversation
.unregisterForDataStoreChanges(this);
463 protected void handleDoubleClick(DoubleClickEvent event
) {
464 // ISelection selection = event.getSelection();
465 // if(selection instanceof IStructuredSelection){
466 // Object firstElement = ((IStructuredSelection) selection).getFirstElement();
467 // if(firstElement instanceof ICdmBase){
468 // NavigationUtil.openEditor((ICdmBase) firstElement, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
471 // If the double click is passed up to the super-class it will
472 // expand/collapse trees.
473 // We do not want that
474 // super.handleDoubleClick(anEvent);
485 public boolean onComplete() {
492 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
496 public void showBusy(boolean busy
) {
497 super.showBusy(busy
);
498 getCommonViewer().getControl().setEnabled(!busy
);
500 partNameCache
= getPartName();
501 setPartName(Messages
.TaxonNavigator_LOAD_DATASOURCE
);
503 if (partNameCache
!= null) {
504 setPartName(partNameCache
);
511 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
514 public void update(Observable o
, Object arg
) {
515 if(o
instanceof LoginManager
){
522 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
525 public ICdmEntitySession
getCdmEntitySession() {
526 return cdmEntitySession
;
530 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
533 public List
<TaxonNodeDto
> getRootEntities() {
535 return root
.getParentBeans();
541 * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
544 public void onChange(CdmChangeEvent event
) {
545 // if(event.getAction() == Action.Delete && Classification.class.equals(event.getEntityType())) {
550 for(CdmBase cb
: event
.getChangedObjects()) {
551 if(cb
instanceof TaxonNode
) {
552 TaxonNode tn
= (TaxonNode
)cb
;
553 if(tn
.getTaxon() == null) {
554 getCommonViewer().refresh(tn
.getClassification());
556 getCommonViewer().refresh(cb
);
558 } else if (cb
instanceof Classification
) {
559 getCommonViewer().refresh();
566 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
569 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
570 Map
<Object
, List
<String
>> propertyPathsMap
= new HashMap
<Object
, List
<String
>>();
571 List
<String
> taxonNodePropertyPaths
= Arrays
.asList(new String
[] {
572 "taxon.name" //$NON-NLS-1$
574 propertyPathsMap
.put("childNodes", taxonNodePropertyPaths
); //$NON-NLS-1$
575 return propertyPathsMap
;