Project

General

Profile

Download (18.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.store.StoreUtil;
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.LayoutConstants;
37
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
38
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
39
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
40
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
41
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
42

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

    
51
    private boolean createNew = false;
52

    
53
//	private EntitySelectionElement<Classification> selection_classification;
54

    
55
	private Classification classification;
56

    
57
	private TaxonNodeSelectionElement selection_parentTaxonNode;
58

    
59
	private TaxonNode parentTreeNode;
60

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

    
67
	private Taxon taxon;
68

    
69
	private TextWithLabelElement textNewTaxonName;
70

    
71
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
72

    
73
//	private CheckboxElement checkbox_openInEditor;
74

    
75
	private CheckboxElement checkbox_unplaced;
76

    
77
	private CheckboxElement checkbox_excluded;
78
	private CheckboxElement checkbox_publish;
79
	private CheckboxElement checkbox_placementDoubtful;
80

    
81

    
82

    
83
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
84

    
85
//	private boolean openInEditor;
86

    
87
	private boolean complete;
88

    
89
	private boolean excluded = false;
90
	private boolean unplaced = false;
91
	private boolean placementDoubtful = false;
92

    
93
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
94

    
95
	public TaxonNodeDetailElement(CdmFormFactory formFactory,
96
			ICdmFormElement formElement, boolean isCreateNew) {
97
		super(formFactory, formElement);
98
		createNew = isCreateNew;
99
	}
100

    
101

    
102

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

    
114
    		selection_parentTaxonNode = formFactory
115
    				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", parentTreeNode,
116
    						EntitySelectionElement.DELETABLE, style);
117

    
118

    
119

    
120
    		selection_Ref = formFactory
121
                    .createSelectionElement(Reference.class,
122
                            formElement, "Parent Child Relation Source", null,
123
                            EntitySelectionElement.DELETABLE, style);
124

    
125

    
126
            microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
127
            microReference.setText(getEntity().getMicroReference());
128

    
129
    		selection_reuseExistingTaxon = formFactory
130
    				.createSelectionElement(Taxon.class,//getConversationHolder(),
131
    				        formElement,
132
    						"Reuse existing taxon", null,
133
    						EntitySelectionElement.DELETABLE, style);
134

    
135
    		selection_reuseExistingName = formFactory
136
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
137
    				        formElement,
138
    						"Reuse existing name", taxon.getName(),
139
    						EntitySelectionElement.DELETABLE, style);
140
    		selection_SecRef = formFactory
141
                    .createSelectionElement(Reference.class,//getConversationHolder(),
142
                            formElement, "Secundum Reference", taxon.getSec(),
143
                            EntitySelectionElement.DELETABLE, style);
144

    
145
    		if (isCreateNew()){
146
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
147
    		    textNewTaxonName.setFocus();
148
    		} else{
149
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
150

    
151
    		}
152
	    }else{
153
	        if (entity.getParent().getTaxon() == null){
154
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
155
                textParent.setEnabled(false);
156
	        }else{
157
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Parent", entity.getParent().getTaxon().getTitleCache(), style);
158
	            textParent.setEnabled(false);
159
	        }
160
//	        TextWithLabelElement textTaxon = formFactory.createTextWithLabelElement(formElement,"Taxon", taxon.getTitleCache(), style);
161
//	        textTaxon.setEnabled(false);
162
	        selection_Ref = formFactory
163
                    .createSelectionElement(Reference.class,
164
                            formElement, "Parent Child Relation Source", null,
165
                            EntitySelectionElement.DELETABLE, style);
166

    
167
	        selection_reuseExistingTaxon = formFactory
168
                  .createSelectionElement(Taxon.class,//getConversationHolder(),
169
                          formElement,
170
                          "Taxon", taxon,
171
                          EntitySelectionElement.DELETABLE, style);
172

    
173
	        textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
174
            if (entity.getTaxon().getSec() != null){
175
                textTaxonSec.setText(taxon.getSec().getTitleCache());
176
            }
177
            textTaxonSec.setEnabled(false);
178

    
179

    
180

    
181
	    }
182

    
183

    
184
        if (!isCreateNew()){
185
            if (entity.getReference() != null){
186
                selection_Ref.setEntity(entity.getReference());
187
            }
188
        }
189
        parentTreeNode = entity.getParent();
190

    
191

    
192

    
193
        checkbox_publish = formFactory.createCheckbox(formElement,
194
		            "Taxon is published", entity.getTaxon().isPublish(), style);
195

    
196
        checkbox_publish.setEnabled(isCreateNew());
197

    
198
        checkbox_placementDoubtful = formFactory.createCheckbox(formElement,
199
                "Placement is doubtful", entity.isDoubtful(), style);
200

    
201

    
202
		if(isCreateNew()){
203
		   setParentTreeNode(entity.getParent());
204

    
205
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
206
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
207
	            checkbox_publish.setSelection(true);
208
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
209
	            checkbox_publish.setSelection(false);
210
	        }else{
211
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && ((TaxonNode)this.getParentTreeNode()).getTaxon() != null){
212
	                checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
213
	            }else{
214
	                checkbox_publish.setSelection(true);
215
	            }
216
	        }
217

    
218
	    } else{
219
            setTreeNode(entity);
220

    
221
            complete = true;
222
        }
223

    
224
		checkbox_unplaced = formFactory.createCheckbox(formElement,
225
		            "Taxon is unplaced", entity.isUnplaced(), style);
226

    
227
		if (isCreateNew()){
228
		    preFillTaxonName();
229
		}
230

    
231
		if (entity instanceof TaxonNode) {
232
		    checkbox_excluded = formFactory.createCheckbox(formElement,
233
		            "Taxon is excluded", entity.isExcluded(), style);
234
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", entity.getExcludedNote(), 50, style);
235
		    multiLanguageTextExcludedNotes.setEnabled(entity.isExcluded());
236
		}else{
237
		    checkbox_excluded = formFactory.createCheckbox(formElement,
238
                    "Taxon is excluded", false, style);
239
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", new HashMap<Language, LanguageString>(), 50, style);
240
            multiLanguageTextExcludedNotes.setEnabled(false);
241
		}
242
		if (!isCreateNew()){
243
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), StoreUtil.getSectionStyle(TaxonNodeAgentRelationCollectionSection.class, entity.getClass().getCanonicalName()));
244
		    selectionNodeAgentRelation.setEntity(entity);
245
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
246
		}
247

    
248
	}
249

    
250
	private void preFillTaxonName() {
251
        if(getEntity() instanceof TaxonNode ){
252
            TaxonNode node = parentTreeNode;
253
            if(node.getTaxon()!=null){
254
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
255
                if(taxon.getName()!=null){
256
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
257
                    if (!isCreateNew()){
258
                        textNewTaxonName.setText(name.getNameCache());
259
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
260
                        String taxonName = "";
261
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
262
                            taxonName = name.getGenusOrUninomial();
263
                        }
264
                        else if(name.isSpecies() || name.isInfraSpecific() ){
265
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
266
                        }
267
                        if (StringUtils.isNotBlank(taxonName)){
268
                        	textNewTaxonName.setText(taxonName + " ");
269
                        	if(textNewTaxonName.getMainControl() instanceof Text){
270
                        		Text text = (Text)textNewTaxonName.getMainControl();
271
                        		text.setSelection(textNewTaxonName.getText().length());
272
                        	}
273
                        }
274
                    }
275
                }
276
            }
277
        }
278
    }
279

    
280
	/** {@inheritDoc} */
281
	@Override
282
	public void handleEvent(Object eventSource) {
283
		if (eventSource == selection_parentTaxonNode) {
284
		    setParentTreeNode(selection_parentTaxonNode.getEntity());
285
		} else if (eventSource == selection_reuseExistingTaxon) {
286
		    if (selection_reuseExistingTaxon.getEntity() != null){
287
		        setTaxon(selection_reuseExistingTaxon.getEntity());
288
		        if (getTaxon().getSec()!= null){
289
		            if (isCreateNew()){
290
		               selection_SecRef.setEntity(getTaxon().getSec());
291
		            }else{
292
		                textTaxonSec.setText((getTaxon().getSec().getTitleCache()));
293
		            }
294
		        }else{
295
		            if (isCreateNew()){
296
	                       selection_SecRef.setEntity(null);
297
	                    }else{
298
	                        textTaxonSec.setText("");
299
	                    }
300
		        }
301
		        checkbox_publish.setSelected(getTaxon().isPublish());
302
            }
303
		    boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
304
		    if (selection_reuseExistingName != null){
305
		        selection_reuseExistingName.setEnabled(enabled);
306
            }
307
	        textNewTaxonName.setEnabled(enabled);
308
			complete = true;
309
		} else if (eventSource == selection_reuseExistingName) {
310
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
311
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
312
			if (selection_reuseExistingTaxon != null){
313
			    selection_reuseExistingTaxon.setEnabled(enabled);
314
			}
315
			textNewTaxonName.setEnabled(enabled);
316
			complete = !textNewTaxonName.getText().isEmpty();
317
		}
318

    
319
        if (eventSource == selection_Ref) {
320
            getEntity().setReference(selection_Ref.getEntity());
321
        }else if (eventSource == selection_SecRef) {
322
            taxon.setSec(selection_SecRef.getEntity());
323
        }else if (eventSource == microReference) {
324
            getEntity().setMicroReference(microReference.getText());
325
        }  else if (eventSource == checkbox_publish) {
326
            taxon.setPublish(checkbox_publish.getSelection());
327
        }
328

    
329
		if (eventSource == checkbox_excluded) {
330
            excluded = checkbox_excluded.getSelection();
331

    
332
            getEntity().setExcluded(excluded);
333

    
334
            multiLanguageTextExcludedNotes.setEnabled(excluded);
335
        }
336
		if (eventSource == checkbox_unplaced) {
337
            unplaced = checkbox_unplaced.getSelection();
338
            getEntity().setUnplaced(unplaced);
339

    
340
        }
341

    
342
		if (eventSource == checkbox_placementDoubtful) {
343
		    placementDoubtful = checkbox_placementDoubtful.getSelection();
344
            getEntity().setDoubtful(placementDoubtful);
345

    
346
        }
347
		if (eventSource == textNewTaxonName) {
348
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
349
            if (selection_reuseExistingTaxon != null){
350
                selection_reuseExistingTaxon.setEnabled(enabled);
351
            }
352
            selection_reuseExistingName.setEnabled(enabled);
353

    
354
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
355
        }
356

    
357
		if (eventSource == selectionNodeAgentRelation){
358
		   boolean allComplete = true;
359
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
360
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
361
		   }
362
		    complete = !isCreateNew() && allComplete;
363
		}
364
	}
365

    
366
	public Classification getClassification() {
367
		return classification;
368
	}
369

    
370

    
371

    
372
	public ITaxonTreeNode getParentTreeNode() {
373
		return parentTreeNode;
374
	}
375

    
376
	private void setClassification(Classification classification) {
377
		this.classification = classification;
378
		//setParentTreeNode(classification);
379
	}
380

    
381
	private void setParentTreeNode(TaxonNode parentTreeNode) {
382
		this.parentTreeNode = parentTreeNode;
383

    
384
		if (parentTreeNode.getTaxon() == null) {
385
			classification = parentTreeNode.getClassification();
386
//			if (selection_classification != null){
387
//			    selection_classification.setEntity(classification);
388
//			}
389
			selection_parentTaxonNode.setEntity(classification.getRootNode());
390
			selection_parentTaxonNode.setClassification(classification);
391
			selection_SecRef.setEntity(classification.getReference());
392
		} else  {
393
			classification = HibernateProxyHelper
394
					.deproxy(parentTreeNode.getClassification());
395
//			if (selection_classification != null){
396
//			    selection_classification.setEntity(classification);
397
//			}
398
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
399
                    .deproxy(parentTreeNode));
400
			selection_parentTaxonNode.setClassification(classification);
401
			selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
402

    
403
		}
404
	}
405

    
406
	private void setTreeNode(TaxonNode treeNode) {
407
        classification = HibernateProxyHelper
408
                    .deproxy(treeNode.getClassification());
409
        if (isCreateNew()){
410
           //selection_classification.setEntity(classification);
411
            selection_parentTaxonNode.setEntity(treeNode.getParent());
412
            selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
413
            selection_SecRef.setEntity(treeNode.getTaxon().getSec());
414
        }
415
	}
416

    
417
	private void setTaxon(Taxon taxon) {
418
		this.taxon = taxon;
419
		getEntity().setTaxon(taxon);
420
		this.set_publish(taxon.isPublish());
421
		if (isCreateNew()){
422
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
423
		    if (taxon.getSec() != null){
424
		        selection_SecRef.setEntity(taxon.getSec());
425
		    }
426
		}else{
427
		    if (taxon.getSec() != null) {
428
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
429
            }
430
		}
431

    
432

    
433
	}
434

    
435
	private void setTaxon(TaxonName taxonName) {
436
		Reference secundum = null;
437
		if (getParentTreeNode() != null) {
438
			if (this.selection_SecRef.getEntity() != null){
439
				secundum = this.selection_SecRef.getEntity();
440
			}
441
		}
442
		if (taxonName != null){
443
		    taxon = Taxon.NewInstance(taxonName, secundum);
444
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
445
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
446
		    }
447
		    getEntity().setTaxon(taxon);
448
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
449
//		        textNewTaxonName.setText(taxonName.getTitleCache());
450
//		    }
451
		} else {
452
		    textNewTaxonName.setText(null);
453
		}
454
	}
455

    
456
//	private void setOpenInEditor(boolean openInEditor) {
457
//		this.openInEditor = openInEditor;
458
//	}
459

    
460
	public String getTaxonName() {
461
        return textNewTaxonName.getText();
462
    }
463
	public Taxon getTaxon() {
464
        return taxon;
465
    }
466

    
467
	public Reference getReference(){
468
	    return selection_SecRef.getSelection();
469
	}
470

    
471
	public Control getMainControl(){
472
	    return textNewTaxonName.getMainControl();
473
	}
474

    
475
	public boolean isComplete() {
476
	    boolean allComplete = true;
477
	    if (!isCreateNew()){
478
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
479
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
480
	        }
481
	        complete = allComplete;
482
	    }
483
		return complete;
484
	}
485

    
486
    public boolean isCreateNew() {
487
        return createNew;
488
    }
489

    
490

    
491

    
492
    public boolean is_publish() {
493
        boolean isPublishPreference = true;
494
        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
495
        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
496
            isPublishPreference = false;
497
        }
498
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
499
    }
500

    
501
    public void set_publish(boolean publish) {
502
        this.checkbox_publish.setSelection(publish);
503
    }
504

    
505

    
506
}
(10-10/12)