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 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
308 if (element
!= null){
310 super.editorDirtyStateChanged();
313 if (element
instanceof TaxonBase
) {
314 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
315 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
316 if (container
!= null) {
321 //TODO: refresh taxon node in taxon navigator
326 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
329 public void forceDirty() {
334 * The accepted taxon that is the input for this editor
336 * @return the accepted taxon
338 public Taxon
getTaxon() {
339 return input
.getTaxon();
346 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
351 * getConversationHolder
354 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
358 public ConversationHolder
getConversationHolder() {
364 * setConversationHolder
367 * @param conversation
368 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
371 public void setConversationHolder(ConversationHolder conversation
) {
372 this.conversation
= conversation
;
377 * Getter for the field <code>undoContext</code>.
380 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
383 public IUndoContext
getUndoContext() {
389 * Setter for the field <code>undoContext</code>.
393 * a {@link org.eclipse.core.commands.operations.IUndoContext}
396 public void setUndoContext(IUndoContext undoContext
) {
397 this.undoContext
= undoContext
;
402 public void setFocus() {
403 // logger.warn("Setting focus to editor");
404 // bind the conversation
405 getConversationHolder().bind();
407 // pass focus to the active editor page
408 getActiveEditor().setFocus();
415 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
416 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
420 public void update(CdmDataChangeMap events
) {
421 if (dataChangeBehavior
== null) {
422 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
425 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
432 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
437 public boolean postOperation(CdmBase objectAffectedByOperation
) {
440 for (IEditorPart editor
: this.getPages()) {
441 if (editor
instanceof IPostOperationEnabled
) {
442 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
444 MessagingUtils
.warn(getClass(), "postOperation not enabled for editor " + editor
);
447 MessagingUtils
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
453 * Returns an <code>IEditorPart</code> implementation by type
457 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
460 public IMultiPageTaxonEditorPage
getPage(Page page
) {
461 for (IEditorPart editor
: this.getPages()) {
462 if (editor
.getClass().equals(page
.getClazz())) {
463 return (IMultiPageTaxonEditorPage
) editor
;
470 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
471 * <code>MultiPageTaxonEditor</code>.
473 * @return a {@link java.util.List} object.
475 public List
<IMultiPageTaxonEditorPage
> getPages() {
476 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
477 for (int i
= 0; i
< this.getPageCount(); i
++) {
479 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
485 * Refreshes a certain page of the MultipageTaxonEditor
488 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
491 public boolean redraw(Page page
) {
492 return redraw(page
, true);
496 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
500 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
505 public boolean redraw(Page page
, boolean focus
) {
506 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
507 return editorPage
!= null && editorPage
.redraw(focus
);
518 public boolean onComplete() {
523 * Reloads the data for this
525 public void reload() {
527 MessagingUtils
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
528 + "refreshed because it contains unsaved data. Refreshing "
529 + "this editor would discard the changes. Please save this editor, "
530 + "close and reopen it manually in order to get the latest content");
532 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
534 UUID uuid
= input
.getTaxonNode().getUuid();
536 conversation
.clear();
539 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
541 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
544 } catch (Exception e
) {
545 MessagingUtils
.messageDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
551 public String
toString() {
552 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
556 public boolean permissionsSatisfied() {
557 IEditorPart activeEditor
= getActiveEditor();
558 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
559 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();
565 * @see eu.etaxonomy.taxeditor.model.IPartContentHasMedia#canAttachMedia()
568 public boolean canAttachMedia() {