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
;
14 import java
.util
.UUID
;
16 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
17 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
20 import org
.eclipse
.ui
.IEditorInput
;
21 import org
.eclipse
.ui
.IEditorPart
;
22 import org
.eclipse
.ui
.IEditorSite
;
23 import org
.eclipse
.ui
.PartInitException
;
24 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
27 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
32 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
33 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
34 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
35 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
36 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
37 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
38 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkableSelectionProvider
;
39 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
40 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
41 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
42 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
46 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
47 * for "Descriptions", "Concepts", "Geography", etc.
54 public class MultiPageTaxonEditor
extends FormEditor
implements
55 IPartContentHasFactualData
, IConversationEnabled
, IPostOperationEnabled
,
56 IDirtyMarkableSelectionProvider
, IPartContentHasDetails
, ISecuredEditor
{
58 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
59 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon";
61 private boolean dirty
;
63 private ConversationHolder conversation
;
64 private IDataChangeBehavior dataChangeBehavior
;
65 private IUndoContext undoContext
;
67 private TaxonEditorInput input
;
71 * Constructor for MultiPageTaxonEditor.
74 public MultiPageTaxonEditor() {
76 undoContext
= new UndoContext();
81 public void dispose() {
82 conversation
.unregisterForDataStoreChanges(this);
90 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
94 protected void addPages() {
95 input
= (TaxonEditorInput
) getEditorInput();
96 conversation
= input
.getConversationHolder();
97 conversation
.registerForDataStoreChanges(this);
100 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
102 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
105 // addPage(Page.DESCRIPTIVE.getIndex(), new
106 // TaxonDescriptionTreeEditor(this), getEditorInput());
107 // setPageText(Page.DESCRIPTIVE.getIndex(),
108 // Page.DESCRIPTIVE.getTitle());
110 // EditorUtil.showPropertySheet();
112 } catch (PartInitException e
) {
113 MessagingUtils
.error(getClass(), e
);
119 public void doSave(IProgressMonitor monitor
) {
120 monitor
.beginTask("Saving Editor", 4);
122 if (!conversation
.isBound()) {
127 for (IEditorPart editorPage
: getPages()) {
128 if (editorPage
instanceof TaxonNameEditor
) {
129 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
130 MessageDialog
.openWarning(AbstractUtility
.getShell(), "No Name Specified",
131 "An attempt was made to save a taxon or synonym with "
132 + "an empty name. Operation was cancelled.");
137 editorPage
.doSave(monitor
);
141 // commit the conversation and start a new transaction immediately
142 conversation
.commit(true);
145 this.setDirty(false);
147 } catch (Exception e
) {
149 MessagingUtils
.operationDialog(this, e
, "saving a taxon", " Please close and reopen the taxon again.");
156 private void disableEditor(boolean isOnError
) {
157 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
159 editorPage
.setOnError();
161 editorPage
.setDisabled();
164 conversation
.unregisterForDataStoreChanges(this);
165 conversation
.close();
169 private void setDirty(boolean dirty
) {
171 firePropertyChange(PROP_DIRTY
);
177 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
187 public boolean isDirty() {
194 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
198 public void editorDirtyStateChanged() {
200 super.editorDirtyStateChanged();
206 * Checks whether nested editors are calling
207 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
208 * place before passing property change along to
209 * <code>super.handlePropertyChange(int propertyId)</code>.
214 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
217 protected void handlePropertyChange(int propertyId
) {
218 if (propertyId
== PROP_DIRTY
) {
221 super.handlePropertyChange(propertyId
);
226 public void doSaveAs() {
231 public boolean isSaveAsAllowed() {
237 public void init(IEditorSite site
, IEditorInput input
)
238 throws PartInitException
{
240 if (!(input
instanceof TaxonEditorInput
)) {
241 throw new PartInitException(
242 "Invalid Input: Must be TaxonEditorInput");
245 this.input
= (TaxonEditorInput
) input
;
248 // // Listen for name changes,
249 // // change tab for this taxon editor accordingly
250 // getTaxon().addPropertyChangeListener("name",
251 // new PropertyChangeListener() {
252 // public void propertyChange(PropertyChangeEvent e) {
256 // } catch (NullPointerException e) {
257 // EditorUtil.warn(getClass(),
258 // "Caught an NPE while initing an editor. This is most " +
259 // "likely due to the unsuccesful attempt to restore the former " +
260 // "state of the application. We ignore this because the workbench " +
261 // "will simply be reset.");
265 super.init(site
, input
);
269 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
270 * text appropriate to the state of the taxon: any taxon that has been saved
271 * will by necessity have a name to display; a new taxon should display
272 * "New taxon" in the editor tab.
274 protected void setPartName() {
276 String partName
= null;
277 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
280 partName
= name
.getTitleCache();
283 if (partName
== null || partName
.equals("")) {
284 partName
= ("New taxon");
287 setPartName(partName
);
293 * Editor pages call this in their postOperation to notify the
294 * MultiPageTaxonEditor of unsaved changes
297 public void changed(Object element
) {
300 super.editorDirtyStateChanged();
301 if (element
instanceof TaxonBase
) {
302 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
303 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
304 if (container
!= null) {
311 * The accepted taxon that is the input for this editor
313 * @return the accepted taxon
315 public Taxon
getTaxon() {
316 return input
.getTaxon();
323 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
328 * getConversationHolder
331 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
335 public ConversationHolder
getConversationHolder() {
341 * setConversationHolder
344 * @param conversation
345 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
348 public void setConversationHolder(ConversationHolder conversation
) {
349 this.conversation
= conversation
;
354 * Getter for the field <code>undoContext</code>.
357 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
360 public IUndoContext
getUndoContext() {
366 * Setter for the field <code>undoContext</code>.
370 * a {@link org.eclipse.core.commands.operations.IUndoContext}
373 public void setUndoContext(IUndoContext undoContext
) {
374 this.undoContext
= undoContext
;
379 public void setFocus() {
380 // logger.warn("Setting focus to editor");
381 // bind the conversation
382 getConversationHolder().bind();
383 // pass focus to the active editor page
384 getActiveEditor().setFocus();
391 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
392 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
396 public void update(CdmDataChangeMap events
) {
397 if (dataChangeBehavior
== null) {
398 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
401 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
408 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
413 public boolean postOperation(CdmBase objectAffectedByOperation
) {
416 for (IEditorPart editor
: this.getPages()) {
417 if (editor
instanceof IPostOperationEnabled
) {
418 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
420 MessagingUtils
.warn(getClass(), "postOperation not enabled for editor " + editor
);
423 MessagingUtils
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
429 * Returns an <code>IEditorPart</code> implementation by type
433 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
436 public IMultiPageTaxonEditorPage
getPage(Page page
) {
437 for (IEditorPart editor
: this.getPages()) {
438 if (editor
.getClass().equals(page
.getClazz())) {
439 return (IMultiPageTaxonEditorPage
) editor
;
446 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
447 * <code>MultiPageTaxonEditor</code>.
449 * @return a {@link java.util.List} object.
451 public List
<IMultiPageTaxonEditorPage
> getPages() {
452 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
453 for (int i
= 0; i
< this.getPageCount(); i
++) {
455 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
461 * Refreshes a certain page of the MultipageTaxonEditor
464 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
467 public boolean redraw(Page page
) {
468 return redraw(page
, true);
472 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
476 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
481 public boolean redraw(Page page
, boolean focus
) {
482 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
483 return editorPage
!= null && editorPage
.redraw(focus
);
494 public boolean onComplete() {
499 * Reloads the data for this
501 public void reload() {
503 MessagingUtils
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
504 + "refreshed because it contains unsaved data. Refreshing "
505 + "this editor would discard the changes. Please save this editor, "
506 + "close and reopen it manually in order to get the latest content");
508 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
510 UUID uuid
= input
.getTaxonNode().getUuid();
512 conversation
.clear();
515 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
517 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
520 } catch (Exception e
) {
521 MessagingUtils
.messageDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
527 public String
toString() {
528 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
532 public boolean permissionsSatisfied() {
533 IEditorPart activeEditor
= getActiveEditor();
534 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
535 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();