ref #8011 Use term search in feature tree context menu to add features
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / handler / AddFeatureHandler.java
index c72898b22d9eff0dcd8e9bb91a7881f63ced6402..e0eb2cbdcf371e275a4b81c02eeb434a0711c9da 100644 (file)
@@ -8,25 +8,29 @@
 */
 package eu.etaxonomy.taxeditor.featuretree.e4.handler;
 
-import java.util.Collection;
+import java.util.List;
 
 import javax.inject.Named;
 
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.e4.core.di.annotations.CanExecute;
 import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.core.di.annotations.Optional;
+import org.eclipse.e4.ui.di.UISynchronize;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.widgets.Shell;
 
-import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
-import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.model.description.FeatureNode;
 import eu.etaxonomy.cdm.model.description.FeatureTree;
-import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
-import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
-import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.cdm.persistence.dto.TermDto;
+import eu.etaxonomy.taxeditor.featuretree.TermChooseWizard;
+import eu.etaxonomy.taxeditor.featuretree.e4.IFeatureTreeEditor;
+import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
 
 /**
  * @author pplitzner
@@ -37,24 +41,40 @@ public class AddFeatureHandler {
 
     @Execute
     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
-            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart) {
-        FeatureTreeEditor editor = ((FeatureTreeEditor) thisPart.getObject());
-        AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
+            @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
+            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart, UISynchronize sync) {
+        IFeatureTreeEditor editor = ((IFeatureTreeEditor) thisPart.getObject());
+
+        if (StoreUtil.promptCheckIsDirty(editor)) {
+            return;
+        }
+
+
+        TermChooseWizard wizard = new TermChooseWizard();
+        //      AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
         WizardDialog dialog = new WizardDialog(shell, wizard);
 
         if (dialog.open() == IStatus.OK) {
-            FeatureNode parent = ((FeatureTree) editor.getViewer().getInput()).getRoot();
-            Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
-            for (Feature feature : additionalFeatures) {
-                CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(parent.getUuid(), feature.getUuid());
+            FeatureTree tree = (FeatureTree) selection.getFirstElement();
+            List<TermDto> selectedTerms = wizard.getSelectedTerms();
+            for (TermDto termDto: selectedTerms) {
+                AddFeatureOperation operation = new AddFeatureOperation(termDto.getUuid(), tree.getRoot(), editor, editor);
+                AbstractUtility.executeOperation(operation, sync);
             }
-            editor.getViewer().refresh();
-            editor.getViewer().expandToLevel(parent, 1);
         }
     }
 
     @CanExecute
-    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart thisPart) {
-        return thisPart.getObject() instanceof FeatureTreeEditor && ((FeatureTreeEditor) thisPart.getObject()).getSelectedFeatureTree()!=null;
+    public boolean canExecute(
+            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
+            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
+            MHandledMenuItem menuItem) {
+        boolean canExecute = false;
+        canExecute = thisPart.getObject() instanceof IFeatureTreeEditor
+                && selection!=null
+                && selection.size()==1
+                && selection.getFirstElement() instanceof FeatureTree;
+        menuItem.setVisible(canExecute);
+        return canExecute;
     }
 }