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();
83 public void dispose() {
84 conversation
.unregisterForDataStoreChanges(this);
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 MessagingUtils
.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()) {
127 MessageDialog
.openWarning(AbstractUtility
.getShell(), "No Name Specified",
128 "An attempt was made to save a taxon or synonym with "
129 + "an empty name. Operation was cancelled.");
134 editorPage
.doSave(monitor
);
138 // commit the conversation and start a new transaction immediately
139 conversation
.commit(true);
142 this.setDirty(false);
144 } catch (Exception e
) {
146 MessagingUtils
.operationDialog(this, e
, TaxeditorEditorPlugin
.PLUGIN_ID
,"saving a taxon", " Please close and reopen the taxon again.");
153 private void disableEditor(boolean isOnError
) {
154 for (IMultiPageTaxonEditorPage editorPage
: getPages()) {
156 editorPage
.setOnError();
158 editorPage
.setDisabled();
161 conversation
.unregisterForDataStoreChanges(this);
162 conversation
.close();
166 private void setDirty(boolean dirty
) {
168 firePropertyChange(PROP_DIRTY
);
172 public boolean isDirty() {
178 public void editorDirtyStateChanged() {
180 super.editorDirtyStateChanged();
186 * Checks whether nested editors are calling
187 * <code>firePropertyChange(PROP_DIRTY)</code> to signal an edit has taken
188 * place before passing property change along to
189 * <code>super.handlePropertyChange(int propertyId)</code>.
192 protected void handlePropertyChange(int propertyId
) {
193 if (propertyId
== PROP_DIRTY
) {
196 super.handlePropertyChange(propertyId
);
201 public void doSaveAs() {
206 public boolean isSaveAsAllowed() {
212 public void init(IEditorSite site
, IEditorInput input
)
213 throws PartInitException
{
215 if (!(input
instanceof TaxonEditorInput
)) {
216 throw new PartInitException(
217 "Invalid Input: Must be TaxonEditorInput");
220 this.input
= (TaxonEditorInput
) input
;
223 // // Listen for name changes,
224 // // change tab for this taxon editor accordingly
225 // getTaxon().addPropertyChangeListener("name",
226 // new PropertyChangeListener() {
227 // public void propertyChange(PropertyChangeEvent e) {
231 // } catch (NullPointerException e) {
232 // EditorUtil.warn(getClass(),
233 // "Caught an NPE while initing an editor. This is most " +
234 // "likely due to the unsuccesful attempt to restore the former " +
235 // "state of the application. We ignore this because the workbench " +
236 // "will simply be reset.");
240 super.init(site
, input
);
244 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code> with
245 * text appropriate to the state of the taxon: any taxon that has been saved
246 * will by necessity have a name to display; a new taxon should display
247 * "New taxon" in the editor tab.
249 protected void setPartName() {
251 String partName
= null;
252 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
255 partName
= name
.getTitleCache();
258 if (partName
== null || partName
.equals("")) {
259 partName
= ("New taxon");
262 setPartName(partName
);
268 * Editor pages call this in their postOperation to notify the
269 * MultiPageTaxonEditor of unsaved changes
272 public void changed(Object element
) {
275 super.editorDirtyStateChanged();
276 if (element
instanceof TaxonBase
) {
277 TaxonNameEditor page
= (TaxonNameEditor
) getPage(Page
.NAME
);
278 AbstractGroupedContainer container
= page
.getContainer((TaxonBase
) element
);
279 if (container
!= null) {
284 //TODO: refresh taxon node in taxon navigator
289 public void forceDirty() {
294 * The accepted taxon that is the input for this editor
296 * @return the accepted taxon
298 public Taxon
getTaxon() {
299 return input
.getTaxon();
303 public ConversationHolder
getConversationHolder() {
307 public void setConversationHolder(ConversationHolder conversation
) {
308 this.conversation
= conversation
;
311 public IUndoContext
getUndoContext() {
315 public void setUndoContext(IUndoContext undoContext
) {
316 this.undoContext
= undoContext
;
321 public void setFocus() {
322 // logger.warn("Setting focus to editor");
323 // bind the conversation
324 getConversationHolder().bind();
325 // pass focus to the active editor page
326 getActiveEditor().setFocus();
331 public void update(CdmDataChangeMap events
) {
332 if (dataChangeBehavior
== null) {
333 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
336 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
341 public boolean postOperation(CdmBase objectAffectedByOperation
) {
344 for (IEditorPart editor
: this.getPages()) {
345 if (editor
instanceof IPostOperationEnabled
) {
346 ((IPostOperationEnabled
) editor
).postOperation(objectAffectedByOperation
);
348 MessagingUtils
.warn(getClass(), "postOperation not enabled for editor " + editor
);
351 MessagingUtils
.warn(getClass(), "postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
357 * Returns an <code>IEditorPart</code> implementation by type
361 * @return a {@link eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage}
364 public IMultiPageTaxonEditorPage
getPage(Page page
) {
365 for (IEditorPart editor
: this.getPages()) {
366 if (editor
.getClass().equals(page
.getClazz())) {
367 return (IMultiPageTaxonEditorPage
) editor
;
374 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
375 * <code>MultiPageTaxonEditor</code>.
377 * @return a {@link java.util.List} object.
379 public List
<IMultiPageTaxonEditorPage
> getPages() {
380 ArrayList
<IMultiPageTaxonEditorPage
> editors
= new ArrayList
<IMultiPageTaxonEditorPage
>();
381 for (int i
= 0; i
< this.getPageCount(); i
++) {
383 editors
.add((IMultiPageTaxonEditorPage
) this.getEditor(i
));
389 * Refreshes a certain page of the MultipageTaxonEditor
392 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
395 public boolean redraw(Page page
) {
396 return redraw(page
, true);
400 * Refreshes a certain page of the MultipageTaxonEditor and sets focus to
404 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
409 public boolean redraw(Page page
, boolean focus
) {
410 IMultiPageTaxonEditorPage editorPage
= getPage(page
);
411 return editorPage
!= null && editorPage
.redraw(focus
);
415 public boolean onComplete() {
420 * Reloads the data for this
422 public void reload() {
424 MessagingUtils
.warningDialog("Editor has unsaved data", getClass(), "This editor can not be "
425 + "refreshed because it contains unsaved data. Refreshing "
426 + "this editor would discard the changes. Please save this editor, "
427 + "close and reopen it manually in order to get the latest content");
429 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
431 UUID uuid
= input
.getTaxonNode().getUuid();
433 conversation
.clear();
436 TaxonEditorInput newInput
= TaxonEditorInput
.NewInstance(uuid
);
438 for (IMultiPageTaxonEditorPage editorPart
: getPages()) {
441 } catch (Exception e
) {
442 MessagingUtils
.messageDialog("Error refreshing editor", getClass(), "Could not refresh this editor", e
);
448 public String
toString() {
449 return String
.format("%s[%s]", this.getClass().getSimpleName(), getEditorInput());
453 public boolean permissionsSatisfied() {
454 IEditorPart activeEditor
= getActiveEditor();
455 if(activeEditor
!= null && ISecuredEditor
.class.isAssignableFrom(activeEditor
.getClass())){
456 return ((ISecuredEditor
)activeEditor
).permissionsSatisfied();
462 public boolean canAttachMedia() {