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