5711dc0648b4075338087894e30bce2f37c5b05f
[taxeditor.git] / 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.conversation.ConversationHolder;
26 import eu.etaxonomy.cdm.api.service.IDescriptionService;
27 import eu.etaxonomy.cdm.api.service.ITaxonService;
28 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
29 import eu.etaxonomy.cdm.model.description.TaxonDescription;
30 import eu.etaxonomy.cdm.model.taxon.Taxon;
31 import eu.etaxonomy.taxeditor.dialogs.filteredSelection.TaxonBaseSelectionDialog;
32 import eu.etaxonomy.taxeditor.editor.EditorUtil;
33 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
34 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
35 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
36 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38
39 /**
40 * @author n.hoffmann
41 * @created Feb 8, 2011
42 * @version 1.0
43 */
44 public class MoveDescriptionElementsHandler extends AbstractHandler {
45
46 /* (non-Javadoc)
47 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
48 */
49 @Override
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51
52 // ConversationHolder conversation = CdmStore.createConversation();
53
54 ISelection selection = HandlerUtil.getActiveMenuSelection(event);
55
56 if(selection instanceof IStructuredSelection){
57
58 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
59
60 List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
61
62 for(Object element : structuredSelection.toArray()){
63 if(element instanceof DescriptionElementBase){
64 UUID uuid = ((DescriptionElementBase) element).getUuid();
65
66 elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
67 }
68 }
69
70 Taxon targetTaxon = TaxonBaseSelectionDialog.selectTaxon(HandlerUtil.getActiveShell(event), EditorUtil.getActiveMultiPageTaxonEditor().getConversationHolder());
71
72 TaxonDescription targetDescription = TaxonDescription.NewInstance(targetTaxon);
73 targetDescription.setTitleCache(String.format("Copied from %s", EditorUtil.getActiveMultiPageTaxonEditor().getTaxon()), true);
74
75 try {
76 AbstractPostOperation operation = new MoveDescriptionElementsOperation(
77 event.getCommand().getName(), EditorUtil.getUndoContext(),
78 targetDescription, elements, false, (IPostOperationEnabled) EditorUtil.getView(DescriptiveViewPart.ID, true));
79 EditorUtil.executeOperation(operation);
80 // conversation.commit(true);
81 CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
82
83 } catch (NotDefinedException e) {
84 EditorUtil.error(getClass(), e);
85 }
86 }
87
88 return null;
89 }
90
91 }