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