Project

General

Profile

Download (17.2 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
	private TextWithLabelElement microReference;
64

    
65
	private Taxon taxon;
66

    
67
	private TextWithLabelElement textNewTaxonName;
68

    
69
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
70

    
71
//	private CheckboxElement checkbox_openInEditor;
72

    
73
	private CheckboxElement checkbox_unplaced;
74

    
75
	private CheckboxElement checkbox_excluded;
76
	private CheckboxElement checkbox_publish;
77

    
78

    
79

    
80
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
81

    
82
//	private boolean openInEditor;
83

    
84
	private boolean complete;
85

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

    
89
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
90

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

    
97

    
98

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

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

    
119
    		selection_reuseExistingName = formFactory
120
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
121
    				        formElement,
122
    						"Reuse existing name", taxon.getName(),
123
    						EntitySelectionElement.DELETABLE, style);
124
    		selection_SecRef = formFactory
125
                    .createSelectionElement(Reference.class,//getConversationHolder(),
126
                            formElement, "Secundum Reference", taxon.getSec(),
127
                            EntitySelectionElement.DELETABLE, style);
128

    
129
    		if (isCreateNew()){
130
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
131
    		    textNewTaxonName.setFocus();
132
    		} else{
133
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
134

    
135
    		}
136
	    }else{
137
	        TextWithLabelElement textClassification = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
138
	        textClassification.setEnabled(false);
139

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

    
143
            TextWithLabelElement textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
144
            if (entity.getTaxon().getSec() != null){
145
                textTaxonSec.setText(taxon.getSec().getTitleCache());
146
            }
147
            textTaxonSec.setEnabled(false);
148

    
149

    
150

    
151
	    }
152

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

    
174
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
175
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
176
	            checkbox_publish.setSelection(true);
177
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
178
	            checkbox_publish.setSelection(false);
179
	        }else{
180
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && ((TaxonNode)this.getParentTreeNode()).getTaxon() != null){
181
	                checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
182
	            }else{
183
	                checkbox_publish.setSelection(true);
184
	            }
185
	        }
186

    
187
	    } else{
188
            setTreeNode(entity);
189
            complete = true;
190
        }
191
		if (entity instanceof TaxonNode) {
192
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
193
		            "Taxon is unplaced", entity.isUnplaced(), style);
194
		}else{
195
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
196
                    "Taxon is unplaced", false, style);
197
		}
198
		if (isCreateNew()){
199
		    preFillTaxonName();
200
		}
201

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

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

    
253
	/** {@inheritDoc} */
254
	@Override
255
	public void handleEvent(Object eventSource) {
256
		if (eventSource == selection_parentTaxonNode) {
257
		    setParentTreeNode(selection_parentTaxonNode.getEntity());
258
		} else if (eventSource == selection_reuseExistingTaxon) {
259
				boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
260
				selection_reuseExistingName.setEnabled(enabled);
261
				if (!enabled){
262
                    setTaxon(selection_reuseExistingTaxon.getEntity());
263
                }else{
264
			        textNewTaxonName.setText(null);
265
			    }
266
				complete = !textNewTaxonName.getText().isEmpty();
267

    
268

    
269
		} else if (eventSource == selection_reuseExistingName) {
270
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
271
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
272
			if (selection_reuseExistingTaxon != null){
273
			    selection_reuseExistingTaxon.setEnabled(enabled);
274
			}
275
			textNewTaxonName.setEnabled(enabled);
276
			complete = !textNewTaxonName.getText().isEmpty();
277
		}
278

    
279
        if (eventSource == selection_Ref) {
280
            getEntity().setReference(selection_Ref.getEntity());
281
        }else if (eventSource == selection_SecRef) {
282
            taxon.setSec(selection_SecRef.getEntity());
283
        }else if (eventSource == microReference) {
284
            getEntity().setMicroReference(microReference.getText());
285
        }  else if (eventSource == checkbox_publish) {
286
            taxon.setPublish(checkbox_publish.getSelection());
287
        }
288

    
289
		if (eventSource == checkbox_excluded) {
290
            excluded = checkbox_excluded.getSelection();
291

    
292
            getEntity().setExcluded(excluded);
293

    
294
            multiLanguageTextExcludedNotes.setEnabled(excluded);
295
        }
296
		if (eventSource == checkbox_unplaced) {
297
            unplaced = checkbox_unplaced.getSelection();
298
            getEntity().setUnplaced(unplaced);
299

    
300
        }
301
		if (eventSource == textNewTaxonName) {
302
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
303
            if (selection_reuseExistingTaxon != null){
304
                selection_reuseExistingTaxon.setEnabled(enabled);
305
            }
306
            selection_reuseExistingName.setEnabled(enabled);
307

    
308
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
309
        }
310

    
311
		if (eventSource == selectionNodeAgentRelation){
312
		   boolean allComplete = true;
313
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
314
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
315
		   }
316
		    complete = !isCreateNew() && allComplete;
317
		}
318
	}
319

    
320
	public Classification getClassification() {
321
		return classification;
322
	}
323

    
324
//	public boolean isOpenInEditor() {
325
//		return openInEditor;
326
//	}
327

    
328
	public ITaxonTreeNode getParentTreeNode() {
329
		return parentTreeNode;
330
	}
331

    
332
	private void setClassification(Classification classification) {
333
		this.classification = classification;
334
		//setParentTreeNode(classification);
335
	}
336

    
337
	private void setParentTreeNode(TaxonNode parentTreeNode) {
338
		this.parentTreeNode = parentTreeNode;
339

    
340
		if (parentTreeNode.getTaxon() == null) {
341
			classification = parentTreeNode.getClassification();
342
//			if (selection_classification != null){
343
//			    selection_classification.setEntity(classification);
344
//			}
345
			selection_parentTaxonNode.setEntity(classification.getRootNode());
346
			selection_parentTaxonNode.setClassification(classification);
347
			selection_SecRef.setEntity(classification.getReference());
348
		} else  {
349
			classification = HibernateProxyHelper
350
					.deproxy(parentTreeNode.getClassification());
351
//			if (selection_classification != null){
352
//			    selection_classification.setEntity(classification);
353
//			}
354
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
355
                    .deproxy(parentTreeNode));
356
			selection_parentTaxonNode.setClassification(classification);
357
			selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
358

    
359
		}
360
	}
361

    
362
	private void setTreeNode(TaxonNode treeNode) {
363
        classification = HibernateProxyHelper
364
                    .deproxy(treeNode.getClassification());
365
        if (isCreateNew()){
366
           //selection_classification.setEntity(classification);
367
            selection_parentTaxonNode.setEntity(treeNode.getParent());
368
            selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
369
            selection_SecRef.setEntity(treeNode.getTaxon().getSec());
370
        }
371
	}
372

    
373
	private void setTaxon(Taxon taxon) {
374
		this.taxon = taxon;
375
		getEntity().setTaxon(taxon);
376
		if (isCreateNew()){
377
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
378
		}
379
		selection_SecRef.setEntity(taxon.getSec());
380
	}
381

    
382
	private void setTaxon(TaxonName taxonName) {
383
		Reference secundum = null;
384
		if (getParentTreeNode() != null) {
385
			if (this.selection_SecRef.getEntity() != null){
386
				secundum = this.selection_SecRef.getEntity();
387
			}
388
		}
389
		if (taxonName != null){
390
		    taxon = Taxon.NewInstance(taxonName, secundum);
391
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
392
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
393
		    }
394
		    getEntity().setTaxon(taxon);
395
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
396
//		        textNewTaxonName.setText(taxonName.getTitleCache());
397
//		    }
398
		} else {
399
		    textNewTaxonName.setText(null);
400
		}
401
	}
402

    
403
//	private void setOpenInEditor(boolean openInEditor) {
404
//		this.openInEditor = openInEditor;
405
//	}
406

    
407
	public String getTaxonName() {
408
        return textNewTaxonName.getText();
409
    }
410
	public Taxon getTaxon() {
411
        return taxon;
412
    }
413

    
414
	public Reference getReference(){
415
	    return selection_SecRef.getSelection();
416
	}
417

    
418
	public Control getMainControl(){
419
	    return textNewTaxonName.getMainControl();
420
	}
421

    
422
	public boolean isComplete() {
423
	    boolean allComplete = true;
424
	    if (!isCreateNew()){
425
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
426
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
427
	        }
428
	        complete = allComplete;
429
	    }
430
		return complete;
431
	}
432

    
433
    public boolean isCreateNew() {
434
        return createNew;
435
    }
436

    
437
    public boolean isExcluded() {
438
        return excluded;
439
    }
440

    
441
    public boolean isUnplaced() {
442
        return unplaced;
443
    }
444

    
445
    public boolean is_publish() {
446
        boolean isPublishPreference = true;
447
        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
448
        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
449
            isPublishPreference = false;
450
        }
451
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
452
    }
453

    
454
    public void set_publish(boolean publish) {
455
        this.checkbox_publish.setSelection(publish);
456
    }
457

    
458

    
459
}
(10-10/12)