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
);
142 private static TaxonEditorInput
getInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
143 if(taxonNodes
.size() == 1){
144 TaxonNode taxonNode
= taxonNodes
.iterator().next();
145 return NewInstance(taxonNode
.getUuid(), conversation
);
146 }else if(taxonNodes
.size() > 1){
147 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
148 if(taxonNode
!= null){
149 return NewInstance(taxonNode
.getUuid(), conversation
);
151 }else if(taxonNodes
.size() == 0){
152 // this is an undesired state
153 EditorUtil
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not part of any classification. This should not have happened.");
158 private static TaxonEditorInput
getInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
159 if(taxa
.size() == 1){
160 Taxon taxon
= taxa
.iterator().next();
161 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
162 return getInputForMultipleNodes(conversation
, nodes
);
163 }else if(taxa
.size() > 1){
164 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
165 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." +
166 " This case is not handled yet by the software.");
167 }else if(taxa
.size() == 0){
168 // this is an undesired state
169 EditorUtil
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
170 " no accepted taxa are present. This should not have happened.");
176 * <p>NewEmptyInstance</p>
178 * @param parentNodeUuid a {@link java.util.UUID} object.
179 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
181 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
182 ConversationHolder conversation
= CdmStore
.createConversation();
184 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
185 ITreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
187 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
188 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference(), null);
190 // add the new taxon to the editors persistence context
191 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
);
193 return new TaxonEditorInput(newTaxonNode
, conversation
);
197 * @see org.eclipse.ui.IEditorInput#exists()
204 public boolean exists() {
205 return taxonNode
!= null;
209 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
212 * <p>getImageDescriptor</p>
214 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
216 public ImageDescriptor
getImageDescriptor() {
221 * @see org.eclipse.ui.IEditorInput#getName()
226 * @return a {@link java.lang.String} object.
228 public String
getName() {
229 if(getTaxon() == null){
232 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
233 if (name
== null || name
.getTitleCache() == null) {
236 return name
.getTitleCache();
241 * @see org.eclipse.ui.IEditorInput#getPersistable()
244 * <p>getPersistable</p>
246 * @return a {@link org.eclipse.ui.IPersistableElement} object.
248 public IPersistableElement
getPersistable() {
249 // if(CdmStore.isActive()){
250 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
251 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
252 // if (isPersistable) {
260 * @see org.eclipse.ui.IEditorInput#getToolTipText()
263 * <p>getToolTipText</p>
265 * @return a {@link java.lang.String} object.
267 public String
getToolTipText() {
272 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
275 public Object
getAdapter(Class adapter
) {
277 if (adapter
== Taxon
.class) {
278 return taxonNode
.getTaxon();
281 if (adapter
== TaxonNode
.class) {
291 * Overrides equals to ensure that a taxon can only be edited by
292 * one editor at a time.
294 public boolean equals(Object obj
) {
295 if (TaxonEditorInput
.class.equals(obj
.getClass())
296 && getTaxon() != null
297 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
298 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
299 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
311 public Taxon
getTaxon(){
312 return taxonNode
.getTaxon();
316 * <p>Getter for the field <code>taxonNode</code>.</p>
318 * @return the taxonNode
320 public TaxonNode
getTaxonNode() {
326 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
329 * <p>getConversationHolder</p>
331 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
333 public ConversationHolder
getConversationHolder() {
339 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
342 public void update(CdmDataChangeMap events
) {
343 if(dataChangeBehavior
== null){
344 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
347 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
351 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
354 * <p>getFactoryId</p>
356 * @return a {@link java.lang.String} object.
358 public String
getFactoryId() {
359 return TaxonEditorInputFactory
.getFactoryId();
363 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
366 public void saveState(IMemento memento
) {
367 TaxonEditorInputFactory
.saveState(memento
, this);
372 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
374 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
376 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
377 this.initiallySelectedTaxonBase
= taxonBase
;
381 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
383 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
385 public TaxonBase
getInitiallySelectedTaxonBase() {
386 return initiallySelectedTaxonBase
;