Project

General

Profile

Download (19.4 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.ArrayList;
13
import java.util.Collection;
14
import java.util.Map;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.widgets.Control;
19
import org.eclipse.swt.widgets.Text;
20

    
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23
import eu.etaxonomy.cdm.model.common.Language;
24
import eu.etaxonomy.cdm.model.common.LanguageString;
25
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
26
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
27
import eu.etaxonomy.cdm.model.metadata.PublishEnum;
28
import eu.etaxonomy.cdm.model.name.TaxonName;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.taxon.Classification;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33
import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
34
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36
import eu.etaxonomy.taxeditor.store.StoreUtil;
37
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
38
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
39
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
40
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
41
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
42
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
43
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
44
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
45
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
46
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
47

    
48
/**
49
 *
50
 * @author n.hoffmann
51
 * @created Sep 27, 2010
52
 * @version 1.0
53
 */
54
public class TaxonNodeDetailElement extends AbstractCdmDetailElement<TaxonNode> {
55

    
56
    private boolean createNew = false;
57
	private Classification classification;
58
	private TaxonNode parentNode;
59
	private Taxon taxon;
60

    
61
	private TaxonNodeSelectionElement selection_parentTaxonNode;
62
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
63
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
64

    
65
    private EntitySelectionElement<Reference> selection_SecRef;
66
	private EntitySelectionElement<Reference> selection_Ref;
67

    
68

    
69
    TextWithLabelElement textTaxonSec;
70

    
71
	private TextWithLabelElement microReference;
72

    
73

    
74
    private TextWithLabelElement textNewTaxonName;
75

    
76
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
77

    
78
	private CheckboxElement checkbox_unplaced;
79
	private CheckboxElement checkbox_excluded;
80
	private CheckboxElement checkbox_publish;
81
	private CheckboxElement checkbox_placementDoubtful;
82

    
83
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
84

    
85

    
86
	private boolean complete;
87

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

    
92

    
93

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

    
100

    
101
	public Map<Language, LanguageString> getMultiLanguageTextExcludedNotes() {
102
        return multiLanguageTextExcludedNotes.getMultilanguageText();
103
    }
104

    
105

    
106

    
107
    public boolean isExcluded() {
108
        return excluded;
109
    }
110

    
111

    
112

    
113
    public boolean isUnplaced() {
114
        return unplaced;
115
    }
116

    
117

    
118

    
119
    public boolean isPlacementDoubtful() {
120
        return placementDoubtful;
121
    }
122

    
123
    public Reference getRef() {
124
        return selection_Ref.getEntity();
125
    }
126
    public TaxonName getExistingName() {
127
        return selection_reuseExistingName.getEntity();
128
    }
129
    public String getMicroReference() {
130
        return microReference.getText();
131
    }
132

    
133
	/** {@inheritDoc} */
134
	@Override
135
	protected void createControls(ICdmFormElement formElement,
136
	        TaxonNode entity, int style) {
137
//	    taxon = entity.getTaxon();
138

    
139
	    if (isCreateNew()){
140
//	        setParentTreeNode(entity);
141
    		selection_parentTaxonNode = formFactory
142
    				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", parentNode,
143
    						EntitySelectionElement.DELETABLE, style, 100);
144

    
145
    		selection_Ref = formFactory
146
                    .createSelectionElement(Reference.class,
147
                            formElement, "Parent Child Relation Source", null,
148
                            EntitySelectionElement.DELETABLE, style, 100);
149

    
150
            microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
151
            microReference.setText(parentNode.getMicroReference());
152

    
153
    		selection_reuseExistingTaxon = formFactory
154
    				.createSelectionElement(Taxon.class,//getConversationHolder(),
155
    				        formElement,
156
    						"Reuse existing taxon", null,
157
    						EntitySelectionElement.DELETABLE, style);
158

    
159
    		selection_reuseExistingName = formFactory
160
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
161
    				        formElement,
162
    						"Reuse existing name", taxon != null? taxon.getName(): null,
163
    						EntitySelectionElement.DELETABLE, style);
164
    		selection_SecRef = formFactory
165
                    .createSelectionElement(Reference.class,//getConversationHolder(),
166
                            formElement, "Secundum Reference", taxon != null? taxon.getSec(): null,
167
                            EntitySelectionElement.DELETABLE, style, 100);
168

    
169
    		if (isCreateNew()){
170
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
171
    		    textNewTaxonName.setFocus();
172
    		} else{
173
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
174

    
175
    		}
176
	    }else{
177
	        taxon = getEntity().getTaxon();
178
	        if (entity.getParent().getTaxon() == null){
179
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
180
                textParent.setEnabled(false);
181
	        }else{
182
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Parent", entity.getParent().getTaxon().getTitleCache(), style);
183
	            textParent.setEnabled(false);
184
	        }
185

    
186
	        selection_Ref = formFactory
187
                    .createSelectionElement(Reference.class,
188
                            formElement, "Parent Child Relation Source", null,
189
                            EntitySelectionElement.DELETABLE, style);
190

    
191
	        selection_reuseExistingTaxon = formFactory
192
                  .createSelectionElement(Taxon.class,//getConversationHolder(),
193
                          formElement,
194
                          "Taxon", taxon,
195
                          EntitySelectionElement.DELETABLE, style);
196

    
197
	        textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
198
            if (entity.getTaxon().getSec() != null){
199
                textTaxonSec.setText(taxon.getSec().getTitleCache());
200
            }
201
            textTaxonSec.setEnabled(false);
202

    
203

    
204

    
205
	    }
206

    
207

    
208
        if (!isCreateNew()){
209
            if (entity.getReference() != null){
210
                selection_Ref.setEntity(entity.getReference());
211
            }
212

    
213
        }
214

    
215

    
216
        Taxon tax = null;
217
        if (entity.getTaxon() != null){
218
            tax = entity.getTaxon();
219
        }
220
        checkbox_publish = formFactory.createCheckbox(formElement,
221
                "Taxon is published", tax != null? tax.isPublish(): true, style);
222

    
223

    
224
        checkbox_publish.setEnabled(isCreateNew());
225

    
226
        checkbox_placementDoubtful = formFactory.createCheckbox(formElement,
227
                "Placement is doubtful", tax!= null? entity.isDoubtful(): false, style);
228

    
229

    
230

    
231
		if(isCreateNew()){
232

    
233

    
234
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
235
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
236
	            checkbox_publish.setSelection(true);
237
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
238
	            checkbox_publish.setSelection(false);
239
	        }else{
240
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && this.getParentTreeNode().getTaxon() != null){
241
	                checkbox_publish.setSelection(this.getParentTreeNode().getTaxon().isPublish());
242
	            }else{
243
	                checkbox_publish.setSelection(true);
244
	            }
245
	        }
246

    
247
	    } else{
248
            setTreeNode(entity);
249
            complete = true;
250
        }
251

    
252
		checkbox_unplaced = formFactory.createCheckbox(formElement,
253
		            "Taxon is unplaced", entity.isUnplaced(), style);
254

    
255
		if (isCreateNew()){
256
		    preFillTaxonName();
257
		}
258

    
259
	    checkbox_excluded = formFactory.createCheckbox(formElement,
260
	            "Taxon is excluded", entity.isExcluded(), style);
261
	    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", entity.getExcludedNote(), 50, style);
262
	    multiLanguageTextExcludedNotes.setEnabled(entity.isExcluded());
263

    
264
		if (!isCreateNew()){
265
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), StoreUtil.getSectionStyle(TaxonNodeAgentRelationCollectionSection.class, entity.getClass().getCanonicalName()));
266
		    selectionNodeAgentRelation.setEntity(entity);
267
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
268
		}
269

    
270
	}
271

    
272
	private void preFillTaxonName() {
273
        TaxonNode node = parentNode;
274
        if(node.getTaxon()!=null){
275
            Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
276
            if(taxon.getName()!=null){
277
                TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
278
                if (!isCreateNew()){
279
                    textNewTaxonName.setText(name.getNameCache());
280
                } else if( ! name.isSupraGeneric() && name.getRank() != null){
281
                    String taxonName = "";
282
                    if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
283
                        taxonName = name.getGenusOrUninomial();
284
                    }
285
                    else if(name.isSpecies() || name.isInfraSpecific() ){
286
                        taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
287
                    }
288
                    if (StringUtils.isNotBlank(taxonName)){
289
                    	textNewTaxonName.setText(taxonName + " ");
290
                    	if(textNewTaxonName.getMainControl() instanceof Text){
291
                    		Text text = (Text)textNewTaxonName.getMainControl();
292
                    		text.setSelection(textNewTaxonName.getText().length());
293
                    	}
294
                    }
295
                }
296
            }
297
        }
298

    
299
    }
300

    
301
	/** {@inheritDoc} */
302
	@Override
303
	public void handleEvent(Object eventSource) {
304
		if (eventSource == selection_parentTaxonNode) {
305
		    setParentTreeNode(selection_parentTaxonNode.getEntity());
306
		} else if (eventSource == selection_reuseExistingTaxon) {
307
		    if (selection_reuseExistingTaxon.getEntity() != null){
308
		        setTaxon(selection_reuseExistingTaxon.getEntity());
309
		        if (getTaxon().getSec()!= null){
310
		            if (isCreateNew()){
311
		               selection_SecRef.setEntity(getTaxon().getSec());
312
		            }else{
313
		               textTaxonSec.setText((getTaxon().getSec().getTitleCache()));
314
		            }
315
		        }else{
316
		            if (isCreateNew()){
317
	                       selection_SecRef.setEntity(null);
318
	                    }else{
319
	                       textTaxonSec.setText("");
320
	                    }
321
		        }
322
		        checkbox_publish.setSelected(getTaxon().isPublish());
323
            }
324
		    boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
325
		    if (selection_reuseExistingName != null){
326
		        selection_reuseExistingName.setEnabled(enabled);
327
            }
328
		    if (textNewTaxonName != null){
329
		        textNewTaxonName.setEnabled(enabled);
330
		    }
331
			complete = true;
332
		} else if (eventSource == selection_reuseExistingName) {
333
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
334
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
335
			if (selection_reuseExistingTaxon != null){
336
			    selection_reuseExistingTaxon.setEnabled(enabled);
337
			}
338
			textNewTaxonName.setEnabled(enabled);
339
			complete = !textNewTaxonName.getText().isEmpty();
340
		}
341

    
342
        if (eventSource == selection_Ref) {
343
            if(!isCreateNew()){
344
                getEntity().setReference(selection_Ref.getEntity());
345
            }
346
        }else if (eventSource == selection_SecRef) {
347
            if (taxon != null){
348
                taxon.setSec(selection_SecRef.getEntity());
349
            }
350
        }else if (eventSource == microReference) {
351
            if(!isCreateNew()){
352
                getEntity().setMicroReference(microReference.getText());
353
            }
354
        }  else if (eventSource == checkbox_publish) {
355
            if (taxon != null){
356
                taxon.setPublish(checkbox_publish.getSelection());
357
            }
358
        }
359

    
360
		if (eventSource == checkbox_excluded) {
361
            excluded = checkbox_excluded.getSelection();
362
            if (!isCreateNew()){
363
                getEntity().setExcluded(excluded);
364
            }
365

    
366
            multiLanguageTextExcludedNotes.setEnabled(excluded);
367
        }
368
		if (eventSource == checkbox_unplaced) {
369
            unplaced = checkbox_unplaced.getSelection();
370
            if (!isCreateNew()){
371
                getEntity().setUnplaced(unplaced);
372
            }
373

    
374
        }
375

    
376
		if (eventSource == checkbox_placementDoubtful) {
377
		    placementDoubtful = checkbox_placementDoubtful.getSelection();
378
		    if (!isCreateNew()){
379
		        getEntity().setDoubtful(placementDoubtful);
380
		    }
381

    
382
        }
383
		if (eventSource == textNewTaxonName) {
384
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
385
            if (selection_reuseExistingTaxon != null){
386
                selection_reuseExistingTaxon.setEnabled(enabled);
387
            }
388
            selection_reuseExistingName.setEnabled(enabled);
389

    
390
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
391
        }
392

    
393
		if (eventSource == selectionNodeAgentRelation){
394
		   boolean allComplete = true;
395
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
396
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
397
		   }
398
		    complete = !isCreateNew() && allComplete;
399
		}
400
	}
401

    
402
	public Classification getClassification() {
403
		return classification;
404
	}
405

    
406

    
407

    
408
	public TaxonNode getParentTreeNode() {
409
		return parentNode;
410
	}
411

    
412
	public void setParentTreeNode(TaxonNode parentTreeNode) {
413
		this.parentNode = parentTreeNode;
414
		updateContent();
415
		if (parentTreeNode.getTaxon() == null) {
416
			classification = parentTreeNode.getClassification();
417
			if (selection_parentTaxonNode != null){
418
    			selection_parentTaxonNode.setEntity(classification.getRootNode());
419
    			selection_parentTaxonNode.setClassification(classification);
420
    			selection_SecRef.setEntity(classification.getReference());
421
			}
422
		} else  {
423
			classification = HibernateProxyHelper
424
					.deproxy(parentTreeNode.getClassification());
425
			if (selection_parentTaxonNode != null){
426
			    selection_parentTaxonNode.setEntity(HibernateProxyHelper.deproxy(parentTreeNode));
427
			    selection_parentTaxonNode.setClassification(classification);
428
	            selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
429
			}
430

    
431

    
432
		}
433
	}
434
	@Override
435
	protected void updateControlStates(){
436
        Collection<Object> except = new ArrayList<Object>();
437
        for(ICdmFormElement formElement:getElements()){
438
            if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
439
                except.add(formElement);
440
            }
441
        }
442
        if (isCreateNew() ){
443
            enabled = true;
444
        }else{
445
            enabled = getEntity() != null && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), requiredCrud);
446
        }
447
        setEnabled(enabled, except);
448
    }
449

    
450
	private void setTreeNode(TaxonNode treeNode) {
451
        classification = HibernateProxyHelper
452
                    .deproxy(treeNode.getClassification());
453
        if (isCreateNew()){
454
           selection_parentTaxonNode.setEntity(treeNode.getParent());
455
           selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
456
           selection_SecRef.setEntity(treeNode.getTaxon().getSec());
457
        }
458
	}
459

    
460
	private void setTaxon(Taxon taxon) {
461
		this.taxon = taxon;
462
		if (!isCreateNew()){
463
		    getEntity().setTaxon(taxon);
464
		}
465
		this.set_publish(taxon.isPublish());
466
		if (isCreateNew()){
467
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
468
		    if (taxon.getSec() != null){
469
		        selection_SecRef.setEntity(taxon.getSec());
470
		    }
471
		}else{
472
		    if (taxon.getSec() != null) {
473
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
474
            }
475
		}
476

    
477

    
478
	}
479

    
480
	private void setTaxon(TaxonName taxonName) {
481
		Reference secundum = null;
482
		if (getParentTreeNode() != null) {
483
			if (this.selection_SecRef.getEntity() != null){
484
				secundum = this.selection_SecRef.getEntity();
485
			}
486
		}
487
		if (taxonName != null){
488
		    taxon = Taxon.NewInstance(taxonName, secundum);
489
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
490
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
491
		    }
492
		    if (!isCreateNew()){
493
		        getEntity().setTaxon(taxon);
494
		    }
495

    
496
		} else {
497
		    textNewTaxonName.setText(null);
498
		}
499
	}
500

    
501
	public String getTaxonName() {
502
        return textNewTaxonName.getText();
503
    }
504
	public Taxon getTaxon() {
505
        return taxon;
506
    }
507

    
508
	public Reference getSecReference(){
509
	    return selection_SecRef.getSelection();
510
	}
511

    
512
	public Control getMainControl(){
513
	    return textNewTaxonName.getMainControl();
514
	}
515

    
516
	public boolean isComplete() {
517
	    boolean allComplete = true;
518
	    if (!isCreateNew()){
519
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
520
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
521
	        }
522
	        complete = allComplete;
523
	    }
524
		return complete;
525
	}
526

    
527
    public boolean isCreateNew() {
528
        return createNew;
529
    }
530

    
531
    public boolean is_publish() {
532
        boolean isPublishPreference = true;
533
        CdmPreference defaultPublish = CdmPreferenceCache.instance().get(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
534
        if (defaultPublish != null && defaultPublish.getValue()!= null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
535
            isPublishPreference = false;
536
        }
537
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
538
    }
539

    
540
    public void set_publish(boolean publish) {
541
        this.checkbox_publish.setSelection(publish);
542
    }
543

    
544
    /**
545
     * Updates all widgets to display the latest data
546
     */
547
    @Override
548
    protected void updateContent() {
549
        removeElements();
550
        if (isCreateNew()){
551
            createControls(this, parentNode, SWT.NONE);
552
        }else{
553
            createControls(this, getEntity(), SWT.NONE);
554
        }
555
        updateControlStates();
556
    }
557

    
558

    
559
}
(10-10/12)