Merge branch 'move-to-luna' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TaxonNavigator.java
index e375d2c0e06358c65133757c11c190055f399187..092b49bc8823ed85140deebeef6cacd4c7d55085 100644 (file)
@@ -11,6 +11,7 @@
 package eu.etaxonomy.taxeditor.navigation.navigator;
 
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Observable;
@@ -21,26 +22,28 @@ import java.util.UUID;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jface.viewers.DoubleClickEvent;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.jface.viewers.TreePath;
 import org.eclipse.ui.IMemento;
 import org.eclipse.ui.IViewSite;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.navigator.CommonNavigator;
-import org.eclipse.jface.viewers.ISelectionChangedListener;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
 import eu.etaxonomy.cdm.api.service.IClassificationService;
 import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.description.FeatureTree;
+import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.TaxonComparatorSearch;
+import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
 import eu.etaxonomy.taxeditor.model.IDataChangeBehavior;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.store.LoginManager;
 
@@ -53,7 +56,7 @@ import eu.etaxonomy.taxeditor.store.LoginManager;
  * @version 1.0
  */
 public class TaxonNavigator extends CommonNavigator implements
-               IPostOperationEnabled, IConversationEnabled, Observer {
+               IPostOperationEnabled, IConversationEnabled, Observer, ICdmEntitySessionEnabled {
 
        /**
         * Constant
@@ -67,10 +70,14 @@ public class TaxonNavigator extends CommonNavigator implements
 
        private ConversationHolder conversation;
 
+       private ICdmEntitySession cdmEntitySession;
+
        private String partNameCache;
 
        private IDataChangeBehavior dataChangeBehavior;
 
+       private Root root;
+
        /*
         * (non-Javadoc)
         *
@@ -79,30 +86,39 @@ public class TaxonNavigator extends CommonNavigator implements
        /** {@inheritDoc} */
        @Override
        protected IAdaptable getInitialInput() {
-               
-               TaxonComparatorSearch comparator = new TaxonComparatorSearch();
+               Comparator comparator;
+               if (PreferencesUtil.getSortNodesNaturally()){
+                       comparator = new TaxonNaturalComparator();
+               } else{
+                       comparator = new TaxonComparatorSearch();
+               }
                TaxonNodeComparator viewerComparator = new TaxonNodeComparator(comparator);
                this.getCommonViewer().setComparator(viewerComparator);
-               this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
-                       
-                       @Override
-                       public void selectionChanged(SelectionChangedEvent arg0) {
-                               IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
-                               
-                               Object firstElement =  selection.getFirstElement();
-                               NavigationUtil.executeEditHandler();
-                               
-                       }
-               } );
-               
+        setLinkingEnabled(true);
+//             this.getCommonViewer().addSelectionChangedListener(new ISelectionChangedListener() {
+//
+//                     @Override
+//                     public void selectionChanged(SelectionChangedEvent arg0) {
+//                             IStructuredSelection selection = (IStructuredSelection) getCommonViewer().getSelection();
+//
+//                             Object firstElement =  selection.getFirstElement();
+//                             //
+//                             if (!(firstElement instanceof Classification)){
+//                                     //NavigationUtil.selectInNavigator(firstElement, null);
+//                                     NavigationUtil.openEditor(firstElement);
+//                             }
+//
+//                     }
+//             } );
+
                if (CdmStore.isActive()) {
-                       
+
                        // TODO when closing and reopening the taxon navigator
                        // we do not preserve state. Closing the view, in contrary to
                        // closing the whole application
                        // should be handled by the state manager too
-
-                       return new Root(conversation);
+                   root = new Root(conversation);
+                       return root;
                }
                return new EmptyRoot();
        }
@@ -112,7 +128,6 @@ public class TaxonNavigator extends CommonNavigator implements
        public void init(IViewSite site) throws PartInitException {
                super.init(site);
                init();
-               
        }
 
        /**
@@ -121,12 +136,16 @@ public class TaxonNavigator extends CommonNavigator implements
         * </p>
         */
        public void init() {
+
                if (CdmStore.isActive() && conversation == null) {
                        conversation = CdmStore.createConversation();
                        conversation.registerForDataStoreChanges(TaxonNavigator.this);
                }
+               if (CdmStore.isActive() && cdmEntitySession == null) {
+                   cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
+                   cdmEntitySession.registerForDataStoreChanges(TaxonNavigator.this);
+               }
                CdmStore.getLoginManager().addObserver(this);
-               
        }
 
        /**
@@ -140,9 +159,22 @@ public class TaxonNavigator extends CommonNavigator implements
                            getConversationHolder().commit();
                        }
                }
+               if (cdmEntitySession != null) {
+                   cdmEntitySession.fireNotifications();
+               }
                getCommonViewer().refresh();
        }
 
+          /**
+     * Refresh this navigators viewer
+     */
+    public void refresh(Set objects) {
+
+        for(Object obj : objects) {
+            getCommonViewer().refresh(obj);
+        }
+    }
+
        /**
         * Removes all content
         */
@@ -161,8 +193,9 @@ public class TaxonNavigator extends CommonNavigator implements
         *            a {@link org.eclipse.core.runtime.IProgressMonitor} object.
         */
        public void restore(IMemento memento, IProgressMonitor monitor) {
+           root = new Root(conversation);
                if (memento == null) {
-                       getCommonViewer().setInput(new Root(conversation));
+                       getCommonViewer().setInput(root);
                        return;
                }
                int mementoWork = 0;
@@ -190,7 +223,7 @@ public class TaxonNavigator extends CommonNavigator implements
                subProgressMonitor.worked(1);
                conversation.registerForDataStoreChanges(TaxonNavigator.this);
                subProgressMonitor.worked(1);
-               getCommonViewer().setInput(new Root(conversation));
+               getCommonViewer().setInput(root);
                subProgressMonitor.worked(1);
                getCommonViewer().refresh();
                subProgressMonitor.worked(1);
@@ -283,7 +316,7 @@ public class TaxonNavigator extends CommonNavigator implements
                                        path += ((CdmBase) segment).getUuid().toString() + " ";
                                        monitor.worked(1);
                                } else {
-                                       NavigationUtil.warn(getClass(),
+                                       MessagingUtils.warn(getClass(),
                                                        "Non-taxon tree path segment " + segment);
                                }
                        }
@@ -351,6 +384,9 @@ public class TaxonNavigator extends CommonNavigator implements
                if (conversation != null) {
                        conversation.unregisterForDataStoreChanges(this);
                }
+               if(cdmEntitySession != null) {
+                   cdmEntitySession.dispose();
+               }
        }
 
        /*
@@ -366,6 +402,9 @@ public class TaxonNavigator extends CommonNavigator implements
                if (getConversationHolder() != null) {
                        getConversationHolder().bind();
                }
+               if(cdmEntitySession != null) {
+                   cdmEntitySession.bind();
+               }
        }
 
        /*
@@ -453,4 +492,23 @@ public class TaxonNavigator extends CommonNavigator implements
                }
 
        }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
+     */
+    @Override
+    public ICdmEntitySession getCdmEntitySession() {
+       return cdmEntitySession;
+    }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
+     */
+    @Override
+    public List<Classification> getRootEntities() {
+        if(root != null) {
+            return root.getParentBeans();
+        }
+        return null;
+    }
 }