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) {
313 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
316 public void forceDirty() {
321 * The accepted taxon that is the input for this editor
323 * @return the accepted taxon
325 public Taxon
getTaxon() {
326 return input
.getTaxon();
333 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
338 * getConversationHolder
341 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
345 public ConversationHolder
getConversationHolder() {
351 * setConversationHolder
354 * @param conversation
355 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
358 public void setConversationHolder(ConversationHolder conversation
) {
359 this.conversation
= conversation
;
364 * Getter for the field <code>undoContext</code>.
367 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
370 public IUndoContext
getUndoContext() {
376 * Setter for the field <code>undoContext</code>.
380 * a {@link org.eclipse.core.commands.operations.IUndoContext}
383 public void setUndoContext(IUndoContext undoContext
) {
384 this.undoContext
= undoContext
;
389 public void setFocus() {
390 // logger.warn("Setting focus to editor");
391 // bind the conversation
392 getConversationHolder().bind();
393 // pass focus to the active editor page
394 getActiveEditor().setFocus();
401 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
402 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
406 public void update(CdmDataChangeMap events
) {
407 if (dataChangeBehavior
== null) {
408 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
411 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
418 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
423 public boolean postOperation(CdmBase objectAffectedByOperation
) {
426 for (IEditorPart editor
: this.getPages()) {
427 if (editor
instanceof IPostOperationEnabled
) {
428 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
430 MessagingUtils
.warn(getClass(), "postOperation not enabled for editor " + editor
);
433 MessagingUtils
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
439 * Returns an <code>IEditorPart</code> implementation by type
443 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
446 public IMultiPageTaxonEditorPage
getPage(Page page
) {
447 for (IEditorPart editor
: this.getPages()) {
448 if (editor
.getClass().equals(page
.getClazz())) {
449 return (IMultiPageTaxonEditorPage
) editor
;
456 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
457 * <code>MultiPageTaxonEditor</code>.
459 * @return a {@link java.util.List} object.
461 public List
<IMultiPageTaxonEditorPage
> getPages() {
462 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
463 for (int i
= 0; i
< this.getPageCount(); i
++) {
465 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
471 * Refreshes a certain page of the MultipageTaxonEditor
474 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
477 public boolean redraw(Page page
) {
478 return redraw(page
, true);
482 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
486 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
491 public boolean redraw(Page page
, boolean focus
) {
492 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
493 return editorPage
!= null && editorPage
.redraw(focus
);
504 public boolean onComplete() {
509 * Reloads the data for this
511 public void reload() {
513 MessagingUtils
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
514 + "refreshed because it contains unsaved data. Refreshing "
515 + "this editor would discard the changes. Please save this editor, "
516 + "close and reopen it manually in order to get the latest content");
518 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
520 UUID uuid
= input
.getTaxonNode().getUuid();
522 conversation
.clear();
525 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
527 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
530 } catch (Exception e
) {
531 MessagingUtils
.messageDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
537 public String
toString() {
538 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
542 public boolean permissionsSatisfied() {
543 IEditorPart activeEditor
= getActiveEditor();
544 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
545 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();