1) Checkin for Anahit 2) Delete NameRelations works now (maybe not behind the scenes...
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / MultiPageTaxonEditor.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.editor;
11
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.ui.IEditorInput;
18 import org.eclipse.ui.IEditorSite;
19 import org.eclipse.ui.PartInitException;
20 import org.eclipse.ui.part.MultiPageEditorPart;
21 import org.springframework.transaction.TransactionStatus;
22
23 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.taxeditor.actions.cdm.SaveTaxonAction;
26 import eu.etaxonomy.taxeditor.editor.description.TaxonDescriptionEditorView;
27 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditorView;
28 import eu.etaxonomy.taxeditor.model.CdmUtil;
29
30 /**
31 *
32 * Generates the tabbed editor with <code>TaxonNameEditorView</code> on top and tabs for
33 * "Desriptions", "Concepts", "Geography", etc.
34 *
35 * @author p.ciardelli
36 * @created 15.05.2008
37 * @version 1.0
38 */
39 public class MultiPageTaxonEditor extends MultiPageEditorPart {
40 private static final Logger logger = Logger.getLogger(MultiPageTaxonEditor.class);
41
42 public static final String ID = "eu.etaxonomy.taxeditor.editor.multipagetaxonview";
43
44 private Taxon taxon;
45 private boolean dirty;
46
47 private TransactionStatus tx;
48
49 public MultiPageTaxonEditor() {
50 super();
51 // tx = CdmUtil.startTransaction();
52 }
53
54 @Override
55 public void dispose() {
56 super.dispose();
57 // CdmUtil.commitTransaction(tx);
58 }
59
60 @Override
61 protected void createPages() {
62
63 try {
64 addPage(0, new TaxonNameEditorView(), getEditorInput());
65 setPageText(0, "Name");
66
67 // TODO lazy create
68 addPage(1, new TaxonDescriptionEditorView(), getEditorInput());
69 setPageText(1, "Descriptive");
70
71 addPage(2, new EmptyEditorView(), getEditorInput());
72 setPageText(2, "Concepts");
73
74 addPage(3, new EmptyEditorView(), getEditorInput());
75 setPageText(3, "Geographic");
76
77 // Hook undo to workbench
78 // IUndoContext undoContext = UiUtil.getWorkbenchUndoContext();
79 // getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), new UndoActionHandler(getSite(), undoContext));
80 // getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.REDO.getId(), new RedoActionHandler(getSite(), undoContext));
81
82 } catch (PartInitException e) {
83 e.printStackTrace();
84 }
85 }
86
87 @Override
88 public void doSave(IProgressMonitor monitor) {
89 // monitor.beginTask(name, totalWork)
90 new SaveTaxonAction(taxon).run();
91 // CdmUtil.commitTransaction(tx);
92 setDirty(false);
93 // tx = CdmUtil.startTransaction();
94 // CdmUtil.getTaxonService().saveTaxon(taxon);
95 }
96
97 private void setDirty(boolean dirty) {
98 this.dirty = dirty;
99 firePropertyChange(PROP_DIRTY);
100 }
101
102 /* (non-Javadoc)
103 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
104 */
105 public boolean isDirty() {
106 return dirty;
107 }
108
109 /**
110 * Checks whether nested editors are calling <code>firePropertyChange(PROP_DIRTY)</code>
111 * to signal an edit has taken place before passing property change along to
112 * <code>super.handlePropertyChange(int propertyId)</code>.
113 */
114 /* (non-Javadoc)
115 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
116 */
117 protected void handlePropertyChange(int propertyId) {
118 if (propertyId == PROP_DIRTY) {
119 setDirty(true);
120 }
121 super.handlePropertyChange(propertyId);
122 }
123
124 @Override
125 public void doSaveAs() {}
126
127 @Override
128 public boolean isSaveAsAllowed() {
129 return false;
130 }
131
132 @Override
133 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
134
135 if (!(input instanceof IEditorInput))
136 throw new PartInitException(
137 "Invalid Input: Must be IEditorInput");
138
139 // Get taxon from editor input
140 if (input.getAdapter(Taxon.class) != null) {
141 taxon = (Taxon) input.getAdapter(Taxon.class);
142 } else {
143 taxon = null;
144 }
145
146 // Save taxon to put it into current transaction
147 CdmUtil.getTaxonService().saveTaxon(taxon);
148
149 // Listen for name changes,
150 // change tab for this taxon editor accordingly
151 taxon.addPropertyChangeListener("name", new PropertyChangeListener() {
152 public void propertyChange(PropertyChangeEvent e) {
153 setPartName();
154 }
155 });
156
157 setPartName();
158
159 super.init(site, input);
160 }
161
162 /**
163 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code>
164 * with text appropriate to the state of the taxon: any taxon that has
165 * been saved will by necessity have a name to display; a new taxon
166 * should display "New taxon" in the editor tab.
167 */
168 private void setPartName() {
169
170 String partName = null;
171 TaxonNameBase name = taxon.getName();
172
173 if (name != null) {
174 partName = name.getTitleCache();
175 }
176
177 if (partName == null || partName.equals("")) {
178 partName = ("New taxon");
179 }
180
181 setPartName(partName);
182 }
183 }