Project

General

Profile

Download (15.2 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.classification;
11

    
12
import org.apache.commons.lang.StringUtils;
13
import org.eclipse.swt.widgets.Control;
14
import org.eclipse.swt.widgets.Text;
15

    
16
import eu.etaxonomy.cdm.common.CdmUtils;
17
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
18
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
19
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
20
import eu.etaxonomy.cdm.model.name.TaxonName;
21
import eu.etaxonomy.cdm.model.reference.Reference;
22
import eu.etaxonomy.cdm.model.taxon.Classification;
23
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
27
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
29
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
30
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
31
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
32
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
33
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
34
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
35
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
36
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
37

    
38
/**
39
 *
40
 * @author n.hoffmann
41
 * @created Sep 27, 2010
42
 * @version 1.0
43
 */
44
public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeNode> {
45

    
46
    private boolean createNew = false;
47

    
48
	private EntitySelectionElement<Classification> selection_classification;
49

    
50
	private Classification classification;
51

    
52
	private TaxonNodeSelectionElement selection_parentTaxonNode;
53

    
54
	private ITaxonTreeNode parentTreeNode;
55

    
56
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
57
	private EntitySelectionElement<Reference> selection_SecRef;
58
	private EntitySelectionElement<Reference> selection_Ref;
59
	private TextWithLabelElement microReference;
60

    
61
	private Taxon taxon;
62

    
63
	private TextWithLabelElement textNewTaxonName;
64

    
65
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
66

    
67
	private CheckboxElement checkbox_openInEditor;
68

    
69
	private CheckboxElement checkbox_unplaced;
70

    
71
	private CheckboxElement checkbox_excluded;
72
	private CheckboxElement checkbox_publish;
73

    
74

    
75

    
76
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
77

    
78
	private boolean openInEditor;
79

    
80
	private boolean complete;
81

    
82
	private boolean excluded = false;
83
	private boolean unplaced = false;
84

    
85
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
86

    
87
	public TaxonNodeDetailElement(CdmFormFactory formFactory,
88
			ICdmFormElement formElement, boolean isCreateNew) {
89
		super(formFactory, formElement);
90
		createNew = isCreateNew;
91
	}
92

    
93

    
94

    
95
	/** {@inheritDoc} */
96
	@Override
97
	protected void createControls(ICdmFormElement formElement,
98
	        ITaxonTreeNode entity, int style) {
99
		selection_classification = formFactory
100
				.createSelectionElement(Classification.class,//getConversationHolder(),
101
				        formElement, "Classification",
102
						null, EntitySelectionElement.DELETABLE, style);
103
		selection_parentTaxonNode = formFactory
104
				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
105
						EntitySelectionElement.DELETABLE, style);
106

    
107
		selection_reuseExistingTaxon = formFactory
108
				.createSelectionElement(Taxon.class,//getConversationHolder(),
109
				        formElement,
110
						"Reuse existing taxon", null,
111
						EntitySelectionElement.DELETABLE, style);
112

    
113
		selection_reuseExistingName = formFactory
114
				.createSelectionElement(TaxonName.class,//getConversationHolder(),
115
				        formElement,
116
						"Reuse existing name", null,
117
						EntitySelectionElement.DELETABLE, style);
118
		selection_SecRef = formFactory
119
                .createSelectionElement(Reference.class,//getConversationHolder(),
120
                        formElement, "Secundum Reference", null,
121
                        EntitySelectionElement.DELETABLE, style);
122
		selection_Ref = formFactory
123
                .createSelectionElement(Reference.class,//getConversationHolder(),
124
                        formElement, "Parent Child Relationship Reference", null,
125
                        EntitySelectionElement.DELETABLE, style);
126
		microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
127
		if (!isCreateNew()){
128
		    selection_Ref.setEntity(entity.getReference());
129
        }
130
		if (isCreateNew()){
131
		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
132
		    textNewTaxonName.setFocus();
133
		} else{
134
		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
135

    
136
		}
137

    
138
		if (isCreateNew()){
139
            setParentTreeNode(entity);
140
        } else{
141
            setTreeNode(entity);
142
            complete = true;
143
        }
144
		if (entity instanceof TaxonNode) {
145
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
146
		            "Taxon is unplaced", ((TaxonNode)entity).isUnplaced(), style);
147
		}
148

    
149
		if (!isCreateNew()){
150
            selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
151
            selectionNodeAgentRelation.setEntity((TaxonNode)entity);
152
            selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
153
        }
154
		preFillTaxonName();
155

    
156
		if (entity instanceof TaxonNode) {
157
		    checkbox_excluded = formFactory.createCheckbox(formElement,
158
		            "Taxon is excluded", ((TaxonNode)entity).isExcluded(), style);
159
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", ((TaxonNode)entity).getExcludedNote(), 50, style);
160
		    multiLanguageTextExcludedNotes.setEnabled(((TaxonNode) entity).isExcluded());
161

    
162
		    checkbox_publish = formFactory.createCheckbox(formElement,
163
                        "Taxon is public", ((TaxonNode)entity).getTaxon().isPublish(), style);
164

    
165
		    if(isCreateNew()){
166
		        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
167
	            if (defaultPublish != null && defaultPublish.getValue().equals(IPreferenceKeys.PUBLISH)){
168
	                checkbox_publish.setSelection(true);
169
	            } else if (defaultPublish != null && defaultPublish.getValue().equals(IPreferenceKeys.DO_NOT_PUBLISH)){
170
	                checkbox_publish.setSelection(false);
171
	            }else{
172
	                if((this.getParentTreeNode()).isInstanceOf(TaxonNode.class)){
173
	                    checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
174
	                }else{
175
	                    checkbox_publish.setSelection(true);
176
	                }
177
	            }
178
		    }
179
		}
180
		checkbox_openInEditor = formFactory.createCheckbox(formElement,
181
		        "Open in Editor", true, style);
182
		setOpenInEditor(true);
183
	}
184

    
185
	private void preFillTaxonName() {
186
        if(getEntity() instanceof TaxonNode ){
187
            TaxonNode node = (TaxonNode)getEntity();
188
            if(node.getTaxon()!=null){
189
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
190
                if(taxon.getName()!=null){
191
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
192
                    if (!isCreateNew()){
193
                        textNewTaxonName.setText(name.getNameCache());
194
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
195
                        String taxonName = "";
196
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
197
                            taxonName = name.getGenusOrUninomial();
198
                        }
199
                        else if(name.isSpecies() || name.isInfraSpecific() ){
200
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
201
                        }
202
                        if (StringUtils.isNotBlank(taxonName)){
203
                        	textNewTaxonName.setText(taxonName + " ");
204
                        	if(textNewTaxonName.getMainControl() instanceof Text){
205
                        		Text text = (Text)textNewTaxonName.getMainControl();
206
                        		text.setSelection(textNewTaxonName.getText().length());
207
                        	}
208
                        }
209
                    }
210
                }
211
            }
212
        }
213
    }
214

    
215
	/** {@inheritDoc} */
216
	@Override
217
	public void handleEvent(Object eventSource) {
218
		if (eventSource == selection_classification) {
219
			setClassification(selection_classification.getEntity());
220
		} else if (eventSource == selection_parentTaxonNode) {
221
			setParentTreeNode(selection_parentTaxonNode.getEntity());
222
		} else if (eventSource == selection_reuseExistingTaxon) {
223
				boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
224
				selection_reuseExistingName.setEnabled(enabled);
225
				if (!enabled){
226
                    setTaxon(selection_reuseExistingTaxon.getEntity());
227
                }else{
228
			        textNewTaxonName.setText(null);
229
			    }
230
				complete = !textNewTaxonName.getText().isEmpty();
231

    
232

    
233
		} else if (eventSource == selection_reuseExistingName) {
234
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
235
			setTaxon(selection_reuseExistingName.getEntity());
236
			selection_reuseExistingTaxon.setEnabled(enabled);
237
			textNewTaxonName.setEnabled(enabled);
238

    
239
			complete = !textNewTaxonName.getText().isEmpty();
240
		}
241
		if(getEntity() instanceof TaxonNode){
242
		    if (eventSource == selection_Ref) {
243
		        ((TaxonNode) getEntity()).setReference(selection_Ref.getEntity());
244
		    }else if (eventSource == selection_SecRef) {
245
		        ((TaxonNode) getEntity()).getTaxon().setSec(selection_SecRef.getEntity());
246
		    }else if (eventSource == microReference) {
247
		        ((TaxonNode) getEntity()).setMicroReference(microReference.getText());
248
		    } else if (eventSource == textNewTaxonName) {
249
		        boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
250
		        selection_reuseExistingTaxon.setEnabled(enabled);
251
		        selection_reuseExistingName.setEnabled(enabled);
252

    
253
		        complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
254
		    } else if (eventSource == checkbox_excluded) {
255
		        excluded = checkbox_excluded.getSelection();
256
		        ((TaxonNode)getEntity()).setExcluded(excluded);
257

    
258
		        multiLanguageTextExcludedNotes.setEnabled(excluded);
259
		    } else if (eventSource == checkbox_unplaced) {
260
		        unplaced = checkbox_unplaced.getSelection();
261
		        ((TaxonNode)getEntity()).setUnplaced(unplaced);
262
		    } else if (eventSource == checkbox_openInEditor) {
263
		        setOpenInEditor(checkbox_openInEditor.getSelection());
264
		    } else if (eventSource == checkbox_publish) {
265
		        ((TaxonNode)getEntity()).getTaxon().setPublish(checkbox_publish.getSelection());
266
		    }
267
		}
268
		else if (eventSource == selectionNodeAgentRelation){
269
		   boolean allComplete = true;
270
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
271
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
272
		   }
273
		    complete = !isCreateNew() && allComplete;
274
		}
275
	}
276

    
277
	public Classification getClassification() {
278
		return classification;
279
	}
280

    
281
	public boolean isOpenInEditor() {
282
		return openInEditor;
283
	}
284

    
285
	public ITaxonTreeNode getParentTreeNode() {
286
		return parentTreeNode;
287
	}
288

    
289
	private void setClassification(Classification classification) {
290
		this.classification = classification;
291
		setParentTreeNode(classification);
292
	}
293

    
294
	private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
295
		this.parentTreeNode = parentTreeNode;
296

    
297
		if (parentTreeNode instanceof Classification) {
298
			classification = (Classification) parentTreeNode;
299
			selection_classification.setEntity(classification);
300
			selection_parentTaxonNode.setEntity(null);
301
			selection_parentTaxonNode.setClassification(classification);
302
			selection_SecRef.setEntity(classification.getReference());
303
		} else if (parentTreeNode instanceof TaxonNode) {
304
			classification = HibernateProxyHelper
305
					.deproxy(((TaxonNode) parentTreeNode).getClassification());
306
			selection_classification.setEntity(classification);
307
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
308
                    .deproxy((TaxonNode) parentTreeNode));
309
			selection_parentTaxonNode.setClassification(classification);
310
			selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
311
		} else if(parentTreeNode == null){
312
			this.parentTreeNode = selection_classification.getEntity();
313
		}
314
	}
315

    
316
	private void setTreeNode(ITaxonTreeNode treeNode) {
317
        classification = HibernateProxyHelper
318
                    .deproxy(((TaxonNode) treeNode).getClassification());
319
        selection_classification.setEntity(classification);
320
        selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
321
        selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
322
        selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
323
	}
324

    
325
	private void setTaxon(Taxon taxon) {
326
		this.taxon = taxon;
327
		if (isCreateNew()){
328
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
329
		}
330
		selection_SecRef.setEntity(taxon.getSec());
331
	}
332

    
333
	private void setTaxon(TaxonName taxonName) {
334
		Reference secundum = null;
335
		if (getParentTreeNode() != null) {
336
			if (this.selection_SecRef.getEntity() != null){
337
				secundum = this.selection_SecRef.getEntity();
338
			}
339
		}
340
		if (taxonName != null){
341
		    taxon = Taxon.NewInstance(taxonName, secundum);
342
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
343
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
344
		    }
345
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
346
//		        textNewTaxonName.setText(taxonName.getTitleCache());
347
//		    }
348
		} else {
349
		    textNewTaxonName.setText(null);
350
		}
351
	}
352

    
353
	private void setOpenInEditor(boolean openInEditor) {
354
		this.openInEditor = openInEditor;
355
	}
356

    
357
	public String getTaxonName() {
358
        return textNewTaxonName.getText();
359
    }
360
	public Taxon getTaxon() {
361
        return taxon;
362
    }
363

    
364
	public Reference getReference(){
365
	    return selection_SecRef.getSelection();
366
	}
367

    
368
	public Control getMainControl(){
369
	    return textNewTaxonName.getMainControl();
370
	}
371

    
372
	public boolean isComplete() {
373
	    boolean allComplete = true;
374
	    if (!isCreateNew()){
375
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
376
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
377
	        }
378
	        complete = allComplete;
379
	    }
380
		return complete;
381
	}
382

    
383
    public boolean isCreateNew() {
384
        return createNew;
385
    }
386

    
387
    public boolean isExcluded() {
388
        return excluded;
389
    }
390

    
391
    public boolean isUnplaced() {
392
        return unplaced;
393
    }
394

    
395
    public boolean is_publish() {
396
        return checkbox_publish!=null?checkbox_publish.getSelection():false;
397
    }
398

    
399
    public void set_publish(boolean publish) {
400
        this.checkbox_publish.setSelection(publish);
401
    }
402

    
403
}
(10-10/12)