~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to app/src/processing/app/syntax/SyntaxUtilities.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
import javax.swing.text.*;
13
13
import java.awt.*;
 
14
import java.util.regex.Matcher;
 
15
import java.util.regex.Pattern;
14
16
 
15
17
 
16
18
/**
93
95
  {
94
96
    SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
95
97
 
96
 
    styles[Token.COMMENT1] = new SyntaxStyle(Color.black,true,false);
97
 
    styles[Token.COMMENT2] = new SyntaxStyle(new Color(0x990033),true,false);
98
 
    styles[Token.KEYWORD1] = new SyntaxStyle(Color.black,false,true);
99
 
    styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta,false,false);
100
 
    styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0x009600),false,false);
101
 
    styles[Token.LITERAL1] = new SyntaxStyle(new Color(0x650099),false,false);
102
 
    styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true);
103
 
    styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true);
104
 
    styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true);
105
 
    styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true);
 
98
    styles[Token.COMMENT1] = new SyntaxStyle(Color.black,true,false,false);
 
99
    styles[Token.COMMENT2] = new SyntaxStyle(new Color(0x990033),true,false,false);
 
100
    styles[Token.KEYWORD1] = new SyntaxStyle(Color.black,false,true,false);
 
101
    styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta,false,false,false);
 
102
    styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0x009600),false,false,false);
 
103
    styles[Token.LITERAL1] = new SyntaxStyle(new Color(0x650099),false,false,false);
 
104
    styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true,false);
 
105
    styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true,false);
 
106
    styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true,false);
 
107
    styles[Token.URL] = new SyntaxStyle(Color.blue,true,false,false);
 
108
    styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true,false);
106
109
 
107
110
    return styles;
108
111
  }
148
151
          styles[id].setGraphicsFlags(gfx,defaultFont);
149
152
 
150
153
        line.count = length;
151
 
        x = Utilities.drawTabbedText(line,x,y,gfx,expander,0);
 
154
        if (id == Token.COMMENT1 || id == Token.COMMENT2)
 
155
          x = drawTabbedCommentsText(line, x, y, gfx, expander, styles, styles[id]);
 
156
        else
 
157
          x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);        
152
158
        line.offset += length;
153
159
        offset += length;
154
160
 
158
164
    return x;
159
165
  }
160
166
 
 
167
  /**
 
168
   * Parse comments and identify "@schematics <b>&lt;something&gt;</b>" pattern.
 
169
   * 
 
170
   * @param line
 
171
   *          A string to parse
 
172
   * @return <b>null</b> if the pattern is not found, otherwise an array of
 
173
   *         String is returned: the elements with index 0, 1 and 2 are
 
174
   *         respectively the preamble, the <b>&lt;something&gt;</b> stuff, and
 
175
   *         the remaining part of the string.
 
176
   */
 
177
  public static String[] parseCommentUrls(String line) {
 
178
    Matcher m = urlPattern.matcher(line.toString());
 
179
    if (!m.find())
 
180
      return null;
 
181
 
 
182
    String res[] = new String[3];
 
183
    res[0] = line.substring(0, m.start(1));
 
184
    res[1] = line.substring(m.start(1), m.end(1));
 
185
    res[2] = line.substring(m.end(1));
 
186
    // System.out.println("0 =>"+res[0]+"<\n1 =>"+res[1]+"< \n2 =>"+res[2]+"<");
 
187
    return res;
 
188
  }
 
189
 
 
190
  static private Pattern urlPattern = Pattern.compile(
 
191
      "((?:https?|ftp)://" +                // ( Protocol
 
192
      "(?:(?:[\\w_\\-]+:)?[\\w_\\-]+@)?" +  // Username and password
 
193
      "(?:[\\w_\\-]+\\.)+[\\w_\\-]+" +      // Domain name
 
194
      "(?::[0-9]{1,5})?" +                  // Port 
 
195
      "(?:/[\\w_\\-./?%&=+]*)?)" +          // Path ) 
 
196
      "(?:\\s|$)");                         // whitespace or EOL
 
197
 
 
198
  public static Segment stringToSegment(String v) {
 
199
    return new Segment(v.toCharArray(), 0, v.length());
 
200
  }
 
201
 
 
202
  private static int drawTabbedCommentsText(Segment line, int x, int y,
 
203
      Graphics gfx, TabExpander expander, SyntaxStyle[] styles, 
 
204
      SyntaxStyle commentStyle) {
 
205
 
 
206
    String parse[] = parseCommentUrls(line.toString());
 
207
    if (parse == null)
 
208
      // Revert to plain writing.
 
209
      return Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
 
210
    Segment pre = stringToSegment(parse[0]);
 
211
    Segment tag = stringToSegment(parse[1]);
 
212
    Segment post = stringToSegment(parse[2]);
 
213
 
 
214
    if (pre.count>0)
 
215
      x = Utilities.drawTabbedText(pre, x, y, gfx, expander, 0);
 
216
 
 
217
    Font f = gfx.getFont();
 
218
    styles[Token.URL].setGraphicsFlags(gfx, f);
 
219
    x = Utilities.drawTabbedText(tag, x, y, gfx, expander, 0);
 
220
 
 
221
    commentStyle.setGraphicsFlags(gfx, f);
 
222
    if (post.count>0)
 
223
      x = Utilities.drawTabbedText(post, x, y, gfx, expander, 0);
 
224
    return x;
 
225
  }
 
226
 
161
227
  // private members
162
228
  private SyntaxUtilities() {}
163
229
}