288211858c672f3abcccc0e1114cedf64e7fc0e2
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / handler / RemotingMoveTaxonNodeHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.navigation.navigator.handler;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.UUID;
15
16 import org.eclipse.core.commands.ExecutionEvent;
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.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.TreeSelection;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
26 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
28 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
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.getUuid());
89
90 boolean moveToNewParent = true;
91
92 if (PreferencesUtil.getSortNodesNaturally()){
93 if(!MessageDialog.openQuestion(activeShell, "Target node", "The choosen target node should be the parent?")){
94 moveToNewParent = false;
95 }
96 parentTaxonNode = TaxonNodeSelectionDialog.select(activeShell,
97 new ConversationHolderMock(),
98 "Choose the taxon above the moved taxon.",
99 excludeTaxa,
100 null,
101 null);
102 } else {
103 parentTaxonNode = TaxonNodeSelectionDialog.select(activeShell,
104 new ConversationHolderMock(),
105 "Choose new parent",
106 excludeTaxa,
107 null,
108 null);
109 }
110
111
112 if(parentTaxonNode != null){
113 if(NavigationUtil.isDirty(parentTaxonNode)){
114 MessageDialog.openWarning(activeShell,
115 "Unsaved Parent Taxon",
116 "There are unsaved changes in the parent taxon. Please save first.");
117 return null;
118 }
119
120 return new RemotingMoveTaxonOperation(event.getTrigger(),
121 false,
122 oldTaxonNode.getUuid(),
123 parentTaxonNode.getUuid(),
124 moveToNewParent);
125 }
126
127 return null;
128 }
129
130 /* (non-Javadoc)
131 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#onComplete()
132 */
133 @Override
134 public void onComplete() {
135 // TODO Auto-generated method stub
136
137 }
138
139 }