001: package jlib;
002:
003: import java.awt.*;
004:
005: public class EditArea extends TextArea {
006:
007: public static final int inExpr = 0;
008: public static final int topLevel = 1;
009: public static final int tooManyParens = 2;
010: public static final int inQuote = 3;
011: public static final int inComment = 4;
012:
013: public static boolean j11enabled, netscapebug;
014: private int state;
015: public static boolean matchParens = false;
016: public static boolean computeCurrentExpr = false;
017: public String currentExpr = "";
018: public jsint.Procedure matchHandler;
019:
020: public EditArea(int r, int c) {
021: super (r, c);
022: try {
023: (java.awt.TextArea.class).getMethod("getCaretPosition",
024: new Class[] {});
025: j11enabled = true;
026: } catch (Exception e) {
027: j11enabled = false;
028: }
029: netscapeBugTest();
030: }
031:
032: public boolean netscapeBugTest() {
033: // test for Netscape bug on Internet Explorer
034: netscapebug = (("45.3".equals(System
035: .getProperty("java.class.version")))
036: && ("Windows NT".equals(System.getProperty("os.name"))) && ("Netscape Communications Corporation"
037: .equals(System.getProperty("java.vendor"))));
038: return netscapebug;
039: }
040:
041: public boolean keyUp(Event e, int k) {
042: int pos, pos2 = 0;
043: if (j11enabled)
044: pos = this .getCaretPosition();
045: else
046: pos = this .getSelectionEnd();
047: char[] chars = this .getText().toCharArray();
048:
049: int i;
050: int parencount;
051:
052: if ((!computeCurrentExpr) && (!matchParens))
053: return super .keyUp(e, k);
054:
055: // first we go through the area removing all comments and quotes
056: int j = 0;
057: while (j < pos) {
058: if (netscapebug && (chars[j] == '\n'))
059: pos--;
060:
061: if (chars[j] == ';') {
062: pos2 = j;
063: do {
064: if ((chars[j] == '(') || (chars[j] == ')'))
065: chars[j] = '|';
066: if (netscapebug && (chars[j] == '\n'))
067: pos--;
068: j++;
069: } while ((j < pos) && (chars[j] != '\n'));
070: if (j == pos) {
071: currentExpr = (this .getText()).substring(pos2, pos);
072: if (matchHandler != null)
073: matchHandler.apply(jsint.U.list(this ,
074: currentExpr, new Integer(inComment)));
075: return super .keyUp(e, k);
076: }
077: } else if (chars[j] == '"') {
078: pos2 = j;
079: boolean val;
080: do {
081: if ((chars[j] == '(') || (chars[j] == ')'))
082: chars[j] = '|';
083: if ((chars[j] == '\\') && (j < pos)
084: && (chars[j + 1] == '"'))
085: chars[j + 1] = '_';
086: if ((chars[j] == '\\') && (j < pos)
087: && (chars[j + 1] == '\\'))
088: chars[j + 1] = '_';
089: if (netscapebug && (chars[j] == '\n'))
090: pos--;
091: j++;
092: } while ((j < pos) && (chars[j] != '"'));
093: if (j == pos) {
094: currentExpr = (this .getText()).substring(pos2, pos);
095: if (matchHandler != null)
096: matchHandler.apply(jsint.U.list(this ,
097: currentExpr, new Integer(inQuote)));
098: return super .keyUp(e, k);
099: } else
100: j++;
101: } else
102: j++;
103: }
104:
105: // find first paren of current expression
106: int parenbias = (pos > 0) ? ((chars[pos - 1] == ')') ? 1 : 0)
107: : 0;
108: i = pos - 1;
109: parencount = 0;
110: if (i >= 0)
111: do {
112: if (chars[i] == ')')
113: parencount++;
114: else if (chars[i] == '(')
115: parencount--;
116: i--;
117: } while ((i >= 0) && (parencount >= parenbias));
118:
119: currentExpr = (this .getText()).substring(i + 1, pos);
120: if (matchParens && (k == 41))
121: flashMatch(i, pos, parencount);
122:
123: if ((i == -1) && (parencount > 0))
124: state = tooManyParens;
125: else
126: state = inExpr;
127: if (matchHandler != null)
128: matchHandler.apply(jsint.U.list(this , currentExpr,
129: new Integer(state)));
130:
131: return super .keyUp(e, k);
132: }
133:
134: private void flashMatch(int i, int pos, int parencount) {
135: if ((i == -1) && (parencount > 0)) {
136: for (int n = 1; n < 5; n++) {
137: if (j11enabled) {
138: this .setSelectionStart(i + 1);
139: this .setSelectionEnd(pos);
140: } else
141: this .select(i + 1, pos);
142: try {
143: Thread.sleep(100);
144: } catch (Exception ee) {
145: ;
146: }
147: if (j11enabled)
148: this .setSelectionStart(pos);
149: else
150: this .select(pos, pos);
151: try {
152: Thread.sleep(100);
153: } catch (Exception ee) {
154: ;
155: }
156: }
157: } else {
158: if (matchParens) {
159: if (j11enabled) {
160: this .setSelectionStart(i + 1);
161: this .setSelectionEnd(pos);
162: } else
163: this .select(i + 1, pos);
164: try {
165: Thread.sleep(250);
166: } catch (Exception ee) {
167: ;
168: }
169: if (j11enabled)
170: this.setSelectionStart(pos);
171: else
172: this.select(pos, pos);
173: }
174: }
175:
176: }
177:
178: }
|