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