Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jlib/EditArea.java


1   package jlib;
2   import java.awt.*;
3   
4   public class EditArea extends TextArea {
5   
6       public static final int inExpr=0;
7       public static final int topLevel=1;
8       public static final int tooManyParens=2;
9       public static final int inQuote=3;
10      public static final int inComment=4;
11  
12  
13      public static boolean j11enabled,netscapebug;
14      private int state;
15      public static boolean matchParens=false;
16      public static boolean computeCurrentExpr=false;
17      public String currentExpr="";
18      public silk.Procedure matchHandler;
19  
20      public EditArea(int r, int c) {
21    super(r,c);
22          try {(java.awt.TextArea.class).getMethod("getCaretPosition",new Class[]{}); j11enabled=true;}
23          catch(Exception e) {j11enabled=false;}
24          netscapeBugTest();
25      }
26  
27      public boolean netscapeBugTest(){
28    // test for Netscape bug on Internet Explorer
29          netscapebug = 
30        (   ("45.3".equals(
31                    System.getProperty("java.class.version")))
32               && ("Windows NT".equals(
33                    System.getProperty("os.name")))
34               && ("Netscape Communications Corporation".equals(
35                    System.getProperty("java.vendor")))
36      );
37          return netscapebug;
38      }
39  
40      public boolean keyUp(Event e, int k) {
41        int pos,pos2=0;
42              if (j11enabled)    pos=this.getCaretPosition();
43              else               pos = this.getSelectionEnd();
44        char[] chars = this.getText().toCharArray();
45  
46        int i;
47        int parencount; 
48  
49              if ((!computeCurrentExpr) && (!matchParens)) return super.keyUp(e,k);
50  
51        // first we go through the area removing all comments and quotes
52              int j=0;
53              while(j<pos) {
54                  if (netscapebug && (chars[j]=='\n')) pos--;
55  
56      if (chars[j]==';') { pos2=j;
57          do {
58        if ((chars[j]=='(')||(chars[j]==')')) chars[j]='|'; 
59                          if (netscapebug && (chars[j]=='\n')) pos--;
60                          j++; }
61                      while ((j<pos)&&(chars[j]!='\n'));
62                      if(j==pos) {
63                      currentExpr = (this.getText()).substring(pos2,pos);
64        if (matchHandler!= null) matchHandler.apply(silk.U.list(
65                              this,currentExpr,new Integer(inComment)));
66        return super.keyUp(e,k);}
67      }
68                  else if (chars[j]=='"') { pos2=j;
69          boolean val;
70          do {
71        if ((chars[j]=='(')||(chars[j]==')')) chars[j]='|'; 
72                          if ((chars[j]=='\\')&&(j<pos)&&(chars[j+1]=='"'))  chars[j+1]='_';
73                          if ((chars[j]=='\\')&&(j<pos)&&(chars[j+1]=='\\'))  chars[j+1]='_';
74                          if (netscapebug && (chars[j]=='\n')) pos--;
75                          j++;
76          }
77          while ((j<pos)&&(chars[j]!='"'));
78                      if(j==pos) {
79                      currentExpr = (this.getText()).substring(pos2,pos);
80        if (matchHandler!= null) matchHandler.apply(silk.U.list(
81                              this,currentExpr,new Integer(inQuote)));
82        return super.keyUp(e,k);}
83                      else j++;
84      }
85                  else  j++;
86        }
87  
88  
89  
90    // find first paren of current expression
91          int parenbias = (pos>0)?((chars[pos-1]==')')?1:0):0;
92          i=pos-1;
93          parencount=0;
94          if (i>=0) do {
95        if (chars[i]==')') parencount++;
96        else if (chars[i]=='(') parencount--;
97        i--;}
98          while ((i>=0) && (parencount>=parenbias));
99  
100   currentExpr = (this.getText()).substring(i+1,pos);
101   if (matchParens && (k==41)) flashMatch(i,pos,parencount);
102 
103         if ((i== -1) && (parencount > 0)) state = tooManyParens;
104         else state = inExpr;
105   if (matchHandler!= null) 
106       matchHandler.apply(silk.U.list(this,currentExpr,new Integer(state)));
107 
108   return super.keyUp(e,k);
109     }
110 
111     private void flashMatch(int i, int pos, int parencount) {
112     if ((i == -1) && (parencount > 0)) {
113         for (int n=1; n<5; n++) {
114       if (j11enabled) {
115           this.setSelectionStart(i+1); 
116           this.setSelectionEnd(pos);
117       }else  this.select(i+1,pos);
118       try {Thread.sleep(100);} catch (Exception ee) {;}
119       if (j11enabled)  this.setSelectionStart(pos);
120       else             this.select(pos,pos);
121       try {Thread.sleep(100);} catch (Exception ee) {;}}
122     } else {
123     if (matchParens) {
124         if (j11enabled) {
125                        this.setSelectionStart(i+1); 
126                        this.setSelectionEnd(pos);
127                     }else  this.select(i+1,pos);
128                    try {Thread.sleep(250);} catch (Exception ee) {;}
129         if (j11enabled)  this.setSelectionStart(pos);
130                     else             this.select(pos,pos);}
131     }
132 
133     }
134 
135 }
136