source: TraumaOntologies/src/main/java/tudelft/healthpsychology/traumaontologies/SimpleGUI.java@ 5

Last change on this file since 5 was 5, checked in by Bart Vastenhouw, 5 years ago

Intermediate update

File size: 8.7 KB
Line 
1package tudelft.healthpsychology.traumaontologies;
2
3import java.awt.BorderLayout;
4import java.awt.HeadlessException;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.util.Arrays;
8
9import javax.swing.BoxLayout;
10import javax.swing.JButton;
11import javax.swing.JComboBox;
12import javax.swing.JFrame;
13import javax.swing.JLabel;
14import javax.swing.JOptionPane;
15import javax.swing.JPanel;
16import javax.swing.JScrollPane;
17import javax.swing.JTextArea;
18import javax.swing.JTextField;
19import javax.swing.SwingUtilities;
20
21import org.semanticweb.owlapi.apibinding.OWLManager;
22import org.semanticweb.owlapi.model.OWLOntology;
23import org.semanticweb.owlapi.model.OWLOntologyCreationException;
24import org.semanticweb.owlapi.model.OWLOntologyManager;
25
26import com.fasterxml.jackson.core.JsonProcessingException;
27import com.fasterxml.jackson.databind.ObjectMapper;
28
29import tudelft.healthpsychology.traumaontologies.answerstate.AnswerState;
30import tudelft.healthpsychology.traumaontologies.answerstate.AnswerStateExplanationDecorator;
31import tudelft.healthpsychology.traumaontologies.answerstate.AnswersBreathFirstState;
32import tudelft.healthpsychology.traumaontologies.answerstate.AnswersDepthFirstState;
33import tudelft.healthpsychology.traumaontologies.answerstate.ListOfAnswersState;
34import tudelft.healthpsychology.traumaontologies.answerstate.OntoPropAnswerState;
35import tudelft.healthpsychology.traumaontologies.answerstate.PropertiesAnswerState;
36import tudelft.healthpsychology.traumaontologies.owltree.OwlOntologyNode;
37import tudelft.healthpsychology.traumaontologies.owltree.OwlTree;
38import tudelft.healthpsychology.traumaontologies.owltree.OwlTreeReasoner;
39import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
40import tudelft.utilities.translator.FragmentCsvTranslator;
41import tudelft.utilities.translator.TranslationFailedException;
42
43@SuppressWarnings("serial")
44public class SimpleGUI extends JFrame {
45
46 private final JComboBox<String> languagesel = new JComboBox<String>(
47 new String[] { "EN", "NL" });
48 private JButton answerbutton;
49 private final JTextArea textArea = new JTextArea(5, 20);
50 private final JTextField answerArea = new JTextField();
51 private AnswerState dialogState;// tracks the user's answers
52 private FragmentCsvTranslator translator;
53 private final JButton savebutton = new JButton("'Save'");
54 private final ObjectMapper jackson = new ObjectMapper();
55
56 private final String EXPLAIN = "-make selection-";
57 private final String[] owlfiles = new String[] { EXPLAIN,
58 "Child Sexual Abuse.owl", "War.owl", "War Afghanistan.owl",
59 "War Bosnia.owl", "War Libya.owl" };
60 JComboBox<String> treatmentCombo = new JComboBox<>(owlfiles);
61 private OwlTree tree;
62
63 public SimpleGUI() throws OWLOntologyCreationException {
64 setDefaultCloseOperation(EXIT_ON_CLOSE);
65
66 JPanel panel = new JPanel();
67 panel.setLayout(new BorderLayout());
68 panel.add(new JLabel("Select treatment"), BorderLayout.NORTH);
69 panel.add(treatmentCombo, BorderLayout.CENTER);
70
71 getContentPane().add(panel);
72
73 treatmentCombo.addActionListener(select -> {
74 if (treatmentCombo.getSelectedIndex() != 0)
75 initPanelContents();
76 });
77
78 pack();
79 setVisible(true);
80 }
81
82 /**
83 * The main interaction panel. This removes the initial question to select
84 * the treatment and start the treatment itself.
85 */
86 private void initPanelContents() {
87 getContentPane().removeAll();
88 setTranslator("EN");
89 try {
90 initAnswerState();
91 } catch (OWLOntologyCreationException e) {
92 e.printStackTrace();
93 return;
94 }
95 answerbutton = new JButton(tr("Antwoord"));
96 JPanel panel = new JPanel();
97 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
98 panel.add(textArea);
99 textArea.setLineWrap(true);
100 textArea.setWrapStyleWord(true);
101 textArea.setEditable(false);
102 panel.add(answerArea);
103 panel.add(answerbutton);
104 panel.add(languagesel);
105 panel.add(savebutton);
106 getContentPane().add(panel);
107 pack();
108 updateQuestion();
109
110 answerbutton.addActionListener(action -> answer());
111 languagesel.addActionListener(action -> languageSelected());
112 savebutton.addActionListener(action -> save());
113
114 }
115
116 private void save() {
117 try {
118 JTextArea showtextarea = new JTextArea(
119 jackson.writeValueAsString(dialogState), 20, 80);
120 showtextarea.setLineWrap(true);
121 showtextarea.setEditable(false);
122 JScrollPane scrollPane1 = new JScrollPane(showtextarea);
123 JFrame saveframe = new JFrame(
124 "This data would be saved if this were no demo");
125 saveframe.getContentPane().add(scrollPane1, BorderLayout.CENTER);
126 saveframe.pack();
127 saveframe.setVisible(true);
128
129 } catch (HeadlessException | JsonProcessingException e) {
130 e.printStackTrace();
131 }
132 }
133
134 private void languageSelected() {
135 setTranslator((String) languagesel.getSelectedItem());
136 updateQuestion();
137 answerbutton.setText(tr("Antwoord"));
138 }
139
140 private void setTranslator(String lang) {
141 try {
142 translator = new FragmentCsvTranslator(
143 getClass().getResourceAsStream("/" + lang + ".csv"));
144 } catch (IOException e) {
145 e.printStackTrace();
146 }
147
148 }
149
150 public static void main(String[] args) throws OWLOntologyCreationException,
151 FileNotFoundException, IOException, TranslationFailedException {
152 new SimpleGUI();
153 }
154
155 /**
156 * This constructs the dialog process: start with asking the geographical
157 * location, then the location type, then the objects in depth first order,
158 * then the persons in breath first order. You can modify this as needed.
159 *
160 * @throws OWLOntologyCreationException
161 */
162 private void initAnswerState() throws OWLOntologyCreationException {
163 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
164 OWLOntology ontology = manager.loadOntologyFromOntologyDocument(
165 getClass().getResourceAsStream(
166 "/" + treatmentCombo.getSelectedItem()));
167 OwlTreeReasoner reas = new OwlTreeReasoner(ontology);
168 tree = new OwlTree(reas);
169
170 AnswerState geoanswer = new PropertiesAnswerState(
171 "Geografische_locatie");
172 AnswerState loctypeanswer = new AnswerStateExplanationDecorator(
173 new OntoPropAnswerState(
174 new OwlOntologyNode("Type_Locatie", reas), null),
175 "In wat voor soort locatie was U?");
176 AnswerState objectsanswer = new AnswerStateExplanationDecorator(
177 new AnswersDepthFirstState("Object",
178 "Zijn er nog meer relevante objecten?", tree),
179 "We verzamelen de relevante objecten op de locatie. We gaan een voor een door alle relevante objecten, beginnend bij het eerste object.");
180 AnswerState personsanswer = new AnswerStateExplanationDecorator(
181 new AnswersBreathFirstState(
182 new OwlOntologyNode("Persoon", reas), "Naam",
183 "Zijn er nog meer relevante personen?"),
184 "We verzamelen de relevante personen op de locatie. We verzamelen eerst de namen.");
185
186 dialogState = new ListOfAnswersState(Arrays.asList(geoanswer,
187 loctypeanswer, objectsanswer, personsanswer));
188 }
189
190 /**
191 * Used to translate fixed general texts. It's a bug if the given text does
192 * not translate but we can't throw as we are in handlers usually. So we
193 * print stacktrace in that case.
194 *
195 * @param text text to be translated.
196 * @return translted text
197 */
198 private String tr(String text) {
199 try {
200 return translator.forward(text);
201 } catch (TranslationFailedException e) {
202 e.printStackTrace();
203 return "ERROR";
204 }
205 }
206
207 private void answer() {
208 try {
209 String answer = answerArea.getText();
210 if (answer.isEmpty()) {
211 JOptionPane.showMessageDialog(this,
212 tr("Je antwoord is leeg. Probeer nog eens."),
213 tr("Probeer nogmaals"), 1);
214 return;
215
216 }
217 TypedQuestion type = dialogState.getOptions(tree);
218 if (type.isTranslated()) {
219 try {
220 answer = translator.reverse(answer);
221 } catch (TranslationFailedException e) {
222 JOptionPane.showMessageDialog(this, tr("Je antwoord "
223 + answer
224 + " kan niet vertaald worden. Kan U uw spelling controleren."),
225 tr("Vertaling mislukt"), 1);
226 return;
227 }
228 }
229 if (!type.fits(answer)) {
230 JOptionPane.showMessageDialog(this, tr(
231 "Uw antwoord is geen goed antwoord op deze vraag. Kan U de vraag nogmaals beantwoorden."),
232 tr("Antwoord past niet bij vraag"), 1);
233 return;
234 }
235
236 dialogState = dialogState.with(answer, tree);
237 updateQuestion();
238 answerArea.setText("");
239 } catch (Exception e) {
240 e.printStackTrace();
241 }
242 }
243
244 private void updateQuestion() {
245 SwingUtilities.invokeLater(new Runnable() {
246
247 @Override
248 public void run() {
249 TypedQuestion opts = dialogState.getOptions(tree);
250 if (opts == null) {
251 // we're done!
252 JOptionPane.showMessageDialog(SimpleGUI.this,
253 tr("We zijn klaar!"), tr("klaar"), 1);
254 setVisible(false);
255 return;
256 }
257 try {
258 textArea.setText(translator.forwardFragments(
259 dialogState.getOptions(tree).getQuestion()));
260 } catch (TranslationFailedException e) {
261 e.printStackTrace();
262 }
263 }
264 });
265 }
266
267}
Note: See TracBrowser for help on using the repository browser.