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