936765e5a22c440299121b824c6a7194dec01d95
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / description / handler / CreateDescriptionElementHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.description.handler;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
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.core.runtime.Status;
21 import org.eclipse.swt.widgets.Event;
22
23 import eu.etaxonomy.cdm.model.common.Language;
24 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
25 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26 import eu.etaxonomy.cdm.model.description.Feature;
27 import eu.etaxonomy.cdm.model.description.TaxonDescription;
28 import eu.etaxonomy.taxeditor.editor.EditorUtil;
29 import eu.etaxonomy.taxeditor.editor.Page;
30 import eu.etaxonomy.taxeditor.editor.description.DescriptionLabelComposite;
31 import eu.etaxonomy.taxeditor.editor.description.DistributionDialog;
32 import eu.etaxonomy.taxeditor.editor.description.LanguageDialog;
33 import eu.etaxonomy.taxeditor.editor.description.TaxonDescriptionEditor;
34 import eu.etaxonomy.taxeditor.store.operations.CreateDescriptionElementOperation;
35
36 /**
37 * @author n.hoffmann
38 * @created 16.04.2009
39 * @version 1.0
40 */
41 public class CreateDescriptionElementHandler extends AbstractHandler {
42 private static final Logger logger = Logger
43 .getLogger(CreateDescriptionElementHandler.class);
44
45 private DescriptionElementBase element;
46
47 /* (non-Javadoc)
48 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 */
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51 TaxonDescriptionEditor editor = (TaxonDescriptionEditor) EditorUtil.getActiveEditorPage(
52 Page.DESCRIPTIVE);
53
54 Feature feature = (Feature) ((Event)event.getTrigger()).data;
55
56 // FIXME right now we have only one description per taxon
57 // this will change in the future and then attaching the feature to
58 // the firstGroupedComposite will not work anymore
59 TaxonDescription description = ((DescriptionLabelComposite)editor.getFirstGroupedComposite()).getDescription();
60
61 if (getElementUserInput(feature).equals(Status.OK_STATUS)) {
62 IUndoableOperation operation;
63 try {
64 operation = new CreateDescriptionElementOperation(event.getCommand().getName(),
65 editor.getUndoContext(), editor.getTaxon(), description, feature, element, editor);
66 EditorUtil.executeOperation(operation);
67 } catch (NotDefinedException e) {
68 logger.warn("Command name not set");
69 }
70 }
71 return null;
72 }
73
74 /**
75 * Some element types require input via the UI - initialize these here
76 *
77 * @param feature
78 * @return
79 */
80 private IStatus getElementUserInput(Feature feature) {
81
82 // Remove object from last execution from element
83 element = null;
84
85 // -------------- Common name -------------- //
86 if (feature.supportsCommonTaxonName()) {
87 Language language = LanguageDialog.getLanguage
88 ("Select language", "Select the common name's language:");
89 if (language == null) {
90 return Status.CANCEL_STATUS;
91 }
92 element = CommonTaxonName.NewInstance("", language);
93 // Feature must be explicitly set to sync w term in persistence layer
94 element.setFeature(feature);
95 }
96
97 // -------------- Distribution -------------- //
98 if (feature.supportsDistribution()) {
99 element = DistributionDialog.getDistribution
100 ("Create distribution", null);
101 if (element == null) {
102 return Status.CANCEL_STATUS;
103 }
104 // Feature must be explicitly set to sync w term in persistence layer
105 element.setFeature(feature);
106 }
107
108 return Status.OK_STATUS;
109 }
110 }