View Javadoc

1   /*
2    * SyntaxUtilities.java - Utility functions used by syntax colorizing
3    * Copyright (C) 1999 Slava Pestov
4    *
5    * You may use and modify this package for any purpose. Redistribution is
6    * permitted, in both source and binary form, provided that this notice
7    * remains intact in all source distributions of this package.
8    */
9   package com.ochafik.swing.syntaxcoloring;
10  
11  import java.awt.Color;
12  import java.awt.Font;
13  import java.awt.Graphics;
14  
15  import javax.swing.text.Segment;
16  import javax.swing.text.TabExpander;
17  import javax.swing.text.Utilities;
18  
19  /**
20   * Class with several utility functions used by jEdit's syntax colorizing
21   * subsystem.
22   *
23   * @author Slava Pestov
24   * @version $Id: SyntaxUtilities.java,v 1.9 1999/12/13 03:40:30 sp Exp $
25   */
26  public class SyntaxUtilities
27  {
28  	/**
29  	 * Checks if a subregion of a <code>Segment</code> is equal to a
30  	 * string.
31  	 * @param ignoreCase True if case should be ignored, false otherwise
32  	 * @param text The segment
33  	 * @param offset The offset into the segment
34  	 * @param match The string to match
35  	 */
36  	public static boolean regionMatches(boolean ignoreCase, Segment text,
37  					    int offset, String match)
38  	{
39  		int length = offset + match.length();
40  		char[] textArray = text.array;
41  		if(length > text.offset + text.count)
42  			return false;
43  		for(int i = offset, j = 0; i < length; i++, j++)
44  		{
45  			char c1 = textArray[i];
46  			char c2 = match.charAt(j);
47  			if(ignoreCase)
48  			{
49  				c1 = Character.toUpperCase(c1);
50  				c2 = Character.toUpperCase(c2);
51  			}
52  			if(c1 != c2)
53  				return false;
54  		}
55  		return true;
56  	}
57  	
58  	/**
59  	 * Checks if a subregion of a <code>Segment</code> is equal to a
60  	 * character array.
61  	 * @param ignoreCase True if case should be ignored, false otherwise
62  	 * @param text The segment
63  	 * @param offset The offset into the segment
64  	 * @param match The character array to match
65  	 */
66  	public static boolean regionMatches(boolean ignoreCase, Segment text,
67  					    int offset, char[] match)
68  	{
69  		int length = offset + match.length;
70  		char[] textArray = text.array;
71  		if(length > text.offset + text.count)
72  			return false;
73  		for(int i = offset, j = 0; i < length; i++, j++)
74  		{
75  			char c1 = textArray[i];
76  			char c2 = match[j];
77  			if(ignoreCase)
78  			{
79  				c1 = Character.toUpperCase(c1);
80  				c2 = Character.toUpperCase(c2);
81  			}
82  			if(c1 != c2)
83  				return false;
84  		}
85  		return true;
86  	}
87  
88  	/**
89  	 * Returns the default style table. This can be passed to the
90  	 * <code>setStyles()</code> method of <code>SyntaxDocument</code>
91  	 * to use the default syntax styles.
92  	 */
93  	public static SyntaxStyle[] getDefaultSyntaxStyles()
94  	{
95  		SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
96  
97  		styles[Token.COMMENT1] = new SyntaxStyle(Color.green,true,false);//Color.black,true,false);
98  		styles[Token.COMMENT2] = new SyntaxStyle(Color.blue,true,false);//new Color(0x990033),true,false);
99  		styles[Token.KEYWORD1] = new SyntaxStyle(Color.black,false,true);
100 		styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta,false,false);
101 		styles[Token.KEYWORD3] = new SyntaxStyle(new Color(0x009600),false,false);
102 		styles[Token.LITERAL1] = new SyntaxStyle(new Color(0x650099),false,false);
103 		styles[Token.LITERAL2] = new SyntaxStyle(Color.blue,false,false);//new Color(0x650099),false,true);
104 		//styles[Token.LITERAL3] = new SyntaxStyle(new Color(0x650099),false,false);
105 		styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true);
106 		styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true);
107 		styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true);
108 
109 		return styles;
110 	}
111 
112 	/**
113 	 * Paints the specified line onto the graphics context. Note that this
114 	 * method munges the offset and count values of the segment.
115 	 * @param line The line segment
116 	 * @param tokens The token list for the line
117 	 * @param styles The syntax style list
118 	 * @param expander The tab expander used to determine tab stops. May
119 	 * be null
120 	 * @param gfx The graphics context
121 	 * @param x The x co-ordinate
122 	 * @param y The y co-ordinate
123 	 * @return The x co-ordinate, plus the width of the painted string
124 	 */
125 	public static int paintSyntaxLine(Segment line, Token tokens,
126 		SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
127 		int x, int y)
128 	{
129 		Font defaultFont = gfx.getFont();
130 		Color defaultColor = gfx.getColor();
131 
132 		int offset = 0;
133 		for(;;)
134 		{
135 			byte id = tokens.id;
136 			if(id == Token.END)
137 				break;
138 
139 			int length = tokens.length;
140 			if(id == Token.NULL)
141 			{
142 				if(!defaultColor.equals(gfx.getColor()))
143 					gfx.setColor(defaultColor);
144 				if(!defaultFont.equals(gfx.getFont()))
145 					gfx.setFont(defaultFont);
146 			}
147 			else
148 				styles[id].setGraphicsFlags(gfx,defaultFont);
149 
150 			line.count = length;
151 			x = Utilities.drawTabbedText(line,x,y,gfx,expander,0);
152 			line.offset += length;
153 			offset += length;
154 
155 			tokens = tokens.next;
156 		}
157 
158 		return x;
159 	}
160 
161 	// private members
162 	private SyntaxUtilities() {}
163 }