c5f570b25307a3e1715f59423a4152a11a8c2359
[taxeditor.git] / taxeditor-navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / handler / QuickCreateChildTaxonHandler.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 org.apache.log4j.Logger;
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.IHandler;
17 import org.eclipse.core.commands.common.NotDefinedException;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.jface.dialogs.InputDialog;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.window.Window;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
27 import eu.etaxonomy.cdm.model.taxon.Taxon;
28 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29 import eu.etaxonomy.taxeditor.editor.EditorUtil;
30 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
31 import eu.etaxonomy.taxeditor.operations.CreateChildTaxonOperation;
32 import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
33 import eu.etaxonomy.taxeditor.parser.CdmParserUtil;
34 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
35
36 /**
37 * @author n.hoffmann
38 * @created 24.03.2009
39 * @version 1.0
40 */
41 public class QuickCreateChildTaxonHandler extends AbstractHandler implements
42 IHandler, IPostOperationEnabled {
43 private static final Logger logger = Logger
44 .getLogger(QuickCreateChildTaxonHandler.class);
45
46 private TaxonNode parentTaxonNode;
47
48 /* (non-Javadoc)
49 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
50 */
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52
53 Shell shell = HandlerUtil.getActiveShell(event);
54 parentTaxonNode = NavigationUtil.getCurrentSelection(event);
55
56 if(NavigationUtil.isDirty(parentTaxonNode)){
57 MessageDialog.openWarning(shell, "Unsaved Parent Taxon", "There are unsaved " +
58 "changes in the parent taxon. Pleas save first.");
59 return null;
60 }
61
62 InputDialog dialog = new InputDialog(shell, "Quick create child taxon",
63 "Enter child taxon's name to create a new taxon", null, null);
64 if (dialog.open() == Window.OK) {
65
66 //TaxonUtil.addChildTaxonBaseIsolated(parentTaxon.getUuid(), dialog.getValue());
67
68 TaxonNameBase name = CdmParserUtil.parseFullReference(dialog.getValue(), null, null);
69
70 CreateChildTaxonOperation operation;
71 try {
72 operation = new CreateChildTaxonOperation(event.getCommand().getName(),
73 NavigationUtil.getUndoContext(), parentTaxonNode, name, this);
74 IStatus status = EditorUtil.executeOperation(operation);
75 if(status.getCode() == TaxeditorStorePlugin.ERROR_SAVING_ZERO_NAME){
76 MessageDialog.openWarning(shell, "No Name Specified", "An attempt was made to save a taxon with" +
77 " an empty name. Operation was cancelled");
78 }
79 } catch (NotDefinedException e) {
80 logger.warn("Command name not set");
81 }
82
83 logger.info("Taxon created");
84
85 }
86
87 return null;
88 }
89
90 public boolean postOperation(CdmBase objectAffectedByOperation) {
91 NavigationUtil.selectInNavigator(objectAffectedByOperation, parentTaxonNode);
92 return true;
93 }
94 }