ref #6746 Add/adapt handlers 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.ui.model.application.ui.basic.MPart;
19 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
20 import org.eclipse.e4.ui.services.IServiceConstants;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.wizard.WizardDialog;
23 import org.eclipse.swt.widgets.Shell;
24
25 import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
26 import eu.etaxonomy.cdm.model.description.Feature;
27 import eu.etaxonomy.cdm.model.description.FeatureTree;
28 import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
29 import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31
32 /**
33 * @author pplitzner
34 * @since Jul 12, 2017
35 *
36 */
37 public class AddFeatureHandler {
38
39 @Execute
40 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
41 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
42 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart) {
43 FeatureTreeEditor editor = ((FeatureTreeEditor) thisPart.getObject());
44 AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
45 WizardDialog dialog = new WizardDialog(shell, wizard);
46
47 if (dialog.open() == IStatus.OK) {
48 FeatureTree tree = (FeatureTree) selection.iterator().next();
49 Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
50 for (Feature feature : additionalFeatures) {
51 CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(tree.getUuid(), feature.getUuid());
52 }
53 editor.getViewer().refresh();
54 editor.getViewer().expandToLevel(selection, 1);
55 }
56 }
57
58 @CanExecute
59 public boolean canExecute(
60 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
61 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
62 MHandledMenuItem menuItem) {
63 boolean canExecute = false;
64 canExecute = selection.size()==1
65 && selection.iterator().next() instanceof FeatureTree
66 && thisPart.getObject() instanceof FeatureTreeEditor;
67 menuItem.setVisible(canExecute);
68 return canExecute;
69 }
70 }