Project

General

Profile

Download (12.8 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.IFungusName;
21
import eu.etaxonomy.cdm.model.name.INonViralName;
22
import eu.etaxonomy.cdm.model.name.IZoologicalName;
23
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
24
import eu.etaxonomy.cdm.model.name.Rank;
25
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
26
import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
27
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
28
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29
import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
30
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
31
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
32
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
33
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
34
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
35
import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
36
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
37
import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
38

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

    
47
    private final Logger logger = Logger.getLogger(NameDetailElement.class);
48

    
49
	private TermComboElement<Rank> combo_rank;
50
	private TextWithLabelElement text_appendedPhrase;
51
	private TextWithLabelElement text_uninomial;
52
	private TextWithLabelElement text_infragenericEpithet;
53
	private TextWithLabelElement text_specificEpithet;
54
	private TextWithLabelElement text_infraspecificEpithet;
55
	private SelectionArbitrator selectionArbitrator;
56
	private CheckboxElement checkbox_anamorphic;
57
	private TextWithLabelElement text_breed;
58
	//private NumberWithLabelElement text_publicationYear;
59
	//private NumberWithLabelElement text_originalPublicationYear;
60
	private int cursorPosition;
61

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

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

    
85
			@Override
86
			public int compare(Rank o1, Rank o2) {
87
				return o2.compareTo(o1);
88
			}
89

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

    
107
    	    if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_RANK)){
108
    	        combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style, comparator);
109
    	    }
110

    
111
    		if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_ATOMISED_EPITHETS)){
112
    			createGenusOrUninomialControls(this, nonViralName, style);
113
    			createInfragenerericEpithetControls(this, nonViralName, style);
114
    			createSpecificEpithetControls(this, nonViralName, style);
115
    			createInfraSpecificEpithetControls(this, nonViralName, style);
116
    			createSpecificNameParts(this, nonViralName, style);
117
    		}
118
    		if (PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_NAME_DETAILS_SECTION_APPENDED_PHRASE)){
119
    				text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
120
           	}
121
	    }
122

    
123
	}
124

    
125
	/**
126
	 * <p>clearCheckRankWarnings</p>
127
	 */
128
	protected void clearCheckRankWarnings() {
129
		if(getEntity().hasProblem(ParserProblem.CheckRank)){
130
			getEntity().removeParsingProblem(ParserProblem.CheckRank);
131

    
132
			// FIXME this has to be reafctored completely. See tickets #1959, #1663, #1957, #1958
133
//			TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
134
//			nameEditor.getSelectedContainer().getNameViewer().clearErrors();
135
		}
136
	}
137

    
138
	/** {@inheritDoc} */
139
	@Override
140
	protected void updateContent() {
141
		if(getEntity() == null){
142
			setEntity(TaxonNameFactory.NewNonViralInstance(null));
143
		}
144

    
145
		super.updateContent();
146
		if (toggleable_cache != null){
147
		    toggleable_cache.setEnabled(getEntity().isProtectedNameCache());
148

    
149
    		if (isAdvancedView){
150
        		if(isIrrelevant()){
151
        			setIrrelevant(isIrrelevant());
152
        		}else{
153
        			setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
154
        		}
155
    		}
156
		}
157
	}
158

    
159

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

    
197

    
198
	private void createBotanicalNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
199
		//nothing to do
200
	}
201

    
202
	private void createFungusNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
203
		IFungusName fungusName = (IFungusName)nonViralName;
204
		checkbox_anamorphic = formFactory.createCheckbox(formElement, "Anamorphic", fungusName.isAnamorphic(), style);
205
	}
206

    
207
	private void createZoologicalNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
208
		IZoologicalName zoologicalName = (IZoologicalName) nonViralName;
209

    
210
		if (isAdvancedView){
211
		    text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
212
		}
213
	}
214

    
215
	private void createGenusOrUninomialControls(ICdmFormElement element, INonViralName nonViralName, int style){
216
		String title = "Genus";
217
		Rank rank = nonViralName.getRank();
218
		if(rank != null && rank.isSupraGeneric()){
219
			title = "Uninomial";
220
		}
221
		text_uninomial = formFactory.createTextWithLabelElement(element, title, nonViralName.getGenusOrUninomial(), style);
222
	}
223

    
224
	private void createInfragenerericEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
225
		if (nonViralName.getNomenclaturalCode() == null || nonViralName.getNomenclaturalCode().equals(NomenclaturalCode.ICZN)){
226
			if(nonViralName.getRank() != null && nonViralName.getRank().isLower(Rank.GENUS()) ){
227
				text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
228
			}
229
		} else{
230
			if((nonViralName.getRank() != null && nonViralName.getRank().isLower(Rank.GENUS())) && !nonViralName.getRank().isLower(Rank.SPECIES()) && !nonViralName.getRank().isSpeciesAggregate()){
231
				text_infragenericEpithet = formFactory.createTextWithLabelElement(element, "Infrageneric Epithet", nonViralName.getInfraGenericEpithet(), style);
232
			}
233
		}
234
	}
235

    
236
	private void createSpecificEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
237
		if(nonViralName.getRank() != null && (nonViralName.getRank().isSpecies()  || nonViralName.getRank().isInfraSpecific() || nonViralName.getRank().isSpeciesAggregate())){
238
			text_specificEpithet = formFactory.createTextWithLabelElement(element, "Specific Epithet", nonViralName.getSpecificEpithet(), SWT.NULL);
239
		}
240
	}
241

    
242
	private void createInfraSpecificEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
243
		if(nonViralName.getRank() != null && nonViralName.getRank().isInfraSpecific()){
244
			text_infraspecificEpithet = formFactory.createTextWithLabelElement(element, "Infraspecific Epithet", nonViralName.getInfraSpecificEpithet(), SWT.NULL);
245
		}
246
	}
247

    
248
	/** {@inheritDoc} */
249
	@Override
250
	public void handleEvent(Object eventSource){
251
		if (eventSource == toggleable_cache) {
252
			getEntity().setNameCache(toggleable_cache.getText(),
253
					toggleable_cache.getState());
254
			//if (!isIrrelevant()) {
255
                setIrrelevant(toggleable_cache.getState(),
256
						Arrays.asList(new Object[] { toggleable_cache }));
257
            //}
258
		}
259
		else if(eventSource == combo_rank){
260
			getEntity().setRank(combo_rank.getSelection());
261
			clearCheckRankWarnings();
262
			updateContent();
263
		}
264
		else if(eventSource == text_appendedPhrase){
265
			getEntity().setAppendedPhrase(text_appendedPhrase.getText());
266
		}
267
		else if(eventSource == text_infragenericEpithet){
268
			getEntity().setInfraGenericEpithet(text_infragenericEpithet.getText());
269
		}
270
		else if(eventSource == text_infraspecificEpithet){
271
			getEntity().setInfraSpecificEpithet(text_infraspecificEpithet.getText());
272
		}
273
		else if(eventSource == text_specificEpithet){
274
			getEntity().setSpecificEpithet(text_specificEpithet.getText());
275
		}
276
		else if(eventSource == text_uninomial){
277
			getEntity().setGenusOrUninomial(text_uninomial.getText());
278
		}
279
		else if(eventSource == checkbox_anamorphic){
280
			((IFungusName)getEntity()).setAnamorphic(checkbox_anamorphic.getSelection());
281
		}else if(eventSource == text_breed){
282
            ((IZoologicalName)getEntity()).setBreed(text_breed.getText());
283
        }
284

    
285
	}
286

    
287
	/*
288
	 * (non-Javadoc)
289
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
290
	 */
291
	/**
292
	 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
293
	 *
294
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator} object.
295
	 */
296
	@Override
297
    public SelectionArbitrator getSelectionArbitrator() {
298
		return selectionArbitrator;
299
	}
300

    
301
	@Override
302
	public void updateToggleableCacheField() {
303
	    if (toggleable_cache != null){
304
    		if(! getEntity().isProtectedNameCache()){
305
    			toggleable_cache.setText(getEntity().getNameCache());
306
    		}
307
	    }
308
	}
309

    
310
	@Override
311
	protected void handleToggleableCacheField() {
312
        boolean pushedState = toggleable_cache.getState();
313

    
314
        getEntity().setTitleCache(toggleable_cache.getText(), pushedState);
315
        setIrrelevant(pushedState, Arrays.asList(new Object[] { toggleable_cache, text_appendedPhrase, checkbox_anamorphic }));
316
        updateToggleableCacheField();
317
    }
318

    
319
	@Override
320
	public void setIrrelevant(boolean irrelevant, Collection<Object> except){
321
	    if (toggleable_cache != null){
322
    	    if (toggleable_cache.getState() == irrelevant){
323
    	        super.setIrrelevant(irrelevant, except);
324
    	    }
325
    	    if (except == null || except.isEmpty()){
326
    	        toggleable_cache.setIrrelevant(irrelevant);
327
    	    }
328
	    }
329

    
330
	}
331

    
332

    
333

    
334

    
335
}
(6-6/21)