ref #6925 Add generic ExpandHandler
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / TaxonNavigatorE4.java
index db55aaf384035d53d1bb7a39a5a5d725c7e40ecd..32c3285037aa4d27ac8241c93ec4aa6097dcb8c2 100644 (file)
@@ -25,17 +25,30 @@ import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 
+import org.eclipse.core.commands.operations.UndoContext;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.e4.core.commands.ECommandService;
+import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.ui.di.Focus;
+import org.eclipse.e4.ui.di.UIEventTopic;
+import org.eclipse.e4.ui.di.UISynchronize;
+import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.services.EMenuService;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
+import org.eclipse.jface.util.LocalSelectionTransfer;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.viewers.TreePath;
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.dnd.DND;
+import org.eclipse.swt.dnd.Transfer;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IMemento;
@@ -55,6 +68,8 @@ import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
 import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
+import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
+import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
 import eu.etaxonomy.taxeditor.model.IContextListener;
 import eu.etaxonomy.taxeditor.model.IDataChangeBehavior;
@@ -70,18 +85,18 @@ 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;
 
 /**
- * Taxonomic tree implementation using Common Navigator Framework.
  *
- * @author p.ciardelli
- * @author n.hoffmann
- * @created 02.06.2009
- * @version 1.0+-
+ * @author pplitzner
+ * @since Sep 7, 2017
+ *
  */
 public class TaxonNavigatorE4 implements
                IPostOperationEnabled, IConversationEnabled, Observer,
-               ICdmEntitySessionEnabled, ICdmChangeListener, IContextListener {
+               ICdmEntitySessionEnabled, ICdmChangeListener, IContextListener,
+               ICollapsableExpandable {
 
     private static final String RESTORING_TAXON_NAVIGATOR = Messages.TaxonNavigator_RESTORE;
 
@@ -89,12 +104,12 @@ public class TaxonNavigatorE4 implements
 
        private static final String TREE_PATHS = "treepaths"; //$NON-NLS-1$
 
+       private final int dndOperations = DND.DROP_MOVE;
+
        private ConversationHolder conversation;
 
        private ICdmEntitySession cdmEntitySession;
 
-       private String partNameCache;
-
        private IDataChangeBehavior dataChangeBehavior;
 
        private Root root;
@@ -104,15 +119,35 @@ public class TaxonNavigatorE4 implements
     @Inject
     private ESelectionService selService;
 
+    @Inject
+    private UISynchronize sync;
+
     private ISelectionChangedListener selectionChangedListener;
 
-       @Inject
+    private UndoContext undoContext;
+
+    @Inject
+    private MPart thisPart;
+
+    @Inject
+    private MApplication application;
+
+    @Inject
+    private EModelService modelService;
+
+    @Inject
+    private EPartService partService;
+
+    private boolean linkWithTaxon = false;
+
+    @Inject
     public TaxonNavigatorE4() {
+           undoContext = new UndoContext();
            CdmStore.getContextManager().addContextListener(this);
     }
 
        @PostConstruct
-       private void create(Composite parent, EMenuService menuService){
+       private void create(Composite parent, EMenuService menuService, ECommandService commandService){
            FillLayout layout = new FillLayout();
            layout.marginHeight = 0;
            layout.marginWidth = 0;
@@ -129,7 +164,7 @@ public class TaxonNavigatorE4 implements
             if(selection instanceof IStructuredSelection){
                 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
                 if(firstElement instanceof ICdmBase){
-                    NavigationUtil.openEditor((ICdmBase) firstElement, viewer.getControl().getShell());
+                    NavigationUtil.openEditor((ICdmBase) firstElement, viewer.getControl().getShell(), modelService, partService, application);
                 }
             }
         });
@@ -141,6 +176,28 @@ public class TaxonNavigatorE4 implements
         //create context menu
         menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.navigator.popupmenu.taxonnavigator");
 
+        //add drag'n'drop support
+        Transfer[] transfers = new Transfer[] {LocalSelectionTransfer.getTransfer()};
+        viewer.addDragSupport(dndOperations, transfers, new TreeNodeDragListenerE4(viewer));
+        viewer.addDropSupport(dndOperations, transfers, new TreeNodeDropAdapterE4(this));
+
+        //add toolbar
+//        MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar();
+//        MHandledToolItem linkWithEditor = MMenuFactory.INSTANCE.createHandledToolItem();
+//        linkWithEditor.setIconURI("platform:/plugin/eu.etaxonomy.taxeditor.store/icons/synced.gif");
+//        Command command = commandService.getCommand("eu.etaxonomy.taxeditor.navigation.command.linkWithTaxon");
+//        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
+//        mCommand.setElementId(command.getId());
+//        try {
+//            mCommand.setCommandName(command.getName());
+//        } catch (NotDefinedException e) {
+//            e.printStackTrace();
+//        }
+//        linkWithEditor.setCommand(mCommand);
+//        linkWithEditor.setType(ItemType.CHECK);
+//        toolBar.getChildren().add(linkWithEditor);
+//        thisPart.setToolbar(toolBar);
+
            init();
        }
 
@@ -156,8 +213,6 @@ public class TaxonNavigatorE4 implements
                }
                TaxonNodeNavigatorComparator viewerComparator = new TaxonNodeNavigatorComparator(comparator);
                viewer.setComparator(viewerComparator);
-               //FIXME E4 migrate linking with editor
-//        setLinkingEnabled(true);
 
                if (CdmStore.isActive()) {
 
@@ -185,6 +240,29 @@ public class TaxonNavigatorE4 implements
         viewer.setInput(getInitialInput());
        }
 
+       //Link with taxon selection
+       @Inject
+       @Optional
+       private void updateCurrentTaxon(@UIEventTopic(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR)ITaxonEditor editor){
+           if(linkWithTaxon){
+               viewer.refresh();
+               TaxonNode taxonNode = null;
+               if(editor.getTaxon()!=null && editor.getTaxon().getTaxonNodes()!=null){
+                   taxonNode = editor.getTaxon().getTaxonNodes().iterator().next();
+                   viewer.reveal(taxonNode);
+                   viewer.setSelection(new StructuredSelection(taxonNode));
+               }
+           }
+       }
+
+    public void setLinkWithTaxon(boolean linkWithTaxon) {
+        this.linkWithTaxon = linkWithTaxon;
+    }
+
+    public boolean isLinkWithTaxon() {
+        return linkWithTaxon;
+    }
+
        /**
         * Refresh this navigators viewer
         */
@@ -298,6 +376,22 @@ public class TaxonNavigatorE4 implements
                return new TreePath(pathList.toArray());
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void collapse() {
+           viewer.collapseAll();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void expand() {
+           viewer.expandAll();
+       }
+
        @Override
        public ConversationHolder getConversationHolder() {
                return conversation;
@@ -329,10 +423,22 @@ public class TaxonNavigatorE4 implements
                }
        }
 
+    public UISynchronize getSync() {
+        return sync;
+    }
+
+    public TreeViewer getViewer() {
+        return viewer;
+    }
+
+    public UndoContext getUndoContext() {
+        return undoContext;
+    }
+
        /** {@inheritDoc} */
        @Override
        public boolean postOperation(CdmBase objectAffectedByOperation) {
-               // nothing to do here
+           viewer.refresh();
                return true;
        }