From 0681acdcc40a703a97c93aaffdf7609fe9ce1d80 Mon Sep 17 00:00:00 2001
From: Christos Christodoulopoulos <christos.c2009@gmail.com>
Date: Thu, 26 Feb 2015 21:10:21 -0600
Subject: [PATCH] Added ability to move annotation labels when editing trees
 Added double-click listener to the start screen lists.

---
 .../datastructure/JBDataStructure.java        | 23 +++++++++
 src/jubilee/toolkit/JBOpenDialog.java         | 20 +++++++-
 src/jubilee/toolkit/JBTreeEditPanel.java      | 51 +++++++++++++++----
 3 files changed, 83 insertions(+), 11 deletions(-)

diff --git a/src/jubilee/datastructure/JBDataStructure.java b/src/jubilee/datastructure/JBDataStructure.java
index 4f70d00..09aab52 100644
--- a/src/jubilee/datastructure/JBDataStructure.java
+++ b/src/jubilee/datastructure/JBDataStructure.java
@@ -158,6 +158,29 @@ public class JBDataStructure {
         }
     }
 
+    public void setTree(String treeString, int offset, int newWordIndex) throws Exception {
+        TBTree newTree = readTBTree(treeString);
+        // Add the propbank annotation to the new tree including the new indices
+        String oldPropbankLabels[] = tbTree.toPropbank().split("\\s+");
+        String newLabels = "";
+        for (String label : oldPropbankLabels) {
+            int index = Integer.parseInt(label.split(":")[0]);
+            if (index >= newWordIndex)
+                index += offset;
+            newLabels += index + ":" + label.split(":")[1] + " ";
+        }
+
+        readPropbankLabels(newLabels, newTree);
+        if (!tbTree.equals(newTree)) {
+            tbTree = newTree;
+            hasChanged(true);
+        }
+        else {
+            JOptionPane.showMessageDialog(null, "No changes detected", "Information",
+                    JOptionPane.INFORMATION_MESSAGE);
+        }
+    }
+
     public void setRoleset(String roleset) {
         if (!this.roleset.equals(roleset)) {
             this.roleset = roleset;
diff --git a/src/jubilee/toolkit/JBOpenDialog.java b/src/jubilee/toolkit/JBOpenDialog.java
index 4bf0dcf..ee810eb 100644
--- a/src/jubilee/toolkit/JBOpenDialog.java
+++ b/src/jubilee/toolkit/JBOpenDialog.java
@@ -97,12 +97,13 @@ public class JBOpenDialog extends JDialog implements ActionListener, ItemListene
 		lm_newTask = new DefaultListModel<String>();
 		ls_newTask = new JList<String>(lm_newTask);
 		ls_newTask.setBorder(new TitledBorder("New Tasks"));
-		ls_newTask.addListSelectionListener(this);
 		
 		lm_myTask = new DefaultListModel<String>();
 		ls_myTask = new JList<String>(lm_myTask);
 		ls_myTask.setBorder(new TitledBorder("My Tasks"));
-		ls_myTask.addListSelectionListener(this);
+
+        addListListeners();
+
 		initJList();
 		
 		// button: cancel, enter
@@ -119,6 +120,21 @@ public class JBOpenDialog extends JDialog implements ActionListener, ItemListene
 		
 		return map;
 	}
+
+    private void addListListeners() {
+        MouseAdapter doubleClickListener = new MouseAdapter() {
+            @Override
+            public void mouseClicked(MouseEvent e) {
+                if (e.getClickCount() == 2) {
+                    actionBtEnter();
+                } else super.mouseClicked(e);
+            }
+        };
+        ls_newTask.addListSelectionListener(this);
+        ls_newTask.addMouseListener(doubleClickListener);
+        ls_myTask.addListSelectionListener(this);
+        ls_myTask.addMouseListener(doubleClickListener);
+    }
 	
 	private JButton getJButton(Container cp, String title) {
 		JButton bt = new JButton(title);
diff --git a/src/jubilee/toolkit/JBTreeEditPanel.java b/src/jubilee/toolkit/JBTreeEditPanel.java
index 7708493..4ee0be1 100644
--- a/src/jubilee/toolkit/JBTreeEditPanel.java
+++ b/src/jubilee/toolkit/JBTreeEditPanel.java
@@ -18,17 +18,19 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
     private final JTextArea textArea;
     private JButton buttonApply, buttonCancel;
     private JBDataStructure annotation;
+    private JTextField fieldWordsNum, fieldWordPos;
 
     JBToolkit parent;
 
     public JBTreeEditPanel(JBToolkit parent, JBDataStructure annotation) {
-        super("Syntax tree");
+        super("Syntax tree edit");
 
         this.annotation = annotation;
         this.parent = parent;
 
         textArea = new JTextArea(annotation.getTbTree().toTextTree());
         textArea.setMargin(new Insets(10, 10, 10, 10));
+        textArea.setPreferredSize(new Dimension(498, 260));
 
         buttonApply = new JButton("Apply");
         buttonApply.addActionListener(this);
@@ -36,13 +38,30 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
         buttonCancel.addActionListener(this);
 
         JPanel buttonPanel = new JPanel();
-        buttonPanel.setLayout(new GridLayout(0, 2));
+        buttonPanel.setLayout(new GridLayout(0, 2, 10, 0));
         buttonPanel.add(buttonApply);
         buttonPanel.add(buttonCancel);
 
-        setLayout(new BorderLayout());
-        add(new JScrollPane(textArea), BorderLayout.CENTER);
-        add(buttonPanel, BorderLayout.SOUTH);
+        String sentStr = annotation.getTbTree().getSentence();
+        String sentLabelStr = "<html>Sentence: <i>" + sentStr + "</i></html>";
+        JLabel sentLabel = new JLabel(sentLabelStr);
+
+        fieldWordsNum = new JTextField(1);
+        fieldWordsNum.setPreferredSize(new Dimension(10, 20));
+        fieldWordPos = new JTextField(1);
+        JPanel addArgsPanel = new JPanel();
+        addArgsPanel.setLayout(new GridLayout(0, 4, 10, 0));
+        addArgsPanel.add(new JLabel("<html>#new words<br/>inserted</html>"));
+        addArgsPanel.add(fieldWordsNum);
+        addArgsPanel.add(new JLabel("<html>#words before<br/>new word</html>"));
+        addArgsPanel.add(fieldWordPos);
+
+        // Set layout and add components
+        setLayout(new FlowLayout());
+        add(new JScrollPane(textArea));
+        add(sentLabel);
+        add(addArgsPanel);
+        add(buttonPanel);
 
         addWindowListener(new WindowAdapter() {
             @Override
@@ -56,17 +75,31 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
     }
 
     @Override
-    public void actionPerformed(ActionEvent e) {
-        if (e.getSource() == buttonCancel)
+    public void actionPerformed(ActionEvent evt) {
+        if (evt.getSource() == buttonCancel)
             dispose();
-        else if (e.getSource() == buttonApply)
+        else if (evt.getSource() == buttonApply) {
+            int newWords = 0, wordPos = 0;
+            // Check if the user added #new words/word position
+            if (!fieldWordPos.getText().isEmpty() || !fieldWordsNum.getText().isEmpty()) {
+                try {
+                    newWords = Integer.parseInt(fieldWordsNum.getText());
+                    wordPos = Integer.parseInt(fieldWordPos.getText());
+                } catch (NumberFormatException e) {
+                    JOptionPane.showMessageDialog(null, "Please type numbers to both fields",
+                            "Error", JOptionPane.ERROR_MESSAGE);
+                    return;
+                }
+            }
             try {
-                annotation.setTree(textArea.getText());
+                if (newWords != 0) annotation.setTree(textArea.getText(), newWords, wordPos);
+                else annotation.setTree(textArea.getText());
                 parent.updateAll();
                 dispose();
             } catch (Exception ex) {
                 JOptionPane.showMessageDialog(null, "There was an error: \n" + ex.getMessage(), "Error",
                         JOptionPane.ERROR_MESSAGE);
             }
+        }
     }
 }
-- 
GitLab