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.NameDetailsConfigurator;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.store.StoreUtil;
32
import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
33
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
35
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
36
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
37
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
38
import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
39
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
40
import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
41

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

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

    
52
	private TermComboElement<Rank> combo_rank;
53
	private TextWithLabelElement text_appendedPhrase;
54
	private TextWithLabelElement text_uninomial;
55
	private TextWithLabelElement text_infragenericEpithet;
56
	private TextWithLabelElement text_specificEpithet;
57
	private TextWithLabelElement text_infraspecificEpithet;
58
	private SelectionArbitrator selectionArbitrator;
59
	private CheckboxElement checkbox_anamorphic;
60
	private TextWithLabelElement text_cultivarName;
61
	private TextWithLabelElement text_subGenusAuthorship;
62

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

    
68
	NameDetailsConfigurator config;
69

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

    
82

    
83
	}
84

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

    
91
		if(PreferencesUtil.getSortRanksHierarchichally()){
92
			comparator = new Comparator<Rank>(){
93

    
94
			@Override
95
			public int compare(Rank o1, Rank o2) {
96
				return o2.compareTo(o1);
97
			}
98

    
99
		};
100
		}
101
	    if (isAdvancedView){
102
	    	toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
103

    
104
	    	combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style, comparator);
105
	    	createGenusOrUninomialControls(this, nonViralName, style);
106
            createInfragenerericEpithetControls(this, nonViralName, style);
107
            createSpecificEpithetControls(this, nonViralName, style);
108
            createInfraSpecificEpithetControls(this, nonViralName, style);
109
            createSpecificNameParts(this, nonViralName, style);
110
            text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
111
	    }else{
112
	    	if (config.isNameCacheActivated()){
113
	    		toggleable_cache = formFactory.createToggleableTextField(this, "Name Cache", nonViralName.getNameCache(), nonViralName.isProtectedNameCache(), style);
114
        	}
115

    
116
    	    if (config.isRankActivated()){
117
    	        combo_rank = formFactory.createDefinedTermComboElement(TermType.Rank, this, "Rank", nonViralName.getRank(), style, comparator);
118
    	    }
119

    
120
    		if (config.isAtomisedEpithetsActivated()){
121
    			createGenusOrUninomialControls(this, nonViralName, style);
122
    			createInfragenerericEpithetControls(this, nonViralName, style);
123
    			createSpecificEpithetControls(this, nonViralName, style);
124
    			createInfraSpecificEpithetControls(this, nonViralName, style);
125
    			createSpecificNameParts(this, nonViralName, style);
126
    		}
127
    		if (config.isAppendedPhraseActivated()){
128
    				text_appendedPhrase = formFactory.createTextWithLabelElement(formElement, "Appended Phrase", nonViralName.getAppendedPhrase(), style);
129
           	}
130
	    }
131
	    if(this.getFormFactory().getSelectionProvider() != null){
132
            selectionArbitrator = this.getFormFactory().createSelectionArbitrator(this);
133
        }
134

    
135
	}
136

    
137
	/**
138
	 * <p>clearCheckRankWarnings</p>
139
	 */
140
	protected void clearCheckRankWarnings() {
141
		if(getEntity().hasProblem(ParserProblem.CheckRank)){
142
			getEntity().removeParsingProblem(ParserProblem.CheckRank);
143

    
144
			// FIXME this has to be reafctored completely. See tickets #1959, #1663, #1957, #1958
145
//			TaxonNameEditor nameEditor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
146
//			nameEditor.getSelectedContainer().getNameViewer().clearErrors();
147
		}
148
	}
149

    
150
	/** {@inheritDoc} */
151
	@Override
152
	public void updateContent() {
153
		if(getEntity() == null){
154
			setEntity(TaxonNameFactory.NewNonViralInstance(null));
155
		}
156

    
157
		super.updateContent();
158
		if (toggleable_cache != null){
159
		    toggleable_cache.setEnabled(getEntity().isProtectedNameCache());
160

    
161
    		if (isAdvancedView){
162
        		if(isIrrelevant()){
163
        			setIrrelevant(isIrrelevant());
164
        		}else{
165
        			setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
166
        		}
167
    		}
168
		}
169
	}
170

    
171

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

    
207

    
208
	private void createBacterialNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style) {
209
		IBacterialName bacterialName = (IBacterialName) nonViralName;
210
		text_subGenusAuthorship = formFactory.createTextWithLabelElement(formElement, "Subgenus Authorship", bacterialName.getSubGenusAuthorship(), style);
211
//		text_nameApprobation = formFactory.createTextWithLabelElement(formElement, "Name Approbation", bacterialName.getNameApprobation(), style);
212

    
213
	}
214

    
215
	private void createCultivarNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style) {
216
		ICultivarPlantName cultivarName = (ICultivarPlantName)nonViralName;
217
		text_cultivarName = formFactory.createTextWithLabelElement(formElement, "Cultivar Name", cultivarName.getCultivarName(), style);
218

    
219
	}
220

    
221
	private void createBotanicalNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
222
		//nothing to do
223
	}
224

    
225
	private void createFungusNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
226
		IFungusName fungusName= (IFungusName)nonViralName;
227
		checkbox_anamorphic = formFactory.createCheckbox(formElement, "Anamorphic", fungusName.isAnamorphic(), style);
228
	}
229

    
230
	private void createZoologicalNameParts(ICdmFormElement formElement, INonViralName nonViralName, int style){
231
		IZoologicalName zoologicalName = (IZoologicalName) nonViralName;
232

    
233
		if (isAdvancedView){
234
		    text_breed = formFactory.createTextWithLabelElement(formElement, "Breed", zoologicalName.getBreed(), style);
235
		}
236
	}
237

    
238
	private void createGenusOrUninomialControls(ICdmFormElement element, INonViralName nonViralName, int style){
239
		String title = "Genus";
240
		Rank rank = nonViralName.getRank();
241
		if(rank != null && rank.isSupraGeneric()){
242
			title = "Uninomial";
243
		}
244
		text_uninomial = formFactory.createTextWithLabelElement(element, title, nonViralName.getGenusOrUninomial(), style);
245
	}
246

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

    
259
	private void createSpecificEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
260
		if(nonViralName.getRank() != null && (nonViralName.getRank().isSpecies()  || nonViralName.getRank().isInfraSpecific() || nonViralName.getRank().isSpeciesAggregate())){
261
			text_specificEpithet = formFactory.createTextWithLabelElement(element, "Specific Epithet", nonViralName.getSpecificEpithet(), SWT.NULL);
262
		}
263
	}
264

    
265
	private void createInfraSpecificEpithetControls(ICdmFormElement element, INonViralName nonViralName, int style){
266
		if(nonViralName.getRank() != null && nonViralName.getRank().isInfraSpecific()){
267
			text_infraspecificEpithet = formFactory.createTextWithLabelElement(element, "Infraspecific Epithet", nonViralName.getInfraSpecificEpithet(), SWT.NULL);
268
		}
269
	}
270

    
271
	/** {@inheritDoc} */
272
	@Override
273
	public void handleEvent(Object eventSource){
274
		if (eventSource == toggleable_cache) {
275
			getEntity().setNameCache(toggleable_cache.getText(),
276
					toggleable_cache.getState());
277
			//if (!isIrrelevant()) {
278
                setIrrelevant(toggleable_cache.getState(),
279
						Arrays.asList(new Object[] { toggleable_cache }));
280
            //}
281
		}
282
		else if(eventSource == combo_rank){
283
			getEntity().setRank(combo_rank.getSelection());
284
			clearCheckRankWarnings();
285
			updateContent();
286
            //this is needed for the "new" wizards in order to redraw correctly
287
            StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
288
		}
289
		else if(eventSource == text_appendedPhrase){
290
			getEntity().setAppendedPhrase(text_appendedPhrase.getText());
291
		}
292
		else if(eventSource == text_infragenericEpithet){
293
			getEntity().setInfraGenericEpithet(text_infragenericEpithet.getText());
294
		}
295
		else if(eventSource == text_infraspecificEpithet){
296
			getEntity().setInfraSpecificEpithet(text_infraspecificEpithet.getText());
297
		}
298
		else if(eventSource == text_specificEpithet){
299
			getEntity().setSpecificEpithet(text_specificEpithet.getText());
300
		}
301
		else if(eventSource == text_uninomial){
302
			getEntity().setGenusOrUninomial(text_uninomial.getText());
303
		}
304
		else if(eventSource == checkbox_anamorphic){
305
			((IFungusName)getEntity()).setAnamorphic(checkbox_anamorphic.getSelection());
306
		}else if(eventSource == text_breed){
307
            ((IZoologicalName)getEntity()).setBreed(text_breed.getText());
308
        }else if(eventSource == this.text_cultivarName){
309
            ((ICultivarPlantName)getEntity()).setCultivarName(text_cultivarName.getText());
310
        }else if(eventSource == this.text_subGenusAuthorship){
311
            ((IBacterialName)getEntity()).setSubGenusAuthorship(text_subGenusAuthorship.getText());
312
        }
313

    
314
	}
315

    
316
	/*
317
	 * (non-Javadoc)
318
	 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#getSelectionArbitrator()
319
	 */
320
	/**
321
	 * <p>Getter for the field <code>selectionArbitrator</code>.</p>
322
	 *
323
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator} object.
324
	 */
325
	@Override
326
    public SelectionArbitrator getSelectionArbitrator() {
327
		return selectionArbitrator;
328
	}
329

    
330
	@Override
331
	public void updateToggleableCacheField() {
332
	    if (toggleable_cache != null){
333
    		if(! getEntity().isProtectedNameCache()){
334
    			toggleable_cache.setText(getEntity().getNameCache());
335
    		}
336
	    }
337
	}
338

    
339
	@Override
340
	protected void handleToggleableCacheField() {
341
        boolean pushedState = toggleable_cache.getState();
342

    
343
        getEntity().setTitleCache(toggleable_cache.getText(), pushedState);
344
        setIrrelevant(pushedState, Arrays.asList(new Object[] { toggleable_cache, text_appendedPhrase, checkbox_anamorphic }));
345
        updateToggleableCacheField();
346
    }
347

    
348
	@Override
349
	public void setIrrelevant(boolean irrelevant, Collection<Object> except){
350
	    if (toggleable_cache != null){
351
    	    if (toggleable_cache.getState() == irrelevant){
352
    	        super.setIrrelevant(irrelevant, except);
353
    	    }
354
    	    if (except == null || except.isEmpty()){
355
    	        toggleable_cache.setIrrelevant(irrelevant);
356
    	    }
357
	    }
358

    
359
	}
360

    
361

    
362

    
363

    
364
}
(6-6/21)