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
);
129 protected void pageChange(int newPageIndex
) {
130 if(getCurrentPage()==-1){
133 super.pageChange(newPageIndex
);
138 public void doSave(IProgressMonitor monitor
) {
139 monitor
.beginTask(Messages
.MultiPageTaxonEditor_SAVING_EDITOR
, 4);
141 if (!conversation
.isBound()) {
146 for (IEditorPart editorPage
: getPages()) {
147 if (editorPage
instanceof TaxonNameEditor
) {
148 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
149 MessageDialog
.openWarning(AbstractUtility
.getShell(), Messages
.MultiPageTaxonEditor_NO_NAME_SPECIFIED
,
150 Messages
.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE
);
155 editorPage
.doSave(monitor
);
159 // commit the conversation and start a new transaction immediately
163 conversation
.commit(true);
164 CdmApplicationState
.getCurrentDataChangeService()
165 .fireChangeEvent(new CdmChangeEvent(Action
.Update
, input
.getTaxonNode() , MultiPageTaxonEditor
.class), true);
168 this.setDirty(false);
170 } catch (Exception e
) {
172 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,Messages
.MultiPageTaxonEditor_SAVING_TAXON
, Messages
.MultiPageTaxonEditor_SAVING_TAXON_MESSAGE
);
179 private void disableEditor(boolean isOnError
) {
180 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
182 editorPage
.setOnError();
184 editorPage
.setDisabled();
188 conversation
.unregisterForDataStoreChanges(this);
189 conversation
.close();
193 private void setDirty(boolean dirty
) {
195 firePropertyChange(PROP_DIRTY
);
201 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
211 public boolean isDirty() {
218 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
222 public void editorDirtyStateChanged() {
224 super.editorDirtyStateChanged();
230 * Checks whether nested editors are calling
231 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
232 * place before passing property change along to
233 * <code>super.handlePropertyChange(int propertyId)</code>.
238 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
241 protected void handlePropertyChange(int propertyId
) {
242 if (propertyId
== PROP_DIRTY
) {
245 super.handlePropertyChange(propertyId
);
250 public void doSaveAs() {
255 public boolean isSaveAsAllowed() {
261 public void init(IEditorSite site
, IEditorInput input
)
262 throws PartInitException
{
264 if (!(input
instanceof TaxonEditorInput
)) {
265 throw new PartInitException(
266 Messages
.MultiPageTaxonEditor_INVALID_INPUT
);
269 this.input
= (TaxonEditorInput
) input
;
273 // // Listen for name changes,
274 // // change tab for this taxon editor accordingly
275 // getTaxon().addPropertyChangeListener("name",
276 // new PropertyChangeListener() {
277 // public void propertyChange(PropertyChangeEvent e) {
281 // } catch (NullPointerException e) {
282 // EditorUtil.warn(getClass(),
283 // "Caught an NPE while initing an editor. This is most " +
284 // "likely due to the unsuccesful attempt to restore the former " +
285 // "state of the application. We ignore this because the workbench " +
286 // "will simply be reset.");
290 super.init(site
, input
);
294 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
295 * text appropriate to the state of the taxon: any taxon that has been saved
296 * will by necessity have a name to display; a new taxon should display
297 * "New taxon" in the editor tab.
299 protected void setPartName() {
301 String partName
= null;
302 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
305 partName
= name
.getTitleCache();
308 if (partName
== null || partName
.equals("")) { //$NON-NLS-1$
309 partName
= (Messages
.MultiPageTaxonEditor_NEW_TAXON
);
312 setPartName(partName
);
318 * Editor pages call this in their postOperation to notify the
319 * MultiPageTaxonEditor of unsaved changes
322 public void changed(Object element
) {
324 // 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
325 if (element
!= null){
327 super.editorDirtyStateChanged();
330 if (element
instanceof TaxonBase
) {
331 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
332 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
333 if (container
!= null) {
338 //TODO: refresh taxon node in taxon navigator
343 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
346 public void forceDirty() {
351 * The accepted taxon that is the input for this editor
353 * @return the accepted taxon
355 public Taxon
getTaxon() {
356 return input
.getTaxon();
363 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
368 * getConversationHolder
371 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
375 public ConversationHolder
getConversationHolder() {
381 * setConversationHolder
384 * @param conversation
385 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
388 public void setConversationHolder(ConversationHolder conversation
) {
389 this.conversation
= conversation
;
394 * Getter for the field <code>undoContext</code>.
397 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
400 public IUndoContext
getUndoContext() {
406 * Setter for the field <code>undoContext</code>.
410 * a {@link org.eclipse.core.commands.operations.IUndoContext}
413 public void setUndoContext(IUndoContext undoContext
) {
414 this.undoContext
= undoContext
;
419 public void setFocus() {
420 // logger.warn("Setting focus to editor");
421 // bind the conversation
422 getConversationHolder().bind();
424 // pass focus to the active editor page
425 getActiveEditor().setFocus();
432 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
433 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
437 public void update(CdmDataChangeMap events
) {
438 if (dataChangeBehavior
== null) {
439 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
442 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
449 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
454 public boolean postOperation(CdmBase objectAffectedByOperation
) {
457 for (IEditorPart editor
: this.getPages()) {
458 if (editor
instanceof IPostOperationEnabled
) {
459 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
461 MessagingUtils
.warn(getClass(), Messages
.MultiPageTaxonEditor_POST_OP_NOT_ENABLED
+ editor
);
464 MessagingUtils
.warn(getClass(), Messages
.MultiPageTaxonEditor_POST_OP_CALLED
);
470 * Returns an <code>IEditorPart</code> implementation by type
474 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
477 public IMultiPageTaxonEditorPage
getPage(Page page
) {
478 for (IEditorPart editor
: this.getPages()) {
479 if (editor
.getClass().equals(page
.getClazz())) {
480 return (IMultiPageTaxonEditorPage
) editor
;
487 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
488 * <code>MultiPageTaxonEditor</code>.
490 * @return a {@link java.util.List} object.
492 public List
<IMultiPageTaxonEditorPage
> getPages() {
493 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
494 for (int i
= 0; i
< this.getPageCount(); i
++) {
496 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
502 * Refreshes a certain page of the MultipageTaxonEditor
505 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
508 public boolean redraw(Page page
) {
509 return redraw(page
, true);
513 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
517 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
522 public boolean redraw(Page page
, boolean focus
) {
523 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
524 return editorPage
!= null && editorPage
.redraw(focus
);
535 public boolean onComplete() {
540 * Reloads the data for this
542 public void reload() {
544 MessagingUtils
.warningDialog(Messages
.MultiPageTaxonEditor_UNSAVED_DATA
, getClass(), Messages
.MultiPageTaxonEditor_UNSAVED_DATA_MESSAGE
);
546 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
548 UUID uuid
= input
.getTaxonNode().getUuid();
550 conversation
.clear();
553 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
555 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
558 } catch (Exception e
) {
559 MessagingUtils
.messageDialog(Messages
.MultiPageTaxonEditor_REFRESH_ERROR
, getClass(), Messages
.MultiPageTaxonEditor_REFRESH_ERROR_MESSAGE
, e
);
565 public String
toString() {
566 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput()); //$NON-NLS-1$
570 public boolean permissionsSatisfied() {
571 IEditorPart activeEditor
= getActiveEditor();
572 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
573 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();
579 * @see eu.etaxonomy.taxeditor.model.IPartContentHasMedia#canAttachMedia()
582 public boolean canAttachMedia() {