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
.name
.TaxonNameEditor
;
34 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
35 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
36 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
37 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkableSelectionProvider
;
38 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
39 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
43 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs
44 * for "Descriptions", "Concepts", "Geography", etc.
51 public class MultiPageTaxonEditor
extends FormEditor
implements
52 IConversationEnabled
, IPostOperationEnabled
,
53 IDirtyMarkableSelectionProvider
, IPartContentHasDetails
, ISecuredEditor
{
55 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon"{trunked}</code> */
56 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon";
58 private boolean dirty
;
60 private ConversationHolder conversation
;
61 private IDataChangeBehavior dataChangeBehavior
;
62 private IUndoContext undoContext
;
64 private TaxonEditorInput input
;
68 * Constructor for MultiPageTaxonEditor.
71 public MultiPageTaxonEditor() {
73 undoContext
= new UndoContext();
78 public void dispose() {
79 conversation
.unregisterForDataStoreChanges(this);
87 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
91 protected void addPages() {
92 input
= (TaxonEditorInput
) getEditorInput();
93 conversation
= input
.getConversationHolder();
94 conversation
.registerForDataStoreChanges(this);
97 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this),
99 // setPageText(Page.NAME.getIndex(), Page.NAME.getTitle());
102 // addPage(Page.DESCRIPTIVE.getIndex(), new
103 // TaxonDescriptionTreeEditor(this), getEditorInput());
104 // setPageText(Page.DESCRIPTIVE.getIndex(),
105 // Page.DESCRIPTIVE.getTitle());
107 // EditorUtil.showPropertySheet();
109 } catch (PartInitException e
) {
110 EditorUtil
.error(getClass(), e
);
116 public void doSave(IProgressMonitor monitor
) {
117 monitor
.beginTask("Saving Editor", 4);
119 if (!conversation
.isBound()) {
124 for (IEditorPart editorPage
: getPages()) {
125 if (editorPage
instanceof TaxonNameEditor
) {
126 if (((TaxonNameEditor
) editorPage
).checkForEmptyNames()) {
129 EditorUtil
.getShell(),
131 "An attempt was made to save a taxon or synonym with "
132 + "an empty name. Operation was cancelled.");
137 editorPage
.doSave(monitor
);
141 // commit the conversation and start a new transaction immediately
142 conversation
.commit(true);
145 this.setDirty(false);
147 } catch (Exception e
) {
151 "An error occurred while saving",
153 "An error occurred while saving the editor. Please close and reopen the taxon again.",
161 private void disableEditor(boolean isOnError
) {
162 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
164 editorPage
.setOnError();
166 editorPage
.setDisabled();
169 conversation
.unregisterForDataStoreChanges(this);
170 conversation
.close();
174 private void setDirty(boolean dirty
) {
176 firePropertyChange(PROP_DIRTY
);
182 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
191 public boolean isDirty() {
198 * @see org.eclipse.ui.forms.editor.FormEditor#editorDirtyStateChanged()
202 public void editorDirtyStateChanged() {
204 super.editorDirtyStateChanged();
210 * Checks whether nested editors are calling
211 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
212 * place before passing property change along to
213 * <code>super.handlePropertyChange(int propertyId)</code>.
218 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
220 protected void handlePropertyChange(int propertyId
) {
221 if (propertyId
== PROP_DIRTY
) {
224 super.handlePropertyChange(propertyId
);
229 public void doSaveAs() {
234 public boolean isSaveAsAllowed() {
240 public void init(IEditorSite site
, IEditorInput input
)
241 throws PartInitException
{
243 if (!(input
instanceof TaxonEditorInput
))
244 throw new PartInitException(
245 "Invalid Input: Must be TaxonEditorInput");
247 this.input
= (TaxonEditorInput
) input
;
250 // // Listen for name changes,
251 // // change tab for this taxon editor accordingly
252 // getTaxon().addPropertyChangeListener("name",
253 // new PropertyChangeListener() {
254 // public void propertyChange(PropertyChangeEvent e) {
258 // } catch (NullPointerException e) {
259 // EditorUtil.warn(getClass(),
260 // "Caught an NPE while initing an editor. This is most " +
261 // "likely due to the unsuccesful attempt to restore the former " +
262 // "state of the application. We ignore this because the workbench " +
263 // "will simply be reset.");
267 super.init(site
, input
);
271 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
272 * text appropriate to the state of the taxon: any taxon that has been saved
273 * will by necessity have a name to display; a new taxon should display
274 * "New taxon" in the editor tab.
276 protected void setPartName() {
278 String partName
= null;
279 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
282 partName
= name
.getTitleCache();
285 if (partName
== null || partName
.equals("")) {
286 partName
= ("New taxon");
289 setPartName(partName
);
295 * Editor pages call this in their postOperation to notify the
296 * MultiPageTaxonEditor of unsaved changes
298 public void changed(Object element
) {
301 super.editorDirtyStateChanged();
302 if (element
instanceof TaxonBase
) {
303 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
304 AbstractGroupedContainer container
= page
305 .getContainer((TaxonBase
) element
);
306 if (container
!= null) {
313 * The accepted taxon that is the input for this editor
315 * @return the accepted taxon
317 public Taxon
getTaxon() {
318 return input
.getTaxon();
325 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
330 * getConversationHolder
333 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
336 public ConversationHolder
getConversationHolder() {
342 * setConversationHolder
345 * @param conversation
346 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
349 public void setConversationHolder(ConversationHolder conversation
) {
350 this.conversation
= conversation
;
355 * Getter for the field <code>undoContext</code>.
358 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
361 public IUndoContext
getUndoContext() {
367 * Setter for the field <code>undoContext</code>.
371 * a {@link org.eclipse.core.commands.operations.IUndoContext}
374 public void setUndoContext(IUndoContext undoContext
) {
375 this.undoContext
= undoContext
;
380 public void setFocus() {
381 // logger.warn("Setting focus to editor");
382 // bind the conversation
383 getConversationHolder().bind();
384 // pass focus to the active editor page
385 getActiveEditor().setFocus();
392 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
393 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
396 public void update(CdmDataChangeMap events
) {
397 if (dataChangeBehavior
== null) {
398 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(
402 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
409 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
413 public boolean postOperation(CdmBase objectAffectedByOperation
) {
416 for (IEditorPart editor
: this.getPages()) {
417 if (editor
instanceof IPostOperationEnabled
) {
418 ((IPostOperationEnabled
) editor
)
419 .postOperation(objectAffectedByOperation
);
421 EditorUtil
.warn(getClass(),
422 "postOperation not enabled for editor " + editor
);
427 "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
433 * Returns an <code>IEditorPart</code> implementation by type
437 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
440 public IMultiPageTaxonEditorPage
getPage(Page page
) {
441 for (IEditorPart editor
: this.getPages()) {
442 if (editor
.getClass().equals(page
.getClazz())) {
443 return (IMultiPageTaxonEditorPage
) editor
;
450 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
451 * <code>MultiPageTaxonEditor</code>.
453 * @return a {@link java.util.List} object.
455 public List
<IMultiPageTaxonEditorPage
> getPages() {
456 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
457 for (int i
= 0; i
< this.getPageCount(); i
++) {
459 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
465 * Refreshes a certain page of the MultipageTaxonEditor
468 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
471 public boolean redraw(Page page
) {
472 return redraw(page
, true);
476 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
480 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
485 public boolean redraw(Page page
, boolean focus
) {
486 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
487 return editorPage
!= null && editorPage
.redraw(focus
);
497 public boolean onComplete() {
502 * Reloads the data for this
504 public void reload() {
508 "Editor has unsaved data",
510 "This editor can not be "
511 + "refreshed because it contains unsaved data. Refreshing "
512 + "this editor would discard the changes. Please save this editor, "
513 + "close and reopen it manually in order to get the latest content");
515 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
517 UUID uuid
= input
.getTaxonNode().getUuid();
519 conversation
.clear();
522 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
524 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
527 } catch (Exception e
) {
528 EditorUtil
.errorDialog("Error refreshing editor", getClass(),
529 "Could not refresh this editor", e
);
535 public String
toString() {
536 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
540 public boolean permissionsSatisfied() {
541 IEditorPart activeEditor
= getActiveEditor();
542 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
543 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();