Fixes #2357
[taxeditor.git] / eu.etaxonomy.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.HashSet;
13 import java.util.Set;
14 import java.util.UUID;
15
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;
20
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.name.TaxonNameBase;
27 import eu.etaxonomy.cdm.model.taxon.ITreeNode;
28 import eu.etaxonomy.cdm.model.taxon.Synonym;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
32 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
33 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
34 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
35 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
36 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38
39 /**
40 * <p>TaxonEditorInput class.</p>
41 *
42 * @author n.hoffmann
43 * @created 19.03.2009
44 * @version 1.0
45 */
46 public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
47
48 private ConversationHolder conversation;
49
50 private TaxonNode taxonNode;
51
52 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
53
54 private TaxonBase initiallySelectedTaxonBase;
55
56 private TaxonEditorInput(TaxonNode taxonNode, ConversationHolder conversation){
57 this.conversation = conversation;
58 this.taxonNode = taxonNode;
59 }
60
61
62
63 /**
64 * <p>NewInstance</p>
65 *
66 * @param taxonNodeUuid a {@link java.util.UUID} object.
67 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
68 * @throws java.lang.Exception if any.
69 */
70 public static TaxonEditorInput NewInstance(UUID taxonNodeUuid) throws Exception{
71 try{
72 ConversationHolder conversation = CdmStore.createConversation();
73 return NewInstance(taxonNodeUuid, conversation);
74 }catch(Exception e){
75 throw e;
76 }
77 }
78
79 /**
80 *
81 * @param taxonNodeUuid
82 * @param conversation
83 * @return
84 */
85 private static TaxonEditorInput NewInstance(UUID taxonNodeUuid, ConversationHolder conversation){
86
87
88 TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, null);
89
90 if(taxonNode == null){
91 EditorUtil.warningDialog("Not yet implemented", TaxonEditorInput.class, "Selected element is not type TaxonBase.");
92 return null;
93 }
94
95 return new TaxonEditorInput(taxonNode, conversation);
96 }
97
98 /**
99 * <p>NewInstanceFromTaxonBase</p>
100 *
101 * @param taxonBaseUuid a {@link java.util.UUID} object.
102 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
103 */
104 public static TaxonEditorInput NewInstanceFromTaxonBase(UUID taxonBaseUuid){
105 ConversationHolder conversation = CdmStore.createConversation();
106
107 TaxonEditorInput input = null;
108
109 TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
110
111 if(taxonBase instanceof Taxon){
112 Taxon taxon = (Taxon) taxonBase;
113
114 if (taxon.isMisapplication()){
115 // TODO get accepted taxon
116 EditorUtil.info("trying to open Mispplied Name ");
117
118 Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
119 Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
120 for(TaxonRelationship relation : relations){
121 if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
122 acceptedTaxa.add(relation.getToTaxon());
123 }
124 }
125 input = getInputForMultipleTaxa(conversation, acceptedTaxa);
126
127 }else{
128 input = getInputForMultipleNodes(conversation, taxon.getTaxonNodes());
129 }
130 }else if(taxonBase instanceof Synonym){
131 Synonym synonym = (Synonym) taxonBase;
132
133 Set<Taxon> taxa = synonym.getAcceptedTaxa();
134 input = getInputForMultipleTaxa(conversation, taxa);
135 }
136
137 input.setInitiallySelectedTaxonBase(taxonBase);
138
139 return input;
140 }
141
142
143
144
145 private static TaxonEditorInput getInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
146 if(taxonNodes.size() == 1){
147 TaxonNode taxonNode = taxonNodes.iterator().next();
148 return NewInstance(taxonNode.getUuid(), conversation);
149 }else if(taxonNodes.size() > 1){
150 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
151 EditorUtil.warningDialog("Not implemented yet", TaxonEditorInput.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
152 " This case is not handled yet by the software.");
153 }else if(taxonNodes.size() == 0){
154 // this is an undesired state
155 EditorUtil.warningDialog("Incorrect state", TaxonEditorInput.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
156 }
157 return null;
158 }
159
160 private static TaxonEditorInput getInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
161 if(taxa.size() == 1){
162 Taxon taxon = taxa.iterator().next();
163 Set<TaxonNode> nodes = taxon.getTaxonNodes();
164 return getInputForMultipleNodes(conversation, nodes);
165 }else if(taxa.size() > 1){
166 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
167 EditorUtil.warningDialog("Not implemented yet", TaxonEditorInput.class, "There are multiple accepted taxa for the current selection. We currently do not know which one you want to open." +
168 " This case is not handled yet by the software.");
169 }else if(taxa.size() == 0){
170 // this is an undesired state
171 EditorUtil.warningDialog("Incorrect state", TaxonEditorInput.class, "Trying to open accepted taxon for a synonym or misapplication but" +
172 " no accepted taxa are present. This should not have happened.");
173 }
174 return null;
175 }
176
177 /**
178 * <p>NewEmptyInstance</p>
179 *
180 * @param parentNodeUuid a {@link java.util.UUID} object.
181 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
182 */
183 public static TaxonEditorInput NewEmptyInstance(UUID parentNodeUuid){
184 ConversationHolder conversation = CdmStore.createConversation();
185
186 TaxonNameBase<?, ?> name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
187 ITreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
188
189 Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
190 TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference(), null);
191
192 // add the new taxon to the editors persistence context
193 UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode);
194
195 return new TaxonEditorInput(newTaxonNode, conversation);
196 }
197
198 /* (non-Javadoc)
199 * @see org.eclipse.ui.IEditorInput#exists()
200 */
201 /**
202 * <p>exists</p>
203 *
204 * @return a boolean.
205 */
206 public boolean exists() {
207 return taxonNode != null;
208 }
209
210 /* (non-Javadoc)
211 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
212 */
213 /**
214 * <p>getImageDescriptor</p>
215 *
216 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
217 */
218 public ImageDescriptor getImageDescriptor() {
219 return null;
220 }
221
222 /* (non-Javadoc)
223 * @see org.eclipse.ui.IEditorInput#getName()
224 */
225 /**
226 * <p>getName</p>
227 *
228 * @return a {@link java.lang.String} object.
229 */
230 public String getName() {
231 if(getTaxon() == null){
232 return null;
233 }
234 TaxonNameBase<?, ?> name = getTaxon().getName();
235 if (name == null || name.getTitleCache() == null) {
236 return "New taxon";
237 } else {
238 return name.getTitleCache();
239 }
240 }
241
242 /* (non-Javadoc)
243 * @see org.eclipse.ui.IEditorInput#getPersistable()
244 */
245 /**
246 * <p>getPersistable</p>
247 *
248 * @return a {@link org.eclipse.ui.IPersistableElement} object.
249 */
250 public IPersistableElement getPersistable() {
251 // if(CdmStore.isActive()){
252 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
253 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
254 // if (isPersistable) {
255 // return this;
256 // }
257 // }
258 return null;
259 }
260
261 /* (non-Javadoc)
262 * @see org.eclipse.ui.IEditorInput#getToolTipText()
263 */
264 /**
265 * <p>getToolTipText</p>
266 *
267 * @return a {@link java.lang.String} object.
268 */
269 public String getToolTipText() {
270 return getName();
271 }
272
273 /* (non-Javadoc)
274 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
275 */
276 /** {@inheritDoc} */
277 public Object getAdapter(Class adapter) {
278
279 if (adapter == Taxon.class) {
280 return taxonNode.getTaxon();
281 }
282
283 if (adapter == TaxonNode.class) {
284 return taxonNode;
285 }
286
287 return null;
288 }
289
290 /**
291 * {@inheritDoc}
292 *
293 * Overrides equals to ensure that a taxon can only be edited by
294 * one editor at a time.
295 */
296 public boolean equals(Object obj) {
297 if (TaxonEditorInput.class.equals(obj.getClass())
298 && getTaxon() != null
299 && getTaxon().equals(((TaxonEditorInput) obj).getTaxon())){
300 if(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase() != null){
301 setInitiallySelectedTaxonBase(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase());
302 }
303 return true;
304 }
305 return false;
306 }
307
308 /**
309 * <p>getTaxon</p>
310 *
311 * @return the taxon
312 */
313 public Taxon getTaxon(){
314 return taxonNode.getTaxon();
315 }
316
317 /**
318 * <p>Getter for the field <code>taxonNode</code>.</p>
319 *
320 * @return the taxonNode
321 */
322 public TaxonNode getTaxonNode() {
323 return taxonNode;
324 }
325
326 /*
327 * (non-Javadoc)
328 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
329 */
330 /**
331 * <p>getConversationHolder</p>
332 *
333 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
334 */
335 public ConversationHolder getConversationHolder() {
336 return conversation;
337 }
338
339 /*
340 * (non-Javadoc)
341 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
342 */
343 /** {@inheritDoc} */
344 public void update(CdmDataChangeMap events) {
345 if(dataChangeBehavior == null){
346 dataChangeBehavior = new TaxonEditorInputDataChangeBehaviour(this);
347 }
348
349 DataChangeBridge.handleDataChange(events, dataChangeBehavior);
350 }
351
352 /* (non-Javadoc)
353 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
354 */
355 /**
356 * <p>getFactoryId</p>
357 *
358 * @return a {@link java.lang.String} object.
359 */
360 public String getFactoryId() {
361 return TaxonEditorInputFactory.getFactoryId();
362 }
363
364 /* (non-Javadoc)
365 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
366 */
367 /** {@inheritDoc} */
368 public void saveState(IMemento memento) {
369 TaxonEditorInputFactory.saveState(memento, this);
370 }
371
372
373 /**
374 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
375 *
376 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
377 */
378 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
379 this.initiallySelectedTaxonBase = taxonBase;
380 }
381
382 /**
383 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
384 *
385 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
386 */
387 public TaxonBase getInitiallySelectedTaxonBase() {
388 return initiallySelectedTaxonBase;
389 }
390 }