Project

General

Profile

Download (21.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.HashMap;
15
import java.util.Map;
16

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

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

    
52
/**
53
 * @author n.hoffmann
54
 * @created Sep 27, 2010
55
 */
56
public class TaxonNodeDetailElement extends AbstractCdmDetailElement<TaxonNode> {
57

    
58
    private static final String STATUS_NOTES = Messages.TaxonNodeWizardPage_STATUS_NOTES;
59
    private static final String CLASSIFICATION_STR = Messages.TaxonNodeWizardPage_CLASSIFICATION;
60
    private static final String REUSE_EXISTING_TAXON = Messages.TaxonNodeWizardPage_REUSE_EXISTING_TAXON;
61
    private static final String REUSE_EXISTING_NAME = Messages.TaxonNodeWizardPage_REUSE_EXISTING_NAME;
62
    private static final String SECUNDUM_REFERENCE = Messages.TaxonNodeWizardPage_SECUNDUM_REFERENCE;
63

    
64
    private boolean createNew = false;
65
	private Classification classification;
66
	private TaxonNode parentNode;
67
	private Taxon taxon;
68
    private boolean complete;
69
    private TaxonNodeStatus status = null;
70
    private Map<Language, LanguageString> multilanguageTextCache;
71

    
72
	private TaxonNodeSelectionElement selection_parentTaxonNode;
73
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
74
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
75
    private EntitySelectionElement<Reference> selection_SecRef;
76
	private EntitySelectionElement<Reference> selection_Ref;
77
    private TextWithLabelElement textTaxonSec;
78
	private TextWithLabelElement microReference;
79
    private TextWithLabelElement textNewTaxonName;
80
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
81
	private EnumComboElement<TaxonNodeStatus> combo_status;
82
	private CheckboxElement checkbox_publish;
83
    private MultilanguageTextElement multiLanguageTextStatusNotes;
84

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

    
91
	public Map<Language, LanguageString> getMultiLanguageTextExcludedNotes() {
92
        return multiLanguageTextStatusNotes.getMultilanguageText();
93
    }
94

    
95
    public boolean isExcluded() {
96
        return status == null? false : status.equals(TaxonNodeStatus.EXCLUDED);
97
    }
98

    
99
    public boolean isUnplaced() {
100
        return status == null? false : status.equals(TaxonNodeStatus.UNPLACED);
101
    }
102

    
103
    public boolean isPlacementDoubtful() {
104
        return status == null? false : status.equals(TaxonNodeStatus.DOUBTFUL);
105
    }
106

    
107
    public Reference getRef() {
108
        return selection_Ref.getEntity();
109
    }
110
    public TaxonName getExistingName() {
111
        return selection_reuseExistingName.getEntity();
112
    }
113
    public String getMicroReference() {
114
        return microReference.getText();
115
    }
116

    
117
	@Override
118
	protected void createControls(ICdmFormElement formElement,
119
	        TaxonNode entity, int style) {
120
//	    taxon = entity.getTaxon();
121

    
122
	    if (isCreateNew()){
123
//	        setParentTreeNode(entity);
124
    		selection_parentTaxonNode = formFactory
125
    				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, Messages.TaxonNodeWizardPage_PARENT, parentNode,
126
    						EntitySelectionElement.DELETABLE, style, 100);
127

    
128
    		selection_Ref = formFactory
129
                    .createSelectionElement(Reference.class,
130
                            formElement, Messages.TaxonNodeWizardPage_PLACEMENT_SOURCE, null,
131
                            EntitySelectionElement.DELETABLE, style, 100);
132

    
133
            microReference = formFactory.createTextWithLabelElement(formElement,
134
                    Messages.TaxonNodeWizardPage_PLACEMENT_SOURCE_DETAIL, "", style);
135
            microReference.setText(parentNode.getMicroReference());
136

    
137
    		selection_reuseExistingTaxon = formFactory
138
    				.createSelectionElement(Taxon.class,//getConversationHolder(),
139
    				        formElement,
140
    						REUSE_EXISTING_TAXON, null,
141
    						EntitySelectionElement.DELETABLE, style);
142

    
143
    		selection_reuseExistingName = formFactory
144
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
145
    				        formElement,
146
    						REUSE_EXISTING_NAME, taxon != null? taxon.getName(): null,
147
    						EntitySelectionElement.DELETABLE, style);
148
    		selection_SecRef = formFactory
149
                    .createSelectionElement(Reference.class,//getConversationHolder(),
150
                            formElement, SECUNDUM_REFERENCE, taxon != null? taxon.getSec(): null,
151
                            EntitySelectionElement.DELETABLE, style, 100);
152

    
153
    		if (isCreateNew()){
154
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
155
    		            Messages.TaxonNodeWizardPage_NEW_TAXON, "", style);
156
    		    textNewTaxonName.setFocus();
157
    		} else{
158
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
159
    		            Messages.TaxonNodeWizardPage_TAXON, "", style);
160
    		}
161
	    }else{
162
	        taxon = getEntity().getTaxon();
163
	        if (entity.getParent().getTaxon() == null){
164
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(
165
	                    formElement,CLASSIFICATION_STR, entity.getClassification().getTitleCache(), style);
166
                textParent.setEnabled(false);
167
	        }else{
168
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(
169
	                    formElement, Messages.TaxonNodeWizardPage_PARENT, entity.getParent().getTaxon().getTitleCache(), style);
170
	            textParent.setEnabled(false);
171
	        }
172

    
173
	        selection_Ref = formFactory
174
                    .createSelectionElement(Reference.class,
175
                            formElement, Messages.TaxonNodeWizardPage_PLACEMENT_SOURCE, null,
176
                            EntitySelectionElement.DELETABLE, style);
177

    
178
	        selection_reuseExistingTaxon = formFactory
179
                  .createSelectionElement(Taxon.class,//getConversationHolder(),
180
                          formElement,
181
                          Messages.TaxonNodeWizardPage_TAXON, taxon,
182
                          EntitySelectionElement.DELETABLE, style);
183

    
184
	        textTaxonSec = formFactory.createTextWithLabelElement(formElement,SECUNDUM_REFERENCE, "", style);
185
            if (entity.getTaxon().getSec() != null){
186
                textTaxonSec.setText(taxon.getSec().getTitleCache());
187
            }
188
            textTaxonSec.setEnabled(false);
189
	    }
190

    
191
        if (!isCreateNew()){
192
            if (entity.getReference() != null){
193
                selection_Ref.setEntity(entity.getReference());
194
            }
195
        }
196

    
197
        Taxon tax = null;
198
        if (entity.getTaxon() != null){
199
            tax = entity.getTaxon();
200
        }
201
        checkbox_publish = formFactory.createCheckbox(formElement,
202
                "Taxon is published", tax != null? tax.isPublish(): true, style);
203

    
204
        checkbox_publish.setEnabled(isCreateNew());
205

    
206
        combo_status = formFactory.createEnumComboElement(TaxonNodeStatus.class, formElement, style, true);
207
        if (!isCreateNew()){
208
            combo_status.setSelection(entity.getStatus());
209
        }
210

    
211
        if(isCreateNew()){
212

    
213
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
214
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
215
	            checkbox_publish.setSelection(true);
216
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
217
	            checkbox_publish.setSelection(false);
218
	        }else{
219
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && this.getParentTreeNode().getTaxon() != null){
220
	                checkbox_publish.setSelection(this.getParentTreeNode().getTaxon().isPublish());
221
	            }else{
222
	                checkbox_publish.setSelection(true);
223
	            }
224
	        }
225
	    } else{
226
            setTreeNode(entity);
227
            complete = true;
228
        }
229

    
230
		if (isCreateNew()){
231
		    preFillTaxonName();
232
		}
233

    
234
	    multiLanguageTextStatusNotes = formFactory.createMultiLanguageTextElement(formElement, STATUS_NOTES, null, 50, style);
235
	    if (!isCreateNew()){
236
	        multiLanguageTextStatusNotes.setMultilanguageText(entity.getStatusNote());
237
	        multiLanguageTextStatusNotes.setEnabled(entity.getStatus() != null);
238
	    }
239

    
240
		if (!isCreateNew()){
241
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), StoreUtil.getSectionStyle(TaxonNodeAgentRelationCollectionSection.class, entity.getClass().getCanonicalName()));
242
		    selectionNodeAgentRelation.setEntity(entity);
243
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
244
		}
245
	}
246

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

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

    
315
        if (eventSource == selection_Ref) {
316
            if(!isCreateNew()){
317
                getEntity().setReference(selection_Ref.getEntity());
318
            }
319
        }else if (eventSource == selection_SecRef) {
320
            if (taxon != null){
321
                taxon.setSec(selection_SecRef.getEntity());
322
            }
323
        }else if (eventSource == microReference) {
324
            if(!isCreateNew()){
325
                getEntity().setMicroReference(microReference.getText());
326
            }
327
        }  else if (eventSource == checkbox_publish) {
328
            if (taxon != null){
329
                taxon.setPublish(checkbox_publish.getSelection());
330
            }
331
        }
332

    
333
		if (eventSource == combo_status) {
334
            status = combo_status.getSelection();
335
            if (status == null){
336
                if (multiLanguageTextStatusNotes.getMultilanguageText() != null && !multiLanguageTextStatusNotes.getMultilanguageText().isEmpty()){
337
                    multilanguageTextCache = new HashMap<>();
338
                    for (LanguageString langString: multiLanguageTextStatusNotes.getMultilanguageText().values()){
339
                        multilanguageTextCache.put(langString.getLanguage(), langString);
340
                    }
341
//                    multilanguageTextCache = multiLanguageTextStatusNotes.getMultilanguageText();
342
                }
343
                multiLanguageTextStatusNotes.setMultilanguageText(new HashMap<>());
344
//                multiLanguageTextStatusNotes.refresh();
345

    
346
            }else if (multilanguageTextCache != null ){
347

    
348
                multiLanguageTextStatusNotes.setMultilanguageText(multilanguageTextCache);
349
                multilanguageTextCache = null;
350
            }
351
            if (!isCreateNew()){
352
                getEntity().setStatus(status);
353
                if (status == null){
354
                    getEntity().getStatusNote().clear();
355
                    multiLanguageTextStatusNotes.setMultilanguageText(new HashMap<>());
356
                    multiLanguageTextStatusNotes.refresh();
357
                }else{
358
                    if (multiLanguageTextStatusNotes.getMultilanguageText() != null){
359
                        for (LanguageString lang:multiLanguageTextStatusNotes.getMultilanguageText().values()){
360
                            getEntity().putStatusNote(lang);
361
                        }
362
                    }
363
                }
364
            }
365

    
366
            multiLanguageTextStatusNotes.setEnabled(status != null);
367

    
368
            complete = true;
369
        }
370

    
371
		if (eventSource == textNewTaxonName) {
372
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
373
            if (selection_reuseExistingTaxon != null){
374
                selection_reuseExistingTaxon.setEnabled(enabled);
375
            }
376
            selection_reuseExistingName.setEnabled(enabled);
377

    
378
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
379
        }
380

    
381
		if (eventSource == selectionNodeAgentRelation){
382
		   boolean allComplete = true;
383
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
384
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
385
		   }
386
		    complete = !isCreateNew() && allComplete;
387
		}
388
	}
389

    
390
	public Classification getClassification() {
391
		return classification;
392
	}
393

    
394
	public TaxonNode getParentTreeNode() {
395
		return parentNode;
396
	}
397

    
398
	public void setParentTreeNode(TaxonNode parentTreeNode) {
399
		this.parentNode = parentTreeNode;
400
		updateContent();
401
		if (parentTreeNode.getTaxon() == null) {
402
			classification = parentTreeNode.getClassification();
403
			if (selection_parentTaxonNode != null){
404
    			selection_parentTaxonNode.setEntity(classification.getRootNode());
405
    			selection_parentTaxonNode.setClassification(classification);
406
    			selection_SecRef.setEntity(classification.getReference());
407
			}
408
		} else  {
409
			classification = HibernateProxyHelper
410
					.deproxy(parentTreeNode.getClassification());
411
			if (selection_parentTaxonNode != null){
412
			    selection_parentTaxonNode.setEntity(HibernateProxyHelper.deproxy(parentTreeNode));
413
			    selection_parentTaxonNode.setClassification(classification);
414
	            selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
415
			}
416
		}
417
	}
418

    
419
	@Override
420
	protected void updateControlStates(){
421
        Collection<Object> except = new ArrayList<Object>();
422
        for(ICdmFormElement formElement:getElements()){
423
            if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
424
                except.add(formElement);
425
            }
426
        }
427
        if (isCreateNew() ){
428
            enabled = true;
429
        }else{
430
            enabled = getEntity() != null && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), requiredCrud);
431
        }
432
        setEnabled(enabled, except);
433
    }
434

    
435
	private void setTreeNode(TaxonNode treeNode) {
436
        classification = HibernateProxyHelper
437
                    .deproxy(treeNode.getClassification());
438
        if (isCreateNew()){
439
           selection_parentTaxonNode.setEntity(treeNode.getParent());
440
           selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
441
           selection_SecRef.setEntity(treeNode.getTaxon().getSec());
442
        }
443
	}
444

    
445
	private void setTaxon(Taxon taxon) {
446
		this.taxon = taxon;
447
		if (!isCreateNew()){
448
		    getEntity().setTaxon(taxon);
449
		}
450
		this.set_publish(taxon.isPublish());
451
		if (isCreateNew()){
452
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
453
		    if (taxon.getSec() != null){
454
		        selection_SecRef.setEntity(taxon.getSec());
455
		    }
456
		}else{
457
		    if (taxon.getSec() != null) {
458
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
459
            }
460
		}
461
	}
462

    
463
	private void setTaxon(TaxonName taxonName) {
464
		Reference secundum = null;
465
		if (getParentTreeNode() != null) {
466
			if (this.selection_SecRef.getEntity() != null){
467
				secundum = this.selection_SecRef.getEntity();
468
			}
469
		}
470
		if (taxonName != null){
471
		    taxon = Taxon.NewInstance(taxonName, secundum);
472
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
473
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
474
		    }
475
		    if (!isCreateNew()){
476
		        getEntity().setTaxon(taxon);
477
		    }
478

    
479
		} else {
480
		    textNewTaxonName.setText(null);
481
		}
482
	}
483

    
484
	public String getTaxonName() {
485
        return textNewTaxonName.getText();
486
    }
487
	public Taxon getTaxon() {
488
        return taxon;
489
    }
490

    
491
	public Reference getSecReference(){
492
	    return selection_SecRef.getSelection();
493
	}
494

    
495
	public Control getMainControl(){
496
	    return textNewTaxonName.getMainControl();
497
	}
498

    
499
	public boolean isComplete() {
500
	    boolean allComplete = true;
501
	    if (!isCreateNew()){
502
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
503
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
504
	        }
505
	        complete = allComplete;
506
	    }
507
		return complete;
508
	}
509

    
510
    public boolean isCreateNew() {
511
        return createNew;
512
    }
513

    
514
    public boolean is_publish() {
515
        boolean isPublishPreference = true;
516
        CdmPreference defaultPublish = CdmPreferenceCache.instance().get(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
517
        if (defaultPublish != null && defaultPublish.getValue()!= null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
518
            isPublishPreference = false;
519
        }
520
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
521
    }
522

    
523
    public void set_publish(boolean publish) {
524
        this.checkbox_publish.setSelection(publish);
525
    }
526

    
527
    /**
528
     * Updates all widgets to display the latest data
529
     */
530
    @Override
531
    protected void updateContent() {
532
        removeElements();
533
        if (isCreateNew()){
534
            createControls(this, parentNode, SWT.NONE);
535
        }else{
536
            createControls(this, getEntity(), SWT.NONE);
537
        }
538
        updateControlStates();
539
    }
540
}
(10-10/12)