- moved ICdmFormElement to campanula.compatibility
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / description / NaturalLanguageDetailElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.section.description;
12
13 import java.util.List;
14
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;
21
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.element.CdmFormFactory;
30 import eu.etaxonomy.taxeditor.ui.element.LabelElement;
31 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
32 import eu.etaxonomy.taxeditor.ui.section.campanula.compatibility.ICdmFormElement;
33
34 /**
35 * <p>NaturalLanguageDetailElement class.</p>
36 *
37 * @author n.hoffmann
38 * @created Sep 16, 2010
39 * @version 1.0
40 */
41 public class NaturalLanguageDetailElement extends AbstractCdmDetailElement<TaxonDescription> {
42
43
44 private static final Logger logger = Logger
45 .getLogger(NaturalLanguageDetailElement.class);
46 private LabelElement label;
47
48 /**
49 * <p>Constructor for NaturalLanguageDetailElement.</p>
50 *
51 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
52 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
53 */
54 public NaturalLanguageDetailElement(CdmFormFactory formFactory,
55 ICdmFormElement formElement) {
56 super(formFactory, formElement);
57 }
58
59 /** {@inheritDoc} */
60 @Override
61 protected void createControls(ICdmFormElement formElement,
62 TaxonDescription entity, int style) {
63 label = formFactory.createLabel(formElement, "Generating Natural Language Description ...");
64
65 if(entity.hasStructuredData()){
66 FetchNaturalLanguageDescriptionJob job = new FetchNaturalLanguageDescriptionJob("Retrieving Natural Language Description", entity);
67 job.schedule();
68 }else{
69 label.setText("Taxon Description does not contain structured data.");
70 }
71 }
72
73 /* (non-Javadoc)
74 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java.lang.Object)
75 */
76 /** {@inheritDoc} */
77 @Override
78 public void handleEvent(Object eventSource) {
79 // nothing gets edited, nothing gets updated
80 }
81
82 private class FetchNaturalLanguageDescriptionJob extends Job {
83
84 private TaxonDescription entity;
85 private NaturalLanguageGenerator generator = new NaturalLanguageGenerator();
86 private FeatureTree featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
87 private Display display = Display.getCurrent();
88
89 public FetchNaturalLanguageDescriptionJob(String jobName, TaxonDescription entity){
90 super(jobName);
91 this.entity = entity;
92 }
93
94 @Override
95 protected IStatus run(IProgressMonitor monitor) {
96 String text = "";
97
98 if(featureTree != null){
99 CdmStore.createConversation();
100 try{
101 List<TextData> naturalLanguageDescription = generator.generateNaturalLanguageDescription(featureTree, entity, CdmStore.getDefaultLanguage());
102
103 for(TextData element : naturalLanguageDescription){
104 text += element.getText(CdmStore.getDefaultLanguage()) + " ";
105 }
106 }catch(Exception e){
107 logger.error(e);
108 }
109 }
110 else{
111 text = "Could not generate natural language description, because no Feature Tree was set.";
112 }
113
114 final String aggregatedText = text;
115
116 display.asyncExec(new Runnable(){
117
118 @Override
119 public void run() {
120 label.setText(aggregatedText);
121 StoreUtil.reflowDetailsViewer();
122 }
123
124 });
125
126 return Status.OK_STATUS;
127 }
128
129 }
130 }