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
;
45 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
46 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
50 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
51 * for "Descriptions", "Concepts", "Geography", etc.
58 public class MultiPageTaxonEditor
extends FormEditor
implements
59 IPartContentHasFactualData
, IConversationEnabled
, ICdmEntitySessionEnabled
, IPostOperationEnabled
,
60 IDirtyMarkable
, IPartContentHasDetails
, ISecuredEditor
, IPartContentHasMedia
{
62 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
63 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon";
65 private boolean dirty
;
67 private ICdmEntitySession cdmEntitySession
;
68 private ConversationHolder conversation
;
69 private IDataChangeBehavior dataChangeBehavior
;
70 private IUndoContext undoContext
;
72 private TaxonEditorInput input
;
76 * Constructor for MultiPageTaxonEditor.
79 public MultiPageTaxonEditor() {
81 undoContext
= new UndoContext();
87 public void dispose() {
88 if(cdmEntitySession
!= null) {
89 cdmEntitySession
.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);
107 cdmEntitySession
.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
);
128 public void doSave(IProgressMonitor monitor
) {
129 monitor
.beginTask("Saving Editor", 4);
131 if (!conversation
.isBound()) {
136 for (IEditorPart editorPage
: getPages()) {
137 if (editorPage
instanceof TaxonNameEditor
) {
138 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
139 MessageDialog
.openWarning(AbstractUtility
.getShell(), "No Name Specified",
140 "An attempt was made to save a taxon or synonym with "
141 + "an empty name. Operation was cancelled.");
146 editorPage
.doSave(monitor
);
150 // commit the conversation and start a new transaction immediately
153 conversation
.commit(true);
156 this.setDirty(false);
158 } catch (Exception e
) {
160 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,"saving a taxon", " Please close and reopen the taxon again.");
167 private void disableEditor(boolean isOnError
) {
168 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
170 editorPage
.setOnError();
172 editorPage
.setDisabled();
175 cdmEntitySession
.unregisterForDataStoreChanges(this);
176 conversation
.unregisterForDataStoreChanges(this);
177 conversation
.close();
181 private void setDirty(boolean dirty
) {
183 firePropertyChange(PROP_DIRTY
);
189 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
199 public boolean isDirty() {
206 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
210 public void editorDirtyStateChanged() {
212 super.editorDirtyStateChanged();
218 * Checks whether nested editors are calling
219 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
220 * place before passing property change along to
221 * <code>super.handlePropertyChange(int propertyId)</code>.
226 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
229 protected void handlePropertyChange(int propertyId
) {
230 if (propertyId
== PROP_DIRTY
) {
233 super.handlePropertyChange(propertyId
);
238 public void doSaveAs() {
243 public boolean isSaveAsAllowed() {
249 public void init(IEditorSite site
, IEditorInput input
)
250 throws PartInitException
{
252 if (!(input
instanceof TaxonEditorInput
)) {
253 throw new PartInitException(
254 "Invalid Input: Must be TaxonEditorInput");
257 this.input
= (TaxonEditorInput
) input
;
258 cdmEntitySession
= this.input
.getCdmEntitySession();
261 // // Listen for name changes,
262 // // change tab for this taxon editor accordingly
263 // getTaxon().addPropertyChangeListener("name",
264 // new PropertyChangeListener() {
265 // public void propertyChange(PropertyChangeEvent e) {
269 // } catch (NullPointerException e) {
270 // EditorUtil.warn(getClass(),
271 // "Caught an NPE while initing an editor. This is most " +
272 // "likely due to the unsuccesful attempt to restore the former " +
273 // "state of the application. We ignore this because the workbench " +
274 // "will simply be reset.");
278 super.init(site
, input
);
282 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
283 * text appropriate to the state of the taxon: any taxon that has been saved
284 * will by necessity have a name to display; a new taxon should display
285 * "New taxon" in the editor tab.
287 protected void setPartName() {
289 String partName
= null;
290 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
293 partName
= name
.getTitleCache();
296 if (partName
== null || partName
.equals("")) {
297 partName
= ("New taxon");
300 setPartName(partName
);
306 * Editor pages call this in their postOperation to notify the
307 * MultiPageTaxonEditor of unsaved changes
310 public void changed(Object element
) {
313 super.editorDirtyStateChanged();
314 if (element
instanceof TaxonBase
) {
315 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
316 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
317 if (container
!= null) {
324 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
327 public void forceDirty() {
332 * The accepted taxon that is the input for this editor
334 * @return the accepted taxon
336 public Taxon
getTaxon() {
337 return input
.getTaxon();
344 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
349 * getConversationHolder
352 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
356 public ConversationHolder
getConversationHolder() {
362 * setConversationHolder
365 * @param conversation
366 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
369 public void setConversationHolder(ConversationHolder conversation
) {
370 this.conversation
= conversation
;
375 * Getter for the field <code>undoContext</code>.
378 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
381 public IUndoContext
getUndoContext() {
387 * Setter for the field <code>undoContext</code>.
391 * a {@link org.eclipse.core.commands.operations.IUndoContext}
394 public void setUndoContext(IUndoContext undoContext
) {
395 this.undoContext
= undoContext
;
400 public void setFocus() {
401 // logger.warn("Setting focus to editor");
402 // bind the conversation
403 getConversationHolder().bind();
404 if(cdmEntitySession
!= null) {
405 cdmEntitySession
.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.session.ICdmEntitySessionEnabled#getCdmEntitySession()
568 public ICdmEntitySession
getCdmEntitySession() {
569 return cdmEntitySession
;