ref #9338: restructure term menues and adapt handling
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / handler / CreateFeatureTreeHandler.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 javax.inject.Named;
12
13 import org.eclipse.e4.core.di.annotations.CanExecute;
14 import org.eclipse.e4.core.di.annotations.Execute;
15 import org.eclipse.e4.ui.di.UISynchronize;
16 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
18 import org.eclipse.e4.ui.services.IServiceConstants;
19
20 import eu.etaxonomy.cdm.model.term.TermTree;
21 import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
22 import eu.etaxonomy.taxeditor.featuretree.e4.IFeatureTreeEditor;
23 import eu.etaxonomy.taxeditor.featuretree.e4.TermTreeEditor;
24 import eu.etaxonomy.taxeditor.featuretree.e4.operation.CreateFeatureTreeOperation;
25 import eu.etaxonomy.taxeditor.store.StoreUtil;
26
27 /**
28 * @author pplitzner
29 * @since Jul 12, 2017
30 */
31 public class CreateFeatureTreeHandler {
32
33 @Execute
34 public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart thisPart, UISynchronize sync){
35 IFeatureTreeEditor editor = (IFeatureTreeEditor) thisPart.getObject();
36
37 if (StoreUtil.promptCheckIsDirty(editor)) {
38 return;
39 }
40 Object[] expandedElements = ((TermTreeEditor)editor).getViewer().getExpandedElements();
41 TermTree<?> newTree = TermTree.NewInstance(editor.getTermType());
42 newTree.setAllowDuplicates(false);
43 newTree.setFlat(false);
44 newTree.setOrderRelevant(true);
45 newTree.setTitleCache(String.format("New %s tree", editor.getTermType().getLabel()));
46
47 CreateFeatureTreeOperation operation = new CreateFeatureTreeOperation(newTree, editor, editor);
48 TermTreeDto newDto = TermTreeDto.fromTree(newTree);
49 editor.setDirty();
50 // AbstractUtility.executeOperation(operation, sync);
51 editor.addOperation(operation);
52 ((TermTreeEditor)editor).putTree(newDto);
53 ((TermTreeEditor)editor).getViewer().setInput(((TermTreeEditor)editor).getTrees());
54
55 ((TermTreeEditor)editor).getViewer().setExpandedElements(expandedElements);
56
57 }
58
59 @CanExecute
60 public boolean canExecute(
61 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
62 MHandledMenuItem menuItem) {
63 boolean canExecute = false;
64 canExecute = thisPart.getObject() instanceof IFeatureTreeEditor;
65 menuItem.setVisible(canExecute);
66 return canExecute;
67 }
68 }