726b217db43996520759c42b9f3306456d301c96
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / MoveDescriptionToOtherTaxonHandler.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.UUID;
9
10 import javax.lang.model.element.Element;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.IHandler;
17 import org.eclipse.core.commands.common.NotDefinedException;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
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.service.IDescriptionService;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
27 import eu.etaxonomy.cdm.model.description.TaxonDescription;
28 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29 import eu.etaxonomy.taxeditor.editor.EditorUtil;
30 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
31 import eu.etaxonomy.taxeditor.editor.Page;
32 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
33 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
34 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionToOtherTaxonOperation;
35 //import eu.etaxonomy.taxeditor.navigator.operation.ChangeAcceptedTaxonToSynonymOperation;
36 import eu.etaxonomy.taxeditor.model.MessagingUtils;
37 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
38 import eu.etaxonomy.taxeditor.store.CdmStore;
39 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
40
41 /**
42 * <p>ChangeAcceptedTaxonToSynonymHandler class.</p>
43 *
44 * @author a.kohlbecker
45 * @created Okt. 11, 2013
46 * @version 1.0
47 *
48 */
49 public class MoveDescriptionToOtherTaxonHandler extends AbstractHandler
50 implements IHandler, IPostOperationEnabled {
51 private static final Logger logger = Logger
52 .getLogger(MoveDescriptionToOtherTaxonHandler.class);
53 private MoveDescriptionToOtherTaxonOperation operation;
54
55 private UUID newAcceptedTaxonNodeUuid;
56 private TaxonNameEditor editor;
57
58 /* (non-Javadoc)
59 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
60 */
61 /** {@inheritDoc} */
62 public Object execute(ExecutionEvent event) throws ExecutionException {
63
64 ISelection selection = HandlerUtil.getCurrentSelection(event);
65
66 if(selection instanceof IStructuredSelection){
67
68 List<TaxonDescription> descriptions = new ArrayList<TaxonDescription>();
69
70 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
71
72 for(Object element : structuredSelection.toArray()){
73 if (element instanceof TaxonDescription){
74 UUID uuid = ((TaxonDescription)element).getUuid();
75 descriptions.add((TaxonDescription) CdmStore.getService(IDescriptionService.class).load(uuid, null));
76 }
77 }
78 if(descriptions.size() == 0){
79 return null;
80 }
81
82 // Choose the target taxon
83 List<UUID> excludeTaxa = new ArrayList<UUID>();
84 editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
85 excludeTaxa.add(descriptions.get(0).getTaxon().getUuid());
86 TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
87 editor.getConversationHolder(),
88 "Choose the accepted taxon",
89 excludeTaxa,
90 null,
91 null);
92
93 if (newAcceptedTaxonNode == null) {
94 return null;
95 }
96
97 newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
98
99 try {
100 for(TaxonDescription description : descriptions){
101 operation = new MoveDescriptionToOtherTaxonOperation(event.getCommand().getName(),
102 editor.getUndoContext(), description, newAcceptedTaxonNode, this, editor);
103 EditorUtil.executeOperation(operation);
104 }
105
106 } catch (NotDefinedException e) {
107 logger.warn("Command name not set");
108 }
109 }
110
111 return null;
112
113 }
114
115 /* (non-Javadoc)
116 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
117 */
118 /** {@inheritDoc} */
119 public boolean postOperation(CdmBase objectAffectedByOperation) {
120 Display.getDefault().asyncExec(new Runnable(){
121
122 public void run() {
123 EditorUtil.close(editor.getMultiPageTaxonEditor());
124
125 try {
126 MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
127 if(possibleOpenEditor != null){
128 EditorUtil.close(possibleOpenEditor);
129 }
130 EditorUtil.openTaxonNode(newAcceptedTaxonNodeUuid);
131 } catch (PartInitException e) {
132 MessagingUtils.error(this.getClass(), e);
133 throw new RuntimeException(e);
134 } catch (Exception e) {
135 MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
136 }
137 }
138
139 });
140
141
142 return true;
143 }
144
145 /**
146 * <p>onComplete</p>
147 *
148 * @return a boolean.
149 */
150 public boolean onComplete() {
151 // TODO Auto-generated method stub
152 return false;
153 }
154 }