Project

General

Profile

Download (14.3 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.ui.section.name;
11

    
12
import java.util.Arrays;
13
import java.util.Collection;
14
import java.util.Comparator;
15

    
16
import org.apache.log4j.Logger;
17
import org.eclipse.swt.SWT;
18

    
19
import eu.etaxonomy.cdm.model.common.TermType;
20
import eu.etaxonomy.cdm.model.name.IBacterialName;
21
import eu.etaxonomy.cdm.model.name.ICultivarPlantName;
22
import eu.etaxonomy.cdm.model.name.IFungusName;
23
import eu.etaxonomy.cdm.model.name.INonViralName;
24
import eu.etaxonomy.cdm.model.name.IZoologicalName;
25
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
26
import eu.etaxonomy.cdm.model.name.Rank;
27
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
28
import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
29
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
32
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
33
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
34
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
35
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
36
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
37
import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
38
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
39
import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
40

    
41
/**
42
 * <p>NameDetailElement class.</p>
43
 *
44
 * @author n.hoffmann
45
 * @created Feb 26, 2010
46
 */
47
public class NameDetailElement extends AbstractIdentifiableEntityDetailElement<INonViralName> implements ISelectableElement, IEnableableFormElement{
48

    
49
    private final Logger logger = Logger.getLogger(NameDetailElement.class);
50

    
51
	private TermComboElement<Rank> combo_rank;
52
	private TextWithLabelElement text_appendedPhrase;
53
	private TextWithLabelElement text_uninomial;
54
	private TextWithLabelElement text_infragenericEpithet;
55
	private TextWithLabelElement text_specificEpithet;
56
	private TextWithLabelElement text_infraspecificEpithet;
57
	private SelectionArbitrator selectionArbitrator;
58
	private CheckboxElement checkbox_anamorphic;
59
	private TextWithLabelElement text_cultivarName;
60
	private TextWithLabelElement text_subGenusAuthorship;
61
	private TextWithLabelElement text_nameApprobation;
62
	private TextWithLabelElement text_breed;
63
	//private NumberWithLabelElement text_publicationYear;
64
	//private NumberWithLabelElement text_originalPublicationYear;
65
	private int cursorPosition;
66

    
67
	/**
68
	 * <p>Constructor for NameDetailElement.</p>
69
	 *
70
	 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
71
	 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
72
	 * @param style a int.
73
	 */
74
	public NameDetailElement(CdmFormFactory cdmFormFactory, ICdmFormElement formElement,
75
			int style) {
76
		super(cdmFormFactory, formElement);
77
		// make this element selectable
78
		if(cdmFormFactory.getSelectionProvider() != null){
79
			selectionArbitrator = cdmFormFactory.createSelectionArbitrator(this);
80
		}
81
	}
82

    
83
	/** {@inheritDoc} */
84
	@Override
85
	protected void createControls(ICdmFormElement formElement, INonViralName nonViralName, int style) {
86
		Comparator<Rank> comparator = null;
87
		if(PreferencesUtil.getSortRanksHierarchichally()){
88
			comparator = new Comparator<Rank>(){
89

    
90
			@Override
91
			public int compare(Rank o1, Rank o2) {
92
				return o2.compareTo(o1);
93
			}
94

    
95
		};
96
		}
97
	    if (isAdvancedView){
98
	    	toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
99
	    	
100
	    	combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style, comparator);
101
	    	createGenusOrUninomialControls(this, nonViralName, style);
102
            createInfragenerericEpithetControls(this, nonViralName, style);
103
            createSpecificEpithetControls(this, nonViralName, style);
104
            createInfraSpecificEpithetControls(this, nonViralName, style);
105
            createSpecificNameParts(this, nonViralName, style);
106
            text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
107
	    }else{
108
	    	if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_NAMECACHE)){
109
	    		toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
110
        	}
111

    
112
    	    if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK)){
113
    	        combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style, comparator);
114
    	    }
115

    
116
    		if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS)){
117
    			createGenusOrUninomialControls(this, nonViralName, style);
118
    			createInfragenerericEpithetControls(this, nonViralName, style);
119
    			createSpecificEpithetControls(this, nonViralName, style);
120
    			createInfraSpecificEpithetControls(this, nonViralName, style);
121
    			createSpecificNameParts(this, nonViralName, style);
122
    		}
123
    		if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE)){
124
    				text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
125
           	}
126
	    }
127

    
128
	}
129

    
130
	/**
131
	 * <p>clearCheckRankWarnings</p>
132
	 */
133
	protected void clearCheckRankWarnings() {
134
		if(getEntity().hasProblem(ParserProblem.CheckRank)){
135
			getEntity().removeParsingProblem(ParserProblem.CheckRank);
136

    
137
			// FIXME this has to be reafctored completely. See tickets #1959, #1663, #1957, #1958
138
//			TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
139
//			nameEditor.getSelectedContainer().getNameViewer().clearErrors();
140
		}
141
	}
142

    
143
	/** {@inheritDoc} */
144
	@Override
145
	public void updateContent() {
146
		if(getEntity() == null){
147
			setEntity(TaxonNameFactory.NewNonViralInstance(null));
148
		}
149

    
150
		super.updateContent();
151
		if (toggleable_cache != null){
152
		    toggleable_cache.setEnabled(getEntity().isProtectedNameCache());
153

    
154
    		if (isAdvancedView){
155
        		if(isIrrelevant()){
156
        			setIrrelevant(isIrrelevant());
157
        		}else{
158
        			setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
159
        		}
160
    		}
161
		}
162
	}
163

    
164

    
165
	private void createSpecificNameParts(ICdmFormElement formElement,
166
			INonViralName nonViralName, int style) {
167
		NomenclaturalCode code = nonViralName.getNomenclaturalCode();
168
		if (code != null){
169
			switch(nonViralName.getNomenclaturalCode()){
170
			case ICNAFP :
171
				//nothing do add
172
				createBotanicalNameParts(formElement, nonViralName, style);
173
				break;
174
			case Fungi :
175
				if (isAdvancedView){
176
					createFungusNameParts(formElement, nonViralName, style);
177
				}
178
				break;
179
			case ICZN:
180
				createZoologicalNameParts(formElement, nonViralName, style);
181
				break;
182
            case ICVCN:
183
                //TODO implement
184
                logger.warn("ICVCN not yet implemented");
185
                break;
186
            case ICNB:
187
            	createBacterialNameParts(formElement, nonViralName, style);
188
                break;
189
            case ICNCP:
190
            	createCultivarNameParts(formElement, nonViralName, style);
191
                break;
192
            case NonViral:
193
                //TODO implement
194
                logger.warn("NonViral not yet implemented");
195
                break;
196
            }
197
		}
198
	}
199

    
200

    
201
	private void createBacterialNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style) {
202
		IBacterialName bacterialName = (IBacterialName) nonViralName;
203
		text_subGenusAuthorship = formFactory.createTextWithLabelElement(formElement, "Subgenus Authorship", bacterialName.getSubGenusAuthorship(), style);
204
		text_nameApprobation = formFactory.createTextWithLabelElement(formElement, "Name Approbation", bacterialName.getNameApprobation(), style);
205
		
206
	}
207

    
208
	private void createCultivarNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style) {
209
		ICultivarPlantName cultivarName = (ICultivarPlantName)nonViralName;
210
		text_cultivarName = formFactory.createTextWithLabelElement(formElement, "Cultivar Name", cultivarName.getCultivarName(), style);
211
		
212
	}
213

    
214
	private void createBotanicalNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
215
		//nothing to do
216
	}
217

    
218
	private void createFungusNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
219
		IFungusName fungusName= (IFungusName)nonViralName;
220
		checkbox_anamorphic = formFactory.createCheckbox(formElement, "Anamorphic", fungusName.isAnamorphic(), style);
221
	}
222

    
223
	private void createZoologicalNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
224
		IZoologicalName zoologicalName = (IZoologicalName) nonViralName;
225

    
226
		if (isAdvancedView){
227
		    text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
228
		}
229
	}
230

    
231
	private void createGenusOrUninomialControls(ICdmFormElement element, INonViralName nonViralName, int style){
232
		String title = "Genus";
233
		Rank rank = nonViralName.getRank();
234
		if(rank != null && rank.isSupraGeneric()){
235
			title = "Uninomial";
236
		}
237
		text_uninomial = formFactory.createTextWithLabelElement(element, title, nonViralName.getGenusOrUninomial(), style);
238
	}
239

    
240
	private void createInfragenerericEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
241
		if (nonViralName.getNomenclaturalCode() == null || nonViralName.getNomenclaturalCode().equals(NomenclaturalCode.ICZN)){
242
			if(nonViralName.getRank() != null && nonViralName.getRank().isLower(Rank.GENUS()) ){
243
				text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
244
			}
245
		} else{
246
			if((nonViralName.getRank() != null && nonViralName.getRank().isLower(Rank.GENUS())) && !nonViralName.getRank().isLower(Rank.SPECIES()) && !nonViralName.getRank().isSpeciesAggregate()){
247
				text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
248
			}
249
		}
250
	}
251

    
252
	private void createSpecificEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
253
		if(nonViralName.getRank() != null && (nonViralName.getRank().isSpecies()  || nonViralName.getRank().isInfraSpecific() || nonViralName.getRank().isSpeciesAggregate())){
254
			text_specificEpithet = formFactory.createTextWithLabelElement(element, "Specific Epithet", nonViralName.getSpecificEpithet(), SWT.NULL);
255
		}
256
	}
257

    
258
	private void createInfraSpecificEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
259
		if(nonViralName.getRank() != null && nonViralName.getRank().isInfraSpecific()){
260
			text_infraspecificEpithet = formFactory.createTextWithLabelElement(element, "Infraspecific Epithet", nonViralName.getInfraSpecificEpithet(), SWT.NULL);
261
		}
262
	}
263

    
264
	/** {@inheritDoc} */
265
	@Override
266
	public void handleEvent(Object eventSource){
267
		if (eventSource == toggleable_cache) {
268
			getEntity().setNameCache(toggleable_cache.getText(),
269
					toggleable_cache.getState());
270
			//if (!isIrrelevant()) {
271
                setIrrelevant(toggleable_cache.getState(),
272
						Arrays.asList(new Object[] { toggleable_cache }));
273
            //}
274
		}
275
		else if(eventSource == combo_rank){
276
			getEntity().setRank(combo_rank.getSelection());
277
			clearCheckRankWarnings();
278
			updateContent();
279
		}
280
		else if(eventSource == text_appendedPhrase){
281
			getEntity().setAppendedPhrase(text_appendedPhrase.getText());
282
		}
283
		else if(eventSource == text_infragenericEpithet){
284
			getEntity().setInfraGenericEpithet(text_infragenericEpithet.getText());
285
		}
286
		else if(eventSource == text_infraspecificEpithet){
287
			getEntity().setInfraSpecificEpithet(text_infraspecificEpithet.getText());
288
		}
289
		else if(eventSource == text_specificEpithet){
290
			getEntity().setSpecificEpithet(text_specificEpithet.getText());
291
		}
292
		else if(eventSource == text_uninomial){
293
			getEntity().setGenusOrUninomial(text_uninomial.getText());
294
		}
295
		else if(eventSource == checkbox_anamorphic){
296
			((IFungusName)getEntity()).setAnamorphic(checkbox_anamorphic.getSelection());
297
		}else if(eventSource == text_breed){
298
            ((IZoologicalName)getEntity()).setBreed(text_breed.getText());
299
        }else if(eventSource == this.text_cultivarName){
300
            ((ICultivarPlantName)getEntity()).setCultivarName(text_cultivarName.getText());
301
        }else if(eventSource == this.text_subGenusAuthorship){
302
            ((IBacterialName)getEntity()).setSubGenusAuthorship(text_subGenusAuthorship.getText());
303
        }else if(eventSource == this.text_nameApprobation){
304
            ((IBacterialName)getEntity()).setNameApprobation(text_nameApprobation.getText());
305
        }
306

    
307
	}
308

    
309
	/*
310
	 * (non-Javadoc)
311
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
312
	 */
313
	/**
314
	 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
315
	 *
316
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator} object.
317
	 */
318
	@Override
319
    public SelectionArbitrator getSelectionArbitrator() {
320
		return selectionArbitrator;
321
	}
322

    
323
	@Override
324
	public void updateToggleableCacheField() {
325
	    if (toggleable_cache != null){
326
    		if(! getEntity().isProtectedNameCache()){
327
    			toggleable_cache.setText(getEntity().getNameCache());
328
    		}
329
	    }
330
	}
331

    
332
	@Override
333
	protected void handleToggleableCacheField() {
334
        boolean pushedState = toggleable_cache.getState();
335

    
336
        getEntity().setTitleCache(toggleable_cache.getText(), pushedState);
337
        setIrrelevant(pushedState, Arrays.asList(new Object[] { toggleable_cache, text_appendedPhrase, checkbox_anamorphic }));
338
        updateToggleableCacheField();
339
    }
340

    
341
	@Override
342
	public void setIrrelevant(boolean irrelevant, Collection<Object> except){
343
	    if (toggleable_cache != null){
344
    	    if (toggleable_cache.getState() == irrelevant){
345
    	        super.setIrrelevant(irrelevant, except);
346
    	    }
347
    	    if (except == null || except.isEmpty()){
348
    	        toggleable_cache.setIrrelevant(irrelevant);
349
    	    }
350
	    }
351

    
352
	}
353

    
354

    
355

    
356

    
357
}
(6-6/21)