package tudelft.healthpsychology.traumaontologies; import java.awt.BorderLayout; import java.awt.HeadlessException; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import tudelft.healthpsychology.traumaontologies.answerstate.AnswerState; import tudelft.healthpsychology.traumaontologies.answerstate.AnswerStateExplanationDecorator; import tudelft.healthpsychology.traumaontologies.answerstate.AnswersBreathFirstState; import tudelft.healthpsychology.traumaontologies.answerstate.AnswersDepthFirstState; import tudelft.healthpsychology.traumaontologies.answerstate.ListOfAnswersState; import tudelft.healthpsychology.traumaontologies.answerstate.OntoPropAnswerState; import tudelft.healthpsychology.traumaontologies.answerstate.PropertiesAnswerState; import tudelft.healthpsychology.traumaontologies.owltree.OwlOntologyNode; import tudelft.healthpsychology.traumaontologies.owltree.OwlTree; import tudelft.healthpsychology.traumaontologies.owltree.OwlTreeReasoner; import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion; import tudelft.utilities.translator.FragmentCsvTranslator; import tudelft.utilities.translator.TranslationFailedException; @SuppressWarnings("serial") public class SimpleGUI extends JFrame { private final JComboBox languagesel = new JComboBox( new String[] { "EN", "NL" }); private JButton answerbutton; private final JTextArea textArea = new JTextArea(5, 20); private final JTextField answerArea = new JTextField(); private AnswerState dialogState;// tracks the user's answers private FragmentCsvTranslator translator; private final JButton savebutton = new JButton("'Save'"); private final ObjectMapper jackson = new ObjectMapper(); private final String EXPLAIN = "-make selection-"; private final String[] owlfiles = new String[] { EXPLAIN, "Child Sexual Abuse.owl", "War.owl", "War Afghanistan.owl", "War Bosnia.owl", "War Libya.owl" }; JComboBox treatmentCombo = new JComboBox<>(owlfiles); private OwlTree tree; public SimpleGUI() throws OWLOntologyCreationException { setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JLabel("Select treatment"), BorderLayout.NORTH); panel.add(treatmentCombo, BorderLayout.CENTER); getContentPane().add(panel); treatmentCombo.addActionListener(select -> { if (treatmentCombo.getSelectedIndex() != 0) initPanelContents(); }); pack(); setVisible(true); } /** * The main interaction panel. This removes the initial question to select * the treatment and start the treatment itself. */ private void initPanelContents() { getContentPane().removeAll(); setTranslator("EN"); try { initAnswerState(); } catch (OWLOntologyCreationException e) { e.printStackTrace(); return; } answerbutton = new JButton(tr("Antwoord")); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(textArea); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); textArea.setEditable(false); panel.add(answerArea); panel.add(answerbutton); panel.add(languagesel); panel.add(savebutton); getContentPane().add(panel); pack(); updateQuestion(); answerbutton.addActionListener(action -> answer()); languagesel.addActionListener(action -> languageSelected()); savebutton.addActionListener(action -> save()); } private void save() { try { JTextArea showtextarea = new JTextArea( jackson.writeValueAsString(dialogState), 20, 80); showtextarea.setLineWrap(true); showtextarea.setEditable(false); JScrollPane scrollPane1 = new JScrollPane(showtextarea); JFrame saveframe = new JFrame( "This data would be saved if this were no demo"); saveframe.getContentPane().add(scrollPane1, BorderLayout.CENTER); saveframe.pack(); saveframe.setVisible(true); } catch (HeadlessException | JsonProcessingException e) { e.printStackTrace(); } } private void languageSelected() { setTranslator((String) languagesel.getSelectedItem()); updateQuestion(); answerbutton.setText(tr("Antwoord")); } private void setTranslator(String lang) { try { translator = new FragmentCsvTranslator( getClass().getResourceAsStream("/" + lang + ".csv")); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws OWLOntologyCreationException, FileNotFoundException, IOException, TranslationFailedException { new SimpleGUI(); } /** * This constructs the dialog process: start with asking the geographical * location, then the location type, then the objects in depth first order, * then the persons in breath first order. You can modify this as needed. * * @throws OWLOntologyCreationException */ private void initAnswerState() throws OWLOntologyCreationException { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntologyFromOntologyDocument( getClass().getResourceAsStream( "/" + treatmentCombo.getSelectedItem())); OwlTreeReasoner reas = new OwlTreeReasoner(ontology); tree = new OwlTree(reas); AnswerState geoanswer = new PropertiesAnswerState( "Geografische_locatie"); AnswerState loctypeanswer = new AnswerStateExplanationDecorator( new OntoPropAnswerState( new OwlOntologyNode("Type_Locatie", reas), null), "In wat voor soort locatie was U?"); AnswerState objectsanswer = new AnswerStateExplanationDecorator( new AnswersDepthFirstState("Object", "Zijn er nog meer relevante objecten?", tree), "We verzamelen de relevante objecten op de locatie. We gaan een voor een door alle relevante objecten, beginnend bij het eerste object."); AnswerState personsanswer = new AnswerStateExplanationDecorator( new AnswersBreathFirstState( new OwlOntologyNode("Persoon", reas), "Naam", "Zijn er nog meer relevante personen?"), "We verzamelen de relevante personen op de locatie. We verzamelen eerst de namen."); dialogState = new ListOfAnswersState(Arrays.asList(geoanswer, loctypeanswer, objectsanswer, personsanswer)); } /** * Used to translate fixed general texts. It's a bug if the given text does * not translate but we can't throw as we are in handlers usually. So we * print stacktrace in that case. * * @param text text to be translated. * @return translted text */ private String tr(String text) { try { return translator.forward(text); } catch (TranslationFailedException e) { e.printStackTrace(); return "ERROR"; } } private void answer() { try { String answer = answerArea.getText(); if (answer.isEmpty()) { JOptionPane.showMessageDialog(this, tr("Je antwoord is leeg. Probeer nog eens."), tr("Probeer nogmaals"), 1); return; } TypedQuestion type = dialogState.getOptions(tree); if (type.isTranslated()) { try { answer = translator.reverse(answer); } catch (TranslationFailedException e) { JOptionPane.showMessageDialog(this, tr("Je antwoord " + answer + " kan niet vertaald worden. Kan U uw spelling controleren."), tr("Vertaling mislukt"), 1); return; } } if (!type.fits(answer)) { JOptionPane.showMessageDialog(this, tr( "Uw antwoord is geen goed antwoord op deze vraag. Kan U de vraag nogmaals beantwoorden."), tr("Antwoord past niet bij vraag"), 1); return; } dialogState = dialogState.with(answer, tree); updateQuestion(); answerArea.setText(""); } catch (Exception e) { e.printStackTrace(); } } private void updateQuestion() { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TypedQuestion opts = dialogState.getOptions(tree); if (opts == null) { // we're done! JOptionPane.showMessageDialog(SimpleGUI.this, tr("We zijn klaar!"), tr("klaar"), 1); setVisible(false); return; } try { textArea.setText(translator.forwardFragments( dialogState.getOptions(tree).getQuestion())); } catch (TranslationFailedException e) { e.printStackTrace(); } } }); } }