ref #6806 Implement operation framework for feature tree editor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / handler / AddFeatureHandler.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.featuretree.e4.handler;
10
11 import java.util.Collection;
12
13 import javax.inject.Named;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.e4.core.di.annotations.CanExecute;
17 import org.eclipse.e4.core.di.annotations.Execute;
18 import org.eclipse.e4.core.di.annotations.Optional;
19 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
21 import org.eclipse.e4.ui.services.IServiceConstants;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.wizard.WizardDialog;
24 import org.eclipse.swt.widgets.Shell;
25
26 import eu.etaxonomy.cdm.model.description.Feature;
27 import eu.etaxonomy.cdm.model.description.FeatureNode;
28 import eu.etaxonomy.cdm.model.description.FeatureTree;
29 import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
30 import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
31 import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
32
33 /**
34 * @author pplitzner
35 * @since Jul 12, 2017
36 *
37 */
38 public class AddFeatureHandler {
39
40 @Execute
41 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
42 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
43 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart) {
44 FeatureTreeEditor editor = ((FeatureTreeEditor) thisPart.getObject());
45 AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
46 WizardDialog dialog = new WizardDialog(shell, wizard);
47
48 if (dialog.open() == IStatus.OK) {
49 FeatureTree tree = (FeatureTree) selection.iterator().next();
50 Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
51 for (Feature feature : additionalFeatures) {
52 FeatureNode childNode = FeatureNode.NewInstance(feature);
53 tree.getRoot().addChild(childNode);
54 AddFeatureOperation operation = new AddFeatureOperation(feature, tree.getRoot(), editor, editor);
55 editor.addOperation(operation);
56 }
57 editor.setDirty(true);
58 editor.getViewer().refresh();
59 editor.getViewer().expandToLevel(selection, 1);
60 }
61 }
62
63 @CanExecute
64 public boolean canExecute(
65 @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
66 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
67 MHandledMenuItem menuItem) {
68 boolean canExecute = false;
69 canExecute = thisPart.getObject() instanceof FeatureTreeEditor
70 && selection!=null
71 && selection.size()==1
72 && selection.iterator().next() instanceof FeatureTree;
73 menuItem.setVisible(canExecute);
74 return canExecute;
75 }
76 }