a9a4a4666da6d54e11119762f12af3ff860ca83b
[taxeditor.git] / taxeditor-navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / 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.handler;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
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.core.commands.operations.IUndoableOperation;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.swt.widgets.Shell;
23
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.taxeditor.dialogs.filteredSelection.FilteredTaxonNodeSelectionDialog;
27 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
28 import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
29 import eu.etaxonomy.taxeditor.operations.MoveTaxonOperation;
30
31 /**
32 * @author n.hoffmann
33 * @created 01.04.2009
34 * @version 1.0
35 */
36 public class MoveTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
37 private static final Logger logger = Logger
38 .getLogger(MoveTaxonHandler.class);
39 private TaxonNode parentTaxonNode;
40
41 /* (non-Javadoc)
42 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
43 */
44 public Object execute(ExecutionEvent event) throws ExecutionException {
45 Shell shell = NavigationUtil.getShell();
46 TaxonNode taxonNode = (TaxonNode) NavigationUtil.getCurrentSelection(event);
47
48 // do not show the current selection
49 List<UUID> excludeTaxa = new ArrayList<UUID>();
50 excludeTaxa.add(taxonNode.getTaxon().getUuid());
51 parentTaxonNode = FilteredTaxonNodeSelectionDialog.selectTaxonNode(shell, "Choose new parent", excludeTaxa, null);
52
53 if(parentTaxonNode != null){
54
55 logger.warn("child " +taxonNode.getTaxon().toString());
56 logger.warn("parent " + parentTaxonNode.toString());
57
58 if(NavigationUtil.isDirty(parentTaxonNode)){
59 MessageDialog.openWarning(shell, "Unsaved Parent Taxon", "There are unsaved " +
60 "changes in the parent taxon. Please save first.");
61 return null;
62 }
63
64 IUndoableOperation operation = new MoveTaxonOperation
65 ("Move taxon to new parent", NavigationUtil.getUndoContext(),
66 taxonNode, parentTaxonNode, this); //$NON-NLS-1$
67 NavigationUtil.executeOperation(operation);
68
69 }
70
71 return null;
72 }
73
74 /* (non-Javadoc)
75 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
76 */
77 public boolean postOperation(CdmBase objectAffectedByOperation) {
78 NavigationUtil.selectInNavigator(objectAffectedByOperation, parentTaxonNode);
79 return true;
80 }
81 }