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
.beans
.PropertyChangeEvent
;
13 import java
.beans
.PropertyChangeListener
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
19 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.ui
.IEditorInput
;
22 import org
.eclipse
.ui
.IEditorSite
;
23 import org
.eclipse
.ui
.PartInitException
;
24 import org
.eclipse
.ui
.part
.MultiPageEditorPart
;
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
.persistence
.hibernate
.CdmDataChangeMap
;
32 import eu
.etaxonomy
.taxeditor
.editor
.description
.TaxonDescriptionEditor
;
33 import eu
.etaxonomy
.taxeditor
.editor
.images
.TaxonImageEditor
;
34 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
35 import eu
.etaxonomy
.taxeditor
.store
.model
.DataChangeBridge
;
36 import eu
.etaxonomy
.taxeditor
.store
.model
.IDataChangeBehavior
;
37 import eu
.etaxonomy
.taxeditor
.store
.operations
.IPostOperationEnabled
;
41 * Generates the tabbed editor with <code>TaxonNameEditor</code> on top and tabs for
42 * "Descriptions", "Concepts", "Geography", etc.
49 public class MultiPageTaxonEditor
extends MultiPageEditorPart
implements IConversationEnabled
, IPostOperationEnabled
{
50 private static final Logger logger
= Logger
.getLogger(MultiPageTaxonEditor
.class);
52 public static final String ID
= "eu.etaxonomy.taxeditor.editor.multipagetaxonview";
55 private boolean dirty
;
57 private ConversationHolder conversation
;
58 private IDataChangeBehavior dataChangeBehavior
;
59 private IUndoContext undoContext
;
62 public MultiPageTaxonEditor() {
64 undoContext
= new UndoContext();
70 public void dispose() {
72 // EditorUtil.checkHidePropertySheet();
76 protected void createPages() {
78 TaxonEditorInput input
= (TaxonEditorInput
) getEditorInput();
79 conversation
= input
.getConversationHolder();
80 conversation
.registerForDataStoreChanges(this);
83 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this), getEditorInput());
84 setPageText(Page
.NAME
.getIndex(), Page
.NAME
.getTitle());
87 addPage(Page
.DESCRIPTIVE
.getIndex(), new TaxonDescriptionEditor(this), getEditorInput());
88 setPageText(Page
.DESCRIPTIVE
.getIndex(), Page
.DESCRIPTIVE
.getTitle());
91 addPage(Page
.IMAGE
.getIndex(), new TaxonImageEditor(this), getEditorInput());
92 setPageText(Page
.IMAGE
.getIndex(), Page
.IMAGE
.getTitle());
94 // EditorUtil.showPropertySheet();
96 } catch (PartInitException e
) {
97 logger
.error("Could not create MultiPageTaxonEditor.", e
);
102 public void doSave(IProgressMonitor monitor
) {
104 if( ! conversation
.isBound()){
108 // commit the conversation and start a new transaction immediately
109 conversation
.commit(true);
111 this.setDirty(false);
114 private void setDirty(boolean dirty
) {
116 firePropertyChange(PROP_DIRTY
);
120 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
122 public boolean isDirty() {
127 * Checks whether nested editors are calling <code>firePropertyChange(PROP_DIRTY)</code>
128 * to signal an edit has taken place before passing property change along to
129 * <code>super.handlePropertyChange(int propertyId)</code>.
132 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
134 protected void handlePropertyChange(int propertyId
) {
135 if (propertyId
== PROP_DIRTY
) {
138 super.handlePropertyChange(propertyId
);
142 public void doSaveAs() {}
145 public boolean isSaveAsAllowed() {
150 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
152 if (!(input
instanceof IEditorInput
))
153 throw new PartInitException(
154 "Invalid Input: Must be IEditorInput");
156 // Get taxon from editor input
157 if (input
.getAdapter(Taxon
.class) != null) {
158 taxon
= (Taxon
) input
.getAdapter(Taxon
.class);
164 // Listen for name changes,
165 // change tab for this taxon editor accordingly
166 taxon
.addPropertyChangeListener("name",
167 new PropertyChangeListener() {
168 public void propertyChange(PropertyChangeEvent e
) {
172 } catch (NullPointerException e
) {
173 logger
.warn("Caught an NPE while initing an editor. This is most " +
174 "likely due to the unsuccesful attempt to restore the former " +
175 "state of the application. We ignore this because the workbench " +
176 "will simply be reset.");
180 super.init(site
, input
);
184 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code>
185 * with text appropriate to the state of the taxon: any taxon that has
186 * been saved will by necessity have a name to display; a new taxon
187 * should display "New taxon" in the editor tab.
189 protected void setPartName() {
191 String partName
= null;
192 TaxonNameBase
<?
, ?
> name
= taxon
.getName();
195 partName
= name
.getTitleCache();
198 if (partName
== null || partName
.equals("")) {
199 partName
= ("New taxon");
202 setPartName(partName
);
206 * Editor pages call this in their postOperation to notify the MultiPageTaxonEditor
209 public void setDirty() {
213 public Taxon
getTaxon(){
219 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
221 public ConversationHolder
getConversationHolder() {
225 public void setConversationHolder(ConversationHolder conversation
){
226 this.conversation
= conversation
;
230 public IUndoContext
getUndoContext() {
234 public void setUndoContext(IUndoContext undoContext
) {
235 this.undoContext
= undoContext
;
239 public void setFocus(){
240 // bind the conversation
241 getConversationHolder().bind();
242 // pass focus to the active editor page
243 getActiveEditorPage().setFocus();
246 public AbstractTaxonEditor
getActiveEditorPage(){
247 return (AbstractTaxonEditor
) getEditor(getActivePage());
252 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
254 public void update(CdmDataChangeMap events
) {
255 if(dataChangeBehavior
== null){
256 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
259 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
265 * @see eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation()
267 public boolean postOperation(CdmBase objectAffectedByOperation
) {
270 for(AbstractTaxonEditor editor
: this.getPages()){
271 editor
.postOperation(objectAffectedByOperation
);
273 logger
.warn("postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
279 * Returns an <code>AbstractTaxonEditor</code> implementation by type
281 * @param page the page type
284 public AbstractTaxonEditor
getPage(Page page
){
285 for(AbstractTaxonEditor editor
: this.getPages()){
286 if(editor
.getClass().equals(page
.getClazz())){
294 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
295 * <code>MultiPageTaxonEditor</code>.
299 public List
<AbstractTaxonEditor
> getPages(){
300 ArrayList
<AbstractTaxonEditor
> editors
= new ArrayList
<AbstractTaxonEditor
>();
301 for(int i
= 0; i
< this.getPageCount(); i
++){
302 editors
.add((AbstractTaxonEditor
) this.getEditor(i
));