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
.preference
.PreferencesUtil
;
38 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
42 * <p>TaxonEditorInput class.</p>
48 public class TaxonEditorInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
50 private ConversationHolder conversation
;
52 private TaxonNode taxonNode
;
54 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior
;
56 private TaxonBase initiallySelectedTaxonBase
;
58 private TaxonEditorInput(TaxonNode taxonNode
, ConversationHolder conversation
){
59 this.conversation
= conversation
;
60 this.taxonNode
= taxonNode
;
68 * @param taxonNodeUuid a {@link java.util.UUID} object.
69 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
70 * @throws java.lang.Exception if any.
72 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
74 ConversationHolder conversation
= CdmStore
.createConversation();
75 return NewInstance(taxonNodeUuid
, conversation
);
83 * @param taxonNodeUuid
87 private static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
, ConversationHolder conversation
){
90 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, null);
92 if(taxonNode
== null){
93 EditorUtil
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not type TaxonBase.");
97 return new TaxonEditorInput(taxonNode
, conversation
);
101 * <p>NewInstanceFromTaxonBase</p>
103 * @param taxonBaseUuid a {@link java.util.UUID} object.
104 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
106 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
107 ConversationHolder conversation
= CdmStore
.createConversation();
109 TaxonEditorInput input
= null;
111 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).find(taxonBaseUuid
);
112 if (taxonBase
!= null){
113 if(taxonBase
.isOrphaned()) {
114 EditorUtil
.warningDialog("Orphaned Taxon", TaxonEditorInput
.class, "This is an orphaned taxon i.e. a taxon that is not connected to a classification and not having any taxonomic relationships. Editing of orphaned taxon is currently not supported.");
116 else if(taxonBase
.isInstanceOf(Taxon
.class)){
117 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
119 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
120 // TODO get accepted taxon
121 EditorUtil
.info("trying to open Mispplied Name ");
123 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
124 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
125 for(TaxonRelationship relation
: relations
){
126 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
127 acceptedTaxa
.add(relation
.getToTaxon());
130 input
= getInputForMultipleTaxa(conversation
, acceptedTaxa
);
133 input
= getInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
135 }else if(taxonBase
instanceof Synonym
){
136 Synonym synonym
= (Synonym
) taxonBase
;
138 Set
<Taxon
> taxa
= synonym
.getAcceptedTaxa();
139 input
= getInputForMultipleTaxa(conversation
, taxa
);
142 input
.setInitiallySelectedTaxonBase(taxonBase
);
150 private static TaxonEditorInput
getInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
151 if(taxonNodes
.size() == 1){
152 TaxonNode taxonNode
= taxonNodes
.iterator().next();
153 return NewInstance(taxonNode
.getUuid(), conversation
);
154 }else if(taxonNodes
.size() > 1){
155 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
156 if(taxonNode
!= null){
157 return NewInstance(taxonNode
.getUuid(), conversation
);
159 }else if(taxonNodes
.size() == 0){
160 // this is an undesired state
161 EditorUtil
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not part of any classification. This should not have happened.");
166 private static TaxonEditorInput
getInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
167 if(taxa
.size() == 1){
168 Taxon taxon
= taxa
.iterator().next();
169 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
170 return getInputForMultipleNodes(conversation
, nodes
);
171 }else if(taxa
.size() > 1){
172 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
173 for ( Taxon taxon
: taxa
){
174 taxonNodes
.addAll(taxon
.getTaxonNodes());
176 return getInputForMultipleNodes(conversation
, taxonNodes
);
177 }else if(taxa
.size() == 0){
178 // this is an undesired state
179 EditorUtil
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
180 " no accepted taxa are present. This should not have happened.");
186 * <p>NewEmptyInstance</p>
188 * @param parentNodeUuid a {@link java.util.UUID} object.
189 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
191 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
192 ConversationHolder conversation
= CdmStore
.createConversation();
194 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
195 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
197 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
198 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
200 // add the new taxon to the editors persistence context
201 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
);
203 return new TaxonEditorInput(newTaxonNode
, conversation
);
207 * @see org.eclipse.ui.IEditorInput#exists()
215 public boolean exists() {
216 return taxonNode
!= null;
220 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
223 * <p>getImageDescriptor</p>
225 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
228 public ImageDescriptor
getImageDescriptor() {
233 * @see org.eclipse.ui.IEditorInput#getName()
238 * @return a {@link java.lang.String} object.
241 public String
getName() {
242 if(getTaxon() == null){
245 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
246 if (name
== null || name
.getTitleCache() == null) {
249 return name
.getTitleCache();
254 * @see org.eclipse.ui.IEditorInput#getPersistable()
257 * <p>getPersistable</p>
259 * @return a {@link org.eclipse.ui.IPersistableElement} object.
262 public IPersistableElement
getPersistable() {
263 // if(CdmStore.isActive()){
264 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
265 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
266 // if (isPersistable) {
274 * @see org.eclipse.ui.IEditorInput#getToolTipText()
277 * <p>getToolTipText</p>
279 * @return a {@link java.lang.String} object.
282 public String
getToolTipText() {
287 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
291 public Object
getAdapter(Class adapter
) {
293 if (adapter
== Taxon
.class) {
294 return taxonNode
.getTaxon();
297 if (adapter
== TaxonNode
.class) {
307 * Overrides equals to ensure that a taxon can only be edited by
308 * one editor at a time.
311 public boolean equals(Object obj
) {
312 if (TaxonEditorInput
.class.equals(obj
.getClass())
313 && getTaxon() != null
314 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
315 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
316 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
328 public Taxon
getTaxon(){
329 return taxonNode
.getTaxon();
333 * <p>Getter for the field <code>taxonNode</code>.</p>
335 * @return the taxonNode
337 public TaxonNode
getTaxonNode() {
343 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
346 * <p>getConversationHolder</p>
348 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
351 public ConversationHolder
getConversationHolder() {
357 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
361 public void update(CdmDataChangeMap events
) {
362 if(dataChangeBehavior
== null){
363 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
366 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
370 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
373 * <p>getFactoryId</p>
375 * @return a {@link java.lang.String} object.
378 public String
getFactoryId() {
379 return TaxonEditorInputFactory
.getFactoryId();
383 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
387 public void saveState(IMemento memento
) {
388 TaxonEditorInputFactory
.saveState(memento
, this);
393 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
395 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
397 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
398 this.initiallySelectedTaxonBase
= taxonBase
;
402 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
404 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
406 public TaxonBase
getInitiallySelectedTaxonBase() {
407 return initiallySelectedTaxonBase
;
411 public String
toString() {
412 return String
.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());