ba681d70c4affeae78491497ba7ebeebf04c73a0
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / handler / MoveTaxonHandler.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.navigation.navigator.handler;
11
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import org.eclipse.core.commands.AbstractHandler;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ExecutionException;
22 import org.eclipse.core.expressions.EvaluationContext;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.TreeSelection;
25 import org.eclipse.ui.IEditorInput;
26 import org.eclipse.ui.IEditorReference;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.handlers.HandlerUtil;
30
31 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
32 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
33 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
34 import eu.etaxonomy.cdm.model.common.CdmBase;
35 import eu.etaxonomy.cdm.model.common.ITreeNode;
36 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
37 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
39 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
40 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
41 import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
42 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
43 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
44 import eu.etaxonomy.taxeditor.store.CdmStore;
45 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
46
47 /**
48 * <p>MoveTaxonHandler class.</p>
49 *
50 * @author n.hoffmann
51 * @created 01.04.2009
52 * @version 1.0
53 */
54 public class MoveTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
55
56 private TaxonNode parentTaxonNode;
57 protected IWorkbenchPage activePage;
58 /* (non-Javadoc)
59 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
60 */
61 /** {@inheritDoc} */
62 public Object execute(ExecutionEvent event) throws ExecutionException {
63 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
64 TaxonNavigator taxonNavigator = (TaxonNavigator)NavigationUtil.showView(TaxonNavigator.ID);
65
66 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
67
68 Iterator selectionIterator = selection.iterator();
69 TaxonNode taxonNode = null;
70 UUID taxonNodeUUID = null;
71 // do not show the current selection
72 List<UUID> excludeTaxa = new ArrayList<UUID>();
73
74 if (selection.size() == 1){
75 Object object = selectionIterator.next();
76 if(object instanceof TaxonNode){
77 taxonNode = HibernateProxyHelper.deproxy(object,TaxonNode.class);
78 taxonNodeUUID = taxonNode.getUuid();
79 excludeTaxa.add(taxonNode.getTaxon().getUuid());
80 }
81 } else{
82 if( MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
83 return null;
84 }
85 }
86
87
88 // TaxonNode taxonNode = (TaxonNode) selection.getFirstElement();
89 if (taxonNode != null){
90 parentTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), taxonNavigator.getConversationHolder(), "Choose new parent", excludeTaxa, null, null);
91
92 if(parentTaxonNode != null){
93 if(NavigationUtil.isDirty(parentTaxonNode)){
94 MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "Unsaved Parent Taxon", "There are unsaved " +
95 "changes in the parent taxon. Please save first.");
96 return null;
97 }
98
99 AbstractPostOperation operation = new MoveTaxonOperation
100 ("Move taxon to new parent", NavigationUtil.getUndoContext(),
101 taxonNode, parentTaxonNode, taxonNavigator, taxonNavigator); //$NON-NLS-1$
102 NavigationUtil.executeOperation(operation);
103 taxonNavigator.refresh();
104
105 }
106 }
107 return null;
108 }
109
110 /* (non-Javadoc)
111 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
112 */
113 /** {@inheritDoc} */
114 public boolean postOperation(CdmBase objectAffectedByOperation) {
115 return true;
116 }
117
118 /**
119 * <p>onComplete</p>
120 *
121 * @return a boolean.
122 */
123 public boolean onComplete() {
124 return false;
125 }
126
127
128
129 }