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
.application
.CdmApplicationState
;
27 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
;
28 import eu
.etaxonomy
.cdm
.api
.application
.CdmChangeEvent
.Action
;
29 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
30 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
36 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
37 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
38 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
39 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
40 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
41 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
42 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
43 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
44 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
45 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
46 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
47 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
48 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
49 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
53 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
54 * for "Descriptions", "Concepts", "Geography", etc.
61 public class MultiPageTaxonEditor
extends FormEditor
implements
62 IPartContentHasFactualData
, IConversationEnabled
, IPostOperationEnabled
,
63 IDirtyMarkable
, IPartContentHasDetails
, ISecuredEditor
, IPartContentHasMedia
, ITaxonEditor
{
65 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
66 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon"; //$NON-NLS-1$
68 private boolean dirty
;
70 private ConversationHolder conversation
;
71 private IDataChangeBehavior dataChangeBehavior
;
72 private IUndoContext undoContext
;
74 private TaxonEditorInput input
;
78 * Constructor for MultiPageTaxonEditor.
81 public MultiPageTaxonEditor() {
83 undoContext
= new UndoContext();
89 public void dispose() {
91 conversation
.unregisterForDataStoreChanges(this);
99 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
103 protected void addPages() {
104 input
= (TaxonEditorInput
) getEditorInput();
105 conversation
= input
.getConversationHolder();
106 conversation
.registerForDataStoreChanges(this);
109 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
111 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
114 // addPage(Page.DESCRIPTIVE.getIndex(), new
115 // TaxonDescriptionTreeEditor(this), getEditorInput());
116 // setPageText(Page.DESCRIPTIVE.getIndex(),
117 // Page.DESCRIPTIVE.getTitle());
119 // EditorUtil.showPropertySheet();
121 } catch (PartInitException e
) {
122 MessagingUtils
.error(getClass(), e
);
130 protected void pageChange(int newPageIndex
) {
131 if(getCurrentPage()==-1){
134 super.pageChange(newPageIndex
);
139 public void doSave(IProgressMonitor monitor
) {
140 monitor
.beginTask(Messages
.MultiPageTaxonEditor_SAVING_EDITOR
, 4);
142 if (!conversation
.isBound()) {
147 for (IEditorPart editorPage
: getPages()) {
148 if (editorPage
instanceof TaxonNameEditor
) {
149 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
150 MessageDialog
.openWarning(AbstractUtility
.getShell(), Messages
.MultiPageTaxonEditor_NO_NAME_SPECIFIED
,
151 Messages
.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE
);
156 editorPage
.doSave(monitor
);
160 // commit the conversation and start a new transaction immediately
164 conversation
.commit(true);
165 CdmApplicationState
.getCurrentDataChangeService()
166 .fireChangeEvent(new CdmChangeEvent(Action
.Update
, input
.getTaxonNode() , MultiPageTaxonEditor
.class), true);
169 this.setDirty(false);
171 } catch (Exception e
) {
173 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,Messages
.MultiPageTaxonEditor_SAVING_TAXON
, Messages
.MultiPageTaxonEditor_SAVING_TAXON_MESSAGE
);
180 private void disableEditor(boolean isOnError
) {
181 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
183 editorPage
.setOnError();
185 editorPage
.setDisabled();
189 conversation
.unregisterForDataStoreChanges(this);
190 conversation
.close();
194 private void setDirty(boolean dirty
) {
196 firePropertyChange(PROP_DIRTY
);
202 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
212 public boolean isDirty() {
219 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
223 public void editorDirtyStateChanged() {
225 super.editorDirtyStateChanged();
231 * Checks whether nested editors are calling
232 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
233 * place before passing property change along to
234 * <code>super.handlePropertyChange(int propertyId)</code>.
239 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
242 protected void handlePropertyChange(int propertyId
) {
243 if (propertyId
== PROP_DIRTY
) {
246 super.handlePropertyChange(propertyId
);
251 public void doSaveAs() {
256 public boolean isSaveAsAllowed() {
262 public void init(IEditorSite site
, IEditorInput input
)
263 throws PartInitException
{
265 if (!(input
instanceof TaxonEditorInput
)) {
266 throw new PartInitException(
267 Messages
.MultiPageTaxonEditor_INVALID_INPUT
);
270 this.input
= (TaxonEditorInput
) input
;
274 // // Listen for name changes,
275 // // change tab for this taxon editor accordingly
276 // getTaxon().addPropertyChangeListener("name",
277 // new PropertyChangeListener() {
278 // public void propertyChange(PropertyChangeEvent e) {
282 // } catch (NullPointerException e) {
283 // EditorUtil.warn(getClass(),
284 // "Caught an NPE while initing an editor. This is most " +
285 // "likely due to the unsuccesful attempt to restore the former " +
286 // "state of the application. We ignore this because the workbench " +
287 // "will simply be reset.");
291 super.init(site
, input
);
295 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
296 * text appropriate to the state of the taxon: any taxon that has been saved
297 * will by necessity have a name to display; a new taxon should display
298 * "New taxon" in the editor tab.
300 protected void setPartName() {
302 String partName
= null;
303 TaxonName name
= getTaxon().getName();
306 partName
= name
.getTitleCache();
309 if (partName
== null || partName
.equals("")) { //$NON-NLS-1$
310 partName
= (Messages
.MultiPageTaxonEditor_NEW_TAXON
);
313 setPartName(partName
);
319 * Editor pages call this in their postOperation to notify the
320 * MultiPageTaxonEditor of unsaved changes
323 public void changed(Object element
) {
325 // if the attribute is null then do not set the dirty flag -> hotfix for the problem that for tasks done in service methods the changes are saved automatically
326 if (element
!= null){
328 super.editorDirtyStateChanged();
331 if (element
instanceof TaxonBase
) {
332 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
333 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
334 if (container
!= null) {
338 if (element
instanceof TaxonRelationship
) {
339 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
340 AbstractGroupedContainer container
= page
.getContainer(((TaxonRelationship
) element
).getFromTaxon());
341 if (container
!= null) {
346 //TODO: refresh taxon node in taxon navigator
351 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
354 public void forceDirty() {
359 * The accepted taxon that is the input for this editor
361 * @return the accepted taxon
363 public Taxon
getTaxon() {
364 return input
.getTaxon();
371 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
376 * getConversationHolder
379 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
383 public ConversationHolder
getConversationHolder() {
389 * setConversationHolder
392 * @param conversation
393 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
396 public void setConversationHolder(ConversationHolder conversation
) {
397 this.conversation
= conversation
;
402 * Getter for the field <code>undoContext</code>.
405 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
408 public IUndoContext
getUndoContext() {
414 * Setter for the field <code>undoContext</code>.
418 * a {@link org.eclipse.core.commands.operations.IUndoContext}
421 public void setUndoContext(IUndoContext undoContext
) {
422 this.undoContext
= undoContext
;
427 public void setFocus() {
428 // logger.warn("Setting focus to editor");
429 // bind the conversation
430 getConversationHolder().bind();
432 // pass focus to the active editor page
433 getActiveEditor().setFocus();
440 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
441 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
445 public void update(CdmDataChangeMap events
) {
446 if (dataChangeBehavior
== null) {
447 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
450 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
457 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
462 public boolean postOperation(CdmBase objectAffectedByOperation
) {
465 for (IEditorPart editor
: this.getPages()) {
466 if (editor
instanceof IPostOperationEnabled
) {
467 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
469 MessagingUtils
.warn(getClass(), Messages
.MultiPageTaxonEditor_POST_OP_NOT_ENABLED
+ editor
);
472 MessagingUtils
.warn(getClass(), Messages
.MultiPageTaxonEditor_POST_OP_CALLED
);
478 * Returns an <code>IEditorPart</code> implementation by type
482 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
485 public IMultiPageTaxonEditorPage
getPage(Page page
) {
486 for (IEditorPart editor
: this.getPages()) {
487 if (editor
.getClass().equals(page
.getClazz())) {
488 return (IMultiPageTaxonEditorPage
) editor
;
495 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
496 * <code>MultiPageTaxonEditor</code>.
498 * @return a {@link java.util.List} object.
500 public List
<IMultiPageTaxonEditorPage
> getPages() {
501 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
502 for (int i
= 0; i
< this.getPageCount(); i
++) {
504 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
510 * Refreshes a certain page of the MultipageTaxonEditor
513 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
516 public boolean redraw(Page page
) {
517 return redraw(page
, true);
521 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
525 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
530 public boolean redraw(Page page
, boolean focus
) {
531 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
532 return editorPage
!= null && editorPage
.redraw(focus
);
543 public boolean onComplete() {
548 * Reloads the data for this
550 public void reload() {
552 MessagingUtils
.warningDialog(Messages
.MultiPageTaxonEditor_UNSAVED_DATA
, getClass(), Messages
.MultiPageTaxonEditor_UNSAVED_DATA_MESSAGE
);
554 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
556 UUID uuid
= input
.getTaxonNode().getUuid();
558 conversation
.clear();
561 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
563 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
566 } catch (Exception e
) {
567 MessagingUtils
.messageDialog(Messages
.MultiPageTaxonEditor_REFRESH_ERROR
, getClass(), Messages
.MultiPageTaxonEditor_REFRESH_ERROR_MESSAGE
, e
);
573 public String
toString() {
574 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput()); //$NON-NLS-1$
578 public boolean permissionsSatisfied() {
579 IEditorPart activeEditor
= getActiveEditor();
580 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
581 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();
587 * @see eu.etaxonomy.taxeditor.model.IPartContentHasMedia#canAttachMedia()
590 public boolean canAttachMedia() {