Merge branch 'release/4.9.0'
[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.services.IServiceConstants;
20 import org.eclipse.jface.wizard.WizardDialog;
21 import org.eclipse.swt.widgets.Shell;
22
23 import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
24 import eu.etaxonomy.cdm.model.description.Feature;
25 import eu.etaxonomy.cdm.model.description.FeatureNode;
26 import eu.etaxonomy.cdm.model.description.FeatureTree;
27 import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
28 import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
29 import eu.etaxonomy.taxeditor.store.CdmStore;
30
31 /**
32 * @author pplitzner
33 * @since Jul 12, 2017
34 *
35 */
36 public class AddFeatureHandler {
37
38 @Execute
39 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
40 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart) {
41 FeatureTreeEditor editor = ((FeatureTreeEditor) thisPart.getObject());
42 AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
43 WizardDialog dialog = new WizardDialog(shell, wizard);
44
45 if (dialog.open() == IStatus.OK) {
46 FeatureNode parent = ((FeatureTree) editor.getViewer().getInput()).getRoot();
47 Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
48 for (Feature feature : additionalFeatures) {
49 if (!editor.getSelectedFeatureTree().getDistinctFeatures().contains(feature)) {
50 CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(parent.getUuid(), feature.getUuid());
51 }
52 }
53 thisPart.setDirty(true);
54 editor.getViewer().refresh();
55 editor.getViewer().expandToLevel(parent, 1);
56 }
57 }
58
59 @CanExecute
60 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart thisPart) {
61 return thisPart.getObject() instanceof FeatureTreeEditor && ((FeatureTreeEditor) thisPart.getObject()).getSelectedFeatureTree()!=null;
62 }
63 }