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
.internal
.TaxeditorEditorPlugin
;
34 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
35 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
36 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
37 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
38 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
39 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
40 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
41 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
42 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
43 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
44 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
48 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
49 * for "Descriptions", "Concepts", "Geography", etc.
56 public class MultiPageTaxonEditor
extends FormEditor
implements
57 IPartContentHasFactualData
, IConversationEnabled
, IPostOperationEnabled
,
58 IDirtyMarkable
, IPartContentHasDetails
, ISecuredEditor
, IPartContentHasMedia
{
60 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
61 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon";
63 private boolean dirty
;
65 private ConversationHolder conversation
;
66 private IDataChangeBehavior dataChangeBehavior
;
67 private IUndoContext undoContext
;
69 private TaxonEditorInput input
;
73 * Constructor for MultiPageTaxonEditor.
76 public MultiPageTaxonEditor() {
78 undoContext
= new UndoContext();
83 public void dispose() {
84 conversation
.unregisterForDataStoreChanges(this);
92 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
96 protected void addPages() {
97 input
= (TaxonEditorInput
) getEditorInput();
98 conversation
= input
.getConversationHolder();
99 conversation
.registerForDataStoreChanges(this);
102 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
104 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
107 // addPage(Page.DESCRIPTIVE.getIndex(), new
108 // TaxonDescriptionTreeEditor(this), getEditorInput());
109 // setPageText(Page.DESCRIPTIVE.getIndex(),
110 // Page.DESCRIPTIVE.getTitle());
112 // EditorUtil.showPropertySheet();
114 } catch (PartInitException e
) {
115 MessagingUtils
.error(getClass(), e
);
121 public void doSave(IProgressMonitor monitor
) {
122 monitor
.beginTask("Saving Editor", 4);
124 if (!conversation
.isBound()) {
129 for (IEditorPart editorPage
: getPages()) {
130 if (editorPage
instanceof TaxonNameEditor
) {
131 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
132 MessageDialog
.openWarning(AbstractUtility
.getShell(), "No Name Specified",
133 "An attempt was made to save a taxon or synonym with "
134 + "an empty name. Operation was cancelled.");
139 editorPage
.doSave(monitor
);
143 // commit the conversation and start a new transaction immediately
144 conversation
.commit(true);
147 this.setDirty(false);
149 } catch (Exception e
) {
151 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,"saving a taxon", " Please close and reopen the taxon again.");
158 private void disableEditor(boolean isOnError
) {
159 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
161 editorPage
.setOnError();
163 editorPage
.setDisabled();
166 conversation
.unregisterForDataStoreChanges(this);
167 conversation
.close();
171 private void setDirty(boolean dirty
) {
173 firePropertyChange(PROP_DIRTY
);
179 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
189 public boolean isDirty() {
196 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
200 public void editorDirtyStateChanged() {
202 super.editorDirtyStateChanged();
208 * Checks whether nested editors are calling
209 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
210 * place before passing property change along to
211 * <code>super.handlePropertyChange(int propertyId)</code>.
216 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
219 protected void handlePropertyChange(int propertyId
) {
220 if (propertyId
== PROP_DIRTY
) {
223 super.handlePropertyChange(propertyId
);
228 public void doSaveAs() {
233 public boolean isSaveAsAllowed() {
239 public void init(IEditorSite site
, IEditorInput input
)
240 throws PartInitException
{
242 if (!(input
instanceof TaxonEditorInput
)) {
243 throw new PartInitException(
244 "Invalid Input: Must be TaxonEditorInput");
247 this.input
= (TaxonEditorInput
) input
;
250 // // Listen for name changes,
251 // // change tab for this taxon editor accordingly
252 // getTaxon().addPropertyChangeListener("name",
253 // new PropertyChangeListener() {
254 // public void propertyChange(PropertyChangeEvent e) {
258 // } catch (NullPointerException e) {
259 // EditorUtil.warn(getClass(),
260 // "Caught an NPE while initing an editor. This is most " +
261 // "likely due to the unsuccesful attempt to restore the former " +
262 // "state of the application. We ignore this because the workbench " +
263 // "will simply be reset.");
267 super.init(site
, input
);
271 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
272 * text appropriate to the state of the taxon: any taxon that has been saved
273 * will by necessity have a name to display; a new taxon should display
274 * "New taxon" in the editor tab.
276 protected void setPartName() {
278 String partName
= null;
279 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
282 partName
= name
.getTitleCache();
285 if (partName
== null || partName
.equals("")) {
286 partName
= ("New taxon");
289 setPartName(partName
);
295 * Editor pages call this in their postOperation to notify the
296 * MultiPageTaxonEditor of unsaved changes
299 public void changed(Object element
) {
302 super.editorDirtyStateChanged();
303 if (element
instanceof TaxonBase
) {
304 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
305 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
306 if (container
!= null) {
311 //TODO: refresh taxon node in taxon navigator
316 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
319 public void forceDirty() {
324 * The accepted taxon that is the input for this editor
326 * @return the accepted taxon
328 public Taxon
getTaxon() {
329 return input
.getTaxon();
336 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
341 * getConversationHolder
344 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
348 public ConversationHolder
getConversationHolder() {
354 * setConversationHolder
357 * @param conversation
358 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
361 public void setConversationHolder(ConversationHolder conversation
) {
362 this.conversation
= conversation
;
367 * Getter for the field <code>undoContext</code>.
370 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
373 public IUndoContext
getUndoContext() {
379 * Setter for the field <code>undoContext</code>.
383 * a {@link org.eclipse.core.commands.operations.IUndoContext}
386 public void setUndoContext(IUndoContext undoContext
) {
387 this.undoContext
= undoContext
;
392 public void setFocus() {
393 // logger.warn("Setting focus to editor");
394 // bind the conversation
395 getConversationHolder().bind();
396 // pass focus to the active editor page
397 getActiveEditor().setFocus();
404 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
405 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
409 public void update(CdmDataChangeMap events
) {
410 if (dataChangeBehavior
== null) {
411 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
414 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
421 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
426 public boolean postOperation(CdmBase objectAffectedByOperation
) {
429 for (IEditorPart editor
: this.getPages()) {
430 if (editor
instanceof IPostOperationEnabled
) {
431 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
433 MessagingUtils
.warn(getClass(), "postOperation not enabled for editor " + editor
);
436 MessagingUtils
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
442 * Returns an <code>IEditorPart</code> implementation by type
446 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
449 public IMultiPageTaxonEditorPage
getPage(Page page
) {
450 for (IEditorPart editor
: this.getPages()) {
451 if (editor
.getClass().equals(page
.getClazz())) {
452 return (IMultiPageTaxonEditorPage
) editor
;
459 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
460 * <code>MultiPageTaxonEditor</code>.
462 * @return a {@link java.util.List} object.
464 public List
<IMultiPageTaxonEditorPage
> getPages() {
465 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
466 for (int i
= 0; i
< this.getPageCount(); i
++) {
468 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
474 * Refreshes a certain page of the MultipageTaxonEditor
477 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
480 public boolean redraw(Page page
) {
481 return redraw(page
, true);
485 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
489 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
494 public boolean redraw(Page page
, boolean focus
) {
495 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
496 return editorPage
!= null && editorPage
.redraw(focus
);
507 public boolean onComplete() {
512 * Reloads the data for this
514 public void reload() {
516 MessagingUtils
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
517 + "refreshed because it contains unsaved data. Refreshing "
518 + "this editor would discard the changes. Please save this editor, "
519 + "close and reopen it manually in order to get the latest content");
521 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
523 UUID uuid
= input
.getTaxonNode().getUuid();
525 conversation
.clear();
528 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
530 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
533 } catch (Exception e
) {
534 MessagingUtils
.messageDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
540 public String
toString() {
541 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
545 public boolean permissionsSatisfied() {
546 IEditorPart activeEditor
= getActiveEditor();
547 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
548 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();