merge-update from trunk
[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.preference.PreferencesUtil;
45 import eu.etaxonomy.taxeditor.store.CdmStore;
46 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
47 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionNaturalOrderDialog;
48
49 /**
50 * <p>MoveTaxonHandler class.</p>
51 *
52 * @author n.hoffmann
53 * @created 01.04.2009
54 * @version 1.0
55 */
56 public class MoveTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
57
58 private TaxonNode parentTaxonNode;
59 protected IWorkbenchPage activePage;
60 /* (non-Javadoc)
61 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
62 */
63 /** {@inheritDoc} */
64 public Object execute(ExecutionEvent event) throws ExecutionException {
65 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
66 TaxonNavigator taxonNavigator = (TaxonNavigator)NavigationUtil.showView(TaxonNavigator.ID);
67
68 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
69
70 Iterator selectionIterator = selection.iterator();
71 TaxonNode taxonNode = null;
72 UUID taxonNodeUUID = null;
73 // do not show the current selection
74 List<UUID> excludeTaxa = new ArrayList<UUID>();
75
76 if (selection.size() == 1){
77 Object object = selectionIterator.next();
78 if(object instanceof TaxonNode){
79 taxonNode = HibernateProxyHelper.deproxy(object,TaxonNode.class);
80 taxonNodeUUID = taxonNode.getUuid();
81 excludeTaxa.add(taxonNode.getTaxon().getUuid());
82 }
83 } else{
84 if( MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
85 return null;
86 }
87 }
88
89
90 // TaxonNode taxonNode = (TaxonNode) selection.getFirstElement();
91 if (taxonNode != null){
92 boolean moveToNewParent = true;
93 if (PreferencesUtil.getSortNodesNaturally()){
94 if(!MessageDialog.openQuestion(null, "Target node", "The choosen target node should be the parent?")){
95 moveToNewParent = false;
96 }
97 parentTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), taxonNavigator.getConversationHolder(), "Choose the taxon above the moved taxon.", excludeTaxa, null, null);
98 }else{
99 parentTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), taxonNavigator.getConversationHolder(), "Choose new parent", excludeTaxa, null, null);
100 }
101 if(parentTaxonNode != null){
102 if(NavigationUtil.isDirty(parentTaxonNode)){
103 MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "Unsaved Parent Taxon", "There are unsaved " +
104 "changes in the parent taxon. Please save first.");
105 return null;
106 }
107
108 AbstractPostOperation operation = new MoveTaxonOperation
109 ("Move taxon to new parent", NavigationUtil.getUndoContext(),
110 taxonNode, parentTaxonNode, taxonNavigator, taxonNavigator, moveToNewParent); //$NON-NLS-1$
111 NavigationUtil.executeOperation(operation);
112 taxonNavigator.refresh();
113
114 }
115 }
116 return null;
117 }
118
119 /* (non-Javadoc)
120 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
121 */
122 /** {@inheritDoc} */
123 public boolean postOperation(CdmBase objectAffectedByOperation) {
124 return true;
125 }
126
127 /**
128 * <p>onComplete</p>
129 *
130 * @return a boolean.
131 */
132 public boolean onComplete() {
133 return false;
134 }
135
136
137
138 }