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
.UUID
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
16 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
17 import org
.eclipse
.ui
.IEditorInput
;
18 import org
.eclipse
.ui
.IMemento
;
19 import org
.eclipse
.ui
.IPersistableElement
;
21 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
23 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.ITreeNode
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
28 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
29 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
30 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 public class TaxonEditorInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
38 private static final Logger logger
= Logger
39 .getLogger(TaxonEditorInput
.class);
41 private ConversationHolder conversation
;
43 private TaxonNode taxonNode
;
45 private TaxonEditorInput(TaxonNode taxonNode
, ConversationHolder conversation
){
46 this.taxonNode
= taxonNode
;
47 this.conversation
= conversation
;
52 * @param taxonNodeUuid
56 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
){
57 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
59 TaxonNode taxonNode
= CdmStore
.getTaxonTreeService().getTaxonNodeByUuid(taxonNodeUuid
);
61 if(taxonNode
== null){
62 MessageDialog
.openWarning(EditorUtil
.getShell(), "Not yet implemented", "Selected element is not type TaxonBase.");
66 // FIXME had to disable synonym handling for now while implementing taxonNode
67 // if (taxonBase instanceof Synonym) {
68 // // TODO: in case of pro parte synonym or any other where we might have multiple
69 // // accepted taxa we have to provide a mechanism that can deal with that
70 // // TODO set focus to the synonym
71 // Set<Taxon> acceptedTaxa = ((Synonym) taxonBase).getAcceptedTaxa();
72 // if(acceptedTaxa.size() != 1){
74 // if(acceptedTaxa.size() == 0){
75 // message = "There is no accepted taxon for the chosen synonym. ";
77 // message = "Multiple taxa found for the chosen synonym.";
79 // MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", message);
82 // taxon = acceptedTaxa.toArray(new Taxon[0])[0];
85 // taxon = (Taxon) taxonBase;
89 logger
.trace(TaxonEditorInput
.class.getSimpleName() + " created");
91 return new TaxonEditorInput(taxonNode
, conversation
);
94 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
95 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
97 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
98 ITreeNode parentNode
= CdmStore
.getTaxonTreeService().getTreeNodeByUuid(parentNodeUuid
);
100 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
101 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference(), null);
103 // add the new taxon to the editors persistence context
104 CdmStore
.getTaxonTreeService().saveTaxonNode(newTaxonNode
);
105 // we pass on the conversation, will be closed when the editor is closed
106 return new TaxonEditorInput(newTaxonNode
, conversation
);
110 * @see org.eclipse.ui.IEditorInput#exists()
112 public boolean exists() {
113 return taxonNode
!= null;
117 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
119 public ImageDescriptor
getImageDescriptor() {
124 * @see org.eclipse.ui.IEditorInput#getName()
126 public String
getName() {
127 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
128 if (name
== null || name
.getTitleCache() == null) {
131 return name
.getTitleCache();
136 * @see org.eclipse.ui.IEditorInput#getPersistable()
138 public IPersistableElement
getPersistable() {
139 ConversationHolder internalConversation
= null;
140 if(conversation
.isClosed())
141 internalConversation
= CdmStore
.NewTransactionalConversation();
143 boolean isPersistable
= CdmStore
.getTaxonTreeService().getTaxonNodeByUuid(taxonNode
.getUuid()) != null;
150 if(internalConversation
!= null)
151 internalConversation
.close();
156 * @see org.eclipse.ui.IEditorInput#getToolTipText()
158 public String
getToolTipText() {
163 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
165 public Object
getAdapter(Class adapter
) {
167 if (adapter
== Taxon
.class) {
168 return taxonNode
.getTaxon();
171 if (adapter
== TaxonNode
.class) {
179 * Overrides equals to ensure that a taxon can only be edited by
180 * one editor at a time.
184 public boolean equals(Object obj
) {
185 if (obj
.getClass().equals(TaxonEditorInput
.class)
186 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon()))
194 public Taxon
getTaxon(){
195 return taxonNode
.getTaxon();
199 * @return the taxonNode
201 public TaxonNode
getTaxonNode() {
207 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
209 public ConversationHolder
getConversationHolder() {
215 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
217 public void update(CdmDataChangeMap events
) {
218 // FIXME update the editor input
222 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
224 public String
getFactoryId() {
225 return TaxonEditorInputFactory
.getFactoryId();
229 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
231 public void saveState(IMemento memento
) {
232 TaxonEditorInputFactory
.saveState(memento
, this);