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
		if(cdmFormFactory.getSelectionProvider() != null){
80
			selectionArbitrator = cdmFormFactory.createSelectionArbitrator(this);
81
		}
82
	}
83

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

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

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

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

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

    
129
	}
130

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

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

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

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

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

    
165

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

    
201

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

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

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

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

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

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

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

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

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

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

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

    
306
	}
307

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

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

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

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

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

    
351
	}
352

    
353

    
354

    
355

    
356
}
(6-6/21)