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
.HashSet
;
14 import java
.util
.UUID
;
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
.api
.service
.IClassificationService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
26 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.ITreeNode
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
34 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
35 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
36 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
37 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
40 * <p>TaxonEditorInput class.</p>
46 public class TaxonEditorInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
48 private ConversationHolder conversation
;
50 private TaxonNode taxonNode
;
52 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior
;
54 private TaxonBase initiallySelectedTaxonBase
;
56 private TaxonEditorInput(TaxonNode taxonNode
, ConversationHolder conversation
){
57 this.conversation
= conversation
;
58 this.taxonNode
= taxonNode
;
66 * @param taxonNodeUuid a {@link java.util.UUID} object.
67 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
68 * @throws java.lang.Exception if any.
70 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
72 ConversationHolder conversation
= CdmStore
.createConversation();
73 return NewInstance(taxonNodeUuid
, conversation
);
81 * @param taxonNodeUuid
85 private static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
, ConversationHolder conversation
){
88 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, null);
90 if(taxonNode
== null){
91 EditorUtil
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not type TaxonBase.");
95 return new TaxonEditorInput(taxonNode
, conversation
);
99 * <p>NewInstanceFromTaxonBase</p>
101 * @param taxonBaseUuid a {@link java.util.UUID} object.
102 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
104 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
105 ConversationHolder conversation
= CdmStore
.createConversation();
107 TaxonEditorInput input
= null;
109 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).find(taxonBaseUuid
);
111 if(taxonBase
instanceof Taxon
){
112 Taxon taxon
= (Taxon
) taxonBase
;
114 if (taxon
.isMisapplication()){
115 // TODO get accepted taxon
116 EditorUtil
.info("trying to open Mispplied Name ");
118 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
119 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
120 for(TaxonRelationship relation
: relations
){
121 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
122 acceptedTaxa
.add(relation
.getToTaxon());
125 input
= getInputForMultipleTaxa(conversation
, acceptedTaxa
);
128 input
= getInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
130 }else if(taxonBase
instanceof Synonym
){
131 Synonym synonym
= (Synonym
) taxonBase
;
133 Set
<Taxon
> taxa
= synonym
.getAcceptedTaxa();
134 input
= getInputForMultipleTaxa(conversation
, taxa
);
137 input
.setInitiallySelectedTaxonBase(taxonBase
);
145 private static TaxonEditorInput
getInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
146 if(taxonNodes
.size() == 1){
147 TaxonNode taxonNode
= taxonNodes
.iterator().next();
148 return NewInstance(taxonNode
.getUuid(), conversation
);
149 }else if(taxonNodes
.size() > 1){
150 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
151 EditorUtil
.warningDialog("Not implemented yet", TaxonEditorInput
.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
152 " This case is not handled yet by the software.");
153 }else if(taxonNodes
.size() == 0){
154 // this is an undesired state
155 EditorUtil
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
160 private static TaxonEditorInput
getInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
161 if(taxa
.size() == 1){
162 Taxon taxon
= taxa
.iterator().next();
163 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
164 return getInputForMultipleNodes(conversation
, nodes
);
165 }else if(taxa
.size() > 1){
166 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
167 EditorUtil
.warningDialog("Not implemented yet", TaxonEditorInput
.class, "There are multiple accepted taxa for the current selection. We currently do not know which one you want to open." +
168 " This case is not handled yet by the software.");
169 }else if(taxa
.size() == 0){
170 // this is an undesired state
171 EditorUtil
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
172 " no accepted taxa are present. This should not have happened.");
178 * <p>NewEmptyInstance</p>
180 * @param parentNodeUuid a {@link java.util.UUID} object.
181 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
183 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
184 ConversationHolder conversation
= CdmStore
.createConversation();
186 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
187 ITreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
189 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
190 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference(), null);
192 // add the new taxon to the editors persistence context
193 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
);
195 return new TaxonEditorInput(newTaxonNode
, conversation
);
199 * @see org.eclipse.ui.IEditorInput#exists()
206 public boolean exists() {
207 return taxonNode
!= null;
211 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
214 * <p>getImageDescriptor</p>
216 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
218 public ImageDescriptor
getImageDescriptor() {
223 * @see org.eclipse.ui.IEditorInput#getName()
228 * @return a {@link java.lang.String} object.
230 public String
getName() {
231 if(getTaxon() == null){
234 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
235 if (name
== null || name
.getTitleCache() == null) {
238 return name
.getTitleCache();
243 * @see org.eclipse.ui.IEditorInput#getPersistable()
246 * <p>getPersistable</p>
248 * @return a {@link org.eclipse.ui.IPersistableElement} object.
250 public IPersistableElement
getPersistable() {
251 // if(CdmStore.isActive()){
252 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
253 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
254 // if (isPersistable) {
262 * @see org.eclipse.ui.IEditorInput#getToolTipText()
265 * <p>getToolTipText</p>
267 * @return a {@link java.lang.String} object.
269 public String
getToolTipText() {
274 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
277 public Object
getAdapter(Class adapter
) {
279 if (adapter
== Taxon
.class) {
280 return taxonNode
.getTaxon();
283 if (adapter
== TaxonNode
.class) {
293 * Overrides equals to ensure that a taxon can only be edited by
294 * one editor at a time.
296 public boolean equals(Object obj
) {
297 if (TaxonEditorInput
.class.equals(obj
.getClass())
298 && getTaxon() != null
299 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
300 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
301 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
313 public Taxon
getTaxon(){
314 return taxonNode
.getTaxon();
318 * <p>Getter for the field <code>taxonNode</code>.</p>
320 * @return the taxonNode
322 public TaxonNode
getTaxonNode() {
328 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
331 * <p>getConversationHolder</p>
333 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
335 public ConversationHolder
getConversationHolder() {
341 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
344 public void update(CdmDataChangeMap events
) {
345 if(dataChangeBehavior
== null){
346 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
349 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
353 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
356 * <p>getFactoryId</p>
358 * @return a {@link java.lang.String} object.
360 public String
getFactoryId() {
361 return TaxonEditorInputFactory
.getFactoryId();
365 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
368 public void saveState(IMemento memento
) {
369 TaxonEditorInputFactory
.saveState(memento
, this);
374 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
376 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
378 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
379 this.initiallySelectedTaxonBase
= taxonBase
;
383 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
385 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
387 public TaxonBase
getInitiallySelectedTaxonBase() {
388 return initiallySelectedTaxonBase
;