Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / handler / RemotingMoveFactualDataHandler.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.viewers.TreeSelection;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
26 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
27 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28 import eu.etaxonomy.taxeditor.editor.EditorUtil;
29 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
30 import eu.etaxonomy.taxeditor.model.AbstractUtility;
31 import eu.etaxonomy.taxeditor.model.MessagingUtils;
32 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
33 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
34 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveFactualDataOperation;
35 import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
36 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
37
38 /**
39 * @author cmathew
40 * @date 19 Jun 2015
41 *
42 */
43 public class RemotingMoveFactualDataHandler extends RemotingCdmHandler {
44
45
46 private TaxonNode sourceTaxonNode;
47 private TaxonNode targetTaxonNode;
48 /**
49 * @param label
50 */
51 public RemotingMoveFactualDataHandler() {
52 super(TaxonNavigatorLabels.MOVE_FACTUAL_DATA_LABEL);
53 }
54
55 /* (non-Javadoc)
56 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#allowOperations(org.eclipse.core.commands.ExecutionEvent)
57 */
58 @Override
59 public IStatus allowOperations(ExecutionEvent event) {
60 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
61 // check that only a single taxon tree node has been selected
62 if(selection.size() > 1) {
63 return new Status(IStatus.ERROR,
64 "unknown",
65 TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
66 }
67
68 // check for no taxon tree node selected
69 if(selection.size() == 0) {
70 return new Status(IStatus.ERROR,
71 "unknown",
72 TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
73 }
74
75 // check that selected object is a taxon node
76 Object obj = selection.iterator().next();
77 if(obj instanceof ITaxonTreeNode) {
78 sourceTaxonNode = (TaxonNode)obj;
79 } else {
80 return new Status(IStatus.ERROR,
81 "unknown",
82 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
83 }
84
85 if(NavigationUtil.isDirty(sourceTaxonNode)) {
86 return new Status(IStatus.ERROR,
87 "unknown",
88 TaxonNavigatorLabels.UNSAVED_CHANGES_MESSAGE);
89 }
90
91 List<UUID> excludeTaxa = new ArrayList<UUID>();
92 excludeTaxa.add(sourceTaxonNode.getTaxon().getUuid());
93
94 targetTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
95 new ConversationHolderMock(),
96 "Choose the accepted taxon",
97 excludeTaxa,
98 null,
99 null);
100
101 if(targetTaxonNode == null) {
102 return new Status(IStatus.CANCEL,
103 "unknown",
104 "");
105 }
106 if(NavigationUtil.isDirty(targetTaxonNode)){
107 return new Status(IStatus.ERROR,
108 "unknown",
109 TaxonNavigatorLabels.UNSAVED_CHANGES_MESSAGE);
110 }
111 return Status.OK_STATUS;
112 }
113
114 /* (non-Javadoc)
115 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#prepareOperation(org.eclipse.core.commands.ExecutionEvent)
116 */
117 @Override
118 public AbstractOperation prepareOperation(ExecutionEvent event) {
119 return new RemotingMoveFactualDataOperation(event.getTrigger(),
120 false,
121 sourceTaxonNode.getTaxon().getUuid(),
122 targetTaxonNode.getTaxon().getUuid());
123 }
124
125 /* (non-Javadoc)
126 * @see eu.etaxonomy.taxeditor.operation.RemotingCdmHandler#onComplete()
127 */
128 @Override
129 public void onComplete() {
130 Display.getDefault().asyncExec(new Runnable(){
131 @Override
132 public void run() {
133 try {
134 //close and re-open to refresh factual data view
135 MultiPageTaxonEditor sourceEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(sourceTaxonNode.getUuid());
136 MultiPageTaxonEditor targetEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(targetTaxonNode.getUuid());
137 if(targetEditor != null){
138 AbstractUtility.close(sourceEditor);
139 AbstractUtility.close(targetEditor);
140 }
141 EditorUtil.openTaxonNode(sourceTaxonNode.getUuid());
142 EditorUtil.openTaxonNode(targetTaxonNode.getUuid());
143 } catch (PartInitException e) {
144 MessagingUtils.error(this.getClass(), e);
145 throw new RuntimeException(e);
146 } catch (Exception e) {
147 MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
148 }
149 }
150
151 });
152
153 }
154
155 }