3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.handler
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
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
;
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
;
52 * @created Feb 8, 2011
55 public class MoveDescriptionElementsHandler
extends AbstractHandler
implements IPostOperationEnabled
{
56 private UUID newAcceptedTaxonNodeUuid
;
57 private TaxonNameEditor editor
;
60 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
62 // ConversationHolder conversation = CdmStore.createConversation();
63 editor
= (TaxonNameEditor
) EditorUtil
.getActiveEditorPage(Page
.NAME
);
64 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
65 Taxon actualTaxon
= null;
66 if(selection
instanceof IStructuredSelection
){
68 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
70 List
<DescriptionElementBase
> elements
= new ArrayList
<DescriptionElementBase
>();
72 for(Object element
: structuredSelection
.toArray()){
73 if(element
instanceof DescriptionElementBase
){
74 UUID uuid
= ((DescriptionElementBase
) element
).getUuid();
76 elements
.add(CdmStore
.getService(IDescriptionService
.class).loadDescriptionElement(uuid
, null));
77 } else if(element
instanceof FeatureNodeContainer
){
78 for(DescriptionElementBase de
: ((FeatureNodeContainer
)element
).getDescriptionElements()){
80 CdmStore
.getService(IDescriptionService
.class).loadDescriptionElement(de
.getUuid(), null)
86 if(elements
.size() == 0){
89 DescriptionBase description
= elements
.get(0).getInDescription();
90 List
<UUID
> excludeTaxa
= new ArrayList
<UUID
>();
91 if (description
!=null && description
.isInstanceOf(TaxonDescription
.class)){
92 TaxonDescription taxonDescription
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
93 actualTaxon
= taxonDescription
.getTaxon();
94 excludeTaxa
.add(actualTaxon
.getUuid());
96 Classification classification
= null;
97 if (actualTaxon
!= null){
98 if (!actualTaxon
.getTaxonNodes().isEmpty() && actualTaxon
.getTaxonNodes().size() ==1){
99 classification
= actualTaxon
.getTaxonNodes().iterator().next().getClassification();
102 TaxonNode newAcceptedTaxonNode
= TaxonNodeSelectionDialog
.select(HandlerUtil
.getActiveShell(event
),
103 editor
.getConversationHolder(),
104 "Choose the accepted taxon",
108 if (newAcceptedTaxonNode
!= null){
109 Taxon targetTaxon
= newAcceptedTaxonNode
.getTaxon();
111 if(targetTaxon
== null){
115 newAcceptedTaxonNodeUuid
= newAcceptedTaxonNode
.getUuid();
117 String moveMessage
= String
.format("Elements moved from %s", EditorUtil
.getActiveMultiPageTaxonEditor().getTaxon());
120 AbstractPostOperation operation
= new MoveDescriptionElementsOperation(
121 event
.getCommand().getName(), EditorUtil
.getUndoContext(),
122 targetTaxon
.getUuid(), moveMessage
, elements
, false, this);
124 AbstractUtility
.executeOperation(operation
);
126 //CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
128 } catch (NotDefinedException e
) {
129 MessagingUtils
.error(getClass(), e
);
139 public boolean postOperation(CdmBase objectAffectedByOperation
) {
141 editor
.getConversationHolder().bind();
142 editor
.getConversationHolder().commit(true);
143 Display
.getDefault().asyncExec(new Runnable(){
147 //AbstractUtility.close(editor.getMultiPageTaxonEditor());
150 MultiPageTaxonEditor possibleOpenEditor
= (MultiPageTaxonEditor
) EditorUtil
.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid
);
151 if(possibleOpenEditor
!= null){
152 AbstractUtility
.close(possibleOpenEditor
);
154 EditorUtil
.openTaxonNode(newAcceptedTaxonNodeUuid
);
155 } catch (PartInitException e
) {
156 MessagingUtils
.error(this.getClass(), e
);
157 throw new RuntimeException(e
);
158 } catch (Exception e
) {
159 MessagingUtils
.warningDialog("Could not create Taxon", this, e
.getMessage());
170 public boolean onComplete() {