Commit 779d5055 authored by Christos Christodoulopoulos's avatar Christos Christodoulopoulos
Browse files

Added bracket highlighting in tree edit

Native MacOS look and feel
parent f23b853b
......@@ -355,6 +355,8 @@ public class JBToolkit extends JFrame implements ActionListener, ItemListener, L
tf_annotator.setBackground(Color.RED.brighter());
else if (annotator.equals("babySRL"))
tf_annotator.setBackground(Color.GREEN);
else if (annotator.equals("gold"))
tf_annotator.setBackground(Color.YELLOW);
else tf_annotator.setBackground(Color.LIGHT_GRAY);
// Update the context window
......
package jubilee.toolkit;
import jubilee.datastructure.JBDataStructure;
import jubilee.util.StringManager;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
......@@ -30,6 +36,7 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
textArea = new JTextArea(annotation.getTbTree().toTextTree());
textArea.setMargin(new Insets(10, 10, 10, 10));
textArea.addCaretListener(new CaretListenerLabel(textArea));
buttonApply = new JButton("Apply");
buttonApply.addActionListener(this);
......@@ -161,4 +168,46 @@ public class JBTreeEditPanel extends JFrame implements ActionListener {
}
if (openBrackets != closeBrackets) throw new Exception("Brackets mismatch");
}
protected class CaretListenerLabel extends JLabel implements CaretListener {
private final Highlighter highlighter;
private final JTextArea textArea;
private final DefaultHighlighter.DefaultHighlightPainter p;
public CaretListenerLabel(JTextArea textArea) {
this.textArea = textArea;
highlighter = textArea.getHighlighter();
p = new DefaultHighlighter.DefaultHighlightPainter(Color.LIGHT_GRAY);
}
public void caretUpdate(CaretEvent e) {
highlighter.removeAllHighlights();
highlightMatchingBracket(e.getDot());
}
protected void highlightMatchingBracket(final int dot) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String text = textArea.getText();
if (dot > 0 && (text.charAt(dot-1) == '(' || text.charAt(dot-1) == ')'))
highlight(dot-1);
}
});
}
private void highlight(int dot) {
String text = textArea.getText();
int matchingBracketInd = StringManager.findMatchingBracket(text, dot);
try {
if (matchingBracketInd == -1)
highlighter.addHighlight(dot, dot + 1, new DefaultHighlighter.DefaultHighlightPainter(Color.RED));
else {
highlighter.addHighlight(matchingBracketInd, matchingBracketInd + 1, p);
highlighter.addHighlight(dot, dot + 1, p);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
}
......@@ -33,37 +33,32 @@ import javax.swing.UnsupportedLookAndFeelException;
* @since 5/11/2014
*/
public class Jubilee {
public static final String VERSION = "3.24";
static public void main(String[] args) {
try {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {e.printStackTrace();}
catch (ClassNotFoundException e) {e.printStackTrace();}
catch (InstantiationException e) {e.printStackTrace();}
catch (IllegalAccessException e) {e.printStackTrace();}
String usage = "Usage: java -jar jubilee.jar -u <userId> [-m <max-annotations=2> -s <system-folder=system>]";
public static final String VERSION = "3.25";
static public void main(String[] args) {
// Migrate the menu to the Mac OSX menu bar
if (System.getProperty("os.name").toLowerCase().contains("mac"))
System.setProperty("apple.laf.useScreenMenuBar", "true");
String usage = "Usage: java -jar jubilee.jar -u <userId> [-m <max-annotations=2> -s <system-folder=system>]";
String title = "Jubilee " + VERSION;
String userId = null;
int maxAnn = 2;
String sysDir = "system" + File.separator;
if (args.length == 0 || args.length % 2 != 0) {System.err.println(usage); return;}
for (int i=0; i<args.length; i+=2) {
String option = args[i];
String value = args[i+1];
if (option.equals("-u")) userId = value;
else if (option.equals("-m")) maxAnn = Integer.parseInt(value);
else if (option.equals("-s")) sysDir = value + File.separator;
else {System.err.println(usage); return;}
}
if (userId == null) {System.err.println(usage); return;}
new JBToolkit(title, sysDir, userId, maxAnn);
}
}
......@@ -24,7 +24,6 @@
package jubilee.util;
import java.io.*;
import java.util.*;
/**
* @author Jinho D. Choi
......@@ -41,21 +40,13 @@ public class StringManager
return build.toString();
}
static public String[] toArray(ArrayList<String> arr)
{
String str[] = new String[arr.size()];
arr.toArray(str);
return str;
}
static public boolean isInteger(String str)
static public boolean isInteger(String str)
{
try
{
Integer.parseInt(str);
}
Integer.parseInt(str)
}
catch (NumberFormatException e){return false;}
return true;
......@@ -69,8 +60,92 @@ public class StringManager
{
utf = new String(str.getBytes(), "UTF-8");
}
catch (UnsupportedEncodingException e) {System.err.println(e);}
catch (UnsupportedEncodingException e) {e.printStackTrace();}
return utf;
}
public static int findMatchingBracket(String doc, int offset) {
if (doc.length() == 0)
return -1;
char c = doc.charAt(offset);
char cprime; // c` - corresponding character
boolean direction; // true = back, false = forward
switch(c) {
case '(' :
cprime = ')';
direction = false;
break;
case ')' :
cprime = '(';
direction = true;
break;
default :
return -1;
}
int count;
// Go back or forward
if (direction) {
// Count is 1 initially because we have already `found' one closing bracket
count = 1;
// Get text[0,offset-1];
String text = doc.substring(0, offset);
// Scan backwards
for (int i = offset - 1; i >= 0; i--) {
// If text[i] == c, we have found another
// closing bracket, therefore we will need
// two opening brackets to complete the
// match.
char x = text.charAt(i);
if (x == c)
count++;
// If text[i] == cprime, we have found a
// opening bracket, so we return i if
// --count == 0
else if (x == cprime) {
if (--count == 0)
return i;
}
}
}
else {
// Count is 1 initially because we have already `found' one opening bracket
count = 1;
// So we don't have to + 1 in every loop
offset++;
// Get text[offset+1,len];
String text = doc.substring(offset, doc.length());
// Scan forwards
for (int i = 0; i < text.length(); i++) {
// If text[i] == c, we have found another
// opening bracket, therefore we will need
// two closing brackets to complete the
// match.
char x = text.charAt(i);
if (x == c)
count++;
// If text[i] == cprime, we have found an
// closing bracket, so we return i if
// --count == 0
else if (x == cprime) {
if (--count == 0)
return i + offset;
}
}
}
// Nothing found
return -1;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment