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