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
.Annotation
;
30 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
33 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
39 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
40 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
41 import eu
.etaxonomy
.taxeditor
.editor
.Page
;
42 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
43 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.MoveDescriptionElementsOperation
;
44 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
45 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
46 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
47 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
48 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
49 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
50 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TaxonNodeSelectionDialog
;
55 * @created Feb 8, 2011
58 public class MoveDescriptionElementsHandler
extends AbstractHandler
implements IPostOperationEnabled
{
59 private UUID newAcceptedTaxonNodeUuid
;
60 private TaxonNameEditor editor
;
62 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
65 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
67 // ConversationHolder conversation = CdmStore.createConversation();
68 editor
= (TaxonNameEditor
) EditorUtil
.getActiveEditorPage(Page
.NAME
);
69 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
70 Taxon actualTaxon
= null;
71 if(selection
instanceof IStructuredSelection
){
73 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
75 List
<DescriptionElementBase
> elements
= new ArrayList
<DescriptionElementBase
>();
77 for(Object element
: structuredSelection
.toArray()){
78 if(element
instanceof DescriptionElementBase
){
79 UUID uuid
= ((DescriptionElementBase
) element
).getUuid();
81 elements
.add(CdmStore
.getService(IDescriptionService
.class).loadDescriptionElement(uuid
, null));
82 } else if(element
instanceof FeatureNodeContainer
){
83 for(DescriptionElementBase de
: ((FeatureNodeContainer
)element
).getDescriptionElements()){
85 CdmStore
.getService(IDescriptionService
.class).loadDescriptionElement(de
.getUuid(), null)
91 if(elements
.size() == 0){
94 DescriptionBase description
= elements
.get(0).getInDescription();
95 List
<UUID
> excludeTaxa
= new ArrayList
<UUID
>();
96 if (description
.isInstanceOf(TaxonDescription
.class)){
97 TaxonDescription taxonDescription
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
98 actualTaxon
= taxonDescription
.getTaxon();
99 excludeTaxa
.add(actualTaxon
.getUuid());
101 Classification classification
= null;
102 if (actualTaxon
!= null){
103 if (!actualTaxon
.getTaxonNodes().isEmpty() && actualTaxon
.getTaxonNodes().size() ==1){
104 classification
= actualTaxon
.getTaxonNodes().iterator().next().getClassification();
107 TaxonNode newAcceptedTaxonNode
= TaxonNodeSelectionDialog
.select(HandlerUtil
.getActiveShell(event
),
108 editor
.getConversationHolder(),
109 "Choose the accepted taxon",
113 if (newAcceptedTaxonNode
!= null){
114 Taxon targetTaxon
= newAcceptedTaxonNode
.getTaxon();
116 if(targetTaxon
== null){
120 newAcceptedTaxonNodeUuid
= newAcceptedTaxonNode
.getUuid();
121 TaxonDescription targetDescription
= TaxonDescription
.NewInstance(targetTaxon
);
122 String moveMessage
= String
.format("Elements moved from %s", EditorUtil
.getActiveMultiPageTaxonEditor().getTaxon());
123 targetDescription
.setTitleCache(moveMessage
, true);
124 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
125 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
126 targetDescription
.addAnnotation(annotation
);
129 AbstractPostOperation operation
= new MoveDescriptionElementsOperation(
130 event
.getCommand().getName(), EditorUtil
.getUndoContext(),
131 targetDescription
, elements
, false, this);
133 EditorUtil
.executeOperation(operation
);
135 //CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
137 } catch (NotDefinedException e
) {
138 MessagingUtils
.error(getClass(), e
);
147 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
151 public boolean postOperation(CdmBase objectAffectedByOperation
) {
153 editor
.getConversationHolder().bind();
154 editor
.getConversationHolder().commit(true);
155 Display
.getDefault().asyncExec(new Runnable(){
159 //AbstractUtility.close(editor.getMultiPageTaxonEditor());
162 MultiPageTaxonEditor possibleOpenEditor
= (MultiPageTaxonEditor
) EditorUtil
.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid
);
163 if(possibleOpenEditor
!= null){
164 AbstractUtility
.close(possibleOpenEditor
);
166 EditorUtil
.openTaxonNode(newAcceptedTaxonNodeUuid
);
167 } catch (PartInitException e
) {
168 MessagingUtils
.error(this.getClass(), e
);
169 throw new RuntimeException(e
);
170 } catch (Exception e
) {
171 MessagingUtils
.warningDialog("Could not create Taxon", this, e
.getMessage());
182 public boolean onComplete() {
183 // TODO Auto-generated method stub