3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.ui
.section
.description
;
13 import java
.util
.List
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.swt
.widgets
.Display
;
22 import eu
.etaxonomy
.cdm
.api
.service
.NaturalLanguageGenerator
;
23 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
24 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
25 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
26 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
27 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
28 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
29 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractCdmDetailElement
;
35 * <p>NaturalLanguageDetailElement class.</p>
38 * @created Sep 16, 2010
41 public class NaturalLanguageDetailElement
extends AbstractCdmDetailElement
<TaxonDescription
> {
44 private static final Logger logger
= Logger
45 .getLogger(NaturalLanguageDetailElement
.class);
46 private LabelElement label
;
49 * <p>Constructor for NaturalLanguageDetailElement.</p>
51 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
52 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
54 public NaturalLanguageDetailElement(CdmFormFactory formFactory
,
55 ICdmFormElement formElement
) {
56 super(formFactory
, formElement
);
61 protected void createControls(ICdmFormElement formElement
,
62 TaxonDescription entity
, int style
) {
63 label
= formFactory
.createLabel(formElement
, "Generating Natural Language Description ...");
65 if(entity
.hasStructuredData()){
66 FetchNaturalLanguageDescriptionJob job
= new FetchNaturalLanguageDescriptionJob("Retrieving Natural Language Description", entity
);
69 label
.setText("Taxon Description does not contain structured data.");
74 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java.lang.Object)
78 public void handleEvent(Object eventSource
) {
79 // nothing gets edited, nothing gets updated
82 private class FetchNaturalLanguageDescriptionJob
extends Job
{
84 private TaxonDescription entity
;
85 private NaturalLanguageGenerator generator
= new NaturalLanguageGenerator();
86 private FeatureTree featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
87 private Display display
= Display
.getCurrent();
89 public FetchNaturalLanguageDescriptionJob(String jobName
, TaxonDescription entity
){
95 protected IStatus
run(IProgressMonitor monitor
) {
98 if(featureTree
!= null){
99 CdmStore
.createConversation();
101 List
<TextData
> naturalLanguageDescription
= generator
.generateNaturalLanguageDescription(featureTree
, entity
, CdmStore
.getDefaultLanguage());
103 for(TextData element
: naturalLanguageDescription
){
104 text
+= element
.getText(CdmStore
.getDefaultLanguage()) + " ";
111 text
= "Could not generate natural language description, because no Feature Tree was set.";
114 final String aggregatedText
= text
;
116 display
.asyncExec(new Runnable(){
120 label
.setText(aggregatedText
);
121 StoreUtil
.reflowDetailsViewer();
126 return Status
.OK_STATUS
;