Project

General

Profile

Download (11.4 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.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.model.name.TaxonNameBase;
24
import eu.etaxonomy.cdm.model.taxon.ITreeNode;
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.model.taxon.TaxonNode;
29
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
30
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
31
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
32
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
33
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
34
import eu.etaxonomy.taxeditor.store.CdmStore;
35

    
36
/**
37
 * <p>TaxonEditorInput class.</p>
38
 *
39
 * @author n.hoffmann
40
 * @created 19.03.2009
41
 * @version 1.0
42
 */
43
public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
44

    
45
	private ConversationHolder conversation;
46
	
47
	private TaxonNode taxonNode;
48

    
49
	private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
50
	
51
	private TaxonBase initiallySelectedTaxonBase;
52
	
53
	private TaxonEditorInput(TaxonNode taxonNode, ConversationHolder conversation){
54
		this.conversation = conversation;
55
		this.taxonNode = taxonNode;
56
	}
57
	
58

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

    
139

    
140

    
141

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

    
207
	/* (non-Javadoc)
208
	 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
209
	 */
210
	/**
211
	 * <p>getImageDescriptor</p>
212
	 *
213
	 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
214
	 */
215
	public ImageDescriptor getImageDescriptor() {
216
		return null;
217
	}
218

    
219
	/* (non-Javadoc)
220
	 * @see org.eclipse.ui.IEditorInput#getName()
221
	 */
222
	/**
223
	 * <p>getName</p>
224
	 *
225
	 * @return a {@link java.lang.String} object.
226
	 */
227
	public String getName() {
228
		if(getTaxon() == null){
229
			return null;
230
		}
231
		TaxonNameBase<?, ?> name = getTaxon().getName();
232
		if (name == null || name.getTitleCache() == null) {
233
			return "New taxon";
234
		} else {
235
			return name.getTitleCache();
236
		}	
237
	}
238

    
239
	/* (non-Javadoc)
240
	 * @see org.eclipse.ui.IEditorInput#getPersistable()
241
	 */
242
	/**
243
	 * <p>getPersistable</p>
244
	 *
245
	 * @return a {@link org.eclipse.ui.IPersistableElement} object.
246
	 */
247
	public IPersistableElement getPersistable() {
248
//		if(CdmStore.isActive()){
249
//			TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
250
//			boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
251
//			if (isPersistable) {
252
//				return this;
253
//			}
254
//		}
255
		return null;
256
	}
257

    
258
	/* (non-Javadoc)
259
	 * @see org.eclipse.ui.IEditorInput#getToolTipText()
260
	 */
261
	/**
262
	 * <p>getToolTipText</p>
263
	 *
264
	 * @return a {@link java.lang.String} object.
265
	 */
266
	public String getToolTipText() {
267
		return getName();
268
	}
269

    
270
	/* (non-Javadoc)
271
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
272
	 */
273
	/** {@inheritDoc} */
274
	public Object getAdapter(Class adapter) {
275

    
276
		if (adapter == Taxon.class) {
277
			return taxonNode.getTaxon();
278
		}
279
		
280
		if (adapter == TaxonNode.class) {
281
			return taxonNode;
282
		}
283
		
284
		return null;
285
	}
286
	
287
	/**
288
	 * {@inheritDoc}
289
	 *
290
	 * Overrides equals to ensure that a taxon can only be edited by
291
	 * one editor at a time.
292
	 */
293
	public boolean equals(Object obj) {
294
		if (TaxonEditorInput.class.equals(obj.getClass()) 
295
				&& getTaxon() != null
296
				&& getTaxon().equals(((TaxonEditorInput) obj).getTaxon())){
297
			if(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase() != null){
298
				setInitiallySelectedTaxonBase(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase());
299
			}
300
			return true;
301
		}
302
		return false;
303
	}
304

    
305
	/**
306
	 * <p>getTaxon</p>
307
	 *
308
	 * @return the taxon
309
	 */
310
	public Taxon getTaxon(){
311
		return taxonNode.getTaxon();
312
	}
313
	
314
	/**
315
	 * <p>Getter for the field <code>taxonNode</code>.</p>
316
	 *
317
	 * @return the taxonNode
318
	 */
319
	public TaxonNode getTaxonNode() {
320
		return taxonNode;
321
	}
322
	
323
	/*
324
	 * (non-Javadoc)
325
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
326
	 */
327
	/**
328
	 * <p>getConversationHolder</p>
329
	 *
330
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
331
	 */
332
	public ConversationHolder getConversationHolder() {
333
		return conversation;
334
	}
335

    
336
	/*
337
	 * (non-Javadoc)
338
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
339
	 */
340
	/** {@inheritDoc} */
341
	public void update(CdmDataChangeMap events) {
342
		if(dataChangeBehavior == null){
343
			dataChangeBehavior = new TaxonEditorInputDataChangeBehaviour(this);
344
		}
345
		
346
		DataChangeBridge.handleDataChange(events, dataChangeBehavior);
347
	}
348

    
349
	/* (non-Javadoc)
350
	 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
351
	 */
352
	/**
353
	 * <p>getFactoryId</p>
354
	 *
355
	 * @return a {@link java.lang.String} object.
356
	 */
357
	public String getFactoryId() {
358
		return TaxonEditorInputFactory.getFactoryId();
359
	}
360

    
361
	/* (non-Javadoc)
362
	 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
363
	 */
364
	/** {@inheritDoc} */
365
	public void saveState(IMemento memento) {
366
		TaxonEditorInputFactory.saveState(memento, this);
367
	}
368

    
369
    
370
    /**
371
     * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
372
     *
373
     * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
374
     */
375
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
376
		this.initiallySelectedTaxonBase = taxonBase;
377
	}
378

    
379
	/**
380
	 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
381
	 *
382
	 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
383
	 */
384
	public TaxonBase getInitiallySelectedTaxonBase() {
385
		return initiallySelectedTaxonBase;
386
	}
387
}
(12-12/15)