Project

General

Profile

Download (14.1 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

    
62
	private TextWithLabelElement text_breed;
63
	//private NumberWithLabelElement text_publicationYear;
64
	//private NumberWithLabelElement text_originalPublicationYear;
65
	private int cursorPosition;
66

    
67

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

    
80
	}
81

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

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

    
94
		};
95
		}
96
	    if (isAdvancedView){
97
	    	toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
98

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

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

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

    
130
	}
131

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

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

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

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

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

    
166

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

    
202

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

    
208
	}
209

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

    
214
	}
215

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

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

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

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

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

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

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

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

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