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
.api
.service
.ITaxonService
;
29 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
31 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
36 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
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
.DescriptiveViewPart
;
44 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.MoveDescriptionElementsOperation
;
45 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
46 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
47 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
48 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
49 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
50 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
51 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TaxonBaseSelectionDialog
;
52 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TaxonNodeSelectionDialog
;
57 * @created Feb 8, 2011
60 public class MoveDescriptionElementsHandler
extends AbstractHandler
implements IPostOperationEnabled
{
61 private UUID newAcceptedTaxonNodeUuid
;
62 private TaxonNameEditor editor
;
64 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
67 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
69 // ConversationHolder conversation = CdmStore.createConversation();
70 editor
= (TaxonNameEditor
) EditorUtil
.getActiveEditorPage(Page
.NAME
);
71 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
73 if(selection
instanceof IStructuredSelection
){
75 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
77 List
<DescriptionElementBase
> elements
= new ArrayList
<DescriptionElementBase
>();
79 for(Object element
: structuredSelection
.toArray()){
80 if(element
instanceof DescriptionElementBase
){
81 UUID uuid
= ((DescriptionElementBase
) element
).getUuid();
83 elements
.add(CdmStore
.getService(IDescriptionService
.class).loadDescriptionElement(uuid
, null));
84 } else if(element
instanceof FeatureNodeContainer
){
85 for(DescriptionElementBase de
: ((FeatureNodeContainer
)element
).getDescriptionElements()){
87 (DescriptionElementBase
)CdmStore
.getService(IDescriptionService
.class).loadDescriptionElement(de
.getUuid(), null)
93 if(elements
.size() == 0){
96 DescriptionBase description
= elements
.get(0).getInDescription();
97 List
<UUID
> excludeTaxa
= new ArrayList
<UUID
>();
98 if (description
.isInstanceOf(TaxonDescription
.class)){
99 TaxonDescription taxonDescription
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
100 Taxon actualTaxon
= taxonDescription
.getTaxon();
101 excludeTaxa
.add(actualTaxon
.getUuid());
105 TaxonNode newAcceptedTaxonNode
= TaxonNodeSelectionDialog
.select(HandlerUtil
.getActiveShell(event
),
106 editor
.getConversationHolder(),
107 "Choose the accepted taxon",
111 Taxon targetTaxon
= newAcceptedTaxonNode
.getTaxon();
113 if(targetTaxon
== null){
117 newAcceptedTaxonNodeUuid
= newAcceptedTaxonNode
.getUuid();
118 TaxonDescription targetDescription
= TaxonDescription
.NewInstance(targetTaxon
);
119 String moveMessage
= String
.format("Elements moved from %s", EditorUtil
.getActiveMultiPageTaxonEditor().getTaxon());
120 targetDescription
.setTitleCache(moveMessage
, true);
121 Annotation annotation
= Annotation
.NewInstance(moveMessage
, Language
.getDefaultLanguage());
122 annotation
.setAnnotationType(AnnotationType
.TECHNICAL());
123 targetDescription
.addAnnotation(annotation
);
126 AbstractPostOperation operation
= new MoveDescriptionElementsOperation(
127 event
.getCommand().getName(), EditorUtil
.getUndoContext(),
128 targetDescription
, elements
, false, this);
130 EditorUtil
.executeOperation(operation
);
132 //CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
134 } catch (NotDefinedException e
) {
135 MessagingUtils
.error(getClass(), e
);
143 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
147 public boolean postOperation(CdmBase objectAffectedByOperation
) {
149 editor
.getConversationHolder().bind();
150 editor
.getConversationHolder().commit(true);
151 Display
.getDefault().asyncExec(new Runnable(){
154 //AbstractUtility.close(editor.getMultiPageTaxonEditor());
157 MultiPageTaxonEditor possibleOpenEditor
= (MultiPageTaxonEditor
) EditorUtil
.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid
);
158 if(possibleOpenEditor
!= null){
159 AbstractUtility
.close(possibleOpenEditor
);
161 EditorUtil
.openTaxonNode(newAcceptedTaxonNodeUuid
);
162 } catch (PartInitException e
) {
163 MessagingUtils
.error(this.getClass(), e
);
164 throw new RuntimeException(e
);
165 } catch (Exception e
) {
166 MessagingUtils
.warningDialog("Could not create Taxon", this, e
.getMessage());
177 public boolean onComplete() {
178 // TODO Auto-generated method stub