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