Project

General

Profile

Download (18 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
                          "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
		            if (isCreateNew()){
270
		               selection_SecRef.setEntity(getTaxon().getSec());
271
		            }else{
272
		                textTaxonSec.setText((getTaxon().getSec().getTitleCache()));
273
		            }
274
		        }else{
275
		            if (isCreateNew()){
276
	                       selection_SecRef.setEntity(null);
277
	                    }else{
278
	                        textTaxonSec.setText("");
279
	                    }
280
		        }
281
		        checkbox_publish.setSelected(getTaxon().isPublish());
282
            }
283
			complete = true;
284
		} else if (eventSource == selection_reuseExistingName) {
285
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
286
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
287
			if (selection_reuseExistingTaxon != null){
288
			    selection_reuseExistingTaxon.setEnabled(enabled);
289
			}
290
			textNewTaxonName.setEnabled(enabled);
291
			complete = !textNewTaxonName.getText().isEmpty();
292
		}
293

    
294
        if (eventSource == selection_Ref) {
295
            getEntity().setReference(selection_Ref.getEntity());
296
        }else if (eventSource == selection_SecRef) {
297
            taxon.setSec(selection_SecRef.getEntity());
298
        }else if (eventSource == microReference) {
299
            getEntity().setMicroReference(microReference.getText());
300
        }  else if (eventSource == checkbox_publish) {
301
            taxon.setPublish(checkbox_publish.getSelection());
302
        }
303

    
304
		if (eventSource == checkbox_excluded) {
305
            excluded = checkbox_excluded.getSelection();
306

    
307
            getEntity().setExcluded(excluded);
308

    
309
            multiLanguageTextExcludedNotes.setEnabled(excluded);
310
        }
311
		if (eventSource == checkbox_unplaced) {
312
            unplaced = checkbox_unplaced.getSelection();
313
            getEntity().setUnplaced(unplaced);
314

    
315
        }
316
		if (eventSource == textNewTaxonName) {
317
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
318
            if (selection_reuseExistingTaxon != null){
319
                selection_reuseExistingTaxon.setEnabled(enabled);
320
            }
321
            selection_reuseExistingName.setEnabled(enabled);
322

    
323
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
324
        }
325

    
326
		if (eventSource == selectionNodeAgentRelation){
327
		   boolean allComplete = true;
328
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
329
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
330
		   }
331
		    complete = !isCreateNew() && allComplete;
332
		}
333
	}
334

    
335
	public Classification getClassification() {
336
		return classification;
337
	}
338

    
339
//	public boolean isOpenInEditor() {
340
//		return openInEditor;
341
//	}
342

    
343
	public ITaxonTreeNode getParentTreeNode() {
344
		return parentTreeNode;
345
	}
346

    
347
	private void setClassification(Classification classification) {
348
		this.classification = classification;
349
		//setParentTreeNode(classification);
350
	}
351

    
352
	private void setParentTreeNode(TaxonNode parentTreeNode) {
353
		this.parentTreeNode = parentTreeNode;
354

    
355
		if (parentTreeNode.getTaxon() == null) {
356
			classification = parentTreeNode.getClassification();
357
//			if (selection_classification != null){
358
//			    selection_classification.setEntity(classification);
359
//			}
360
			selection_parentTaxonNode.setEntity(classification.getRootNode());
361
			selection_parentTaxonNode.setClassification(classification);
362
			selection_SecRef.setEntity(classification.getReference());
363
		} else  {
364
			classification = HibernateProxyHelper
365
					.deproxy(parentTreeNode.getClassification());
366
//			if (selection_classification != null){
367
//			    selection_classification.setEntity(classification);
368
//			}
369
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
370
                    .deproxy(parentTreeNode));
371
			selection_parentTaxonNode.setClassification(classification);
372
			selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
373

    
374
		}
375
	}
376

    
377
	private void setTreeNode(TaxonNode treeNode) {
378
        classification = HibernateProxyHelper
379
                    .deproxy(treeNode.getClassification());
380
        if (isCreateNew()){
381
           //selection_classification.setEntity(classification);
382
            selection_parentTaxonNode.setEntity(treeNode.getParent());
383
            selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
384
            selection_SecRef.setEntity(treeNode.getTaxon().getSec());
385
        }
386
	}
387

    
388
	private void setTaxon(Taxon taxon) {
389
		this.taxon = taxon;
390
		getEntity().setTaxon(taxon);
391
		if (isCreateNew()){
392
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
393
		    if (taxon.getSec() != null){
394
		        selection_SecRef.setEntity(taxon.getSec());
395
		    }
396
		}else{
397
		    if (taxon.getSec() != null) {
398
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
399
            }
400
		}
401

    
402

    
403
	}
404

    
405
	private void setTaxon(TaxonName taxonName) {
406
		Reference secundum = null;
407
		if (getParentTreeNode() != null) {
408
			if (this.selection_SecRef.getEntity() != null){
409
				secundum = this.selection_SecRef.getEntity();
410
			}
411
		}
412
		if (taxonName != null){
413
		    taxon = Taxon.NewInstance(taxonName, secundum);
414
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
415
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
416
		    }
417
		    getEntity().setTaxon(taxon);
418
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
419
//		        textNewTaxonName.setText(taxonName.getTitleCache());
420
//		    }
421
		} else {
422
		    textNewTaxonName.setText(null);
423
		}
424
	}
425

    
426
//	private void setOpenInEditor(boolean openInEditor) {
427
//		this.openInEditor = openInEditor;
428
//	}
429

    
430
	public String getTaxonName() {
431
        return textNewTaxonName.getText();
432
    }
433
	public Taxon getTaxon() {
434
        return taxon;
435
    }
436

    
437
	public Reference getReference(){
438
	    return selection_SecRef.getSelection();
439
	}
440

    
441
	public Control getMainControl(){
442
	    return textNewTaxonName.getMainControl();
443
	}
444

    
445
	public boolean isComplete() {
446
	    boolean allComplete = true;
447
	    if (!isCreateNew()){
448
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
449
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
450
	        }
451
	        complete = allComplete;
452
	    }
453
		return complete;
454
	}
455

    
456
    public boolean isCreateNew() {
457
        return createNew;
458
    }
459

    
460
    public boolean isExcluded() {
461
        return excluded;
462
    }
463

    
464
    public boolean isUnplaced() {
465
        return unplaced;
466
    }
467

    
468
    public boolean is_publish() {
469
        boolean isPublishPreference = true;
470
        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
471
        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
472
            isPublishPreference = false;
473
        }
474
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
475
    }
476

    
477
    public void set_publish(boolean publish) {
478
        this.checkbox_publish.setSelection(publish);
479
    }
480

    
481

    
482
}
(10-10/12)