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
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
35 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
36 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
37 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
38 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
43 * <p>TaxonEditorInput class.</p>
49 public class TaxonEditorInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
51 private final ConversationHolder conversation
;
53 private final TaxonNode taxonNode
;
55 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior
;
57 private TaxonBase initiallySelectedTaxonBase
;
59 private TaxonEditorInput(TaxonNode taxonNode
, ConversationHolder conversation
){
60 this.conversation
= conversation
;
61 this.taxonNode
= taxonNode
;
69 * @param taxonNodeUuid a {@link java.util.UUID} object.
70 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
71 * @throws java.lang.Exception if any.
73 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
75 ConversationHolder conversation
= CdmStore
.createConversation();
76 return NewInstance(taxonNodeUuid
, conversation
);
84 * @param taxonNodeUuid
88 private static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
, ConversationHolder conversation
){
91 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, null);
93 if(taxonNode
== null){
94 MessagingUtils
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not of type TaxonNode but [null].");
98 return new TaxonEditorInput(taxonNode
, conversation
);
102 * <p>NewInstanceFromTaxonBase</p>
104 * @param taxonBaseUuid a {@link java.util.UUID} object.
105 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
107 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
108 ConversationHolder conversation
= CdmStore
.createConversation();
110 TaxonEditorInput input
= null;
112 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).find(taxonBaseUuid
);
113 if (taxonBase
!= null){
114 if(taxonBase
.isInstanceOf(Taxon
.class)){
115 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
117 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
118 // TODO get accepted taxon
119 MessagingUtils
.info("trying to open misapplied Name ");
121 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
122 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
123 for(TaxonRelationship relation
: relations
){
124 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
125 acceptedTaxa
.add(relation
.getToTaxon());
128 input
= getInputForMultipleTaxa(conversation
, acceptedTaxa
);
131 input
= getInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
133 }else if(taxonBase
instanceof Synonym
){
134 Synonym synonym
= (Synonym
) taxonBase
;
136 Set
<Taxon
> taxa
= synonym
.getAcceptedTaxa();
137 input
= getInputForMultipleTaxa(conversation
, taxa
);
140 input
.setInitiallySelectedTaxonBase(taxonBase
);
148 private static TaxonEditorInput
getInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
149 if(taxonNodes
.size() == 1){
150 TaxonNode taxonNode
= taxonNodes
.iterator().next();
151 return NewInstance(taxonNode
.getUuid(), conversation
);
152 }else if(taxonNodes
.size() > 1){
153 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
154 if(taxonNode
!= null){
155 return NewInstance(taxonNode
.getUuid(), conversation
);
157 }else if(taxonNodes
.size() == 0){
158 // this is an undesired state
159 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not part of any classification. This should not have happened.");
164 private static TaxonEditorInput
getInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
165 if(taxa
.size() == 1){
166 Taxon taxon
= taxa
.iterator().next();
167 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
168 return getInputForMultipleNodes(conversation
, nodes
);
169 }else if(taxa
.size() > 1){
170 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
171 for ( Taxon taxon
: taxa
){
172 taxonNodes
.addAll(taxon
.getTaxonNodes());
174 return getInputForMultipleNodes(conversation
, taxonNodes
);
175 }else if(taxa
.size() == 0){
176 // this is an undesired state
177 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
178 " no accepted taxa are present. This should not have happened.");
184 * <p>NewEmptyInstance</p>
186 * @param parentNodeUuid a {@link java.util.UUID} object.
187 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
189 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
190 ConversationHolder conversation
= CdmStore
.createConversation();
192 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
193 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
195 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
196 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
198 // add the new taxon to the editors persistence context
199 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
201 return new TaxonEditorInput(newTaxonNode
, conversation
);
205 * @see org.eclipse.ui.IEditorInput#exists()
213 public boolean exists() {
214 return taxonNode
!= null;
218 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
221 * <p>getImageDescriptor</p>
223 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
226 public ImageDescriptor
getImageDescriptor() {
231 * @see org.eclipse.ui.IEditorInput#getName()
236 * @return a {@link java.lang.String} object.
239 public String
getName() {
240 if(getTaxon() == null){
243 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
244 if (name
== null || name
.getTitleCache() == null) {
247 return name
.getTitleCache();
252 * @see org.eclipse.ui.IEditorInput#getPersistable()
255 * <p>getPersistable</p>
257 * @return a {@link org.eclipse.ui.IPersistableElement} object.
260 public IPersistableElement
getPersistable() {
261 // if(CdmStore.isActive()){
262 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
263 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
264 // if (isPersistable) {
272 * @see org.eclipse.ui.IEditorInput#getToolTipText()
275 * <p>getToolTipText</p>
277 * @return a {@link java.lang.String} object.
280 public String
getToolTipText() {
285 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
289 public Object
getAdapter(Class adapter
) {
291 if (adapter
== Taxon
.class) {
292 return taxonNode
.getTaxon();
295 if (adapter
== TaxonNode
.class) {
305 * Overrides equals to ensure that a taxon can only be edited by
306 * one editor at a time.
309 public boolean equals(Object obj
) {
310 if (TaxonEditorInput
.class.equals(obj
.getClass())
311 && getTaxon() != null
312 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
313 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
314 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
326 public Taxon
getTaxon(){
327 return taxonNode
.getTaxon();
331 * <p>Getter for the field <code>taxonNode</code>.</p>
333 * @return the taxonNode
335 public TaxonNode
getTaxonNode() {
341 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
344 * <p>getConversationHolder</p>
346 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
349 public ConversationHolder
getConversationHolder() {
355 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
359 public void update(CdmDataChangeMap events
) {
360 if(dataChangeBehavior
== null){
361 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
364 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
368 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
371 * <p>getFactoryId</p>
373 * @return a {@link java.lang.String} object.
376 public String
getFactoryId() {
377 return TaxonEditorInputFactory
.getFactoryId();
381 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
385 public void saveState(IMemento memento
) {
386 TaxonEditorInputFactory
.saveState(memento
, this);
391 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
393 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
395 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
396 this.initiallySelectedTaxonBase
= taxonBase
;
400 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
402 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
404 public TaxonBase
getInitiallySelectedTaxonBase() {
405 return initiallySelectedTaxonBase
;
409 public String
toString() {
410 return String
.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());