Project

General

Profile

Download (4.8 KB) Statistics
| Branch: | Tag: | Revision:
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.operation.MoveTaxonOperation;
34
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
35
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
36
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
38

    
39
/**
40
 * <p>MoveTaxonHandler class.</p>
41
 *
42
 * @author n.hoffmann
43
 * @created 01.04.2009
44
 * @version 1.0
45
 */
46
public class MoveTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
47

    
48
	private TaxonNode parentTaxonNode;
49
	protected IWorkbenchPage activePage;
50
	/* (non-Javadoc)
51
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
52
	 */
53
	/** {@inheritDoc} */
54
	@Override
55
    public Object execute(ExecutionEvent event) throws ExecutionException {
56
		activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
57
		TaxonNavigator taxonNavigator = (TaxonNavigator)NavigationUtil.showView(TaxonNavigator.ID);
58

    
59
		TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event);
60

    
61
		Iterator selectionIterator = selection.iterator();
62
		Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
63
		TaxonNode taxonNode= null;
64
		Set<UUID> taxonNodeUUIDs = new HashSet<UUID>();
65
		// do not show the current selection
66
		List<UUID> excludeTaxa = new ArrayList<UUID>();
67

    
68
		//if (selection.size() == 1){
69

    
70
		while (selectionIterator.hasNext()){
71
			Object object = selectionIterator.next();
72
			if(object instanceof TaxonNode){
73
			    taxonNode = HibernateProxyHelper.deproxy(object,TaxonNode.class);
74
				taxonNodes.add(taxonNode);
75
				taxonNodeUUIDs.add(taxonNode.getUuid());
76
				excludeTaxa.add(taxonNode.getTaxon().getUuid());
77
			}
78
		}
79
		/*} else{
80
			if( MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
81
				return null;
82
			}
83
		}*/
84

    
85

    
86
//		TaxonNode taxonNode = (TaxonNode) selection.getFirstElement();
87
		if (taxonNodes.size() >= 1){
88
		    Classification classification = taxonNodes.iterator().next().getClassification();
89
			boolean moveToNewParent = true;
90
			if (PreferencesUtil.getSortNodesNaturally()){
91
				if(!MessageDialog.openQuestion(null, "Target node", "The choosen target node should be the parent?")){
92
					moveToNewParent = false;
93
				}
94
				parentTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), taxonNavigator.getConversationHolder(), "Choose the taxon above the moved taxon.", excludeTaxa, null, classification);
95
			}else{
96
				parentTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), taxonNavigator.getConversationHolder(), "Choose new parent", excludeTaxa, null, classification);
97
			}
98
			if(parentTaxonNode != null){
99
				if(NavigationUtil.isDirty(parentTaxonNode)){
100
					MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "Unsaved Parent Taxon", "There are unsaved " +
101
							"changes in the parent taxon. Please save first.");
102
					return null;
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
	/* (non-Javadoc)
117
	 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
118
	 */
119
	/** {@inheritDoc} */
120
	@Override
121
    public boolean postOperation(CdmBase objectAffectedByOperation) {
122
		return true;
123
	}
124

    
125
	/**
126
	 * <p>onComplete</p>
127
	 *
128
	 * @return a boolean.
129
	 */
130
	@Override
131
    public boolean onComplete() {
132
		return false;
133
	}
134

    
135

    
136

    
137
}
(6-6/15)