b91f68a7bec50a0481af5447497ccd252333e278
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / MoveDescriptionElementsHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.commands.common.NotDefinedException;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.handlers.HandlerUtil;
26
27 import eu.etaxonomy.cdm.api.service.IDescriptionService;
28 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.description.DescriptionBase;
31 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32 import eu.etaxonomy.cdm.model.description.TaxonDescription;
33 import eu.etaxonomy.cdm.model.taxon.Classification;
34 import eu.etaxonomy.cdm.model.taxon.Taxon;
35 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36 import eu.etaxonomy.taxeditor.editor.EditorUtil;
37 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
38 import eu.etaxonomy.taxeditor.editor.Page;
39 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
40 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
41 import eu.etaxonomy.taxeditor.model.AbstractUtility;
42 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
43 import eu.etaxonomy.taxeditor.model.MessagingUtils;
44 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
45 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
46 import eu.etaxonomy.taxeditor.store.CdmStore;
47 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
48
49
50 /**
51 * @author n.hoffmann
52 * @created Feb 8, 2011
53 * @version 1.0
54 */
55 public class MoveDescriptionElementsHandler extends AbstractHandler implements IPostOperationEnabled{
56 private UUID newAcceptedTaxonNodeUuid;
57 private TaxonNameEditor editor;
58 /* (non-Javadoc)
59 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
60 */
61 @Override
62 public Object execute(ExecutionEvent event) throws ExecutionException {
63
64 // ConversationHolder conversation = CdmStore.createConversation();
65 editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
66 ISelection selection = HandlerUtil.getCurrentSelection(event);
67 Taxon actualTaxon= null;
68 if(selection instanceof IStructuredSelection){
69
70 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
71
72 List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
73
74 for(Object element : structuredSelection.toArray()){
75 if(element instanceof DescriptionElementBase){
76 UUID uuid = ((DescriptionElementBase) element).getUuid();
77
78 elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
79 } else if(element instanceof FeatureNodeContainer){
80 for(DescriptionElementBase de : ((FeatureNodeContainer)element).getDescriptionElements()){
81 elements.add(
82 CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null)
83 );
84 }
85 }
86 }
87
88 if(elements.size() == 0){
89 return null;
90 }
91 DescriptionBase description = elements.get(0).getInDescription();
92 List<UUID> excludeTaxa = new ArrayList<UUID>();
93 if (description.isInstanceOf(TaxonDescription.class)){
94 TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
95 actualTaxon = taxonDescription.getTaxon();
96 excludeTaxa.add(actualTaxon.getUuid());
97 }
98 Classification classification = null;
99 if (actualTaxon != null){
100 if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
101 classification = actualTaxon.getTaxonNodes().iterator().next().getClassification();
102 }
103 }
104 TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
105 editor.getConversationHolder(),
106 "Choose the accepted taxon",
107 excludeTaxa,
108 null,classification
109 );
110 if (newAcceptedTaxonNode != null){
111 Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
112
113 if(targetTaxon == null){
114 // canceled
115 return null;
116 }
117 newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
118
119 String moveMessage = String.format("Elements moved from %s", EditorUtil.getActiveMultiPageTaxonEditor().getTaxon());
120
121 try {
122 AbstractPostOperation operation = new MoveDescriptionElementsOperation(
123 event.getCommand().getName(), EditorUtil.getUndoContext(),
124 targetTaxon.getUuid(), moveMessage, elements, false, this);
125
126 EditorUtil.executeOperation(operation);
127
128 //CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
129
130 } catch (NotDefinedException e) {
131 MessagingUtils.error(getClass(), e);
132 }
133 }
134 }
135
136 return null;
137 }
138
139 /* (non-Javadoc)
140 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
141 */
142 /** {@inheritDoc} */
143 @Override
144 public boolean postOperation(CdmBase objectAffectedByOperation) {
145
146 editor.getConversationHolder().bind();
147 editor.getConversationHolder().commit(true);
148 Display.getDefault().asyncExec(new Runnable(){
149
150 @Override
151 public void run() {
152 //AbstractUtility.close(editor.getMultiPageTaxonEditor());
153
154 try {
155 MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
156 if(possibleOpenEditor != null){
157 AbstractUtility.close(possibleOpenEditor);
158 }
159 EditorUtil.openTaxonNode(newAcceptedTaxonNodeUuid);
160 } catch (PartInitException e) {
161 MessagingUtils.error(this.getClass(), e);
162 throw new RuntimeException(e);
163 } catch (Exception e) {
164 MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
165 }
166 }
167
168 });
169
170
171 return true;
172 }
173
174 @Override
175 public boolean onComplete() {
176 // TODO Auto-generated method stub
177 return false;
178 }
179
180 }