Merge branch 'develop' into remoting-4.0
[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.Iterator;
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.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.TreeSelection;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
29 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
30 import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
31 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
34 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
35
36 /**
37 * <p>MoveTaxonHandler class.</p>
38 *
39 * @author n.hoffmann
40 * @created 01.04.2009
41 * @version 1.0
42 */
43 public class MoveTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
44
45 private TaxonNode parentTaxonNode;
46 protected IWorkbenchPage activePage;
47 /* (non-Javadoc)
48 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 */
50 /** {@inheritDoc} */
51
52 @Override
53 public Object execute(ExecutionEvent event) throws ExecutionException {
54 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
55 TaxonNavigator taxonNavigator = (TaxonNavigator)NavigationUtil.showView(TaxonNavigator.ID);
56
57 TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
58
59 Iterator selectionIterator = selection.iterator();
60 TaxonNode taxonNode = null;
61 UUID taxonNodeUUID = null;
62 // do not show the current selection
63 List<UUID> excludeTaxa = new ArrayList<UUID>();
64
65
66 if (selection.size() == 1){
67 Object object = selectionIterator.next();
68 if(object instanceof TaxonNode){
69 taxonNode = HibernateProxyHelper.deproxy(object,TaxonNode.class);
70 taxonNodeUUID = taxonNode.getUuid();
71 excludeTaxa.add(taxonNode.getTaxon().getUuid());
72 }
73 } else{
74 if( MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
75 return null;
76 }
77 }
78
79
80
81 // TaxonNode taxonNode = (TaxonNode) selection.getFirstElement();
82 if (taxonNode != null){
83 boolean moveToNewParent = true;
84 if (PreferencesUtil.getSortNodesNaturally()){
85 if(!MessageDialog.openQuestion(null, "Target node", "The choosen target node should be the parent?")){
86 moveToNewParent = false;
87 }
88 parentTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), taxonNavigator.getConversationHolder(), "Choose the taxon above the moved taxon.", excludeTaxa, null, null);
89 }else{
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, moveToNewParent); //$NON-NLS-1$
102 NavigationUtil.executeOperation(operation);
103 taxonNavigator.refresh();
104
105 }
106 }
107
108 return null;
109 }
110
111 /* (non-Javadoc)
112 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
113 */
114 /** {@inheritDoc} */
115 @Override
116 public boolean postOperation(CdmBase objectAffectedByOperation) {
117 return true;
118 }
119
120 /**
121 * <p>onComplete</p>
122 *
123 * @return a boolean.
124 */
125 @Override
126 public boolean onComplete() {
127 return false;
128 }
129
130 }