.
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / TaxonEditorInput.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor;
11
12 import java.util.Set;
13 import java.util.UUID;
14
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;
21
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;
31
32 /**
33 * @author n.hoffmann
34 * @created 19.03.2009
35 * @version 1.0
36 */
37 public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
38 private static final Logger logger = Logger
39 .getLogger(TaxonEditorInput.class);
40
41 private Taxon taxon;
42 private ConversationHolder conversation;
43
44 private TaxonEditorInput(Taxon taxon, ConversationHolder conversation){
45 this.taxon = taxon;
46 this.conversation = conversation;
47 }
48
49 /**
50 *
51 * @param uuid
52 * @param conversation
53 * @return
54 */
55 public static TaxonEditorInput NewInstance(UUID uuid){
56 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
57
58 TaxonBase<?> taxonBase = CdmStore.getTaxonService().getTaxonByUuid(uuid);
59
60 if(taxonBase == null){
61 MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", "Selected element is not if type TaxonBase.");
62 return null;
63 }
64
65 Taxon taxon;
66
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){
73 String message;
74 if(acceptedTaxa.size() == 0){
75 message = "There is no accepted taxon for the chosen synonym. ";
76 }else{
77 message = "Multiple taxa found for the chosen synonym.";
78 }
79 MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", message);
80 return null;
81 }else{
82 taxon = acceptedTaxa.toArray(new Taxon[0])[0];
83 }
84 } else {
85 taxon = (Taxon) taxonBase;
86 }
87
88 logger.trace(TaxonEditorInput.class.getSimpleName() + " created");
89
90 return new TaxonEditorInput(taxon, conversation);
91 }
92
93 public static TaxonEditorInput NewEmptyInstance(UUID parentTaxonUuid){
94 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
95
96 TaxonNameBase<?, ?> name = PreferencesUtil.getInstanceOfPreferredNameClass();
97
98 Taxon newTaxon = null;
99 if(parentTaxonUuid == null){
100 newTaxon = Taxon.NewInstance(name, CdmStore.getDefault().getDefaultSec());
101 }else{
102 Taxon parentTaxon = (Taxon) CdmStore.getTaxonService().getTaxonByUuid(parentTaxonUuid);
103 newTaxon = Taxon.NewInstance(name, parentTaxon.getSec());
104 parentTaxon.addTaxonomicChild(newTaxon, null, null);
105 }
106 // add the new taxon to the editors persistence context
107 CdmStore.getTaxonService().save(newTaxon);
108
109 return new TaxonEditorInput(newTaxon, conversation);
110 }
111
112 /* (non-Javadoc)
113 * @see org.eclipse.ui.IEditorInput#exists()
114 */
115 public boolean exists() {
116 return taxon != null;
117 }
118
119 /* (non-Javadoc)
120 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
121 */
122 public ImageDescriptor getImageDescriptor() {
123 return null;
124 }
125
126 /* (non-Javadoc)
127 * @see org.eclipse.ui.IEditorInput#getName()
128 */
129 public String getName() {
130 TaxonNameBase<?, ?> name = taxon.getName();
131 if (name == null || name.getTitleCache() == null) {
132 return "New taxon";
133 } else {
134 return name.getTitleCache();
135 }
136 }
137
138 /* (non-Javadoc)
139 * @see org.eclipse.ui.IEditorInput#getPersistable()
140 */
141 public IPersistableElement getPersistable() {
142 return this;
143 }
144
145 /* (non-Javadoc)
146 * @see org.eclipse.ui.IEditorInput#getToolTipText()
147 */
148 public String getToolTipText() {
149 return getName();
150 }
151
152 /* (non-Javadoc)
153 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
154 */
155 public Object getAdapter(Class adapter) {
156
157 if (adapter == Taxon.class) {
158 return taxon;
159 }
160
161 return null;
162 }
163
164 /**
165 * Overrides equals to ensure that a taxon can only be edited by
166 * one editor at a time.
167 *
168 * @return boolean
169 */
170 public boolean equals(Object obj) {
171 if (obj.getClass().equals(TaxonEditorInput.class)
172 && taxon.equals(((TaxonEditorInput) obj).taxon))
173 return true;
174 return false;
175 }
176
177 public Taxon getTaxon(){
178 return taxon;
179 }
180
181 /*
182 * (non-Javadoc)
183 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
184 */
185 public ConversationHolder getConversationHolder() {
186 return conversation;
187 }
188
189 /*
190 * (non-Javadoc)
191 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
192 */
193 public void update(CdmDataChangeMap events) {
194 // FIXME update the editor input
195 }
196
197 /* (non-Javadoc)
198 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
199 */
200 public String getFactoryId() {
201 return TaxonEditorInputFactory.getFactoryId();
202 }
203
204 /* (non-Javadoc)
205 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
206 */
207 public void saveState(IMemento memento) {
208 TaxonEditorInputFactory.saveState(memento, this);
209 }
210 }