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
.Arrays
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
21 import org
.eclipse
.ui
.IEditorInput
;
22 import org
.eclipse
.ui
.IMemento
;
23 import org
.eclipse
.ui
.IPersistableElement
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
39 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
40 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
41 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
42 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
43 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
47 * <p>TaxonEditorInput class.</p>
53 public class TaxonEditorInput
extends CdmEntitySessionInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
55 private final ConversationHolder conversation
;
57 private TaxonNode taxonNode
;
59 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior
;
61 private TaxonBase initiallySelectedTaxonBase
;
63 private enum CdmType
{
69 private TaxonEditorInput(UUID uuid
, CdmType type
) {
70 this.conversation
= CdmStore
.createConversation();
72 case PARENT_TAXON_NODE
:
73 initForParentTaxonNode(uuid
);
76 initForTaxonBase(uuid
);
79 initForTaxonNode(uuid
);
84 private void init(TaxonNode taxonNode
) {
85 this.taxonNode
= taxonNode
;
92 * @param taxonNodeUuid a {@link java.util.UUID} object.
93 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
94 * @throws java.lang.Exception if any.
96 private void initForTaxonNode(UUID taxonNodeUuid
) {
98 List
<String
> taxonNodePropertyPaths
= Arrays
.asList(new String
[] {
105 "taxon.descriptions",
106 "taxon.synonymRelations.relatedFrom.name.homotypicalGroup.typifiedNames",
107 "taxon.relationsToThisTaxon",
108 "taxon.relationsFromThisTaxon",
111 "taxon.name.homotypicalGroup.typifiedNames",
112 "taxon.name.taxonBases",
113 "taxon.name.descriptions.descriptionElements",
114 "taxon.name.descriptions.markers"
119 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, taxonNodePropertyPaths
);
121 if(taxonNode
== null){
122 MessagingUtils
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not type TaxonBase.");
128 private void initForTaxonBase(UUID taxonBaseUuid
) {
131 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
139 "relationsToThisTaxon",
140 "relationsFromThisTaxon",
143 "name.nomenclaturalReference",
144 "synonymRelations.relatedFrom.name.status.type",
145 "synonymRelations.relatedFrom.name.nomenclaturalReference.inReference",
147 "name.descriptions.descriptionElements",
148 "name.descriptions.markers",
149 "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.relatedFrom.name.*"
151 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(taxonBaseUuid
, taxonBasePropertyPaths
);
152 if (taxonBase
!= null){
153 if(taxonBase
.isInstanceOf(Taxon
.class)){
154 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
156 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
157 // TODO get accepted taxon
158 MessagingUtils
.info("trying to open Mispplied Name ");
160 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
161 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
162 for(TaxonRelationship relation
: relations
){
163 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
164 acceptedTaxa
.add(relation
.getToTaxon());
167 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
170 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
172 }else if(taxonBase
instanceof Synonym
){
173 Synonym synonym
= (Synonym
) taxonBase
;
175 Set
<Taxon
> taxa
= synonym
.getAcceptedTaxa();
176 setInputForMultipleTaxa(conversation
, taxa
);
183 * <p>NewEmptyInstance</p>
185 * @param parentNodeUuid a {@link java.util.UUID} object.
186 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
188 private void initForParentTaxonNode(UUID parentNodeUuid
){
191 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
192 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
194 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
195 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
197 // add the new taxon to the editors persistence context
198 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
200 initForTaxonNode(newTaxonNodeUuid
);
206 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
207 if(taxonNodes
.size() == 1){
208 TaxonNode taxonNode
= taxonNodes
.iterator().next();
210 }else if(taxonNodes
.size() > 1){
211 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
212 if(taxonNode
!= null){
215 }else if(taxonNodes
.size() == 0){
216 // this is an undesired state
217 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not part of any classification. This should not have happened.");
221 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
222 if(taxa
.size() == 1){
223 Taxon taxon
= taxa
.iterator().next();
224 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
225 setInputForMultipleNodes(conversation
, nodes
);
226 }else if(taxa
.size() > 1){
227 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
228 for ( Taxon taxon
: taxa
){
229 taxonNodes
.addAll(taxon
.getTaxonNodes());
231 setInputForMultipleNodes(conversation
, taxonNodes
);
232 }else if(taxa
.size() == 0){
233 // this is an undesired state
234 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
235 " no accepted taxa are present. This should not have happened.");
242 * @param taxonNodeUuid a {@link java.util.UUID} object.
243 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
244 * @throws java.lang.Exception if any.
246 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
247 return new TaxonEditorInput(taxonNodeUuid
, CdmType
.TAXON_NODE
);
252 * <p>NewInstanceFromTaxonBase</p>
254 * @param taxonBaseUuid a {@link java.util.UUID} object.
255 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
257 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
258 return new TaxonEditorInput(taxonBaseUuid
, CdmType
.TAXON_BASE
);
263 * <p>NewEmptyInstance</p>
265 * @param parentNodeUuid a {@link java.util.UUID} object.
266 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
268 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
269 return new TaxonEditorInput(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
273 * @see org.eclipse.ui.IEditorInput#exists()
281 public boolean exists() {
282 return taxonNode
!= null;
286 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
289 * <p>getImageDescriptor</p>
291 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
294 public ImageDescriptor
getImageDescriptor() {
299 * @see org.eclipse.ui.IEditorInput#getName()
304 * @return a {@link java.lang.String} object.
307 public String
getName() {
308 if(getTaxon() == null){
311 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
312 if (name
== null || name
.getTitleCache() == null) {
315 return name
.getTitleCache();
320 * @see org.eclipse.ui.IEditorInput#getPersistable()
323 * <p>getPersistable</p>
325 * @return a {@link org.eclipse.ui.IPersistableElement} object.
328 public IPersistableElement
getPersistable() {
329 // if(CdmStore.isActive()){
330 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
331 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
332 // if (isPersistable) {
340 * @see org.eclipse.ui.IEditorInput#getToolTipText()
343 * <p>getToolTipText</p>
345 * @return a {@link java.lang.String} object.
348 public String
getToolTipText() {
353 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
357 public Object
getAdapter(Class adapter
) {
359 if (adapter
== Taxon
.class) {
363 if (adapter
== TaxonNode
.class) {
373 * Overrides equals to ensure that a taxon can only be edited by
374 * one editor at a time.
377 public boolean equals(Object obj
) {
378 if (TaxonEditorInput
.class.equals(obj
.getClass())
379 && getTaxon() != null
380 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
381 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
382 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
394 public Taxon
getTaxon(){
395 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
400 * <p>Getter for the field <code>taxonNode</code>.</p>
402 * @return the taxonNode
404 public TaxonNode
getTaxonNode() {
410 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
413 * <p>getConversationHolder</p>
415 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
418 public ConversationHolder
getConversationHolder() {
424 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
428 public void update(CdmDataChangeMap events
) {
429 if(dataChangeBehavior
== null){
430 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
433 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
437 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
440 * <p>getFactoryId</p>
442 * @return a {@link java.lang.String} object.
445 public String
getFactoryId() {
446 return TaxonEditorInputFactory
.getFactoryId();
450 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
454 public void saveState(IMemento memento
) {
455 TaxonEditorInputFactory
.saveState(memento
, this);
460 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
462 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
464 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
465 this.initiallySelectedTaxonBase
= taxonBase
;
469 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
471 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
473 public TaxonBase
getInitiallySelectedTaxonBase() {
474 return initiallySelectedTaxonBase
;
478 // public String toString() {
479 // return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
484 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
487 public List
<TaxonNode
> getRootEntities() {
488 return Arrays
.asList(taxonNode
);
492 * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#merge()
495 public void merge() {
496 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
);
500 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
501 Map
<Object
, List
<String
>> propertyPathsMap
= new HashMap
<Object
, List
<String
>>();
502 // List<String> taxonNameBasePropertyPaths = Arrays.asList(new String[] {
504 // "taxonBases.taxonNodes",
505 // "homotypicalGroup.typifiedNames.taxonBases.synonymRelations",
506 // "taxonBases.relationsToThisTaxon",
507 // "taxonBases.relationsFromThisTaxon",
510 // propertyPathsMap.put(TaxonNameBase.class, taxonNameBasePropertyPaths);
512 // List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
513 // "taxon.annotations",
516 // "taxon.extensions",
519 // "taxon.descriptions"
521 // propertyPathsMap.put(TaxonNode.class, taxonBasePropertyPaths);
522 // List<String> homotypicalGroupPropertyPaths = Arrays.asList(new String[] {
523 // "typifiedNames.taxonBases.synonymRelations.relatedFrom.name",
524 // "typifiedNames.taxonBases.synonymRelations.relatedFrom.name.status"
526 // propertyPathsMap.put(HomotypicalGroup.class, homotypicalGroupPropertyPaths);
527 // return propertyPathsMap;