KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jedit > syntax > SyntaxUtilities


1 package org.jedit.syntax;
2
3 /*
4  * SyntaxUtilities.java - Utility functions used by syntax colorizing
5  * Copyright (C) 1999 Slava Pestov
6  *
7  * You may use and modify this package for any purpose. Redistribution is
8  * permitted, in both source and binary form, provided that this notice
9  * remains intact in all source distributions of this package.
10  */

11
12 import javax.swing.text.*;
13 import java.awt.*;
14
15 /**
16  * Class with several utility functions used by jEdit's syntax colorizing
17  * subsystem.
18  *
19  * @author Slava Pestov
20  * @version $Id: SyntaxUtilities.java,v 1.1 2003/12/14 16:29:49 daggerrz Exp $
21  */

22 public class SyntaxUtilities
23 {
24    /**
25     * Checks if a subregion of a <code>Segment</code> is equal to a
26     * string.
27     * @param ignoreCase True if case should be ignored, false otherwise
28     * @param text The segment
29     * @param offset The offset into the segment
30     * @param match The string to match
31     */

32    public static boolean regionMatches(boolean ignoreCase, Segment text,
33                    int offset, String JavaDoc match)
34    {
35       int length = offset + match.length();
36       char[] textArray = text.array;
37       if(length > text.offset + text.count)
38          return false;
39       for(int i = offset, j = 0; i < length; i++, j++)
40       {
41          char c1 = textArray[i];
42          char c2 = match.charAt(j);
43          if(ignoreCase)
44          {
45             c1 = Character.toUpperCase(c1);
46             c2 = Character.toUpperCase(c2);
47          }
48          if(c1 != c2)
49             return false;
50       }
51       return true;
52    }
53    
54    /**
55     * Checks if a subregion of a <code>Segment</code> is equal to a
56     * character array.
57     * @param ignoreCase True if case should be ignored, false otherwise
58     * @param text The segment
59     * @param offset The offset into the segment
60     * @param match The character array to match
61     */

62    public static boolean regionMatches(boolean ignoreCase, Segment text,
63                    int offset, char[] match)
64    {
65       int length = offset + match.length;
66       char[] textArray = text.array;
67       if(length > text.offset + text.count)
68          return false;
69       for(int i = offset, j = 0; i < length; i++, j++)
70       {
71          char c1 = textArray[i];
72          char c2 = match[j];
73          if(ignoreCase)
74          {
75             c1 = Character.toUpperCase(c1);
76             c2 = Character.toUpperCase(c2);
77          }
78          if(c1 != c2)
79             return false;
80       }
81       return true;
82    }
83
84    /**
85     * Returns the default style table. This can be passed to the
86     * <code>setStyles()</code> method of <code>SyntaxDocument</code>
87     * to use the default syntax styles.
88     */

89    public static SyntaxStyle[] getDefaultSyntaxStyles()
90    {
91       SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
92
93       styles[Token.COMMENT1] = new SyntaxStyle(Color.black,true,false);
94       styles[Token.COMMENT2] = new SyntaxStyle(new Color(0x990033),true,false);
95       styles[Token.KEYWORD1] = new SyntaxStyle(Color.black,false,true);
96       styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta,false,false);
97       styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0x009600),false,false);
98       styles[Token.LITERAL1] = new SyntaxStyle(new Color(0x650099),false,false);
99       styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true);
100       styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true);
101       styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true);
102       styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true);
103
104       return styles;
105    }
106
107    /**
108     * Paints the specified line onto the graphics context. Note that this
109     * method munges the offset and count values of the segment.
110     * @param line The line segment
111     * @param tokens The token list for the line
112     * @param styles The syntax style list
113     * @param expander The tab expander used to determine tab stops. May
114     * be null
115     * @param gfx The graphics context
116     * @param x The x co-ordinate
117     * @param y The y co-ordinate
118     * @return The x co-ordinate, plus the width of the painted string
119     */

120    public static int paintSyntaxLine(Segment line, Token tokens,
121       SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
122       int x, int y)
123    {
124       Font defaultFont = gfx.getFont();
125       Color defaultColor = gfx.getColor();
126
127       int offset = 0;
128       for(;;)
129       {
130          byte id = tokens.id;
131          if(id == Token.END)
132             break;
133
134          int length = tokens.length;
135          if(id == Token.NULL)
136          {
137             if(!defaultColor.equals(gfx.getColor()))
138                gfx.setColor(defaultColor);
139             if(!defaultFont.equals(gfx.getFont()))
140                gfx.setFont(defaultFont);
141          }
142          else
143             styles[id].setGraphicsFlags(gfx,defaultFont);
144
145          line.count = length;
146          x = Utilities.drawTabbedText(line,x,y,gfx,expander,0);
147          line.offset += length;
148          offset += length;
149
150          tokens = tokens.next;
151       }
152
153       return x;
154    }
155
156    // private members
157
private SyntaxUtilities() {}
158 }
159
Popular Tags