Project

General

Profile

Download (14.6 KB) Statistics
| Branch: | Tag: | Revision:
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.Arrays;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.eclipse.jface.resource.ImageDescriptor;
19
import org.eclipse.ui.IEditorInput;
20
import org.eclipse.ui.IMemento;
21
import org.eclipse.ui.IPersistableElement;
22

    
23
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
25
import eu.etaxonomy.cdm.api.service.IClassificationService;
26
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
27
import eu.etaxonomy.cdm.api.service.ITaxonService;
28
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29
import eu.etaxonomy.cdm.model.taxon.ITreeNode;
30
import eu.etaxonomy.cdm.model.taxon.Synonym;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
33
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
35
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
36
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
37
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
38
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
39
import eu.etaxonomy.taxeditor.store.CdmStore;
40

    
41
/**
42
 * <p>TaxonEditorInput class.</p>
43
 *
44
 * @author n.hoffmann
45
 * @created 19.03.2009
46
 * @version 1.0
47
 */
48
public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
49
	
50
	private ConversationHolder conversation;
51
	
52
	private TaxonNode taxonNode;
53

    
54
	private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
55
	
56
	private TaxonBase initiallySelectedTaxonBase;
57

    
58
	private Taxon taxon;
59
	
60
	private TaxonEditorInput(TaxonNode taxonNode, ConversationHolder conversation){
61
		this.conversation = conversation;
62
		this.taxonNode = taxonNode;
63
	}
64
	
65

    
66
	
67
	/**
68
	 * <p>NewInstance</p>
69
	 *
70
	 * @param taxonNodeUuid a {@link java.util.UUID} object.
71
	 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
72
	 * @throws java.lang.Exception if any.
73
	 */
74
	public static TaxonEditorInput NewInstance(UUID taxonNodeUuid) throws Exception{
75
		try{
76
			ConversationHolder conversation = CdmStore.createConversation();
77
			return NewInstance(taxonNodeUuid, conversation);
78
		}catch(Exception e){
79
			throw e;
80
		}
81
	}
82
	
83
    /**
84
     * 
85
     * @param taxonNodeUuid
86
     * @param conversation
87
     * @return
88
     */
89
    private static TaxonEditorInput NewInstance(UUID taxonNodeUuid, ConversationHolder conversation){
90
//    	List<String> propertyPaths = Arrays.asList(new String[]{
91
//				"taxon.sec", 
92
//				"taxon.name"
93
//		});
94
    	
95
    	TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid);//), propertyPaths);
96
		
97
		if(taxonNode == null){
98
			EditorUtil.warningDialog("Not yet implemented", TaxonEditorInput.class, "Selected element is not type TaxonBase.");
99
			return null;
100
		}	
101
		
102
    	return new TaxonEditorInput(taxonNode, conversation);
103
    }
104
    
105
    /**
106
     * <p>NewInstanceFromTaxonBase</p>
107
     *
108
     * @param taxonBaseUuid a {@link java.util.UUID} object.
109
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
110
     */
111
    public static TaxonEditorInput NewInstanceFromTaxonBase(UUID taxonBaseUuid){
112
    	ConversationHolder conversation = CdmStore.createConversation();
113
    	
114
    	TaxonEditorInput input = null;
115
    	
116
    	TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
117
    	
118
    	if(taxonBase instanceof Taxon){
119
    		Taxon taxon = (Taxon) taxonBase;
120
    		
121
    		if (taxon.isMisapplication()){
122
    			// TODO get accepted taxon
123
    			EditorUtil.info("trying to open Mispplied Name ");
124
    			
125
    			Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
126
    			Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
127
    			for(TaxonRelationship relation : relations){
128
    				if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
129
    					acceptedTaxa.add(relation.getToTaxon());
130
    				}
131
    			}
132
    			input =  getInputForMultipleTaxa(conversation, acceptedTaxa);
133
    			
134
    		}else{
135
    			input = getInputForMultipleNodes(conversation, taxon.getTaxonNodes());
136
    		}
137
    	}else if(taxonBase instanceof Synonym){
138
    		Synonym synonym = (Synonym) taxonBase;
139
    		
140
    		Set<Taxon> taxa = synonym.getAcceptedTaxa();
141
    		input = getInputForMultipleTaxa(conversation, taxa);
142
    	}
143
    	
144
    	input.setInitiallySelectedTaxonBase(taxonBase);
145
    	
146
    	return input;
147
    }
148

    
149

    
150

    
151

    
152
	private static TaxonEditorInput getInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
153
    	if(taxonNodes.size() == 1){
154
    		TaxonNode taxonNode = taxonNodes.iterator().next();
155
    		return NewInstance(taxonNode.getUuid(), conversation);
156
    	}else if(taxonNodes.size() > 1){
157
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
158
			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." +
159
					" This case is not handled yet by the software.");
160
		}else if(taxonNodes.size() == 0){
161
			// this is an undesired state
162
			EditorUtil.warningDialog("Incorrect state", TaxonEditorInput.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
163
		}
164
    	return null;
165
    }
166
    
167
    private static TaxonEditorInput getInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
168
    	if(taxa.size() == 1){
169
    		Taxon taxon = taxa.iterator().next();
170
    		Set<TaxonNode> nodes = taxon.getTaxonNodes();
171
    		return getInputForMultipleNodes(conversation, nodes);
172
    	}else if(taxa.size() > 1){
173
			// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
174
			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." +
175
					" This case is not handled yet by the software.");
176
		}else if(taxa.size() == 0){
177
			// this is an undesired state
178
			EditorUtil.warningDialog("Incorrect state", TaxonEditorInput.class, "Trying to open accepted taxon for a synonym or misapplication but" +
179
					" no accepted taxa are present. This should not have happened.");
180
		}
181
    	return null;
182
    }
183
    
184
    /**
185
     * <p>NewEmptyInstance</p>
186
     *
187
     * @param parentNodeUuid a {@link java.util.UUID} object.
188
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
189
     */
190
    public static TaxonEditorInput NewEmptyInstance(UUID parentNodeUuid){
191
    	ConversationHolder conversation = CdmStore.createConversation();
192
		
193
		TaxonNameBase<?, ?> name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
194
		ITreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
195
		
196
		Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
197
		TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference(), null);
198
		
199
		// add the new taxon to the editors persistence context
200
		UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode);
201
		
202
		return new TaxonEditorInput(newTaxonNode, conversation);
203
    }
204
	
205
	/* (non-Javadoc)
206
	 * @see org.eclipse.ui.IEditorInput#exists()
207
	 */
208
	/**
209
	 * <p>exists</p>
210
	 *
211
	 * @return a boolean.
212
	 */
213
	public boolean exists() {
214
		return taxonNode != null;
215
	}
216

    
217
	/* (non-Javadoc)
218
	 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
219
	 */
220
	/**
221
	 * <p>getImageDescriptor</p>
222
	 *
223
	 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
224
	 */
225
	public ImageDescriptor getImageDescriptor() {
226
		return null;
227
	}
228

    
229
	/* (non-Javadoc)
230
	 * @see org.eclipse.ui.IEditorInput#getName()
231
	 */
232
	/**
233
	 * <p>getName</p>
234
	 *
235
	 * @return a {@link java.lang.String} object.
236
	 */
237
	public String getName() {
238
		if(getTaxon() == null){
239
			return null;
240
		}
241
		TaxonNameBase<?, ?> name = getTaxon().getName();
242
		if (name == null || name.getTitleCache() == null) {
243
			return "New taxon";
244
		} else {
245
			return name.getTitleCache();
246
		}	
247
	}
248

    
249
	/* (non-Javadoc)
250
	 * @see org.eclipse.ui.IEditorInput#getPersistable()
251
	 */
252
	/**
253
	 * <p>getPersistable</p>
254
	 *
255
	 * @return a {@link org.eclipse.ui.IPersistableElement} object.
256
	 */
257
	public IPersistableElement getPersistable() {
258
//		if(CdmStore.isActive()){
259
//			TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
260
//			boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
261
//			if (isPersistable) {
262
//				return this;
263
//			}
264
//		}
265
		return null;
266
	}
267

    
268
	/* (non-Javadoc)
269
	 * @see org.eclipse.ui.IEditorInput#getToolTipText()
270
	 */
271
	/**
272
	 * <p>getToolTipText</p>
273
	 *
274
	 * @return a {@link java.lang.String} object.
275
	 */
276
	public String getToolTipText() {
277
		return getName();
278
	}
279

    
280
	/* (non-Javadoc)
281
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
282
	 */
283
	/** {@inheritDoc} */
284
	public Object getAdapter(Class adapter) {
285

    
286
		if (adapter == Taxon.class) {
287
			return getTaxon();
288
		}
289
		
290
		if (adapter == TaxonNode.class) {
291
			return taxonNode;
292
		}
293
		
294
		return null;
295
	}
296
	
297
	/**
298
	 * {@inheritDoc}
299
	 *
300
	 * Overrides equals to ensure that a taxon can only be edited by
301
	 * one editor at a time.
302
	 */
303
	public boolean equals(Object obj) {
304
		if (TaxonEditorInput.class.equals(obj.getClass()) 
305
				&& getTaxon() != null
306
				&& getTaxon().equals(((TaxonEditorInput) obj).getTaxon())){
307
			if(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase() != null){
308
				setInitiallySelectedTaxonBase(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase());
309
			}
310
			return true;
311
		}
312
		return false;
313
	}
314

    
315
	/**
316
	 * <p>getTaxon</p>
317
	 *
318
	 * @return the taxon
319
	 */
320
	public Taxon getTaxon(){
321
		if(taxon == null){
322
			List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String []{
323
					"*",
324
					// synonyms
325
					"synonymRelations.synonym.name.homotypicalGroup.typifiedNames",
326
					"synonymRelations.synonym.name.status",
327
					"synonymRelations.synonym.name.relationsToThisName.fromTaxon.name",
328
					// synonym name
329
					"synonymRelations.synonym.name.descriptions.elements",
330
					"synonymRelations.synonym.name.typeDesignations", 
331
					"synonymRelations.synonym.name.relationsToThisName",
332
					"synonymRelations.synonym.name.relationsFromThisName",
333
					"synonymRelations.synonym.name.homotypicalGroup.typifiedNames",
334
					"synonymRelations.synonym.name.rank.representations",
335
					"synonymRelations.synonym.name.status.type.representations",
336
					// snonym name supplemental
337
					"synonymRelations.synonym.name.annotations.annotationType",
338
					"synonymRelations.synonym.name.markers",
339
					"synonymRelations.synonym.name.credits",
340
					"synonymRelations.synonym.name.extensions",
341
					"synonymRelations.synonym.name.rights",
342
					"synonymRelations.synonym.name.sources",
343
					//
344
					"relationsToThisTaxon",
345
					"relationsFromThisTaxon",
346
					// taxon relations 
347
					"relationsToThisName.fromTaxon.name",
348
					// the name
349
					"name.descriptions.elements",
350
					"name.typeDesignations", 
351
					"name.relationsToThisName",
352
					"name.relationsFromThisName",
353
					"name.homotypicalGroup.typifiedNames",
354
					"name.rank.representations",
355
					"name.status.type.representations",
356
					// name supplemental
357
					"name.annotations.annotationType",
358
					"name.markers",
359
					"name.credits",
360
					"name.extensions",
361
					"name.rights",
362
					"name.sources",
363
					// taxon descriptions
364
					"descriptions.describedSpecimenOrObservations",
365
					"descriptions.sources",
366
					"descriptions.scopes",
367
					// taxon description supplemental
368
					"descriptions.annotations.annotationType",
369
					"descriptions.markers",
370
					"descriptions.credits",
371
					"descriptions.extensions",
372
					"descriptions.rights",
373
					"descriptions.sources",
374
					// description elements
375
					"descriptions.elements.*",
376
					"descriptions.elements.sources",
377
					"descriptions.elements.area.$",
378
					"descriptions.elements.multilanguageText",
379
					"descriptions.elements.media.representations.parts",
380
					"descriptions.elements.media.title",
381
					// descriptin element supplemental
382
					"descriptions.elements.annotations.annotationType",
383
					"descriptions.elements.markers",
384
					"descriptions.elements.credits",
385
					"descriptions.elements.extensions",
386
					"descriptions.elements.rights",
387
					"descriptions.elements.sources",
388
					// supplemental
389
					"annotations.annotationType",
390
					"markers",
391
					"credits",
392
					"extensions",
393
					"rights",
394
					"sources"
395
					});
396
			
397
			taxon = (Taxon) CdmStore.getService(ITaxonService.class).load(taxonNode.getTaxon().getUuid(), TAXON_INIT_STRATEGY);
398
		}
399
		
400
		return taxon;
401
	}
402
	
403
	/**
404
	 * <p>Getter for the field <code>taxonNode</code>.</p>
405
	 *
406
	 * @return the taxonNode
407
	 */
408
	public TaxonNode getTaxonNode() {
409
		return taxonNode;
410
	}
411
	
412
	/*
413
	 * (non-Javadoc)
414
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
415
	 */
416
	/**
417
	 * <p>getConversationHolder</p>
418
	 *
419
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
420
	 */
421
	public ConversationHolder getConversationHolder() {
422
		return conversation;
423
	}
424

    
425
	/*
426
	 * (non-Javadoc)
427
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
428
	 */
429
	/** {@inheritDoc} */
430
	public void update(CdmDataChangeMap events) {
431
		if(dataChangeBehavior == null){
432
			dataChangeBehavior = new TaxonEditorInputDataChangeBehaviour(this);
433
		}
434
		
435
		DataChangeBridge.handleDataChange(events, dataChangeBehavior);
436
	}
437

    
438
	/* (non-Javadoc)
439
	 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
440
	 */
441
	/**
442
	 * <p>getFactoryId</p>
443
	 *
444
	 * @return a {@link java.lang.String} object.
445
	 */
446
	public String getFactoryId() {
447
		return TaxonEditorInputFactory.getFactoryId();
448
	}
449

    
450
	/* (non-Javadoc)
451
	 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
452
	 */
453
	/** {@inheritDoc} */
454
	public void saveState(IMemento memento) {
455
		TaxonEditorInputFactory.saveState(memento, this);
456
	}
457

    
458
    
459
    /**
460
     * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
461
     *
462
     * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
463
     */
464
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
465
		this.initiallySelectedTaxonBase = taxonBase;
466
	}
467

    
468
	/**
469
	 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
470
	 *
471
	 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
472
	 */
473
	public TaxonBase getInitiallySelectedTaxonBase() {
474
		return initiallySelectedTaxonBase;
475
	}
476
}
(12-12/15)