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
.TaxonNameBase
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
35 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
36 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
37 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
38 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
39 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
40 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
41 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
42 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
43 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
44 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
45 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
46 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
47 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
48 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
52 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
53 * for "Descriptions", "Concepts", "Geography", etc.
60 public class MultiPageTaxonEditor
extends FormEditor
implements
61 IPartContentHasFactualData
, IConversationEnabled
, IPostOperationEnabled
,
62 IDirtyMarkable
, IPartContentHasDetails
, ISecuredEditor
, IPartContentHasMedia
{
64 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
65 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon"; //$NON-NLS-1$
67 private boolean dirty
;
69 private ConversationHolder conversation
;
70 private IDataChangeBehavior dataChangeBehavior
;
71 private IUndoContext undoContext
;
73 private TaxonEditorInput input
;
77 * Constructor for MultiPageTaxonEditor.
80 public MultiPageTaxonEditor() {
82 undoContext
= new UndoContext();
88 public void dispose() {
90 conversation
.unregisterForDataStoreChanges(this);
98 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
102 protected void addPages() {
103 input
= (TaxonEditorInput
) getEditorInput();
104 conversation
= input
.getConversationHolder();
105 conversation
.registerForDataStoreChanges(this);
108 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
110 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
113 // addPage(Page.DESCRIPTIVE.getIndex(), new
114 // TaxonDescriptionTreeEditor(this), getEditorInput());
115 // setPageText(Page.DESCRIPTIVE.getIndex(),
116 // Page.DESCRIPTIVE.getTitle());
118 // EditorUtil.showPropertySheet();
120 } catch (PartInitException e
) {
121 MessagingUtils
.error(getClass(), e
);
127 public void doSave(IProgressMonitor monitor
) {
128 monitor
.beginTask(Messages
.MultiPageTaxonEditor_SAVING_EDITOR
, 4);
130 if (!conversation
.isBound()) {
135 for (IEditorPart editorPage
: getPages()) {
136 if (editorPage
instanceof TaxonNameEditor
) {
137 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
138 MessageDialog
.openWarning(AbstractUtility
.getShell(), Messages
.MultiPageTaxonEditor_NO_NAME_SPECIFIED
,
139 Messages
.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE
);
144 editorPage
.doSave(monitor
);
148 // commit the conversation and start a new transaction immediately
152 conversation
.commit(true);
153 CdmApplicationState
.getCurrentDataChangeService()
154 .fireChangeEvent(new CdmChangeEvent(Action
.Update
, input
.getTaxonNode() , MultiPageTaxonEditor
.class), true);
157 this.setDirty(false);
159 } catch (Exception e
) {
161 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,Messages
.MultiPageTaxonEditor_SAVING_TAXON
, Messages
.MultiPageTaxonEditor_SAVING_TAXON_MESSAGE
);
168 private void disableEditor(boolean isOnError
) {
169 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
171 editorPage
.setOnError();
173 editorPage
.setDisabled();
177 conversation
.unregisterForDataStoreChanges(this);
178 conversation
.close();
182 private void setDirty(boolean dirty
) {
184 firePropertyChange(PROP_DIRTY
);
190 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
200 public boolean isDirty() {
207 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
211 public void editorDirtyStateChanged() {
213 super.editorDirtyStateChanged();
219 * Checks whether nested editors are calling
220 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
221 * place before passing property change along to
222 * <code>super.handlePropertyChange(int propertyId)</code>.
227 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
230 protected void handlePropertyChange(int propertyId
) {
231 if (propertyId
== PROP_DIRTY
) {
234 super.handlePropertyChange(propertyId
);
239 public void doSaveAs() {
244 public boolean isSaveAsAllowed() {
250 public void init(IEditorSite site
, IEditorInput input
)
251 throws PartInitException
{
253 if (!(input
instanceof TaxonEditorInput
)) {
254 throw new PartInitException(
255 Messages
.MultiPageTaxonEditor_INVALID_INPUT
);
258 this.input
= (TaxonEditorInput
) input
;
262 // // Listen for name changes,
263 // // change tab for this taxon editor accordingly
264 // getTaxon().addPropertyChangeListener("name",
265 // new PropertyChangeListener() {
266 // public void propertyChange(PropertyChangeEvent e) {
270 // } catch (NullPointerException e) {
271 // EditorUtil.warn(getClass(),
272 // "Caught an NPE while initing an editor. This is most " +
273 // "likely due to the unsuccesful attempt to restore the former " +
274 // "state of the application. We ignore this because the workbench " +
275 // "will simply be reset.");
279 super.init(site
, input
);
283 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
284 * text appropriate to the state of the taxon: any taxon that has been saved
285 * will by necessity have a name to display; a new taxon should display
286 * "New taxon" in the editor tab.
288 protected void setPartName() {
290 String partName
= null;
291 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
294 partName
= name
.getTitleCache();
297 if (partName
== null || partName
.equals("")) { //$NON-NLS-1$
298 partName
= (Messages
.MultiPageTaxonEditor_NEW_TAXON
);
301 setPartName(partName
);
307 * Editor pages call this in their postOperation to notify the
308 * MultiPageTaxonEditor of unsaved changes
311 public void changed(Object element
) {
313 // 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
314 if (element
!= null){
316 super.editorDirtyStateChanged();
319 if (element
instanceof TaxonBase
) {
320 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
321 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
322 if (container
!= null) {
327 //TODO: refresh taxon node in taxon navigator
332 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
335 public void forceDirty() {
340 * The accepted taxon that is the input for this editor
342 * @return the accepted taxon
344 public Taxon
getTaxon() {
345 return input
.getTaxon();
352 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
357 * getConversationHolder
360 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
364 public ConversationHolder
getConversationHolder() {
370 * setConversationHolder
373 * @param conversation
374 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
377 public void setConversationHolder(ConversationHolder conversation
) {
378 this.conversation
= conversation
;
383 * Getter for the field <code>undoContext</code>.
386 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
389 public IUndoContext
getUndoContext() {
395 * Setter for the field <code>undoContext</code>.
399 * a {@link org.eclipse.core.commands.operations.IUndoContext}
402 public void setUndoContext(IUndoContext undoContext
) {
403 this.undoContext
= undoContext
;
408 public void setFocus() {
409 // logger.warn("Setting focus to editor");
410 // bind the conversation
411 getConversationHolder().bind();
413 // pass focus to the active editor page
414 getActiveEditor().setFocus();
421 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
422 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
426 public void update(CdmDataChangeMap events
) {
427 if (dataChangeBehavior
== null) {
428 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
431 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
438 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
443 public boolean postOperation(CdmBase objectAffectedByOperation
) {
446 for (IEditorPart editor
: this.getPages()) {
447 if (editor
instanceof IPostOperationEnabled
) {
448 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
450 MessagingUtils
.warn(getClass(), Messages
.MultiPageTaxonEditor_POST_OP_NOT_ENABLED
+ editor
);
453 MessagingUtils
.warn(getClass(), Messages
.MultiPageTaxonEditor_POST_OP_CALLED
);
459 * Returns an <code>IEditorPart</code> implementation by type
463 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
466 public IMultiPageTaxonEditorPage
getPage(Page page
) {
467 for (IEditorPart editor
: this.getPages()) {
468 if (editor
.getClass().equals(page
.getClazz())) {
469 return (IMultiPageTaxonEditorPage
) editor
;
476 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
477 * <code>MultiPageTaxonEditor</code>.
479 * @return a {@link java.util.List} object.
481 public List
<IMultiPageTaxonEditorPage
> getPages() {
482 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
483 for (int i
= 0; i
< this.getPageCount(); i
++) {
485 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
491 * Refreshes a certain page of the MultipageTaxonEditor
494 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
497 public boolean redraw(Page page
) {
498 return redraw(page
, true);
502 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
506 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
511 public boolean redraw(Page page
, boolean focus
) {
512 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
513 return editorPage
!= null && editorPage
.redraw(focus
);
524 public boolean onComplete() {
529 * Reloads the data for this
531 public void reload() {
533 MessagingUtils
.warningDialog(Messages
.MultiPageTaxonEditor_UNSAVED_DATA
, getClass(), Messages
.MultiPageTaxonEditor_UNSAVED_DATA_MESSAGE
);
535 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
537 UUID uuid
= input
.getTaxonNode().getUuid();
539 conversation
.clear();
542 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
544 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
547 } catch (Exception e
) {
548 MessagingUtils
.messageDialog(Messages
.MultiPageTaxonEditor_REFRESH_ERROR
, getClass(), Messages
.MultiPageTaxonEditor_REFRESH_ERROR_MESSAGE
, e
);
554 public String
toString() {
555 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput()); //$NON-NLS-1$
559 public boolean permissionsSatisfied() {
560 IEditorPart activeEditor
= getActiveEditor();
561 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
562 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();
568 * @see eu.etaxonomy.taxeditor.model.IPartContentHasMedia#canAttachMedia()
571 public boolean canAttachMedia() {