~ubuntu-branches/ubuntu/utopic/sikuli/utopic

« back to all changes in this revision

Viewing changes to sikuli-ide/src/main/java/org/sikuli/ide/SikuliDocument.java

  • Committer: Bazaar Package Importer
  • Author(s): Gilles Filippini
  • Date: 2011-01-24 01:07:30 UTC
  • Revision ID: james.westby@ubuntu.com-20110124010730-lzj19enqag94g1ye
Tags: upstream-1.0~x~rc1-dfsg1
ImportĀ upstreamĀ versionĀ 1.0~x~rc1-dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.sikuli.ide;
 
2
 
 
3
import javax.swing.text.*;
 
4
import java.util.*;
 
5
import java.awt.*;
 
6
import javax.swing.text.AbstractDocument;
 
7
 
 
8
public class SikuliDocument extends DefaultStyledDocument{
 
9
   private String word = "";
 
10
   private SimpleAttributeSet keyword = new SimpleAttributeSet();
 
11
   private SimpleAttributeSet string = new SimpleAttributeSet();
 
12
   private SimpleAttributeSet normal = new SimpleAttributeSet();
 
13
   private SimpleAttributeSet number = new SimpleAttributeSet();
 
14
   private SimpleAttributeSet comments = new SimpleAttributeSet();
 
15
   private int currentPos = 0;
 
16
   private Vector keywords = new Vector();
 
17
   public static int STRING_MODE = 10;
 
18
   public static int TEXT_MODE = 11;
 
19
   public static int NUMBER_MODE = 12;
 
20
   public static int COMMENT_MODE = 13;
 
21
   private int mode = TEXT_MODE;
 
22
 
 
23
    private static String[] arrKeywords = {
 
24
       "and",       "del",       "for",       "is",        "raise",    
 
25
       "assert",    "elif",      "from",      "lambda",    "return",   
 
26
       "break",     "else",      "global",    "not",       "try",      
 
27
       "class",     "except",    "if",        "or",        "while",    
 
28
       "continue",  "exec",      "import",    "pass",      "yield",    
 
29
       "def",       "finally",   "in",        "print" };
 
30
 
 
31
   private void initKeywords(){
 
32
      for(int i=0;i<arrKeywords.length;i++)
 
33
         keywords.add(arrKeywords[i]);
 
34
   }
 
35
   
 
36
   public SikuliDocument() {
 
37
      initKeywords();
 
38
      //set the bold attribute
 
39
      StyleConstants.setBold(keyword, true);
 
40
      StyleConstants.setForeground(string, Color.magenta);
 
41
      StyleConstants.setForeground(number, Color.orange);
 
42
      StyleConstants.setForeground(comments, Color.blue);
 
43
      StyleConstants.setForeground(keyword,Color.red);
 
44
      StyleConstants.setItalic(comments, true);
 
45
   }
 
46
 
 
47
 
 
48
   private void insertKeyword(String str, int pos){
 
49
      try{
 
50
         //remove the old word and formatting
 
51
         this.remove(pos - str.length(), str.length());
 
52
         /*replace it with the same word, but new formatting
 
53
          *we MUST call the super class insertString method here, otherwise we
 
54
          *would end up in an infinite loop !!!!!*/
 
55
         super.insertString(pos - str.length(), str, keyword);
 
56
      }
 
57
      catch (Exception ex){
 
58
         ex.printStackTrace();
 
59
      }
 
60
   }
 
61
 
 
62
 
 
63
   private void insertTextString(String str, int pos){
 
64
      try{
 
65
         //remove the old word and formatting
 
66
         this.remove(pos,str.length());
 
67
         super.insertString(pos, str, string);
 
68
      }
 
69
      catch (Exception ex){
 
70
         ex.printStackTrace();
 
71
      }
 
72
   }
 
73
 
 
74
 
 
75
   private void insertNumberString(String str, int pos){
 
76
      try{
 
77
         //remove the old word and formatting
 
78
         this.remove(pos,str.length());
 
79
         super.insertString(pos, str, number);
 
80
      }
 
81
      catch (Exception ex){
 
82
         ex.printStackTrace();
 
83
      }
 
84
   }
 
85
 
 
86
 
 
87
   private void insertCommentString(String str, int pos){
 
88
      try{
 
89
         //remove the old word and formatting
 
90
         this.remove(pos,str.length());
 
91
         super.insertString(pos, str, comments);
 
92
      }
 
93
      catch (Exception ex){
 
94
         ex.printStackTrace();
 
95
      }
 
96
   }
 
97
 
 
98
 
 
99
   private void checkForString(){
 
100
      int offs = this.currentPos;
 
101
      Element element = this.getParagraphElement(offs);
 
102
      String elementText = "";
 
103
      try{
 
104
         //this gets our chuck of current text for the element we're on
 
105
         elementText = this.getText(element.getStartOffset(),
 
106
               element.getEndOffset() -
 
107
               element.getStartOffset());
 
108
      }
 
109
      catch(Exception ex){
 
110
         //whoops!
 
111
         System.out.println("no text");
 
112
      }
 
113
      int strLen = elementText.length();
 
114
      if (strLen == 0) {return;}
 
115
      int i = 0;
 
116
 
 
117
 
 
118
      if (element.getStartOffset() > 0){
 
119
         //translates backward if neccessary
 
120
         offs = offs - element.getStartOffset();
 
121
      }
 
122
      int quoteCount = 0;
 
123
      if ((offs >= 0) && (offs <= strLen-1)){
 
124
         i = offs;
 
125
         while (i >0){
 
126
            //the while loop walks back until we hit a delimiter
 
127
 
 
128
 
 
129
            char charAt = elementText.charAt(i);
 
130
            if ((charAt == '"')){
 
131
               quoteCount ++;
 
132
            }
 
133
            i--;
 
134
         }
 
135
         int rem = quoteCount % 2;
 
136
         //System.out.println(rem);
 
137
         mode = (rem == 0) ? TEXT_MODE: STRING_MODE;
 
138
      }
 
139
   }
 
140
 
 
141
 
 
142
   private void checkForKeyword(){
 
143
      if (mode != TEXT_MODE) {
 
144
         return;
 
145
      }
 
146
      int offs = this.currentPos;
 
147
      Element element = this.getParagraphElement(offs);
 
148
      String elementText = "";
 
149
      try{
 
150
         //this gets our chuck of current text for the element we're on
 
151
         elementText = this.getText(element.getStartOffset(),
 
152
               element.getEndOffset() - element.getStartOffset());
 
153
      }
 
154
      catch(Exception ex){
 
155
         //whoops!
 
156
         System.out.println("no text");
 
157
      }
 
158
      int strLen = elementText.length();
 
159
      if (strLen == 0) {return;}
 
160
      int i = 0;
 
161
 
 
162
 
 
163
      if (element.getStartOffset() > 0){
 
164
         //translates backward if neccessary
 
165
         offs = offs - element.getStartOffset();
 
166
      }
 
167
      if ((offs >= 0) && (offs <= strLen-1)){
 
168
         i = offs;
 
169
         while (i >0){
 
170
            //the while loop walks back until we hit a delimiter
 
171
            i--;
 
172
            char charAt = elementText.charAt(i);
 
173
            if ((charAt == ' ') | (i == 0) | (charAt =='(') | (charAt ==')') |
 
174
                  (charAt == '{') | (charAt == '}')){ //if i == 0 then we're atthe begininng
 
175
               if(i != 0){
 
176
                  i++;
 
177
               }
 
178
               word = elementText.substring(i, offs);//skip the period
 
179
 
 
180
 
 
181
               String s = word.trim().toLowerCase();
 
182
               //this is what actually checks for a matching keyword
 
183
               if (keywords.contains(s)){
 
184
                  insertKeyword(word, currentPos);
 
185
               }
 
186
               break;
 
187
                  }
 
188
         }
 
189
      }
 
190
   }
 
191
 
 
192
 
 
193
   private void checkForNumber(){
 
194
      int offs = this.currentPos;
 
195
      Element element = this.getParagraphElement(offs);
 
196
      String elementText = "";
 
197
      try{
 
198
         //this gets our chuck of current text for the element we're on
 
199
         elementText = this.getText(element.getStartOffset(),
 
200
               element.getEndOffset() - element.getStartOffset());
 
201
      }
 
202
      catch(Exception ex){
 
203
         //whoops!
 
204
         System.out.println("no text");
 
205
      }
 
206
      int strLen = elementText.length();
 
207
      if (strLen == 0) {return;}
 
208
      int i = 0;
 
209
 
 
210
 
 
211
      if (element.getStartOffset() > 0){
 
212
         //translates backward if neccessary
 
213
         offs = offs - element.getStartOffset();
 
214
      }
 
215
      mode = TEXT_MODE;
 
216
      if ((offs >= 0) && (offs <= strLen-1)){
 
217
         i = offs;
 
218
         while (i >0){
 
219
            //the while loop walks back until we hit a delimiter
 
220
            char charAt = elementText.charAt(i);
 
221
            if ((charAt == ' ') | (i == 0) | (charAt =='(') | (charAt ==')') |
 
222
                  (charAt == '{') | (charAt == '}') /*|*/){ //if i == 0 then we're at the begininng
 
223
               if(i != 0){
 
224
                  i++;
 
225
               }
 
226
               mode = NUMBER_MODE;
 
227
               break;
 
228
                  }
 
229
            else if (!(charAt >= '0' & charAt <= '9' | charAt=='.'
 
230
                     | charAt=='+' | charAt=='-'
 
231
                     | charAt=='/' | charAt=='*'| charAt=='%' | charAt=='=')){
 
232
               mode = TEXT_MODE;
 
233
               break;
 
234
                     }
 
235
            i--;
 
236
         }
 
237
      }
 
238
   }
 
239
 
 
240
 
 
241
   private void checkForComment(){
 
242
      int offs = this.currentPos;
 
243
      Element element = this.getParagraphElement(offs);
 
244
      String elementText = "";
 
245
      try{
 
246
         //this gets our chuck of current text for the element we're on
 
247
         elementText = this.getText(element.getStartOffset(),
 
248
               element.getEndOffset() - element.getStartOffset());
 
249
      }
 
250
      catch(Exception ex){
 
251
         //whoops!
 
252
         System.out.println("no text");
 
253
      }
 
254
      int strLen = elementText.length();
 
255
      if (strLen == 0) {return;}
 
256
      int i = 0;
 
257
 
 
258
 
 
259
      if (element.getStartOffset() > 0){
 
260
         //translates backward if neccessary
 
261
         offs = offs - element.getStartOffset();
 
262
      }
 
263
      if ((offs >= 1) && (offs <= strLen-1)){
 
264
         i = offs;
 
265
         char commentStartChar1 = elementText.charAt(i-1);
 
266
         char commentStartChar2 = elementText.charAt(i);
 
267
         if ((commentStartChar1 == '/' && commentStartChar2 == '*')){
 
268
            mode = COMMENT_MODE;
 
269
            this.insertCommentString("/*", currentPos-1);
 
270
         }
 
271
         else if (commentStartChar1 == '*' && commentStartChar2 == '/'){
 
272
            mode = TEXT_MODE;
 
273
            this.insertCommentString("*/", currentPos-1);
 
274
         }
 
275
      }
 
276
   }
 
277
 
 
278
 
 
279
   private void processChar(String str){
 
280
      char strChar = str.charAt(0);
 
281
      if (mode != this.COMMENT_MODE){
 
282
         mode = TEXT_MODE;
 
283
      }
 
284
      switch (strChar){
 
285
         case ('{'):case ('}'):case (' '): case('\n'):
 
286
         case ('('):case (')'):case (';'):case ('.'):{
 
287
                                                        checkForKeyword();
 
288
                                                        if (mode == STRING_MODE && strChar == '\n'){
 
289
                                                           mode = TEXT_MODE;
 
290
                                                        }
 
291
         }
 
292
         break;
 
293
         case ('"'):{
 
294
                       insertTextString(str, currentPos);
 
295
                       this.checkForString();
 
296
         }
 
297
         break;
 
298
         case ('0'):case ('1'):case ('2'):case ('3'):case ('4'):
 
299
         case ('5'):case ('6'):case ('7'):case ('8'):case ('9'):{
 
300
                                                                   checkForNumber();
 
301
         }
 
302
         break;
 
303
         case ('*'):case ('/'):{
 
304
                                  checkForComment();
 
305
         }
 
306
         break;
 
307
      }
 
308
      if (mode == this.TEXT_MODE){
 
309
         this.checkForString();
 
310
      }
 
311
      if (mode == this.STRING_MODE){
 
312
         insertTextString(str, this.currentPos);
 
313
      }
 
314
      else if (mode == this.NUMBER_MODE){
 
315
         insertNumberString(str, this.currentPos);
 
316
      }
 
317
      else if (mode == this.COMMENT_MODE){
 
318
         insertCommentString(str, this.currentPos);
 
319
      }
 
320
 
 
321
 
 
322
   }
 
323
 
 
324
 
 
325
   private void processChar(char strChar){
 
326
      char[] chrstr = new char[1];
 
327
      chrstr[0] = strChar;
 
328
      String str = new String(chrstr);
 
329
      processChar(str);
 
330
   }
 
331
 
 
332
 
 
333
   public void insertString(int offs,
 
334
         String str,
 
335
         AttributeSet a) throws BadLocationException{
 
336
      super.insertString(offs, str, normal);
 
337
 
 
338
      System.out.println("insertString");
 
339
 
 
340
      int strLen = str.length();
 
341
      int endpos = offs + strLen;
 
342
      int strpos;
 
343
      for (int i=offs;i<endpos;i++){
 
344
         currentPos = i;
 
345
         strpos = i - offs;
 
346
         processChar(str.charAt(strpos));
 
347
      }
 
348
      currentPos = offs;
 
349
   }
 
350
 
 
351
 
 
352
   public Vector getKeywords(){
 
353
      return this.keywords;
 
354
   }
 
355
 
 
356
 
 
357
   public void setKeywords(Vector aKeywordList){
 
358
      if (aKeywordList != null){
 
359
         this.keywords = aKeywordList;
 
360
      }
 
361
   }
 
362