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
.model
.DataChangeBridge
;
36 import eu
.etaxonomy
.taxeditor
.model
.IDataChangeBehavior
;
37 import eu
.etaxonomy
.taxeditor
.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";
54 private boolean dirty
;
56 private ConversationHolder conversation
;
57 private IDataChangeBehavior dataChangeBehavior
;
58 private IUndoContext undoContext
;
60 private TaxonEditorInput input
;
63 public MultiPageTaxonEditor() {
65 undoContext
= new UndoContext();
71 public void dispose() {
73 // EditorUtil.checkHidePropertySheet();
77 protected void createPages() {
79 input
= (TaxonEditorInput
) getEditorInput();
80 conversation
= input
.getConversationHolder();
81 conversation
.registerForDataStoreChanges(this);
84 addPage(Page
.NAME
.getIndex(), new TaxonNameEditor(this), getEditorInput());
85 setPageText(Page
.NAME
.getIndex(), Page
.NAME
.getTitle());
88 addPage(Page
.DESCRIPTIVE
.getIndex(), new TaxonDescriptionEditor(this), getEditorInput());
89 setPageText(Page
.DESCRIPTIVE
.getIndex(), Page
.DESCRIPTIVE
.getTitle());
92 addPage(Page
.IMAGE
.getIndex(), new TaxonImageEditor(this), getEditorInput());
93 setPageText(Page
.IMAGE
.getIndex(), Page
.IMAGE
.getTitle());
95 EditorUtil
.showPropertySheet();
97 } catch (PartInitException e
) {
98 logger
.error("Could not create MultiPageTaxonEditor.", e
);
103 public void doSave(IProgressMonitor monitor
) {
105 if( ! conversation
.isBound()){
109 // commit the conversation and start a new transaction immediately
110 conversation
.commit(true);
112 this.setDirty(false);
115 private void setDirty(boolean dirty
) {
117 firePropertyChange(PROP_DIRTY
);
121 * @see org.eclipse.ui.part.MultiPageEditorPart#isDirty()
123 public boolean isDirty() {
128 * Checks whether nested editors are calling <code>firePropertyChange(PROP_DIRTY)</code>
129 * to signal an edit has taken place before passing property change along to
130 * <code>super.handlePropertyChange(int propertyId)</code>.
133 * @see org.eclipse.ui.part.MultiPageEditorPart#handlePropertyChange(int)
135 protected void handlePropertyChange(int propertyId
) {
136 if (propertyId
== PROP_DIRTY
) {
139 super.handlePropertyChange(propertyId
);
143 public void doSaveAs() {}
146 public boolean isSaveAsAllowed() {
151 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
153 if (!(input
instanceof TaxonEditorInput
))
154 throw new PartInitException(
155 "Invalid Input: Must be TaxonEditorInput");
157 // FIXME looks like we do it differently now
158 // Get taxon from editor input
159 // if (input.getAdapter(Taxon.class) != null) {
160 // taxon = (Taxon) input.getAdapter(Taxon.class);
165 this.input
= (TaxonEditorInput
) input
;
168 // Listen for name changes,
169 // change tab for this taxon editor accordingly
170 getTaxon().addPropertyChangeListener("name",
171 new PropertyChangeListener() {
172 public void propertyChange(PropertyChangeEvent e
) {
176 } catch (NullPointerException e
) {
177 logger
.warn("Caught an NPE while initing an editor. This is most " +
178 "likely due to the unsuccesful attempt to restore the former " +
179 "state of the application. We ignore this because the workbench " +
180 "will simply be reset.");
184 super.init(site
, input
);
188 * Calls <code>MultiPageEditorPart.setPartName(String partName)</code>
189 * with text appropriate to the state of the taxon: any taxon that has
190 * been saved will by necessity have a name to display; a new taxon
191 * should display "New taxon" in the editor tab.
193 protected void setPartName() {
195 String partName
= null;
196 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
199 partName
= name
.getTitleCache();
202 if (partName
== null || partName
.equals("")) {
203 partName
= ("New taxon");
206 setPartName(partName
);
210 * Editor pages call this in their postOperation to notify the MultiPageTaxonEditor
213 public void setDirty() {
217 public Taxon
getTaxon(){
218 return input
.getTaxon();
223 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
225 public ConversationHolder
getConversationHolder() {
229 public void setConversationHolder(ConversationHolder conversation
){
230 this.conversation
= conversation
;
234 public IUndoContext
getUndoContext() {
238 public void setUndoContext(IUndoContext undoContext
) {
239 this.undoContext
= undoContext
;
243 public void setFocus(){
244 // bind the conversation
245 getConversationHolder().bind();
246 // pass focus to the active editor page
247 getActiveEditorPage().setFocus();
250 public AbstractTaxonEditor
getActiveEditorPage(){
251 return (AbstractTaxonEditor
) getEditor(getActivePage());
256 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
258 public void update(CdmDataChangeMap events
) {
259 if(dataChangeBehavior
== null){
260 dataChangeBehavior
= new MultiPageTaxonEditorDataChangeBehaviour(this);
263 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
269 * @see eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation()
271 public boolean postOperation(CdmBase objectAffectedByOperation
) {
274 for(AbstractTaxonEditor editor
: this.getPages()){
275 editor
.postOperation(objectAffectedByOperation
);
277 logger
.warn("postOperation called on MultiPageTaxonEditor. Can you make it more specific?");
283 * Returns an <code>AbstractTaxonEditor</code> implementation by type
285 * @param page the page type
288 public AbstractTaxonEditor
getPage(Page page
){
289 for(AbstractTaxonEditor editor
: this.getPages()){
290 if(editor
.getClass().equals(page
.getClazz())){
298 * Return a list of <code>AbstractTaxonEditor</code>s registered with this
299 * <code>MultiPageTaxonEditor</code>.
303 public List
<AbstractTaxonEditor
> getPages(){
304 ArrayList
<AbstractTaxonEditor
> editors
= new ArrayList
<AbstractTaxonEditor
>();
305 for(int i
= 0; i
< this.getPageCount(); i
++){
306 editors
.add((AbstractTaxonEditor
) this.getEditor(i
));