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
.IDirtyMarkableSelectionProvider
;
40 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
41 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
42 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
43 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
47 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
48 * for "Descriptions", "Concepts", "Geography", etc.
55 public class MultiPageTaxonEditor
extends FormEditor
implements
56 IPartContentHasFactualData
, IConversationEnabled
, IPostOperationEnabled
,
57 IDirtyMarkableSelectionProvider
, IPartContentHasDetails
, ISecuredEditor
{
59 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
60 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon";
62 private boolean dirty
;
64 private ConversationHolder conversation
;
65 private IDataChangeBehavior dataChangeBehavior
;
66 private IUndoContext undoContext
;
68 private TaxonEditorInput input
;
72 * Constructor for MultiPageTaxonEditor.
75 public MultiPageTaxonEditor() {
77 undoContext
= new UndoContext();
82 public void dispose() {
83 conversation
.unregisterForDataStoreChanges(this);
91 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
95 protected void addPages() {
96 input
= (TaxonEditorInput
) getEditorInput();
97 conversation
= input
.getConversationHolder();
98 conversation
.registerForDataStoreChanges(this);
101 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
103 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
106 // addPage(Page.DESCRIPTIVE.getIndex(), new
107 // TaxonDescriptionTreeEditor(this), getEditorInput());
108 // setPageText(Page.DESCRIPTIVE.getIndex(),
109 // Page.DESCRIPTIVE.getTitle());
111 // EditorUtil.showPropertySheet();
113 } catch (PartInitException e
) {
114 MessagingUtils
.error(getClass(), e
);
120 public void doSave(IProgressMonitor monitor
) {
121 monitor
.beginTask("Saving Editor", 4);
123 if (!conversation
.isBound()) {
128 for (IEditorPart editorPage
: getPages()) {
129 if (editorPage
instanceof TaxonNameEditor
) {
130 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
131 MessageDialog
.openWarning(AbstractUtility
.getShell(), "No Name Specified",
132 "An attempt was made to save a taxon or synonym with "
133 + "an empty name. Operation was cancelled.");
138 editorPage
.doSave(monitor
);
142 // commit the conversation and start a new transaction immediately
143 conversation
.commit(true);
146 this.setDirty(false);
148 } catch (Exception e
) {
150 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,"saving a taxon", " Please close and reopen the taxon again.");
157 private void disableEditor(boolean isOnError
) {
158 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
160 editorPage
.setOnError();
162 editorPage
.setDisabled();
165 conversation
.unregisterForDataStoreChanges(this);
166 conversation
.close();
170 private void setDirty(boolean dirty
) {
172 firePropertyChange(PROP_DIRTY
);
178 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
188 public boolean isDirty() {
195 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
199 public void editorDirtyStateChanged() {
201 super.editorDirtyStateChanged();
207 * Checks whether nested editors are calling
208 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
209 * place before passing property change along to
210 * <code>super.handlePropertyChange(int propertyId)</code>.
215 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
218 protected void handlePropertyChange(int propertyId
) {
219 if (propertyId
== PROP_DIRTY
) {
222 super.handlePropertyChange(propertyId
);
227 public void doSaveAs() {
232 public boolean isSaveAsAllowed() {
238 public void init(IEditorSite site
, IEditorInput input
)
239 throws PartInitException
{
241 if (!(input
instanceof TaxonEditorInput
)) {
242 throw new PartInitException(
243 "Invalid Input: Must be TaxonEditorInput");
246 this.input
= (TaxonEditorInput
) input
;
249 // // Listen for name changes,
250 // // change tab for this taxon editor accordingly
251 // getTaxon().addPropertyChangeListener("name",
252 // new PropertyChangeListener() {
253 // public void propertyChange(PropertyChangeEvent e) {
257 // } catch (NullPointerException e) {
258 // EditorUtil.warn(getClass(),
259 // "Caught an NPE while initing an editor. This is most " +
260 // "likely due to the unsuccesful attempt to restore the former " +
261 // "state of the application. We ignore this because the workbench " +
262 // "will simply be reset.");
266 super.init(site
, input
);
270 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
271 * text appropriate to the state of the taxon: any taxon that has been saved
272 * will by necessity have a name to display; a new taxon should display
273 * "New taxon" in the editor tab.
275 protected void setPartName() {
277 String partName
= null;
278 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
281 partName
= name
.getTitleCache();
284 if (partName
== null || partName
.equals("")) {
285 partName
= ("New taxon");
288 setPartName(partName
);
294 * Editor pages call this in their postOperation to notify the
295 * MultiPageTaxonEditor of unsaved changes
298 public void changed(Object element
) {
301 super.editorDirtyStateChanged();
302 if (element
instanceof TaxonBase
) {
303 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
304 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
305 if (container
!= null) {
312 * The accepted taxon that is the input for this editor
314 * @return the accepted taxon
316 public Taxon
getTaxon() {
317 return input
.getTaxon();
324 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
329 * getConversationHolder
332 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
336 public ConversationHolder
getConversationHolder() {
342 * setConversationHolder
345 * @param conversation
346 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
349 public void setConversationHolder(ConversationHolder conversation
) {
350 this.conversation
= conversation
;
355 * Getter for the field <code>undoContext</code>.
358 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
361 public IUndoContext
getUndoContext() {
367 * Setter for the field <code>undoContext</code>.
371 * a {@link org.eclipse.core.commands.operations.IUndoContext}
374 public void setUndoContext(IUndoContext undoContext
) {
375 this.undoContext
= undoContext
;
380 public void setFocus() {
381 // logger.warn("Setting focus to editor");
382 // bind the conversation
383 getConversationHolder().bind();
384 // pass focus to the active editor page
385 getActiveEditor().setFocus();
392 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
393 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
397 public void update(CdmDataChangeMap events
) {
398 if (dataChangeBehavior
== null) {
399 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
402 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
409 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
414 public boolean postOperation(CdmBase objectAffectedByOperation
) {
417 for (IEditorPart editor
: this.getPages()) {
418 if (editor
instanceof IPostOperationEnabled
) {
419 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
421 MessagingUtils
.warn(getClass(), "postOperation not enabled for editor " + editor
);
424 MessagingUtils
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
430 * Returns an <code>IEditorPart</code> implementation by type
434 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
437 public IMultiPageTaxonEditorPage
getPage(Page page
) {
438 for (IEditorPart editor
: this.getPages()) {
439 if (editor
.getClass().equals(page
.getClazz())) {
440 return (IMultiPageTaxonEditorPage
) editor
;
447 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
448 * <code>MultiPageTaxonEditor</code>.
450 * @return a {@link java.util.List} object.
452 public List
<IMultiPageTaxonEditorPage
> getPages() {
453 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
454 for (int i
= 0; i
< this.getPageCount(); i
++) {
456 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
462 * Refreshes a certain page of the MultipageTaxonEditor
465 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
468 public boolean redraw(Page page
) {
469 return redraw(page
, true);
473 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
477 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
482 public boolean redraw(Page page
, boolean focus
) {
483 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
484 return editorPage
!= null && editorPage
.redraw(focus
);
495 public boolean onComplete() {
500 * Reloads the data for this
502 public void reload() {
504 MessagingUtils
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
505 + "refreshed because it contains unsaved data. Refreshing "
506 + "this editor would discard the changes. Please save this editor, "
507 + "close and reopen it manually in order to get the latest content");
509 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
511 UUID uuid
= input
.getTaxonNode().getUuid();
513 conversation
.clear();
516 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
518 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
521 } catch (Exception e
) {
522 MessagingUtils
.messageDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
528 public String
toString() {
529 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
533 public boolean permissionsSatisfied() {
534 IEditorPart activeEditor
= getActiveEditor();
535 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
536 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();