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
.operation
.IPostOperationEnabled
;
44 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
45 * for "Descriptions", "Concepts", "Geography", etc.
52 public class MultiPageTaxonEditor
extends FormEditor
implements
53 IConversationEnabled
, IPostOperationEnabled
,
54 IDirtyMarkableSelectionProvider
, IPartContentHasDetails
, ISecuredEditor
{
56 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
57 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon";
59 private boolean dirty
;
61 private ConversationHolder conversation
;
62 private IDataChangeBehavior dataChangeBehavior
;
63 private IUndoContext undoContext
;
65 private TaxonEditorInput input
;
69 * Constructor for MultiPageTaxonEditor.
72 public MultiPageTaxonEditor() {
74 undoContext
= new UndoContext();
79 public void dispose() {
80 conversation
.unregisterForDataStoreChanges(this);
88 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
92 protected void addPages() {
93 input
= (TaxonEditorInput
) getEditorInput();
94 conversation
= input
.getConversationHolder();
95 conversation
.registerForDataStoreChanges(this);
98 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
100 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
103 // addPage(Page.DESCRIPTIVE.getIndex(), new
104 // TaxonDescriptionTreeEditor(this), getEditorInput());
105 // setPageText(Page.DESCRIPTIVE.getIndex(),
106 // Page.DESCRIPTIVE.getTitle());
108 // EditorUtil.showPropertySheet();
110 } catch (PartInitException e
) {
111 AbstractUtility
.error(getClass(), e
);
117 public void doSave(IProgressMonitor monitor
) {
118 monitor
.beginTask("Saving Editor", 4);
120 if (!conversation
.isBound()) {
125 for (IEditorPart editorPage
: getPages()) {
126 if (editorPage
instanceof TaxonNameEditor
) {
127 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
128 MessageDialog
.openWarning(AbstractUtility
.getShell(), "No Name Specified",
129 "An attempt was made to save a taxon or synonym with "
130 + "an empty name. Operation was cancelled.");
135 editorPage
.doSave(monitor
);
139 // commit the conversation and start a new transaction immediately
140 conversation
.commit(true);
143 this.setDirty(false);
145 } catch (Exception e
) {
147 AbstractUtility
.errorDialog("An error occurred while saving", getClass(),
148 "An error occurred while saving the editor. Please close and reopen the taxon again.", e
);
155 private void disableEditor(boolean isOnError
) {
156 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
158 editorPage
.setOnError();
160 editorPage
.setDisabled();
163 conversation
.unregisterForDataStoreChanges(this);
164 conversation
.close();
168 private void setDirty(boolean dirty
) {
170 firePropertyChange(PROP_DIRTY
);
176 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
186 public boolean isDirty() {
193 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
197 public void editorDirtyStateChanged() {
199 super.editorDirtyStateChanged();
205 * Checks whether nested editors are calling
206 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
207 * place before passing property change along to
208 * <code>super.handlePropertyChange(int propertyId)</code>.
213 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
216 protected void handlePropertyChange(int propertyId
) {
217 if (propertyId
== PROP_DIRTY
) {
220 super.handlePropertyChange(propertyId
);
225 public void doSaveAs() {
230 public boolean isSaveAsAllowed() {
236 public void init(IEditorSite site
, IEditorInput input
)
237 throws PartInitException
{
239 if (!(input
instanceof TaxonEditorInput
)) {
240 throw new PartInitException(
241 "Invalid Input: Must be TaxonEditorInput");
244 this.input
= (TaxonEditorInput
) input
;
247 // // Listen for name changes,
248 // // change tab for this taxon editor accordingly
249 // getTaxon().addPropertyChangeListener("name",
250 // new PropertyChangeListener() {
251 // public void propertyChange(PropertyChangeEvent e) {
255 // } catch (NullPointerException e) {
256 // EditorUtil.warn(getClass(),
257 // "Caught an NPE while initing an editor. This is most " +
258 // "likely due to the unsuccesful attempt to restore the former " +
259 // "state of the application. We ignore this because the workbench " +
260 // "will simply be reset.");
264 super.init(site
, input
);
268 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
269 * text appropriate to the state of the taxon: any taxon that has been saved
270 * will by necessity have a name to display; a new taxon should display
271 * "New taxon" in the editor tab.
273 protected void setPartName() {
275 String partName
= null;
276 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
279 partName
= name
.getTitleCache();
282 if (partName
== null || partName
.equals("")) {
283 partName
= ("New taxon");
286 setPartName(partName
);
292 * Editor pages call this in their postOperation to notify the
293 * MultiPageTaxonEditor of unsaved changes
296 public void changed(Object element
) {
299 super.editorDirtyStateChanged();
300 if (element
instanceof TaxonBase
) {
301 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
302 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
303 if (container
!= null) {
310 * The accepted taxon that is the input for this editor
312 * @return the accepted taxon
314 public Taxon
getTaxon() {
315 return input
.getTaxon();
322 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
327 * getConversationHolder
330 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
334 public ConversationHolder
getConversationHolder() {
340 * setConversationHolder
343 * @param conversation
344 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
347 public void setConversationHolder(ConversationHolder conversation
) {
348 this.conversation
= conversation
;
353 * Getter for the field <code>undoContext</code>.
356 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
359 public IUndoContext
getUndoContext() {
365 * Setter for the field <code>undoContext</code>.
369 * a {@link org.eclipse.core.commands.operations.IUndoContext}
372 public void setUndoContext(IUndoContext undoContext
) {
373 this.undoContext
= undoContext
;
378 public void setFocus() {
379 // logger.warn("Setting focus to editor");
380 // bind the conversation
381 getConversationHolder().bind();
382 // pass focus to the active editor page
383 getActiveEditor().setFocus();
390 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
391 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
395 public void update(CdmDataChangeMap events
) {
396 if (dataChangeBehavior
== null) {
397 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
400 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
407 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
412 public boolean postOperation(CdmBase objectAffectedByOperation
) {
415 for (IEditorPart editor
: this.getPages()) {
416 if (editor
instanceof IPostOperationEnabled
) {
417 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
419 AbstractUtility
.warn(getClass(), "postOperation not enabled for editor " + editor
);
422 AbstractUtility
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
428 * Returns an <code>IEditorPart</code> implementation by type
432 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
435 public IMultiPageTaxonEditorPage
getPage(Page page
) {
436 for (IEditorPart editor
: this.getPages()) {
437 if (editor
.getClass().equals(page
.getClazz())) {
438 return (IMultiPageTaxonEditorPage
) editor
;
445 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
446 * <code>MultiPageTaxonEditor</code>.
448 * @return a {@link java.util.List} object.
450 public List
<IMultiPageTaxonEditorPage
> getPages() {
451 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
452 for (int i
= 0; i
< this.getPageCount(); i
++) {
454 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
460 * Refreshes a certain page of the MultipageTaxonEditor
463 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
466 public boolean redraw(Page page
) {
467 return redraw(page
, true);
471 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
475 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
480 public boolean redraw(Page page
, boolean focus
) {
481 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
482 return editorPage
!= null && editorPage
.redraw(focus
);
493 public boolean onComplete() {
498 * Reloads the data for this
500 public void reload() {
502 AbstractUtility
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
503 + "refreshed because it contains unsaved data. Refreshing "
504 + "this editor would discard the changes. Please save this editor, "
505 + "close and reopen it manually in order to get the latest content");
507 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
509 UUID uuid
= input
.getTaxonNode().getUuid();
511 conversation
.clear();
514 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
516 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
519 } catch (Exception e
) {
520 AbstractUtility
.errorDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
526 public String
toString() {
527 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
531 public boolean permissionsSatisfied() {
532 IEditorPart activeEditor
= getActiveEditor();
533 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
534 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();