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();
84 public void dispose() {
86 conversation
.unregisterForDataStoreChanges(this);
94 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
98 protected void addPages() {
99 input
= (TaxonEditorInput
) getEditorInput();
100 conversation
= input
.getConversationHolder();
101 conversation
.registerForDataStoreChanges(this);
104 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
106 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
109 // addPage(Page.DESCRIPTIVE.getIndex(), new
110 // TaxonDescriptionTreeEditor(this), getEditorInput());
111 // setPageText(Page.DESCRIPTIVE.getIndex(),
112 // Page.DESCRIPTIVE.getTitle());
114 // EditorUtil.showPropertySheet();
116 } catch (PartInitException e
) {
117 MessagingUtils
.error(getClass(), e
);
123 public void doSave(IProgressMonitor monitor
) {
124 monitor
.beginTask("Saving Editor", 4);
126 if (!conversation
.isBound()) {
131 for (IEditorPart editorPage
: getPages()) {
132 if (editorPage
instanceof TaxonNameEditor
) {
133 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
134 MessageDialog
.openWarning(AbstractUtility
.getShell(), "No Name Specified",
135 "An attempt was made to save a taxon or synonym with "
136 + "an empty name. Operation was cancelled.");
141 editorPage
.doSave(monitor
);
145 // commit the conversation and start a new transaction immediately
148 conversation
.commit(true);
151 this.setDirty(false);
153 } catch (Exception e
) {
155 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,"saving a taxon", " Please close and reopen the taxon again.");
162 private void disableEditor(boolean isOnError
) {
163 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
165 editorPage
.setOnError();
167 editorPage
.setDisabled();
171 conversation
.unregisterForDataStoreChanges(this);
172 conversation
.close();
176 private void setDirty(boolean dirty
) {
178 firePropertyChange(PROP_DIRTY
);
184 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
194 public boolean isDirty() {
201 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
205 public void editorDirtyStateChanged() {
207 super.editorDirtyStateChanged();
213 * Checks whether nested editors are calling
214 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
215 * place before passing property change along to
216 * <code>super.handlePropertyChange(int propertyId)</code>.
221 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
224 protected void handlePropertyChange(int propertyId
) {
225 if (propertyId
== PROP_DIRTY
) {
228 super.handlePropertyChange(propertyId
);
233 public void doSaveAs() {
238 public boolean isSaveAsAllowed() {
244 public void init(IEditorSite site
, IEditorInput input
)
245 throws PartInitException
{
247 if (!(input
instanceof TaxonEditorInput
)) {
248 throw new PartInitException(
249 "Invalid Input: Must be TaxonEditorInput");
252 this.input
= (TaxonEditorInput
) input
;
256 // // Listen for name changes,
257 // // change tab for this taxon editor accordingly
258 // getTaxon().addPropertyChangeListener("name",
259 // new PropertyChangeListener() {
260 // public void propertyChange(PropertyChangeEvent e) {
264 // } catch (NullPointerException e) {
265 // EditorUtil.warn(getClass(),
266 // "Caught an NPE while initing an editor. This is most " +
267 // "likely due to the unsuccesful attempt to restore the former " +
268 // "state of the application. We ignore this because the workbench " +
269 // "will simply be reset.");
273 super.init(site
, input
);
277 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
278 * text appropriate to the state of the taxon: any taxon that has been saved
279 * will by necessity have a name to display; a new taxon should display
280 * "New taxon" in the editor tab.
282 protected void setPartName() {
284 String partName
= null;
285 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
288 partName
= name
.getTitleCache();
291 if (partName
== null || partName
.equals("")) {
292 partName
= ("New taxon");
295 setPartName(partName
);
301 * Editor pages call this in their postOperation to notify the
302 * MultiPageTaxonEditor of unsaved changes
305 public void changed(Object element
) {
307 if (element
!= null){
309 super.editorDirtyStateChanged();
312 if (element
instanceof TaxonBase
) {
313 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
314 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
315 if (container
!= null) {
320 //TODO: refresh taxon node in taxon navigator
325 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
328 public void forceDirty() {
333 * The accepted taxon that is the input for this editor
335 * @return the accepted taxon
337 public Taxon
getTaxon() {
338 return input
.getTaxon();
345 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
350 * getConversationHolder
353 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
357 public ConversationHolder
getConversationHolder() {
363 * setConversationHolder
366 * @param conversation
367 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
370 public void setConversationHolder(ConversationHolder conversation
) {
371 this.conversation
= conversation
;
376 * Getter for the field <code>undoContext</code>.
379 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
382 public IUndoContext
getUndoContext() {
388 * Setter for the field <code>undoContext</code>.
392 * a {@link org.eclipse.core.commands.operations.IUndoContext}
395 public void setUndoContext(IUndoContext undoContext
) {
396 this.undoContext
= undoContext
;
401 public void setFocus() {
402 // logger.warn("Setting focus to editor");
403 // bind the conversation
404 getConversationHolder().bind();
406 // pass focus to the active editor page
407 getActiveEditor().setFocus();
414 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
415 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
419 public void update(CdmDataChangeMap events
) {
420 if (dataChangeBehavior
== null) {
421 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
424 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
431 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
436 public boolean postOperation(CdmBase objectAffectedByOperation
) {
439 for (IEditorPart editor
: this.getPages()) {
440 if (editor
instanceof IPostOperationEnabled
) {
441 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
443 MessagingUtils
.warn(getClass(), "postOperation not enabled for editor " + editor
);
446 MessagingUtils
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
452 * Returns an <code>IEditorPart</code> implementation by type
456 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
459 public IMultiPageTaxonEditorPage
getPage(Page page
) {
460 for (IEditorPart editor
: this.getPages()) {
461 if (editor
.getClass().equals(page
.getClazz())) {
462 return (IMultiPageTaxonEditorPage
) editor
;
469 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
470 * <code>MultiPageTaxonEditor</code>.
472 * @return a {@link java.util.List} object.
474 public List
<IMultiPageTaxonEditorPage
> getPages() {
475 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
476 for (int i
= 0; i
< this.getPageCount(); i
++) {
478 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
484 * Refreshes a certain page of the MultipageTaxonEditor
487 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
490 public boolean redraw(Page page
) {
491 return redraw(page
, true);
495 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
499 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
504 public boolean redraw(Page page
, boolean focus
) {
505 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
506 return editorPage
!= null && editorPage
.redraw(focus
);
517 public boolean onComplete() {
522 * Reloads the data for this
524 public void reload() {
526 MessagingUtils
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
527 + "refreshed because it contains unsaved data. Refreshing "
528 + "this editor would discard the changes. Please save this editor, "
529 + "close and reopen it manually in order to get the latest content");
531 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
533 UUID uuid
= input
.getTaxonNode().getUuid();
535 conversation
.clear();
538 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
540 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
543 } catch (Exception e
) {
544 MessagingUtils
.messageDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
550 public String
toString() {
551 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
555 public boolean permissionsSatisfied() {
556 IEditorPart activeEditor
= getActiveEditor();
557 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
558 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();
564 * @see eu.etaxonomy.taxeditor.model.IPartContentHasMedia#canAttachMedia()
567 public boolean canAttachMedia() {