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.name.TaxonName;
|
25
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
27
|
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
|
28
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
29
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
30
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
31
|
import eu.etaxonomy.taxeditor.preference.PublishEnum;
|
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
|
CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
|
174
|
if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.Publish.getKey())){
|
175
|
checkbox_publish.setSelection(true);
|
176
|
} else if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
|
177
|
checkbox_publish.setSelection(false);
|
178
|
}else{
|
179
|
if(this.getParentTreeNode() != null && this.getParentTreeNode().isInstanceOf(TaxonNode.class) && ((TaxonNode)this.getParentTreeNode()).getTaxon() != null){
|
180
|
checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
|
181
|
}else{
|
182
|
checkbox_publish.setSelection(true);
|
183
|
}
|
184
|
}
|
185
|
|
186
|
} else{
|
187
|
setTreeNode(entity);
|
188
|
complete = true;
|
189
|
}
|
190
|
if (entity instanceof TaxonNode) {
|
191
|
checkbox_unplaced = formFactory.createCheckbox(formElement,
|
192
|
"Taxon is unplaced", entity.isUnplaced(), style);
|
193
|
}else{
|
194
|
checkbox_unplaced = formFactory.createCheckbox(formElement,
|
195
|
"Taxon is unplaced", false, style);
|
196
|
}
|
197
|
if (isCreateNew()){
|
198
|
preFillTaxonName();
|
199
|
}
|
200
|
|
201
|
if (entity instanceof TaxonNode) {
|
202
|
checkbox_excluded = formFactory.createCheckbox(formElement,
|
203
|
"Taxon is excluded", entity.isExcluded(), style);
|
204
|
multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", entity.getExcludedNote(), 50, style);
|
205
|
multiLanguageTextExcludedNotes.setEnabled(entity.isExcluded());
|
206
|
}else{
|
207
|
checkbox_excluded = formFactory.createCheckbox(formElement,
|
208
|
"Taxon is excluded", false, style);
|
209
|
multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", new HashMap<Language, LanguageString>(), 50, style);
|
210
|
multiLanguageTextExcludedNotes.setEnabled(false);
|
211
|
}
|
212
|
if (!isCreateNew()){
|
213
|
selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
|
214
|
selectionNodeAgentRelation.setEntity(entity);
|
215
|
selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
216
|
}
|
217
|
// checkbox_openInEditor = formFactory.createCheckbox(formElement,
|
218
|
// "Open in Editor", true, style);
|
219
|
// setOpenInEditor(true);
|
220
|
}
|
221
|
|
222
|
private void preFillTaxonName() {
|
223
|
if(getEntity() instanceof TaxonNode ){
|
224
|
TaxonNode node = parentTreeNode;
|
225
|
if(node.getTaxon()!=null){
|
226
|
Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
|
227
|
if(taxon.getName()!=null){
|
228
|
TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
|
229
|
if (!isCreateNew()){
|
230
|
textNewTaxonName.setText(name.getNameCache());
|
231
|
} else if( ! name.isSupraGeneric() && name.getRank() != null){
|
232
|
String taxonName = "";
|
233
|
if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
|
234
|
taxonName = name.getGenusOrUninomial();
|
235
|
}
|
236
|
else if(name.isSpecies() || name.isInfraSpecific() ){
|
237
|
taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
|
238
|
}
|
239
|
if (StringUtils.isNotBlank(taxonName)){
|
240
|
textNewTaxonName.setText(taxonName + " ");
|
241
|
if(textNewTaxonName.getMainControl() instanceof Text){
|
242
|
Text text = (Text)textNewTaxonName.getMainControl();
|
243
|
text.setSelection(textNewTaxonName.getText().length());
|
244
|
}
|
245
|
}
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
}
|
251
|
|
252
|
/** {@inheritDoc} */
|
253
|
@Override
|
254
|
public void handleEvent(Object eventSource) {
|
255
|
if (eventSource == selection_parentTaxonNode) {
|
256
|
setParentTreeNode(selection_parentTaxonNode.getEntity());
|
257
|
} else if (eventSource == selection_reuseExistingTaxon) {
|
258
|
boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
|
259
|
selection_reuseExistingName.setEnabled(enabled);
|
260
|
if (!enabled){
|
261
|
setTaxon(selection_reuseExistingTaxon.getEntity());
|
262
|
}else{
|
263
|
textNewTaxonName.setText(null);
|
264
|
}
|
265
|
complete = !textNewTaxonName.getText().isEmpty();
|
266
|
|
267
|
|
268
|
} else if (eventSource == selection_reuseExistingName) {
|
269
|
boolean enabled = selection_reuseExistingName.getEntity() == null;
|
270
|
setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
|
271
|
if (selection_reuseExistingTaxon != null){
|
272
|
selection_reuseExistingTaxon.setEnabled(enabled);
|
273
|
}
|
274
|
textNewTaxonName.setEnabled(enabled);
|
275
|
complete = !textNewTaxonName.getText().isEmpty();
|
276
|
}
|
277
|
|
278
|
if (eventSource == selection_Ref) {
|
279
|
getEntity().setReference(selection_Ref.getEntity());
|
280
|
}else if (eventSource == selection_SecRef) {
|
281
|
taxon.setSec(selection_SecRef.getEntity());
|
282
|
}else if (eventSource == microReference) {
|
283
|
getEntity().setMicroReference(microReference.getText());
|
284
|
} else if (eventSource == checkbox_publish) {
|
285
|
taxon.setPublish(checkbox_publish.getSelection());
|
286
|
}
|
287
|
|
288
|
if (eventSource == checkbox_excluded) {
|
289
|
excluded = checkbox_excluded.getSelection();
|
290
|
|
291
|
getEntity().setExcluded(excluded);
|
292
|
|
293
|
multiLanguageTextExcludedNotes.setEnabled(excluded);
|
294
|
}
|
295
|
if (eventSource == checkbox_unplaced) {
|
296
|
unplaced = checkbox_unplaced.getSelection();
|
297
|
getEntity().setUnplaced(unplaced);
|
298
|
|
299
|
}
|
300
|
if (eventSource == textNewTaxonName) {
|
301
|
boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
|
302
|
if (selection_reuseExistingTaxon != null){
|
303
|
selection_reuseExistingTaxon.setEnabled(enabled);
|
304
|
}
|
305
|
selection_reuseExistingName.setEnabled(enabled);
|
306
|
|
307
|
complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
|
308
|
}
|
309
|
|
310
|
if (eventSource == selectionNodeAgentRelation){
|
311
|
boolean allComplete = true;
|
312
|
for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
|
313
|
allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
|
314
|
}
|
315
|
complete = !isCreateNew() && allComplete;
|
316
|
}
|
317
|
}
|
318
|
|
319
|
public Classification getClassification() {
|
320
|
return classification;
|
321
|
}
|
322
|
|
323
|
// public boolean isOpenInEditor() {
|
324
|
// return openInEditor;
|
325
|
// }
|
326
|
|
327
|
public ITaxonTreeNode getParentTreeNode() {
|
328
|
return parentTreeNode;
|
329
|
}
|
330
|
|
331
|
private void setClassification(Classification classification) {
|
332
|
this.classification = classification;
|
333
|
//setParentTreeNode(classification);
|
334
|
}
|
335
|
|
336
|
private void setParentTreeNode(TaxonNode parentTreeNode) {
|
337
|
this.parentTreeNode = parentTreeNode;
|
338
|
|
339
|
if (parentTreeNode.getTaxon() == null) {
|
340
|
classification = parentTreeNode.getClassification();
|
341
|
// if (selection_classification != null){
|
342
|
// selection_classification.setEntity(classification);
|
343
|
// }
|
344
|
selection_parentTaxonNode.setEntity(classification.getRootNode());
|
345
|
selection_parentTaxonNode.setClassification(classification);
|
346
|
selection_SecRef.setEntity(classification.getReference());
|
347
|
} else {
|
348
|
classification = HibernateProxyHelper
|
349
|
.deproxy(parentTreeNode.getClassification());
|
350
|
// if (selection_classification != null){
|
351
|
// selection_classification.setEntity(classification);
|
352
|
// }
|
353
|
selection_parentTaxonNode.setEntity(HibernateProxyHelper
|
354
|
.deproxy(parentTreeNode));
|
355
|
selection_parentTaxonNode.setClassification(classification);
|
356
|
selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
|
357
|
|
358
|
}
|
359
|
}
|
360
|
|
361
|
private void setTreeNode(TaxonNode treeNode) {
|
362
|
classification = HibernateProxyHelper
|
363
|
.deproxy(treeNode.getClassification());
|
364
|
if (isCreateNew()){
|
365
|
//selection_classification.setEntity(classification);
|
366
|
selection_parentTaxonNode.setEntity(treeNode.getParent());
|
367
|
selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
|
368
|
selection_SecRef.setEntity(treeNode.getTaxon().getSec());
|
369
|
}
|
370
|
}
|
371
|
|
372
|
private void setTaxon(Taxon taxon) {
|
373
|
this.taxon = taxon;
|
374
|
getEntity().setTaxon(taxon);
|
375
|
if (isCreateNew()){
|
376
|
textNewTaxonName.setText(taxon.getName().getTitleCache());
|
377
|
}
|
378
|
selection_SecRef.setEntity(taxon.getSec());
|
379
|
}
|
380
|
|
381
|
private void setTaxon(TaxonName taxonName) {
|
382
|
Reference secundum = null;
|
383
|
if (getParentTreeNode() != null) {
|
384
|
if (this.selection_SecRef.getEntity() != null){
|
385
|
secundum = this.selection_SecRef.getEntity();
|
386
|
}
|
387
|
}
|
388
|
if (taxonName != null){
|
389
|
taxon = Taxon.NewInstance(taxonName, secundum);
|
390
|
if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
|
391
|
textNewTaxonName.setText(taxonName.getTitleCache());
|
392
|
}
|
393
|
getEntity().setTaxon(taxon);
|
394
|
// if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
|
395
|
// textNewTaxonName.setText(taxonName.getTitleCache());
|
396
|
// }
|
397
|
} else {
|
398
|
textNewTaxonName.setText(null);
|
399
|
}
|
400
|
}
|
401
|
|
402
|
// private void setOpenInEditor(boolean openInEditor) {
|
403
|
// this.openInEditor = openInEditor;
|
404
|
// }
|
405
|
|
406
|
public String getTaxonName() {
|
407
|
return textNewTaxonName.getText();
|
408
|
}
|
409
|
public Taxon getTaxon() {
|
410
|
return taxon;
|
411
|
}
|
412
|
|
413
|
public Reference getReference(){
|
414
|
return selection_SecRef.getSelection();
|
415
|
}
|
416
|
|
417
|
public Control getMainControl(){
|
418
|
return textNewTaxonName.getMainControl();
|
419
|
}
|
420
|
|
421
|
public boolean isComplete() {
|
422
|
boolean allComplete = true;
|
423
|
if (!isCreateNew()){
|
424
|
for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
|
425
|
allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
|
426
|
}
|
427
|
complete = allComplete;
|
428
|
}
|
429
|
return complete;
|
430
|
}
|
431
|
|
432
|
public boolean isCreateNew() {
|
433
|
return createNew;
|
434
|
}
|
435
|
|
436
|
public boolean isExcluded() {
|
437
|
return excluded;
|
438
|
}
|
439
|
|
440
|
public boolean isUnplaced() {
|
441
|
return unplaced;
|
442
|
}
|
443
|
|
444
|
public boolean is_publish() {
|
445
|
boolean isPublishPreference = true;
|
446
|
CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
|
447
|
if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
|
448
|
isPublishPreference = false;
|
449
|
}
|
450
|
return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
|
451
|
}
|
452
|
|
453
|
public void set_publish(boolean publish) {
|
454
|
this.checkbox_publish.setSelection(publish);
|
455
|
}
|
456
|
|
457
|
|
458
|
}
|