Massive refactoring of the methodology in former class UiUtils
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / ui / AddQuickNameAction.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.actions.ui;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.resource.ImageDescriptor;
15
16 import eu.etaxonomy.cdm.model.name.NonViralName;
17 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
18 import eu.etaxonomy.cdm.model.taxon.Taxon;
19 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
20 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
21 import eu.etaxonomy.taxeditor.controller.TreeController;
22 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
23 import eu.etaxonomy.taxeditor.navigation.TaxonomicTreeViewer;
24 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
25
26 /**
27 * Open up a celleditor in the taxonomic tree
28 * to add a quickname to taxon
29 *
30 * @author p.ciardelli
31 * @created 29.05.2008
32 * @version 1.0
33 */
34 public class AddQuickNameAction extends Action {
35 private static final Logger logger = Logger
36 .getLogger(AddQuickNameAction.class);
37
38 private static String text = "Add child taxon with quick name";
39 private ImageDescriptor image = TaxEditorPlugin.getDefault()
40 .getImageDescriptor(ITaxEditorConstants.QUICK_ADD_ICON);
41
42 private Taxon parentTaxon;
43 private Taxon quickNameTaxon;
44
45 private AddQuickNameAction() {
46 super(text);
47 setImageDescriptor(image);
48 }
49
50 public AddQuickNameAction(Taxon parentTaxon) {
51 this();
52
53 this.parentTaxon = parentTaxon;
54 }
55
56 public void run() {
57
58 // Create empty Taxon, add it to its parent
59 NonViralName name = PreferencesUtil.getInstanceOfPreferredNameClass();
60 ReferenceBase sec = parentTaxon.getSec();
61 quickNameTaxon = Taxon.NewInstance(name, sec);
62 parentTaxon.addTaxonomicChild(quickNameTaxon, null, null);
63
64 CdmSessionDataRepository.getDefault().addTaxon(quickNameTaxon);
65
66 // ULTRA HACKY, ULTRA MYSTERIOUS - if this is not called, name created with quick name
67 // then deleted in the same session causes error
68 // "org.springframework.dao.InvalidDataAccessApiUsageException: deleted object would be re-saved by cascade"
69 // (new PropertySourceAdapter(quickNameTaxon.getName())).getPropertySource();
70
71 firePropertyChange(ITaxEditorConstants.QUICK_NAME_TAXON, null, quickNameTaxon);
72
73 TaxonomicTreeViewer treeViewer = TreeController.getTreeViewer();
74 if (treeViewer == null) {
75 return;
76 }
77
78 // Open new node
79 treeViewer.revealTaxon(quickNameTaxon);
80
81 // Put cursor in empty node
82 treeViewer.editElement(quickNameTaxon, 0);
83 }
84
85 public Taxon getQuickNameTaxon() {
86 return quickNameTaxon;
87 }
88 }