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
.HomotypicalGroup
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
40 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
41 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
42 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
43 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
44 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
48 * <p>TaxonEditorInput class.</p>
54 public class TaxonEditorInput
extends CdmEntitySessionInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
56 private final ConversationHolder conversation
;
58 private TaxonNode taxonNode
;
60 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior
;
62 private TaxonBase initiallySelectedTaxonBase
;
64 private enum CdmType
{
70 private TaxonEditorInput(UUID uuid
, CdmType type
) {
71 this.conversation
= CdmStore
.createConversation();
73 case PARENT_TAXON_NODE
:
74 initForParentTaxonNode(uuid
);
77 initForTaxonBase(uuid
);
80 initForTaxonNode(uuid
);
85 private void init(TaxonNode taxonNode
) {
86 this.taxonNode
= taxonNode
;
93 * @param taxonNodeUuid a {@link java.util.UUID} object.
94 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
95 * @throws java.lang.Exception if any.
97 private void initForTaxonNode(UUID taxonNodeUuid
) {
99 List
<String
> taxonNodePropertyPaths
= Arrays
.asList(new String
[] {
106 "taxon.descriptions",
107 "taxon.synonymRelations.relatedFrom.name.homotypicalGroup.typifiedNames",
108 "taxon.relationsToThisTaxon",
109 "taxon.relationsFromThisTaxon",
112 "taxon.name.homotypicalGroup.typifiedNames",
113 "taxon.name.taxonBases",
114 "taxon.name.descriptions.descriptionElements",
115 "taxon.name.descriptions.markers"
120 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, taxonNodePropertyPaths
);
122 if(taxonNode
== null){
123 MessagingUtils
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not type TaxonBase.");
129 private void initForTaxonBase(UUID taxonBaseUuid
) {
132 List
<String
> taxonBasePropertyPaths
= Arrays
.asList(new String
[] {
140 "relationsToThisTaxon",
141 "relationsFromThisTaxon",
144 "name.nomenclaturalReference",
145 "synonymRelations.relatedFrom.name.status.type",
146 "synonymRelations.relatedFrom.name.nomenclaturalReference.inReference",
148 "name.descriptions.descriptionElements",
149 "name.descriptions.markers"
153 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(taxonBaseUuid
, taxonBasePropertyPaths
);
154 if (taxonBase
!= null){
155 if(taxonBase
.isInstanceOf(Taxon
.class)){
156 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
158 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
159 // TODO get accepted taxon
160 MessagingUtils
.info("trying to open Mispplied Name ");
162 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
163 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
164 for(TaxonRelationship relation
: relations
){
165 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
166 acceptedTaxa
.add(relation
.getToTaxon());
169 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
172 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
174 }else if(taxonBase
instanceof Synonym
){
175 Synonym synonym
= (Synonym
) taxonBase
;
177 Set
<Taxon
> taxa
= synonym
.getAcceptedTaxa();
178 setInputForMultipleTaxa(conversation
, taxa
);
185 * <p>NewEmptyInstance</p>
187 * @param parentNodeUuid a {@link java.util.UUID} object.
188 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
190 private void initForParentTaxonNode(UUID parentNodeUuid
){
193 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
194 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
196 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
197 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
199 // add the new taxon to the editors persistence context
200 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
202 initForTaxonNode(newTaxonNodeUuid
);
208 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
209 if(taxonNodes
.size() == 1){
210 TaxonNode taxonNode
= taxonNodes
.iterator().next();
212 }else if(taxonNodes
.size() > 1){
213 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
214 if(taxonNode
!= null){
217 }else if(taxonNodes
.size() == 0){
218 // this is an undesired state
219 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not part of any classification. This should not have happened.");
223 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
224 if(taxa
.size() == 1){
225 Taxon taxon
= taxa
.iterator().next();
226 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
227 setInputForMultipleNodes(conversation
, nodes
);
228 }else if(taxa
.size() > 1){
229 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
230 for ( Taxon taxon
: taxa
){
231 taxonNodes
.addAll(taxon
.getTaxonNodes());
233 setInputForMultipleNodes(conversation
, taxonNodes
);
234 }else if(taxa
.size() == 0){
235 // this is an undesired state
236 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
237 " no accepted taxa are present. This should not have happened.");
244 * @param taxonNodeUuid a {@link java.util.UUID} object.
245 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
246 * @throws java.lang.Exception if any.
248 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
249 return new TaxonEditorInput(taxonNodeUuid
, CdmType
.TAXON_NODE
);
254 * <p>NewInstanceFromTaxonBase</p>
256 * @param taxonBaseUuid a {@link java.util.UUID} object.
257 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
259 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
260 return new TaxonEditorInput(taxonBaseUuid
, CdmType
.TAXON_BASE
);
265 * <p>NewEmptyInstance</p>
267 * @param parentNodeUuid a {@link java.util.UUID} object.
268 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
270 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
271 return new TaxonEditorInput(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
275 * @see org.eclipse.ui.IEditorInput#exists()
283 public boolean exists() {
284 return taxonNode
!= null;
288 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
291 * <p>getImageDescriptor</p>
293 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
296 public ImageDescriptor
getImageDescriptor() {
301 * @see org.eclipse.ui.IEditorInput#getName()
306 * @return a {@link java.lang.String} object.
309 public String
getName() {
310 if(getTaxon() == null){
313 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
314 if (name
== null || name
.getTitleCache() == null) {
317 return name
.getTitleCache();
322 * @see org.eclipse.ui.IEditorInput#getPersistable()
325 * <p>getPersistable</p>
327 * @return a {@link org.eclipse.ui.IPersistableElement} object.
330 public IPersistableElement
getPersistable() {
331 // if(CdmStore.isActive()){
332 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
333 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
334 // if (isPersistable) {
342 * @see org.eclipse.ui.IEditorInput#getToolTipText()
345 * <p>getToolTipText</p>
347 * @return a {@link java.lang.String} object.
350 public String
getToolTipText() {
355 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
359 public Object
getAdapter(Class adapter
) {
361 if (adapter
== Taxon
.class) {
365 if (adapter
== TaxonNode
.class) {
375 * Overrides equals to ensure that a taxon can only be edited by
376 * one editor at a time.
379 public boolean equals(Object obj
) {
380 if (TaxonEditorInput
.class.equals(obj
.getClass())
381 && getTaxon() != null
382 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
383 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
384 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
396 public Taxon
getTaxon(){
397 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
402 * <p>Getter for the field <code>taxonNode</code>.</p>
404 * @return the taxonNode
406 public TaxonNode
getTaxonNode() {
412 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
415 * <p>getConversationHolder</p>
417 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
420 public ConversationHolder
getConversationHolder() {
426 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
430 public void update(CdmDataChangeMap events
) {
431 if(dataChangeBehavior
== null){
432 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
435 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
439 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
442 * <p>getFactoryId</p>
444 * @return a {@link java.lang.String} object.
447 public String
getFactoryId() {
448 return TaxonEditorInputFactory
.getFactoryId();
452 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
456 public void saveState(IMemento memento
) {
457 TaxonEditorInputFactory
.saveState(memento
, this);
462 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
464 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
466 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
467 this.initiallySelectedTaxonBase
= taxonBase
;
471 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
473 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
475 public TaxonBase
getInitiallySelectedTaxonBase() {
476 return initiallySelectedTaxonBase
;
480 // public String toString() {
481 // return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
486 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
489 public List
<TaxonNode
> getRootEntities() {
490 return Arrays
.asList(taxonNode
);
494 * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#merge()
497 public void merge() {
498 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
);
502 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
503 Map
<Object
, List
<String
>> propertyPathsMap
= new HashMap
<Object
, List
<String
>>();
504 // List<String> taxonNameBasePropertyPaths = Arrays.asList(new String[] {
506 // "taxonBases.taxonNodes",
507 // "homotypicalGroup.typifiedNames.taxonBases.synonymRelations",
508 // "taxonBases.relationsToThisTaxon",
509 // "taxonBases.relationsFromThisTaxon",
512 // propertyPathsMap.put(TaxonNameBase.class, taxonNameBasePropertyPaths);
514 // List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
515 // "taxon.annotations",
518 // "taxon.extensions",
521 // "taxon.descriptions"
523 // propertyPathsMap.put(TaxonNode.class, taxonBasePropertyPaths);
524 List
<String
> homotypicalGroupPropertyPaths
= Arrays
.asList(new String
[] {
525 "typifiedNames.taxonBases.synonymRelations.relatedFrom.name",
526 "typifiedNames.taxonBases.synonymRelations.relatedFrom.name.status"
528 propertyPathsMap
.put(HomotypicalGroup
.class, homotypicalGroupPropertyPaths
);
529 return propertyPathsMap
;