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