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
.model
.taxon
.TaxonNode
;
33 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
34 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
35 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
36 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
37 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
38 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
39 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
40 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
41 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
42 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
43 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
44 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
45 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
46 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
47 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
51 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
52 * for "Descriptions", "Concepts", "Geography", etc.
59 public class MultiPageTaxonEditor
extends FormEditor
implements
60 IPartContentHasFactualData
, IConversationEnabled
, ICdmEntitySessionEnabled
, IPostOperationEnabled
,
61 IDirtyMarkable
, IPartContentHasDetails
, ISecuredEditor
, IPartContentHasMedia
{
63 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
64 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon";
66 private boolean dirty
;
68 private ICdmEntitySession cdmEntitySession
;
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() {
89 if(cdmEntitySession
!= null) {
90 cdmEntitySession
.dispose();
92 conversation
.unregisterForDataStoreChanges(this);
100 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
104 protected void addPages() {
105 input
= (TaxonEditorInput
) getEditorInput();
106 conversation
= input
.getConversationHolder();
107 conversation
.registerForDataStoreChanges(this);
108 cdmEntitySession
.registerForDataStoreChanges(this);
110 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
112 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
115 // addPage(Page.DESCRIPTIVE.getIndex(), new
116 // TaxonDescriptionTreeEditor(this), getEditorInput());
117 // setPageText(Page.DESCRIPTIVE.getIndex(),
118 // Page.DESCRIPTIVE.getTitle());
120 // EditorUtil.showPropertySheet();
122 } catch (PartInitException e
) {
123 MessagingUtils
.error(getClass(), e
);
129 public void doSave(IProgressMonitor monitor
) {
130 monitor
.beginTask("Saving Editor", 4);
132 if (!conversation
.isBound()) {
137 for (IEditorPart editorPage
: getPages()) {
138 if (editorPage
instanceof TaxonNameEditor
) {
139 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
140 MessageDialog
.openWarning(AbstractUtility
.getShell(), "No Name Specified",
141 "An attempt was made to save a taxon or synonym with "
142 + "an empty name. Operation was cancelled.");
147 editorPage
.doSave(monitor
);
151 // commit the conversation and start a new transaction immediately
154 conversation
.commit(true);
157 this.setDirty(false);
159 } catch (Exception e
) {
161 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,"saving a taxon", " Please close and reopen the taxon again.");
168 private void disableEditor(boolean isOnError
) {
169 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
171 editorPage
.setOnError();
173 editorPage
.setDisabled();
176 cdmEntitySession
.unregisterForDataStoreChanges(this);
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 "Invalid Input: Must be TaxonEditorInput");
258 this.input
= (TaxonEditorInput
) input
;
259 cdmEntitySession
= this.input
.getCdmEntitySession();
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("")) {
298 partName
= ("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
) {
314 super.editorDirtyStateChanged();
315 if (element
instanceof TaxonBase
) {
316 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
317 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
318 if (container
!= null) {
323 //TODO: refresh taxon node in taxon navigator
328 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
331 public void forceDirty() {
336 * The accepted taxon that is the input for this editor
338 * @return the accepted taxon
340 public Taxon
getTaxon() {
341 return input
.getTaxon();
348 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
353 * getConversationHolder
356 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
360 public ConversationHolder
getConversationHolder() {
366 * setConversationHolder
369 * @param conversation
370 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
373 public void setConversationHolder(ConversationHolder conversation
) {
374 this.conversation
= conversation
;
379 * Getter for the field <code>undoContext</code>.
382 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
385 public IUndoContext
getUndoContext() {
391 * Setter for the field <code>undoContext</code>.
395 * a {@link org.eclipse.core.commands.operations.IUndoContext}
398 public void setUndoContext(IUndoContext undoContext
) {
399 this.undoContext
= undoContext
;
404 public void setFocus() {
405 // logger.warn("Setting focus to editor");
406 // bind the conversation
407 getConversationHolder().bind();
408 if(cdmEntitySession
!= null) {
409 cdmEntitySession
.bind();
411 // pass focus to the active editor page
412 getActiveEditor().setFocus();
419 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
420 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
424 public void update(CdmDataChangeMap events
) {
425 if (dataChangeBehavior
== null) {
426 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
429 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
436 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
441 public boolean postOperation(CdmBase objectAffectedByOperation
) {
444 for (IEditorPart editor
: this.getPages()) {
445 if (editor
instanceof IPostOperationEnabled
) {
446 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
448 MessagingUtils
.warn(getClass(), "postOperation not enabled for editor " + editor
);
451 MessagingUtils
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
457 * Returns an <code>IEditorPart</code> implementation by type
461 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
464 public IMultiPageTaxonEditorPage
getPage(Page page
) {
465 for (IEditorPart editor
: this.getPages()) {
466 if (editor
.getClass().equals(page
.getClazz())) {
467 return (IMultiPageTaxonEditorPage
) editor
;
474 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
475 * <code>MultiPageTaxonEditor</code>.
477 * @return a {@link java.util.List} object.
479 public List
<IMultiPageTaxonEditorPage
> getPages() {
480 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
481 for (int i
= 0; i
< this.getPageCount(); i
++) {
483 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
489 * Refreshes a certain page of the MultipageTaxonEditor
492 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
495 public boolean redraw(Page page
) {
496 return redraw(page
, true);
500 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
504 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
509 public boolean redraw(Page page
, boolean focus
) {
510 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
511 return editorPage
!= null && editorPage
.redraw(focus
);
522 public boolean onComplete() {
527 * Reloads the data for this
529 public void reload() {
531 MessagingUtils
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
532 + "refreshed because it contains unsaved data. Refreshing "
533 + "this editor would discard the changes. Please save this editor, "
534 + "close and reopen it manually in order to get the latest content");
536 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
538 UUID uuid
= input
.getTaxonNode().getUuid();
540 conversation
.clear();
543 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
545 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
548 } catch (Exception e
) {
549 MessagingUtils
.messageDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
555 public String
toString() {
556 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
560 public boolean permissionsSatisfied() {
561 IEditorPart activeEditor
= getActiveEditor();
562 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
563 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();
569 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
572 public ICdmEntitySession
getCdmEntitySession() {
573 return cdmEntitySession
;
577 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
580 public List
<TaxonNode
> getRootEntities() {
581 return input
.getRootEntities();