sort taxon nodes in taxon filter of DDS editor
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / TaxonNavigatorE4.java
index c384c5b19f0a9bf7ee4afe7a9481ce970edb066b..1eff1eefaf384308c4c87197204481f57918715d 100644 (file)
@@ -6,16 +6,12 @@
  * The contents of this file are subject to the Mozilla Public License Version 1.1
  * See LICENSE.TXT at the top of this package for the full license terms.
  */
-
 package eu.etaxonomy.taxeditor.navigation.navigator.e4;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Comparator;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Observable;
 import java.util.Observer;
 import java.util.Set;
@@ -78,6 +74,7 @@ import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoNaturalComparator;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
+import eu.etaxonomy.taxeditor.event.EventUtility;
 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
@@ -92,22 +89,18 @@ import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNodeNavigatorComparator;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.preference.NavigatorOrderEnum;
 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;
 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
 import eu.etaxonomy.taxeditor.workbench.part.ICollapsableExpandable;
 
 /**
- *
  * @author pplitzner
  * @since Sep 7, 2017
- *
  */
 public class TaxonNavigatorE4 implements
                IPostOperationEnabled, IConversationEnabled, Observer,
-               ICdmEntitySessionEnabled, ICdmChangeListener, IContextListener,
+               ICdmChangeListener, IContextListener,
                ICollapsableExpandable {
 
     private static final String RESTORING_TAXON_NAVIGATOR = Messages.TaxonNavigator_RESTORE;
@@ -120,8 +113,6 @@ public class TaxonNavigatorE4 implements
 
        private ConversationHolder conversation;
 
-       private ICdmEntitySession cdmEntitySession;
-
        private IDataChangeBehavior dataChangeBehavior;
 
        private Root root;
@@ -197,8 +188,6 @@ public class TaxonNavigatorE4 implements
 
         updateSyncButton();
 
-
-
            init();
        }
 
@@ -216,18 +205,9 @@ public class TaxonNavigatorE4 implements
         }
     }
 
-       /** {@inheritDoc} */
        protected IAdaptable getInitialInput() {
-               Comparator<TaxonNodeDto> comparator;
-               NavigatorOrderEnum orderValue = PreferencesUtil.getSortNodes();
-               if (orderValue.equals(NavigatorOrderEnum.NaturalOrder)){
-                       comparator = new TaxonNodeDtoNaturalComparator();
-               } else if (orderValue.equals(NavigatorOrderEnum.AlphabeticalOrder)){
-                       comparator = new TaxonNodeDtoByNameComparator();
-               }else {
-                       comparator = new TaxonNodeDtoByRankAndNameComparator();
-               }
-
+               Comparator<TaxonNodeDto> comparator = PreferencesUtil.getNodeComparator();
+               
                TaxonNodeNavigatorComparator viewerComparator = new TaxonNodeNavigatorComparator(comparator);
                viewer.setComparator(viewerComparator);
                viewer.setComparer(new IElementComparer() {
@@ -279,7 +259,7 @@ public class TaxonNavigatorE4 implements
                        conversation.registerForDataStoreChanges(TaxonNavigatorE4.this);
                }
                if (CdmStore.isActive()) {
-                   cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
+//                 cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
                    CdmApplicationState.getCurrentDataChangeService().register(this);
                    viewer.setInput(getInitialInput());
                }
@@ -302,13 +282,26 @@ public class TaxonNavigatorE4 implements
                            taxonNode = new TaxonNodeDto(editor.getTaxon().getTaxonNodes().iterator().next());
                        }
                    }
-
-                   viewer.reveal(taxonNode);
-                   viewer.setSelection(new StructuredSelection(taxonNode));
+                if (taxonNode != null){
+                    viewer.reveal(taxonNode);
+                    viewer.setSelection(new StructuredSelection(taxonNode));
+                }else{
+                    //TODO: show message in status bar
+                }
                }
            }
        }
 
+       @Inject
+    @Optional
+    public void updateLinkWithTaxon(@UIEventTopic(WorkbenchEventConstants.UPDATE_LINK_WITH_TAXON) boolean isDummy){
+           this.linkWithTaxon = !linkWithTaxon;
+
+        if(this.isLinkWithTaxon()){
+            this.updateCurrentTaxon(EventUtility.getTaxonEditor());
+        }
+       }
+
     public void setLinkWithTaxon(boolean linkWithTaxon) {
         this.linkWithTaxon = linkWithTaxon;
     }
@@ -329,6 +322,10 @@ public class TaxonNavigatorE4 implements
                        }
                }
                if(!viewer.getTree().isDisposed()){
+                   if (CdmStore.isActive()){
+
+                       viewer.setInput(getInitialInput());
+                   }
                    viewer.refresh();
                }
 
@@ -367,7 +364,7 @@ public class TaxonNavigatorE4 implements
                        return;
                }
                int mementoWork = 0;
-               Set<TreePath> treePaths = new HashSet<TreePath>();
+               Set<TreePath> treePaths = new HashSet<>();
                IMemento[] treePathMementos = null;
 
                IMemento treePathsMemento = memento.getChild(TREE_PATHS);
@@ -416,7 +413,7 @@ public class TaxonNavigatorE4 implements
 
        private TreePath createTreePathFromString(String string) {
 
-               List<CdmBase> pathList = new ArrayList<CdmBase>();
+               List<CdmBase> pathList = new ArrayList<>();
 
                if (string.length() == 0) {
             return null;
@@ -441,17 +438,11 @@ public class TaxonNavigatorE4 implements
                return new TreePath(pathList.toArray());
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public void collapse() {
            viewer.collapseAll();
        }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public void expand() {
            viewer.expandAll();
@@ -462,7 +453,6 @@ public class TaxonNavigatorE4 implements
                return conversation;
        }
 
-       /** {@inheritDoc} */
        @PreDestroy
        public void dispose() {
                dataChangeBehavior = null;
@@ -470,24 +460,16 @@ public class TaxonNavigatorE4 implements
                        conversation.unregisterForDataStoreChanges(this);
                        conversation.close();
                }
-               if(cdmEntitySession != null) {
-                   cdmEntitySession.dispose();
-                   cdmEntitySession = null;
-               }
                if(CdmApplicationState.getCurrentDataChangeService() != null) {
                    CdmApplicationState.getCurrentDataChangeService().unregister(this);
                }
        }
 
-       /** {@inheritDoc} */
        @Focus
        public void setFocus() {
                if (getConversationHolder() != null) {
                        getConversationHolder().bind();
                }
-               if(cdmEntitySession != null) {
-                   cdmEntitySession.bind();
-               }
        }
 
     public UISynchronize getSync() {
@@ -502,7 +484,6 @@ public class TaxonNavigatorE4 implements
         return undoContext;
     }
 
-       /** {@inheritDoc} */
        @Override
        public boolean postOperation(Object objectAffectedByOperation) {
            viewer.refresh();
@@ -519,9 +500,8 @@ public class TaxonNavigatorE4 implements
                if(o instanceof LoginManager){
                        refresh();
                }
-
        }
-          /** {@inheritDoc} */
+
     @Override
     public void update(CdmDataChangeMap changeEvents) {
         if (dataChangeBehavior == null) {
@@ -533,12 +513,6 @@ public class TaxonNavigatorE4 implements
 
     }
 
-    @Override
-    public ICdmEntitySession getCdmEntitySession() {
-       return cdmEntitySession;
-    }
-
-    @Override
     public List<TaxonNodeDto> getRootEntities() {
         if(root != null) {
             return root.getParentBeans();
@@ -548,6 +522,7 @@ public class TaxonNavigatorE4 implements
 
     @Override
     public void onChange(CdmChangeEvent event) {
+        Object[] expandedElements = viewer.getExpandedElements();
         refresh();
         for(CdmBase cb : event.getChangedObjects()) {
                if(cb instanceof TaxonNode) {
@@ -566,37 +541,18 @@ public class TaxonNavigatorE4 implements
                 viewer.refresh();
             }
         }
+        viewer.setExpandedElements(expandedElements);
     }
 
-    @Override
-    public Map<Object, List<String>> getPropertyPathsMap() {
-        Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
-        List<String> taxonNodePropertyPaths = Arrays.asList(new String[] {
-                "taxon.name" //$NON-NLS-1$
-        });
-         propertyPathsMap.put("childNodes", taxonNodePropertyPaths); //$NON-NLS-1$
-        return propertyPathsMap;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
         // TODO Auto-generated method stub
-
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void contextStop(IMemento memento, IProgressMonitor monitor) {
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void contextStart(IMemento memento, IProgressMonitor monitor) {
         if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
@@ -604,25 +560,37 @@ public class TaxonNavigatorE4 implements
         }
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void contextRefresh(IProgressMonitor monitor) {
+//        viewer.refresh();
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
     }
 
+    @Inject
+    @Optional
+    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAVIGATOR)TaxonNodeDto dto){
+        if(dto!= null){
+            Object[] expandedElements = viewer.getExpandedElements();
+            ISelection sel = viewer.getSelection();
+            refresh(dto);
+            viewer.setExpandedElements(expandedElements);
+            if (sel != null && !sel.isEmpty()){
+                viewer.reveal(dto);
+                viewer.setSelection(sel);
+            }
+        }
+    }
+
     @Inject
     @Optional
     private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_NAVIGATOR)boolean refresh){
         if(refresh){
+            Object[] expandedElements = viewer.getExpandedElements();
             refresh();
+            viewer.setExpandedElements(expandedElements);
         }
     }
-}
+}
\ No newline at end of file