2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.section
.description
;
11 import java
.util
.List
;
13 import org
.apache
.logging
.log4j
.LogManager
;
14 import org
.apache
.logging
.log4j
.Logger
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.core
.runtime
.IStatus
;
17 import org
.eclipse
.core
.runtime
.Status
;
18 import org
.eclipse
.core
.runtime
.jobs
.Job
;
19 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
20 import org
.eclipse
.swt
.widgets
.Display
;
22 import eu
.etaxonomy
.cdm
.api
.service
.NaturalLanguageGenerator
;
23 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
24 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
25 import eu
.etaxonomy
.cdm
.model
.term
.TermTree
;
26 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
27 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
28 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractCdmDetailElement
;
36 * @created Sep 16, 2010
38 public class NaturalLanguageDetailElement
extends AbstractCdmDetailElement
<TaxonDescription
> {
40 private static final Logger logger
= LogManager
.getLogger();
42 private LabelElement label
;
45 * <p>Constructor for NaturalLanguageDetailElement.</p>
47 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
48 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
50 public NaturalLanguageDetailElement(CdmFormFactory formFactory
,
51 ICdmFormElement formElement
) {
52 super(formFactory
, formElement
);
57 protected void createControls(ICdmFormElement formElement
,
58 TaxonDescription entity
, int style
) {
59 label
= formFactory
.createLabel(formElement
, "Generating Natural Language Description ...");
61 if(entity
.hasStructuredData()){
62 FetchNaturalLanguageDescriptionJob job
= new FetchNaturalLanguageDescriptionJob("Retrieving Natural Language Description", entity
);
65 label
.setText("Taxon Description does not contain structured data.");
70 public void handleEvent(Object eventSource
) {
71 // nothing gets edited, nothing gets updated
74 private class FetchNaturalLanguageDescriptionJob
extends Job
{
76 private TaxonDescription entity
;
77 private NaturalLanguageGenerator generator
= new NaturalLanguageGenerator();
78 private TermTree
<?
> featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
79 private Display display
= Display
.getCurrent();
81 public FetchNaturalLanguageDescriptionJob(String jobName
, TaxonDescription entity
){
87 protected IStatus
run(IProgressMonitor monitor
) {
90 if(featureTree
!= null){
92 List
<TextData
> naturalLanguageDescription
= generator
.generateNaturalLanguageDescription(featureTree
, entity
, CdmStore
.getDefaultLanguage());
94 for(TextData element
: naturalLanguageDescription
){
95 text
+= element
.getText(CdmStore
.getDefaultLanguage()) + " ";
102 text
= "Could not generate natural language description, because no Feature Tree was set.";
105 final String aggregatedText
= text
;
107 display
.asyncExec(new Runnable(){
111 if(!label
.isDisposed()){
112 label
.setText(aggregatedText
);
113 AbstractUtility
.reflowDetailsViewer(formFactory
.getContext().get(EPartService
.class));
119 return Status
.OK_STATUS
;