1
package org.sikuli.ide;
3
import javax.swing.text.*;
6
import javax.swing.text.AbstractDocument;
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;
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" };
31
private void initKeywords(){
32
for(int i=0;i<arrKeywords.length;i++)
33
keywords.add(arrKeywords[i]);
36
public SikuliDocument() {
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);
48
private void insertKeyword(String str, int pos){
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);
63
private void insertTextString(String str, int pos){
65
//remove the old word and formatting
66
this.remove(pos,str.length());
67
super.insertString(pos, str, string);
75
private void insertNumberString(String str, int pos){
77
//remove the old word and formatting
78
this.remove(pos,str.length());
79
super.insertString(pos, str, number);
87
private void insertCommentString(String str, int pos){
89
//remove the old word and formatting
90
this.remove(pos,str.length());
91
super.insertString(pos, str, comments);
99
private void checkForString(){
100
int offs = this.currentPos;
101
Element element = this.getParagraphElement(offs);
102
String elementText = "";
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());
111
System.out.println("no text");
113
int strLen = elementText.length();
114
if (strLen == 0) {return;}
118
if (element.getStartOffset() > 0){
119
//translates backward if neccessary
120
offs = offs - element.getStartOffset();
123
if ((offs >= 0) && (offs <= strLen-1)){
126
//the while loop walks back until we hit a delimiter
129
char charAt = elementText.charAt(i);
130
if ((charAt == '"')){
135
int rem = quoteCount % 2;
136
//System.out.println(rem);
137
mode = (rem == 0) ? TEXT_MODE: STRING_MODE;
142
private void checkForKeyword(){
143
if (mode != TEXT_MODE) {
146
int offs = this.currentPos;
147
Element element = this.getParagraphElement(offs);
148
String elementText = "";
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());
156
System.out.println("no text");
158
int strLen = elementText.length();
159
if (strLen == 0) {return;}
163
if (element.getStartOffset() > 0){
164
//translates backward if neccessary
165
offs = offs - element.getStartOffset();
167
if ((offs >= 0) && (offs <= strLen-1)){
170
//the while loop walks back until we hit a delimiter
172
char charAt = elementText.charAt(i);
173
if ((charAt == ' ') | (i == 0) | (charAt =='(') | (charAt ==')') |
174
(charAt == '{') | (charAt == '}')){ //if i == 0 then we're atthe begininng
178
word = elementText.substring(i, offs);//skip the period
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);
193
private void checkForNumber(){
194
int offs = this.currentPos;
195
Element element = this.getParagraphElement(offs);
196
String elementText = "";
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());
204
System.out.println("no text");
206
int strLen = elementText.length();
207
if (strLen == 0) {return;}
211
if (element.getStartOffset() > 0){
212
//translates backward if neccessary
213
offs = offs - element.getStartOffset();
216
if ((offs >= 0) && (offs <= strLen-1)){
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
229
else if (!(charAt >= '0' & charAt <= '9' | charAt=='.'
230
| charAt=='+' | charAt=='-'
231
| charAt=='/' | charAt=='*'| charAt=='%' | charAt=='=')){
241
private void checkForComment(){
242
int offs = this.currentPos;
243
Element element = this.getParagraphElement(offs);
244
String elementText = "";
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());
252
System.out.println("no text");
254
int strLen = elementText.length();
255
if (strLen == 0) {return;}
259
if (element.getStartOffset() > 0){
260
//translates backward if neccessary
261
offs = offs - element.getStartOffset();
263
if ((offs >= 1) && (offs <= strLen-1)){
265
char commentStartChar1 = elementText.charAt(i-1);
266
char commentStartChar2 = elementText.charAt(i);
267
if ((commentStartChar1 == '/' && commentStartChar2 == '*')){
269
this.insertCommentString("/*", currentPos-1);
271
else if (commentStartChar1 == '*' && commentStartChar2 == '/'){
273
this.insertCommentString("*/", currentPos-1);
279
private void processChar(String str){
280
char strChar = str.charAt(0);
281
if (mode != this.COMMENT_MODE){
285
case ('{'):case ('}'):case (' '): case('\n'):
286
case ('('):case (')'):case (';'):case ('.'):{
288
if (mode == STRING_MODE && strChar == '\n'){
294
insertTextString(str, currentPos);
295
this.checkForString();
298
case ('0'):case ('1'):case ('2'):case ('3'):case ('4'):
299
case ('5'):case ('6'):case ('7'):case ('8'):case ('9'):{
303
case ('*'):case ('/'):{
308
if (mode == this.TEXT_MODE){
309
this.checkForString();
311
if (mode == this.STRING_MODE){
312
insertTextString(str, this.currentPos);
314
else if (mode == this.NUMBER_MODE){
315
insertNumberString(str, this.currentPos);
317
else if (mode == this.COMMENT_MODE){
318
insertCommentString(str, this.currentPos);
325
private void processChar(char strChar){
326
char[] chrstr = new char[1];
328
String str = new String(chrstr);
333
public void insertString(int offs,
335
AttributeSet a) throws BadLocationException{
336
super.insertString(offs, str, normal);
338
System.out.println("insertString");
340
int strLen = str.length();
341
int endpos = offs + strLen;
343
for (int i=offs;i<endpos;i++){
346
processChar(str.charAt(strpos));
352
public Vector getKeywords(){
353
return this.keywords;
357
public void setKeywords(Vector aKeywordList){
358
if (aKeywordList != null){
359
this.keywords = aKeywordList;