ref #7010 Adapt edit polytomous key handler
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / RemotingMoveTaxonNodeHandlerE4.java
1 /**
2 * Copyright (C) 2015 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.navigation.navigator.e4.handler;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14
15 import javax.inject.Named;
16
17 import org.eclipse.core.commands.operations.AbstractOperation;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.e4.core.di.annotations.CanExecute;
21 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23 import org.eclipse.e4.ui.services.IServiceConstants;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.TreeSelection;
27 import org.eclipse.swt.widgets.Shell;
28
29 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
30 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
32 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
33 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
34 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
35 import eu.etaxonomy.taxeditor.navigation.navigator.e4.TreeNodeDropAdapterE4.MovingType;
36 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
37 import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
38 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
41
42 /**
43 * @author cmathew
44 * @date 19 Jun 2015
45 *
46 */
47 public class RemotingMoveTaxonNodeHandlerE4 extends RemotingCdmHandlerE4 {
48
49 private TaxonNodeDto oldTaxonNode;
50
51 public RemotingMoveTaxonNodeHandlerE4() {
52 super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
53 }
54
55 @Override
56 public IStatus allowOperations(IStructuredSelection selection,
57 Shell shell,
58 MPart activePart,
59 MHandledMenuItem menuItem) {
60 // check that only a single taxon tree node has been selected
61 if(selection.size() > 1) {
62 return new Status(IStatus.ERROR,
63 "unknown", //$NON-NLS-1$
64 TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
65 }
66
67 // check for no taxon tree node selected
68 if(selection.size() == 0) {
69 return new Status(IStatus.ERROR,
70 "unknown", //$NON-NLS-1$
71 TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
72 }
73 // check that selected object is a taxon node
74 Object obj = selection.iterator().next();
75 if(obj instanceof TaxonNodeDto) {
76 oldTaxonNode = (TaxonNodeDto)obj;
77 } else {
78 return new Status(IStatus.ERROR,
79 "unknown", //$NON-NLS-1$
80 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
81 }
82 return Status.OK_STATUS;
83 }
84
85 @Override
86 public AbstractOperation prepareOperation(IStructuredSelection selection,
87 Shell shell,
88 MPart activePart,
89 MHandledMenuItem menuItem) {
90 TaxonNode parentTaxonNode;
91
92 List<UUID> excludeTaxa = new ArrayList<UUID>();
93 excludeTaxa.add(oldTaxonNode.getTaxonUuid());
94
95 MovingType moveToNewParent = MovingType.CHILD;
96
97 if (PreferencesUtil.getSortNodesNaturally()){
98
99
100 parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
101 // new ConversationHolderMock(),
102 Messages.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON,
103 excludeTaxa,
104 null,
105 oldTaxonNode.getClassificationUUID(), true);
106 String[] buttonLables = {Messages.RemotingMoveTaxonNodeHandler_CHILD, Messages.RemotingMoveTaxonNodeHandler_BEHIND,Messages.RemotingMoveTaxonNodeHandler_CANCEL};
107 MessageDialog dialog = new MessageDialog(null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE, null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE_MESSAGE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
108 dialog.open();
109 int returnCode = dialog.getReturnCode();
110 if (returnCode == 0){
111 moveToNewParent = MovingType.CHILD;
112 } else if (returnCode == 1){
113 moveToNewParent = MovingType.BEHIND;
114 }
115 } else {
116 parentTaxonNode = TaxonNodeSelectionDialog.select(shell,
117 // new ConversationHolderMock(),
118 Messages.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT,
119 excludeTaxa,
120 CdmStore.getService(ITaxonNodeService.class).find(oldTaxonNode.getUuid()),
121 oldTaxonNode.getClassificationUUID(), true);
122 }
123
124
125 if(parentTaxonNode != null){
126 if(NavigationUtil.isDirty(parentTaxonNode, partService)){
127 MessageDialog.openWarning(shell,
128 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT,
129 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE);
130 return null;
131 }
132
133 return new RemotingMoveTaxonOperation(getTrigger(),
134 false,
135 oldTaxonNode.getUuid(),
136 parentTaxonNode.getUuid(),
137 moveToNewParent);
138 }
139
140 return null;
141 }
142
143 @CanExecute
144 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
145 boolean canExecute = false;
146 canExecute = (selection.getFirstElement() instanceof TaxonNodeDto) && ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
147 menuItem.setVisible(canExecute);
148 return canExecute;
149 }
150
151 @Override
152 public void onComplete() {
153 }
154
155 /**
156 * {@inheritDoc}
157 */
158 @Override
159 protected Object getTrigger() {
160 return this;
161 }
162 }