2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
16 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.ui
.IEditorInput
;
19 import org
.eclipse
.ui
.IEditorPart
;
20 import org
.eclipse
.ui
.IEditorSite
;
21 import org
.eclipse
.ui
.PartInitException
;
22 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
24 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
30 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
31 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
32 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
33 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkableSelectionProvider
;
34 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
35 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
39 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs for
40 * "Descriptions", "Concepts", "Geography", etc.
47 public class MultiPageTaxonEditor
extends FormEditor
implements IConversationEnabled
, IPostOperationEnabled
, IDirtyMarkableSelectionProvider
, IPartContentHasDetails
{
49 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.multipage"{trunked}</code> */
50 public static final String ID
= "eu.etaxonomy.taxeditor.editor.multipagetaxonview";
52 private boolean dirty
;
54 private ConversationHolder conversation
;
55 private IDataChangeBehavior dataChangeBehavior
;
56 private IUndoContext undoContext
;
58 private TaxonEditorInput input
;
61 * <p>Constructor for MultiPageTaxonEditor.</p>
63 public MultiPageTaxonEditor() {
65 undoContext
= new UndoContext();
70 public void dispose() {
71 conversation
.unregisterForDataStoreChanges(this);
77 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
81 protected void addPages() {
82 input
= (TaxonEditorInput
) getEditorInput();
83 conversation
= input
.getConversationHolder();
84 conversation
.registerForDataStoreChanges(this);
87 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this), getEditorInput());
88 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
91 // addPage(Page.DESCRIPTIVE.getIndex(), new TaxonDescriptionTreeEditor(this), getEditorInput());
92 // setPageText(Page.DESCRIPTIVE.getIndex(), Page.DESCRIPTIVE.getTitle());
94 // EditorUtil.showPropertySheet();
96 } catch (PartInitException e
) {
97 EditorUtil
.error(getClass(), e
);
104 public void doSave(IProgressMonitor monitor
) {
105 monitor
.beginTask("Saving Editor", 4);
107 if( ! conversation
.isBound()){
112 for(IEditorPart editorPage
: getPages()){
113 if(editorPage
instanceof TaxonNameEditor
){
114 if(((TaxonNameEditor
) editorPage
).checkForEmptyNames()){
120 editorPage
.doSave(monitor
);
124 // commit the conversation and start a new transaction immediately
125 conversation
.commit(true);
128 this.setDirty(false);
136 private void setDirty(boolean dirty
) {
138 firePropertyChange(PROP_DIRTY
);
142 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
149 public boolean isDirty() {
154 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
158 public void editorDirtyStateChanged() {
160 super.editorDirtyStateChanged();
167 * Checks whether nested editors are calling <code>firePropertyChange(PROP_DIRTY)</code>
168 * to signal an edit has taken place before passing property change along to
169 * <code>super.handlePropertyChange(int propertyId)</code>.
172 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
174 protected void handlePropertyChange(int propertyId
) {
175 if (propertyId
== PROP_DIRTY
) {
178 super.handlePropertyChange(propertyId
);
183 public void doSaveAs() {}
187 public boolean isSaveAsAllowed() {
193 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
195 if (!(input
instanceof TaxonEditorInput
))
196 throw new PartInitException(
197 "Invalid Input: Must be TaxonEditorInput");
199 this.input
= (TaxonEditorInput
) input
;
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) {
210 // } catch (NullPointerException e) {
211 // EditorUtil.warn(getClass(), "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.");
218 super.init(site
, input
);
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.
227 protected void setPartName() {
229 String partName
= null;
230 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
233 partName
= name
.getTitleCache();
236 if (partName
== null || partName
.equals("")) {
237 partName
= ("New taxon");
240 setPartName(partName
);
246 * Editor pages call this in their postOperation to notify the MultiPageTaxonEditor
249 public void changed(Object element
) {
252 super.editorDirtyStateChanged();
253 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
254 page
.getSelectedObject().refresh();
258 * The accepted taxon that is the input for this editor
260 * @return the accepted taxon
262 public Taxon
getTaxon(){
263 return input
.getTaxon();
268 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
271 * <p>getConversationHolder</p>
273 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
275 public ConversationHolder
getConversationHolder() {
280 * <p>setConversationHolder</p>
282 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
284 public void setConversationHolder(ConversationHolder conversation
){
285 this.conversation
= conversation
;
290 * <p>Getter for the field <code>undoContext</code>.</p>
292 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
294 public IUndoContext
getUndoContext() {
299 * <p>Setter for the field <code>undoContext</code>.</p>
301 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
303 public void setUndoContext(IUndoContext undoContext
) {
304 this.undoContext
= undoContext
;
309 public void setFocus(){
310 //logger.warn("Setting focus to editor");
311 // bind the conversation
312 getConversationHolder().bind();
313 // pass focus to the active editor page
314 getActiveEditor().setFocus();
319 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
322 public void update(CdmDataChangeMap events
) {
323 if(dataChangeBehavior
== null){
324 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
327 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
333 * @see eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation()
336 public boolean postOperation(CdmBase objectAffectedByOperation
) {
339 for(IEditorPart editor
: this.getPages()){
340 if (editor
instanceof IPostOperationEnabled
) {
341 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
343 EditorUtil
.warn(getClass(), "postOperation not enabled for editor " + editor
);
346 EditorUtil
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
352 * Returns an <code>IEditorPart</code> implementation by type
354 * @param page the page type
355 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage} object.
357 public IMultiPageTaxonEditorPage
getPage(Page page
){
358 for(IEditorPart editor
: this.getPages()){
359 if(editor
.getClass().equals(page
.getClazz())){
360 return (IMultiPageTaxonEditorPage
) editor
;
367 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
368 * <code>MultiPageTaxonEditor</code>.
370 * @return a {@link java.util.List} object.
372 public List
<IEditorPart
> getPages(){
373 ArrayList
<IEditorPart
> editors
= new ArrayList
<IEditorPart
>();
374 for(int i
= 0; i
< this.getPageCount(); i
++){
375 editors
.add(this.getEditor(i
));
381 * Refreshes a certain page of the MultipageTaxonEditor
383 * @param page a {@link eu.etaxonomy.taxeditor.editor.Page} object.
386 public boolean redraw(Page page
){
387 return redraw(page
, true);
391 * Refreshes a certain page of the MultipageTaxonEditor and
392 * sets focus to that page
394 * @param page a {@link eu.etaxonomy.taxeditor.editor.Page} object.
395 * @param focus a boolean.
398 public boolean redraw(Page page
, boolean focus
){
399 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
400 return editorPage
!= null && editorPage
.redraw(focus
);
408 public boolean onComplete() {
409 // TODO Auto-generated method stub