Merge branch 'release/4.6.0'
[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.l10n.Messages;
28 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
29 import eu.etaxonomy.taxeditor.navigation.navigator.TreeNodeDropAdapter.MovingType;
30 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
31 import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
32 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
34
35 /**
36 * @author cmathew
37 * @date 19 Jun 2015
38 *
39 */
40 public class RemotingMoveTaxonNodeHandler extends RemotingCdmHandler {
41
42 private TaxonNode oldTaxonNode;
43
44 public RemotingMoveTaxonNodeHandler() {
45 super(TaxonNavigatorLabels.MOVE_TAXON_LABEL);
46 }
47
48 /* (non-Javadoc)
49 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#allowOperations(org.eclipse.core.commands.ExecutionEvent)
50 */
51 @Override
52 public IStatus allowOperations(ExecutionEvent event) {
53 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
54 // check that only a single taxon tree node has been selected
55 if(selection.size() > 1) {
56 return new Status(IStatus.ERROR,
57 "unknown", //$NON-NLS-1$
58 TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
59 }
60
61 // check for no taxon tree node selected
62 if(selection.size() == 0) {
63 return new Status(IStatus.ERROR,
64 "unknown", //$NON-NLS-1$
65 TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
66 }
67 // check that selected object is a taxon node
68 Object obj = selection.iterator().next();
69 if(obj instanceof TaxonNode) {
70 oldTaxonNode = (TaxonNode)obj;
71 } else {
72 return new Status(IStatus.ERROR,
73 "unknown", //$NON-NLS-1$
74 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
75 }
76 return Status.OK_STATUS;
77 }
78
79
80 /* (non-Javadoc)
81 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#prepareOperation(org.eclipse.core.commands.ExecutionEvent)
82 */
83 @Override
84 public AbstractOperation prepareOperation(ExecutionEvent event) {
85 Shell activeShell = HandlerUtil.getActiveShell(event);
86 TaxonNode parentTaxonNode;
87
88 List<UUID> excludeTaxa = new ArrayList<UUID>();
89 excludeTaxa.add(oldTaxonNode.getTaxon().getUuid());
90
91 MovingType moveToNewParent = MovingType.CHILD;
92
93 if (PreferencesUtil.getSortNodesNaturally()){
94
95
96 parentTaxonNode = TaxonNodeSelectionDialog.select(activeShell,
97 new ConversationHolderMock(),
98 Messages.RemotingMoveTaxonNodeHandler_CHOOSE_TAXON,
99 excludeTaxa,
100 null,
101 oldTaxonNode.getClassification());
102 String[] buttonLables = {Messages.RemotingMoveTaxonNodeHandler_CHILD, Messages.RemotingMoveTaxonNodeHandler_BEHIND,Messages.RemotingMoveTaxonNodeHandler_CANCEL};
103 MessageDialog dialog = new MessageDialog(null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE, null, Messages.RemotingMoveTaxonNodeHandler_TARGET_NODE_MESSAGE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
104 dialog.open();
105 int returnCode = dialog.getReturnCode();
106 if (returnCode == 0){
107 moveToNewParent = MovingType.CHILD;
108 } else if (returnCode == 1){
109 moveToNewParent = MovingType.BEHIND;
110 }
111 } else {
112 parentTaxonNode = TaxonNodeSelectionDialog.select(activeShell,
113 new ConversationHolderMock(),
114 Messages.RemotingMoveTaxonNodeHandler_CHOOSE_PARENT,
115 excludeTaxa,
116 null,
117 oldTaxonNode.getClassification());
118 }
119
120
121 if(parentTaxonNode != null){
122 if(NavigationUtil.isDirty(parentTaxonNode)){
123 MessageDialog.openWarning(activeShell,
124 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT,
125 Messages.RemotingMoveTaxonNodeHandler_UNSAVED_PARENT_MESSAGE);
126 return null;
127 }
128
129 return new RemotingMoveTaxonOperation(event.getTrigger(),
130 false,
131 oldTaxonNode.getUuid(),
132 parentTaxonNode.getUuid(),
133 moveToNewParent);
134 }
135
136 return null;
137 }
138
139 /* (non-Javadoc)
140 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#onComplete()
141 */
142 @Override
143 public void onComplete() {
144 // TODO Auto-generated method stub
145
146 }
147
148 }