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
;
13 import java
.util
.UUID
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
17 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
18 import org
.eclipse
.ui
.IEditorInput
;
19 import org
.eclipse
.ui
.IMemento
;
20 import org
.eclipse
.ui
.IPersistableElement
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
23 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
24 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
28 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
29 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
30 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 public class TaxonEditorInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
38 private static final Logger logger
= Logger
39 .getLogger(TaxonEditorInput
.class);
42 private ConversationHolder conversation
;
44 private TaxonEditorInput(Taxon taxon
, ConversationHolder conversation
){
46 this.conversation
= conversation
;
55 public static TaxonEditorInput
NewInstance(UUID uuid
){
56 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
58 TaxonBase
<?
> taxonBase
= CdmStore
.getTaxonService().getTaxonByUuid(uuid
);
60 if(taxonBase
== null){
61 MessageDialog
.openWarning(EditorUtil
.getShell(), "Not yet implemented", "Selected element is not if type TaxonBase.");
67 if (taxonBase
instanceof Synonym
) {
68 // TODO: in case of pro parte synonym or any other where we might have multiple
69 // accepted taxa we have to provide a mechanism that can deal with that
70 // TODO set focus to the synonym
71 Set
<Taxon
> acceptedTaxa
= ((Synonym
) taxonBase
).getAcceptedTaxa();
72 if(acceptedTaxa
.size() != 1){
74 if(acceptedTaxa
.size() == 0){
75 message
= "There is no accepted taxon for the chosen synonym. ";
77 message
= "Multiple taxa found for the chosen synonym.";
79 MessageDialog
.openWarning(EditorUtil
.getShell(), "Not yet implemented", message
);
82 taxon
= acceptedTaxa
.toArray(new Taxon
[0])[0];
85 taxon
= (Taxon
) taxonBase
;
88 logger
.trace(TaxonEditorInput
.class.getSimpleName() + " created");
90 return new TaxonEditorInput(taxon
, conversation
);
93 public static TaxonEditorInput
NewEmptyInstance(UUID parentTaxonUuid
){
94 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
96 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getInstanceOfPreferredNameClass();
98 Taxon newTaxon
= null;
99 if(parentTaxonUuid
== null){
100 newTaxon
= Taxon
.NewInstance(name
, CdmStore
.getDefault().getDefaultSec());
102 Taxon parentTaxon
= (Taxon
) CdmStore
.getTaxonService().getTaxonByUuid(parentTaxonUuid
);
103 newTaxon
= Taxon
.NewInstance(name
, parentTaxon
.getSec());
104 parentTaxon
.addTaxonomicChild(newTaxon
, null, null);
106 // add the new taxon to the editors persistence context
107 CdmStore
.getTaxonService().save(newTaxon
);
109 return new TaxonEditorInput(newTaxon
, conversation
);
113 * @see org.eclipse.ui.IEditorInput#exists()
115 public boolean exists() {
116 return taxon
!= null;
120 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
122 public ImageDescriptor
getImageDescriptor() {
127 * @see org.eclipse.ui.IEditorInput#getName()
129 public String
getName() {
130 TaxonNameBase
<?
, ?
> name
= taxon
.getName();
131 if (name
== null || name
.getTitleCache() == null) {
134 return name
.getTitleCache();
139 * @see org.eclipse.ui.IEditorInput#getPersistable()
141 public IPersistableElement
getPersistable() {
146 * @see org.eclipse.ui.IEditorInput#getToolTipText()
148 public String
getToolTipText() {
153 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
155 public Object
getAdapter(Class adapter
) {
157 if (adapter
== Taxon
.class) {
165 * Overrides equals to ensure that a taxon can only be edited by
166 * one editor at a time.
170 public boolean equals(Object obj
) {
171 if (obj
.getClass().equals(TaxonEditorInput
.class)
172 && taxon
.equals(((TaxonEditorInput
) obj
).taxon
))
177 public Taxon
getTaxon(){
183 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
185 public ConversationHolder
getConversationHolder() {
191 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
193 public void update(CdmDataChangeMap events
) {
194 // FIXME update the editor input
198 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
200 public String
getFactoryId() {
201 return TaxonEditorInputFactory
.getFactoryId();
205 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
207 public void saveState(IMemento memento
) {
208 TaxonEditorInputFactory
.saveState(memento
, this);