09543016bba83d022a95ddade33d6d18695ffd67
[taxeditor.git] / taxeditor-editor / src / main / java / 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 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.commands.operations.IUndoContext;
19 import org.eclipse.core.commands.operations.UndoContext;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.IEditorSite;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.forms.editor.FormEditor;
26
27 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
28 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31 import eu.etaxonomy.cdm.model.taxon.Taxon;
32 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
33 import eu.etaxonomy.taxeditor.editor.descriptiontree.TaxonDescriptionTreeEditor;
34 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
35 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
36 import eu.etaxonomy.taxeditor.model.IDataChangeBehavior;
37 import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
38
39 /**
40 *
41 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs for
42 * "Descriptions", "Concepts", "Geography", etc.
43 *
44 * @author p.ciardelli
45 * @author n.hoffmann
46 * @created 15.05.2008
47 * @version 1.0
48 */
49 //public class MultiPageTaxonEditor extends MultiPageEditorPart implements IConversationEnabled, IPostOperationEnabled {
50 public class MultiPageTaxonEditor extends FormEditor implements IConversationEnabled, IPostOperationEnabled {
51 private static final Logger logger = Logger.getLogger(MultiPageTaxonEditor.class);
52
53 public static final String ID = "eu.etaxonomy.taxeditor.editor.multipagetaxonview";
54
55 private boolean dirty;
56
57 private ConversationHolder conversation;
58 private IDataChangeBehavior dataChangeBehavior;
59 private IUndoContext undoContext;
60
61 private TaxonEditorInput input;
62
63
64 public MultiPageTaxonEditor() {
65 super();
66 undoContext = new UndoContext();
67 }
68
69
70
71 @Override
72 public void dispose() {
73 super.dispose();
74 // EditorUtil.checkHidePropertySheet();
75 }
76
77
78
79
80 /* (non-Javadoc)
81 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
82 */
83 @Override
84 protected void addPages() {
85 input = (TaxonEditorInput) getEditorInput();
86 conversation = input.getConversationHolder();
87 conversation.registerForDataStoreChanges(this);
88
89 try {
90 addPage(Page.NAME.getIndex(), new TaxonNameEditor(this), getEditorInput());
91 setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
92
93 // // TODO lazy create
94 // addPage(Page.DESCRIPTIVE.getIndex(), new TaxonDescriptionEditor(this), getEditorInput());
95 // setPageText(Page.DESCRIPTIVE.getIndex(), Page.DESCRIPTIVE.getTitle());
96
97 // // TODO lazy create
98 // addPage(Page.IMAGE.getIndex(), new TaxonImageEditor(this), getEditorInput());
99 // setPageText(Page.IMAGE.getIndex(), Page.IMAGE.getTitle());
100
101 // TODO lazy create
102 addPage(Page.DESCRIPTIVE.getIndex(), new TaxonDescriptionTreeEditor(this), getEditorInput());
103 // addPage(Page.DESCRIPTIVE_NEW.getIndex(), new TaxonDescriptionTreeEditor(this));
104 setPageText(Page.DESCRIPTIVE.getIndex(), Page.DESCRIPTIVE.getTitle());
105
106 EditorUtil.showPropertySheet();
107
108 } catch (PartInitException e) {
109 logger.error("Could not create MultiPageTaxonEditor.", e);
110 }
111 }
112
113 // @Override
114 // protected void createPages() {
115 //
116 //
117 // }
118
119 @Override
120 public void doSave(IProgressMonitor monitor) {
121
122 if( ! conversation.isBound()){
123 conversation.bind();
124 }
125
126 //handle existing names and authors
127 DuplicateArbitrator duplicateArbitrator = (DuplicateArbitrator) getAdapter(DuplicateArbitrator.class);
128
129 if(duplicateArbitrator != null){
130 // disable for now because of transaction problems.
131 duplicateArbitrator.arbitrate();
132 }
133
134 // commit the conversation and start a new transaction immediately
135 conversation.commit(true);
136
137 this.setDirty(false);
138 }
139
140 private void setDirty(boolean dirty) {
141 this.dirty = dirty;
142 firePropertyChange(PROP_DIRTY);
143 }
144
145 /* (non-Javadoc)
146 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
147 */
148 public boolean isDirty() {
149 return dirty;
150 }
151
152 /* (non-Javadoc)
153 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
154 */
155 @Override
156 public void editorDirtyStateChanged() {
157 dirty = true;
158 super.editorDirtyStateChanged();
159 }
160
161 /**
162 * Checks whether nested editors are calling <code>firePropertyChange(PROP_DIRTY)</code>
163 * to signal an edit has taken place before passing property change along to
164 * <code>super.handlePropertyChange(int propertyId)</code>.
165 */
166 /* (non-Javadoc)
167 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
168 */
169 protected void handlePropertyChange(int propertyId) {
170 if (propertyId == PROP_DIRTY) {
171 setDirty(true);
172 }
173 super.handlePropertyChange(propertyId);
174 }
175
176 @Override
177 public void doSaveAs() {}
178
179 @Override
180 public boolean isSaveAsAllowed() {
181 return false;
182 }
183
184 @Override
185 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
186
187 if (!(input instanceof TaxonEditorInput))
188 throw new PartInitException(
189 "Invalid Input: Must be TaxonEditorInput");
190
191 // FIXME looks like we do it differently now
192 // Get taxon from editor input
193 // if (input.getAdapter(Taxon.class) != null) {
194 // taxon = (Taxon) input.getAdapter(Taxon.class);
195 // } else {
196 // taxon = null;
197 // }
198
199 this.input = (TaxonEditorInput) input;
200
201 try {
202 // Listen for name changes,
203 // change tab for this taxon editor accordingly
204 getTaxon().addPropertyChangeListener("name",
205 new PropertyChangeListener() {
206 public void propertyChange(PropertyChangeEvent e) {
207 setPartName();
208 }
209 });
210 } catch (NullPointerException e) {
211 logger.warn("Caught an NPE while initing an editor. This is most " +
212 "likely due to the unsuccesful attempt to restore the former " +
213 "state of the application. We ignore this because the workbench " +
214 "will simply be reset.");
215 }
216 setPartName();
217
218 super.init(site, input);
219 }
220
221 /**
222 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code>
223 * with text appropriate to the state of the taxon: any taxon that has
224 * been saved will by necessity have a name to display; a new taxon
225 * should display "New taxon" in the editor tab.
226 */
227 protected void setPartName() {
228
229 String partName = null;
230 TaxonNameBase<?, ?> name = getTaxon().getName();
231
232 if (name != null) {
233 partName = name.getTitleCache();
234 }
235
236 if (partName == null || partName.equals("")) {
237 partName = ("New taxon");
238 }
239
240 setPartName(partName);
241 }
242
243 /**
244 * Editor pages call this in their postOperation to notify the MultiPageTaxonEditor
245 * of unsaved changes
246 */
247 public void setDirty() {
248 setDirty(true);
249 }
250
251 public Taxon getTaxon(){
252 return input.getTaxon();
253 }
254
255 /*
256 * (non-Javadoc)
257 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
258 */
259 public ConversationHolder getConversationHolder() {
260 return conversation;
261 }
262
263 public void setConversationHolder(ConversationHolder conversation){
264 this.conversation = conversation;
265 }
266
267
268 public IUndoContext getUndoContext() {
269 return undoContext;
270 }
271
272 public void setUndoContext(IUndoContext undoContext) {
273 this.undoContext = undoContext;
274 }
275
276 @Override
277 public void setFocus(){
278 // bind the conversation
279 getConversationHolder().bind();
280 // pass focus to the active editor page
281 getActiveEditor().setFocus();
282 }
283
284 /*
285 * (non-Javadoc)
286 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
287 */
288 public void update(CdmDataChangeMap events) {
289 if(dataChangeBehavior == null){
290 dataChangeBehavior = new MultiPageTaxonEditorDataChangeBehaviour(this);
291 }
292
293 DataChangeBridge.handleDataChange(events, dataChangeBehavior);
294 }
295
296
297 /*
298 * (non-Javadoc)
299 * @see eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation()
300 */
301 public boolean postOperation(CdmBase objectAffectedByOperation) {
302 setDirty(true);
303
304 for(IEditorPart editor : this.getPages()){
305 if (editor instanceof IPostOperationEnabled) {
306 ((IPostOperationEnabled) editor).postOperation(objectAffectedByOperation);
307 } else {
308 logger.warn("postOperation not enabled for editor " + editor);
309 }
310 }
311 logger.warn("postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
312
313 return false;
314 }
315
316 /**
317 * Returns an <code>AbstractTaxonEditor</code> implementation by type
318 *
319 * @param page the page type
320 * @return
321 */
322 public IEditorPart getPage(Page page){
323 for(IEditorPart editor : this.getPages()){
324 if(editor.getClass().equals(page.getClazz())){
325 return editor;
326 }
327 }
328 return null;
329 }
330
331 /**
332 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
333 * <code>MultiPageTaxonEditor</code>.
334 *
335 * @return
336 */
337 public List<IEditorPart> getPages(){
338 ArrayList<IEditorPart> editors = new ArrayList<IEditorPart>();
339 for(int i = 0; i < this.getPageCount(); i++){
340 editors.add(this.getEditor(i));
341 }
342 return editors;
343 }
344
345 }