Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / handler / RemotingMoveTaxonNodeHandler.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.handler;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.operations.AbstractOperation;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.TreeSelection;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.handlers.HandlerUtil;
23
24 import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
27 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
28 import eu.etaxonomy.taxeditor.navigation.navigator.TreeNodeDropAdapter.MovingType;
29 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
30 import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
31 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
33
34 /**
35 * @author cmathew
36 * @date 19 Jun 2015
37 *
38 */
39 public class RemotingMoveTaxonNodeHandler extends RemotingCdmHandler {
40
41 private TaxonNode oldTaxonNode;
42
43 public RemotingMoveTaxonNodeHandler() {
44 super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
45 }
46
47 /* (non-Javadoc)
48 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#allowOperations(org.eclipse.core.commands.ExecutionEvent)
49 */
50 @Override
51 public IStatus allowOperations(ExecutionEvent event) {
52 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
53 // check that only a single taxon tree node has been selected
54 if(selection.size() > 1) {
55 return new Status(IStatus.ERROR,
56 "unknown",
57 TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
58 }
59
60 // check for no taxon tree node selected
61 if(selection.size() == 0) {
62 return new Status(IStatus.ERROR,
63 "unknown",
64 TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
65 }
66 // check that selected object is a taxon node
67 Object obj = selection.iterator().next();
68 if(obj instanceof TaxonNode) {
69 oldTaxonNode = (TaxonNode)obj;
70 } else {
71 return new Status(IStatus.ERROR,
72 "unknown",
73 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
74 }
75 return Status.OK_STATUS;
76 }
77
78
79 /* (non-Javadoc)
80 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#prepareOperation(org.eclipse.core.commands.ExecutionEvent)
81 */
82 @Override
83 public AbstractOperation prepareOperation(ExecutionEvent event) {
84 Shell activeShell = HandlerUtil.getActiveShell(event);
85 TaxonNode parentTaxonNode;
86
87 List<UUID> excludeTaxa = new ArrayList<UUID>();
88 excludeTaxa.add(oldTaxonNode.getTaxon().getUuid());
89
90 MovingType moveToNewParent = MovingType.CHILD;
91
92 if (PreferencesUtil.getSortNodesNaturally()){
93
94
95 parentTaxonNode = TaxonNodeSelectionDialog.select(activeShell,
96 new ConversationHolderMock(),
97 "Choose the taxon above the moved taxon.",
98 excludeTaxa,
99 null,
100 oldTaxonNode.getClassification());
101 String[] buttonLables = {"Child", "Behind","Cancel"};
102 MessageDialog dialog = new MessageDialog(null, "Target node", null, "Do you want to move the Taxonnode as child or behind the target node.", MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
103 dialog.open();
104 int returnCode = dialog.getReturnCode();
105 if (returnCode == 0){
106 moveToNewParent = MovingType.CHILD;
107 } else if (returnCode == 1){
108 moveToNewParent = MovingType.BEHIND;
109 }
110 } else {
111 parentTaxonNode = TaxonNodeSelectionDialog.select(activeShell,
112 new ConversationHolderMock(),
113 "Choose new parent",
114 excludeTaxa,
115 null,
116 oldTaxonNode.getClassification());
117 }
118
119
120 if(parentTaxonNode != null){
121 if(NavigationUtil.isDirty(parentTaxonNode)){
122 MessageDialog.openWarning(activeShell,
123 "Unsaved Parent Taxon",
124 "There are unsaved changes in the parent taxon. Please save first.");
125 return null;
126 }
127
128 return new RemotingMoveTaxonOperation(event.getTrigger(),
129 false,
130 oldTaxonNode.getUuid(),
131 parentTaxonNode.getUuid(),
132 moveToNewParent);
133 }
134
135 return null;
136 }
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#onComplete()
140 */
141 @Override
142 public void onComplete() {
143 // TODO Auto-generated method stub
144
145 }
146
147 }