Project

General

Profile

Download (17.7 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 java.util.HashMap;
13

    
14
import org.apache.commons.lang.StringUtils;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.Text;
17

    
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20
import eu.etaxonomy.cdm.model.common.Language;
21
import eu.etaxonomy.cdm.model.common.LanguageString;
22
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
23
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
24
import eu.etaxonomy.cdm.model.metadata.PublishEnum;
25
import eu.etaxonomy.cdm.model.name.TaxonName;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.taxon.Classification;
28
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
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.LayoutConstants;
36
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
37
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
38
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
39
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
40
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
41

    
42
/**
43
 *
44
 * @author n.hoffmann
45
 * @created Sep 27, 2010
46
 * @version 1.0
47
 */
48
public class TaxonNodeDetailElement extends AbstractCdmDetailElement<TaxonNode> {
49

    
50
    private boolean createNew = false;
51

    
52
//	private EntitySelectionElement<Classification> selection_classification;
53

    
54
	private Classification classification;
55

    
56
	private TaxonNodeSelectionElement selection_parentTaxonNode;
57

    
58
	private TaxonNode parentTreeNode;
59

    
60
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
61
	private EntitySelectionElement<Reference> selection_SecRef;
62
	private EntitySelectionElement<Reference> selection_Ref;
63
	TextWithLabelElement textTaxonSec;
64
	private TextWithLabelElement microReference;
65

    
66
	private Taxon taxon;
67

    
68
	private TextWithLabelElement textNewTaxonName;
69

    
70
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
71

    
72
//	private CheckboxElement checkbox_openInEditor;
73

    
74
	private CheckboxElement checkbox_unplaced;
75

    
76
	private CheckboxElement checkbox_excluded;
77
	private CheckboxElement checkbox_publish;
78

    
79

    
80

    
81
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
82

    
83
//	private boolean openInEditor;
84

    
85
	private boolean complete;
86

    
87
	private boolean excluded = false;
88
	private boolean unplaced = false;
89

    
90
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
91

    
92
	public TaxonNodeDetailElement(CdmFormFactory formFactory,
93
			ICdmFormElement formElement, boolean isCreateNew) {
94
		super(formFactory, formElement);
95
		createNew = isCreateNew;
96
	}
97

    
98

    
99

    
100
	/** {@inheritDoc} */
101
	@Override
102
	protected void createControls(ICdmFormElement formElement,
103
	        TaxonNode entity, int style) {
104
	    taxon = entity.getTaxon();
105
	    if (isCreateNew()){
106
//    		selection_classification = formFactory
107
//    				.createSelectionElement(Classification.class,//getConversationHolder(),
108
//    				        formElement, "Classification",
109
//    						null, EntitySelectionElement.DELETABLE, style);
110
    		selection_parentTaxonNode = formFactory
111
    				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", parentTreeNode,
112
    						EntitySelectionElement.DELETABLE, style);
113

    
114
//    		selection_reuseExistingTaxon = formFactory
115
//    				.createSelectionElement(Taxon.class,//getConversationHolder(),
116
//    				        formElement,
117
//    						"Reuse existing taxon", null,
118
//    						EntitySelectionElement.DELETABLE, style);
119

    
120
    		selection_reuseExistingName = formFactory
121
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
122
    				        formElement,
123
    						"Reuse existing name", taxon.getName(),
124
    						EntitySelectionElement.DELETABLE, style);
125
    		selection_SecRef = formFactory
126
                    .createSelectionElement(Reference.class,//getConversationHolder(),
127
                            formElement, "Secundum Reference", taxon.getSec(),
128
                            EntitySelectionElement.DELETABLE, style);
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
	    }else{
138
	        TextWithLabelElement textClassification = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
139
	        textClassification.setEnabled(false);
140

    
141
//	        TextWithLabelElement textTaxon = formFactory.createTextWithLabelElement(formElement,"Taxon", taxon.getTitleCache(), style);
142
//	        textTaxon.setEnabled(false);
143

    
144
	        selection_reuseExistingTaxon = formFactory
145
                  .createSelectionElement(Taxon.class,//getConversationHolder(),
146
                          formElement,
147
                          "Reuse existing taxon", taxon,
148
                          EntitySelectionElement.DELETABLE, style);
149

    
150
	        textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
151
            if (entity.getTaxon().getSec() != null){
152
                textTaxonSec.setText(taxon.getSec().getTitleCache());
153
            }
154
            textTaxonSec.setEnabled(false);
155

    
156

    
157

    
158
	    }
159

    
160
	    selection_Ref = formFactory
161
                .createSelectionElement(Reference.class,//getConversationHolder(),
162
                        formElement, "Parent Child Relation Source", null,
163
                        EntitySelectionElement.DELETABLE, style);
164
        microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
165
        microReference.setText(getEntity().getMicroReference());
166
        if (!isCreateNew()){
167
            selection_Ref.setEntity(entity.getReference());
168
        }
169
        parentTreeNode = entity.getParent();
170
		if (entity instanceof TaxonNode && parentTreeNode.getTaxon() != null) {
171
		    checkbox_publish = formFactory.createCheckbox(formElement,
172
		            "Taxon is public", parentTreeNode.getTaxon().isPublish(), style);
173
		}else{
174
		    checkbox_publish = formFactory.createCheckbox(formElement,
175
                    "Taxon is public", true, style);
176
		}
177
		checkbox_publish.setEnabled(isCreateNew());
178
		if(isCreateNew()){
179
		   setParentTreeNode(entity.getParent());
180

    
181
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
182
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
183
	            checkbox_publish.setSelection(true);
184
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
185
	            checkbox_publish.setSelection(false);
186
	        }else{
187
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && ((TaxonNode)this.getParentTreeNode()).getTaxon() != null){
188
	                checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
189
	            }else{
190
	                checkbox_publish.setSelection(true);
191
	            }
192
	        }
193

    
194
	    } else{
195
            setTreeNode(entity);
196
            complete = true;
197
        }
198
		if (entity instanceof TaxonNode) {
199
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
200
		            "Taxon is unplaced", entity.isUnplaced(), style);
201
		}else{
202
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
203
                    "Taxon is unplaced", false, style);
204
		}
205
		if (isCreateNew()){
206
		    preFillTaxonName();
207
		}
208

    
209
		if (entity instanceof TaxonNode) {
210
		    checkbox_excluded = formFactory.createCheckbox(formElement,
211
		            "Taxon is excluded", entity.isExcluded(), style);
212
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", entity.getExcludedNote(), 50, style);
213
		    multiLanguageTextExcludedNotes.setEnabled(entity.isExcluded());
214
		}else{
215
		    checkbox_excluded = formFactory.createCheckbox(formElement,
216
                    "Taxon is excluded", false, style);
217
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", new HashMap<Language, LanguageString>(), 50, style);
218
            multiLanguageTextExcludedNotes.setEnabled(false);
219
		}
220
		if (!isCreateNew()){
221
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
222
		    selectionNodeAgentRelation.setEntity(entity);
223
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
224
		}
225
//		checkbox_openInEditor = formFactory.createCheckbox(formElement,
226
//		        "Open in Editor", true, style);
227
//		setOpenInEditor(true);
228
	}
229

    
230
	private void preFillTaxonName() {
231
        if(getEntity() instanceof TaxonNode ){
232
            TaxonNode node = parentTreeNode;
233
            if(node.getTaxon()!=null){
234
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
235
                if(taxon.getName()!=null){
236
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
237
                    if (!isCreateNew()){
238
                        textNewTaxonName.setText(name.getNameCache());
239
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
240
                        String taxonName = "";
241
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
242
                            taxonName = name.getGenusOrUninomial();
243
                        }
244
                        else if(name.isSpecies() || name.isInfraSpecific() ){
245
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
246
                        }
247
                        if (StringUtils.isNotBlank(taxonName)){
248
                        	textNewTaxonName.setText(taxonName + " ");
249
                        	if(textNewTaxonName.getMainControl() instanceof Text){
250
                        		Text text = (Text)textNewTaxonName.getMainControl();
251
                        		text.setSelection(textNewTaxonName.getText().length());
252
                        	}
253
                        }
254
                    }
255
                }
256
            }
257
        }
258
    }
259

    
260
	/** {@inheritDoc} */
261
	@Override
262
	public void handleEvent(Object eventSource) {
263
		if (eventSource == selection_parentTaxonNode) {
264
		    setParentTreeNode(selection_parentTaxonNode.getEntity());
265
		} else if (eventSource == selection_reuseExistingTaxon) {
266
		    if (selection_reuseExistingTaxon.getEntity() != null){
267
		        setTaxon(selection_reuseExistingTaxon.getEntity());
268
		        if (getTaxon().getSec()!= null){
269
		            textTaxonSec.setText((getTaxon().getSec().getTitleCache()));
270
		        }else{
271
		            textTaxonSec.setText("");
272
		        }
273
		        checkbox_publish.setSelected(getTaxon().isPublish());
274
            }
275
			complete = true;
276
		} else if (eventSource == selection_reuseExistingName) {
277
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
278
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
279
			if (selection_reuseExistingTaxon != null){
280
			    selection_reuseExistingTaxon.setEnabled(enabled);
281
			}
282
			textNewTaxonName.setEnabled(enabled);
283
			complete = !textNewTaxonName.getText().isEmpty();
284
		}
285

    
286
        if (eventSource == selection_Ref) {
287
            getEntity().setReference(selection_Ref.getEntity());
288
        }else if (eventSource == selection_SecRef) {
289
            taxon.setSec(selection_SecRef.getEntity());
290
        }else if (eventSource == microReference) {
291
            getEntity().setMicroReference(microReference.getText());
292
        }  else if (eventSource == checkbox_publish) {
293
            taxon.setPublish(checkbox_publish.getSelection());
294
        }
295

    
296
		if (eventSource == checkbox_excluded) {
297
            excluded = checkbox_excluded.getSelection();
298

    
299
            getEntity().setExcluded(excluded);
300

    
301
            multiLanguageTextExcludedNotes.setEnabled(excluded);
302
        }
303
		if (eventSource == checkbox_unplaced) {
304
            unplaced = checkbox_unplaced.getSelection();
305
            getEntity().setUnplaced(unplaced);
306

    
307
        }
308
		if (eventSource == textNewTaxonName) {
309
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
310
            if (selection_reuseExistingTaxon != null){
311
                selection_reuseExistingTaxon.setEnabled(enabled);
312
            }
313
            selection_reuseExistingName.setEnabled(enabled);
314

    
315
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
316
        }
317

    
318
		if (eventSource == selectionNodeAgentRelation){
319
		   boolean allComplete = true;
320
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
321
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
322
		   }
323
		    complete = !isCreateNew() && allComplete;
324
		}
325
	}
326

    
327
	public Classification getClassification() {
328
		return classification;
329
	}
330

    
331
//	public boolean isOpenInEditor() {
332
//		return openInEditor;
333
//	}
334

    
335
	public ITaxonTreeNode getParentTreeNode() {
336
		return parentTreeNode;
337
	}
338

    
339
	private void setClassification(Classification classification) {
340
		this.classification = classification;
341
		//setParentTreeNode(classification);
342
	}
343

    
344
	private void setParentTreeNode(TaxonNode parentTreeNode) {
345
		this.parentTreeNode = parentTreeNode;
346

    
347
		if (parentTreeNode.getTaxon() == null) {
348
			classification = parentTreeNode.getClassification();
349
//			if (selection_classification != null){
350
//			    selection_classification.setEntity(classification);
351
//			}
352
			selection_parentTaxonNode.setEntity(classification.getRootNode());
353
			selection_parentTaxonNode.setClassification(classification);
354
			selection_SecRef.setEntity(classification.getReference());
355
		} else  {
356
			classification = HibernateProxyHelper
357
					.deproxy(parentTreeNode.getClassification());
358
//			if (selection_classification != null){
359
//			    selection_classification.setEntity(classification);
360
//			}
361
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
362
                    .deproxy(parentTreeNode));
363
			selection_parentTaxonNode.setClassification(classification);
364
			selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
365

    
366
		}
367
	}
368

    
369
	private void setTreeNode(TaxonNode treeNode) {
370
        classification = HibernateProxyHelper
371
                    .deproxy(treeNode.getClassification());
372
        if (isCreateNew()){
373
           //selection_classification.setEntity(classification);
374
            selection_parentTaxonNode.setEntity(treeNode.getParent());
375
            selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
376
            selection_SecRef.setEntity(treeNode.getTaxon().getSec());
377
        }
378
	}
379

    
380
	private void setTaxon(Taxon taxon) {
381
		this.taxon = taxon;
382
		getEntity().setTaxon(taxon);
383
		if (isCreateNew()){
384
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
385
		    if (taxon.getSec() != null){
386
		        selection_SecRef.setEntity(taxon.getSec());
387
		    }
388
		}else{
389
		    if (taxon.getSec() != null) {
390
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
391
            }
392
		}
393

    
394

    
395
	}
396

    
397
	private void setTaxon(TaxonName taxonName) {
398
		Reference secundum = null;
399
		if (getParentTreeNode() != null) {
400
			if (this.selection_SecRef.getEntity() != null){
401
				secundum = this.selection_SecRef.getEntity();
402
			}
403
		}
404
		if (taxonName != null){
405
		    taxon = Taxon.NewInstance(taxonName, secundum);
406
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
407
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
408
		    }
409
		    getEntity().setTaxon(taxon);
410
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
411
//		        textNewTaxonName.setText(taxonName.getTitleCache());
412
//		    }
413
		} else {
414
		    textNewTaxonName.setText(null);
415
		}
416
	}
417

    
418
//	private void setOpenInEditor(boolean openInEditor) {
419
//		this.openInEditor = openInEditor;
420
//	}
421

    
422
	public String getTaxonName() {
423
        return textNewTaxonName.getText();
424
    }
425
	public Taxon getTaxon() {
426
        return taxon;
427
    }
428

    
429
	public Reference getReference(){
430
	    return selection_SecRef.getSelection();
431
	}
432

    
433
	public Control getMainControl(){
434
	    return textNewTaxonName.getMainControl();
435
	}
436

    
437
	public boolean isComplete() {
438
	    boolean allComplete = true;
439
	    if (!isCreateNew()){
440
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
441
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
442
	        }
443
	        complete = allComplete;
444
	    }
445
		return complete;
446
	}
447

    
448
    public boolean isCreateNew() {
449
        return createNew;
450
    }
451

    
452
    public boolean isExcluded() {
453
        return excluded;
454
    }
455

    
456
    public boolean isUnplaced() {
457
        return unplaced;
458
    }
459

    
460
    public boolean is_publish() {
461
        boolean isPublishPreference = true;
462
        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
463
        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
464
            isPublishPreference = false;
465
        }
466
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
467
    }
468

    
469
    public void set_publish(boolean publish) {
470
        this.checkbox_publish.setSelection(publish);
471
    }
472

    
473

    
474
}
(10-10/12)