2ccfea94774506665600a3386c94ae94e2af9a57
[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.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.api.service.IDescriptionService;
26 import eu.etaxonomy.cdm.api.service.ITaxonService;
27 import eu.etaxonomy.cdm.model.common.Annotation;
28 import eu.etaxonomy.cdm.model.common.AnnotationType;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31 import eu.etaxonomy.cdm.model.description.TaxonDescription;
32 import eu.etaxonomy.cdm.model.taxon.Taxon;
33 import eu.etaxonomy.taxeditor.editor.EditorUtil;
34 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
35 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
36 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
37 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
38 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonBaseSelectionDialog;
41
42
43 /**
44 * @author n.hoffmann
45 * @created Feb 8, 2011
46 * @version 1.0
47 */
48 public class MoveDescriptionElementsHandler extends AbstractHandler {
49
50 /* (non-Javadoc)
51 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
52 */
53 @Override
54 public Object execute(ExecutionEvent event) throws ExecutionException {
55
56 // ConversationHolder conversation = CdmStore.createConversation();
57
58 ISelection selection = HandlerUtil.getCurrentSelection(event);
59
60 if(selection instanceof IStructuredSelection){
61
62 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
63
64 List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
65
66 for(Object element : structuredSelection.toArray()){
67 if(element instanceof DescriptionElementBase){
68 UUID uuid = ((DescriptionElementBase) element).getUuid();
69
70 elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
71 } else if(element instanceof FeatureNodeContainer){
72 for(DescriptionElementBase de : ((FeatureNodeContainer)element).getDescriptionElements()){
73 elements.add(
74 (DescriptionElementBase)CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null)
75 );
76 }
77 }
78 }
79
80 if(elements.size() == 0){
81 return null;
82 }
83
84 Taxon targetTaxon = TaxonBaseSelectionDialog.selectTaxon(HandlerUtil.getActiveShell(event), EditorUtil.getActiveMultiPageTaxonEditor().getConversationHolder(), null);
85
86 if(targetTaxon == null){
87 // canceled
88 return null;
89 }
90
91 TaxonDescription targetDescription = TaxonDescription.NewInstance(targetTaxon);
92 String moveMessage = String.format("Elements moved from %s", EditorUtil.getActiveMultiPageTaxonEditor().getTaxon());
93 targetDescription.setTitleCache(moveMessage, true);
94 Annotation annotation = Annotation.NewInstance(moveMessage, Language.getDefaultLanguage());
95 annotation.setAnnotationType(AnnotationType.TECHNICAL());
96 targetDescription.addAnnotation(annotation);
97
98 try {
99 AbstractPostOperation operation = new MoveDescriptionElementsOperation(
100 event.getCommand().getName(), EditorUtil.getUndoContext(),
101 targetDescription, elements, false, (IPostOperationEnabled) EditorUtil.getView(DescriptiveViewPart.ID, true));
102 EditorUtil.executeOperation(operation);
103 // conversation.commit(true);
104 CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
105
106 } catch (NotDefinedException e) {
107 EditorUtil.error(getClass(), e);
108 }
109 }
110
111 return null;
112 }
113
114 }