Project

General

Profile

Download (12.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
					"synonymRelations",
325
					"relationsToThisTaxon",
326
					"relationsFromThisTaxon",
327
					// taxon relations 
328
					"relationsToThisName.fromTaxon.name",
329
					// the name
330
					"name",
331
					"name.homotypicalGroup",
332
					"name.homotypicalGroup.typifiedNames",
333
					"name.rank.representations",
334
					"name.status.type.representations",
335
					
336
					// taxon descriptions
337
					"descriptions.elements.area.$",
338
					"descriptions.elements.multilanguageText",
339
					"descriptions.elements.media.representations.parts",
340
					"descriptions.elements.media.title",
341
					
342
					});
343
			
344
			taxon = (Taxon) CdmStore.getService(ITaxonService.class).load(taxonNode.getTaxon().getUuid(), TAXON_INIT_STRATEGY);
345
		}
346
		
347
		return taxon;
348
	}
349
	
350
	/**
351
	 * <p>Getter for the field <code>taxonNode</code>.</p>
352
	 *
353
	 * @return the taxonNode
354
	 */
355
	public TaxonNode getTaxonNode() {
356
		return taxonNode;
357
	}
358
	
359
	/*
360
	 * (non-Javadoc)
361
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
362
	 */
363
	/**
364
	 * <p>getConversationHolder</p>
365
	 *
366
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
367
	 */
368
	public ConversationHolder getConversationHolder() {
369
		return conversation;
370
	}
371

    
372
	/*
373
	 * (non-Javadoc)
374
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
375
	 */
376
	/** {@inheritDoc} */
377
	public void update(CdmDataChangeMap events) {
378
		if(dataChangeBehavior == null){
379
			dataChangeBehavior = new TaxonEditorInputDataChangeBehaviour(this);
380
		}
381
		
382
		DataChangeBridge.handleDataChange(events, dataChangeBehavior);
383
	}
384

    
385
	/* (non-Javadoc)
386
	 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
387
	 */
388
	/**
389
	 * <p>getFactoryId</p>
390
	 *
391
	 * @return a {@link java.lang.String} object.
392
	 */
393
	public String getFactoryId() {
394
		return TaxonEditorInputFactory.getFactoryId();
395
	}
396

    
397
	/* (non-Javadoc)
398
	 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
399
	 */
400
	/** {@inheritDoc} */
401
	public void saveState(IMemento memento) {
402
		TaxonEditorInputFactory.saveState(memento, this);
403
	}
404

    
405
    
406
    /**
407
     * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
408
     *
409
     * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
410
     */
411
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
412
		this.initiallySelectedTaxonBase = taxonBase;
413
	}
414

    
415
	/**
416
	 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
417
	 *
418
	 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
419
	 */
420
	public TaxonBase getInitiallySelectedTaxonBase() {
421
		return initiallySelectedTaxonBase;
422
	}
423
}
(12-12/15)