001: // Copyright (c) 2000, 2005 BlueJ Group, Deakin University
002: //
003: // This software is made available under the terms of the "MIT License"
004: // A copy of this license is included with this source distribution
005: // in "license.txt" and is also available at:
006: // http://www.opensource.org/licenses/mit-license.html
007: // Any queries should be directed to Michael Kolling mik@bluej.org
008:
009: package bluej.editor.moe;
010:
011: import javax.swing.text.*;
012:
013: import java.awt.Color;
014: import bluej.Config;
015:
016: import org.syntax.jedit.*;
017: import org.syntax.jedit.tokenmarker.*;
018:
019: /**
020: * A simple implementation of <code>SyntaxDocument</code> that
021: * inherits from SyntaxDocument. It takes
022: * care of inserting and deleting lines from the token marker's state.
023: * It adds the ability to handle paragraph attributes on a per line basis.
024: *
025: * @author Bruce Quig
026: * @author Jo Wood (Modified to allow user-defined colours, March 2001)
027: *
028: */
029: public class MoeSyntaxDocument extends SyntaxDocument {
030: public static final String OUTPUT = "output";
031: public static final String ERROR = "error";
032:
033: private static Color[] colors = null;
034:
035: private static Color defaultColour = null;
036: private static Color backgroundColour = null;
037:
038: public MoeSyntaxDocument() {
039: super (getUserColors());
040: // defaults to 4 if cannot read property
041: int tabSize = Config.getPropInteger("bluej.editor.tabsize", 4);
042: putProperty(tabSizeAttribute, new Integer(tabSize));
043: }
044:
045: /**
046: * Sets attributes for a paragraph. This method was added to
047: * provide the ability to replicate DefaultStyledDocument's ability to
048: * set each lines attributes easily.
049: * This is an added method for the BlueJ adaption of jedit's Syntax
050: * package
051: *
052: * @param offset the offset into the paragraph >= 0
053: * @param length the number of characters affected >= 0
054: * @param s the attributes
055: * @param replace whether to replace existing attributes, or merge them
056: */
057: public void setParagraphAttributes(int offset, AttributeSet s) {
058: // modified version of method from DefaultStyleDocument
059: try {
060: writeLock();
061:
062: Element paragraph = getParagraphElement(offset);
063: MutableAttributeSet attr = (MutableAttributeSet) paragraph
064: .getAttributes();
065: attr.addAttributes(s);
066: } finally {
067: writeUnlock();
068: }
069: }
070:
071: /**
072: * Get the default colour for MoeSyntaxDocuments.
073: */
074: public static Color getDefaultColor() {
075: return defaultColour;
076: }
077:
078: /**
079: * Get the background colour for MoeSyntaxDocuments.
080: */
081: public static Color getBackgroundColor() {
082: return backgroundColour;
083: }
084:
085: /**
086: * Allows user-defined colours to be set for synax highlighting. The file
087: * containing the colour values is 'lib/moe.defs'. If this file is
088: * not found, or not all colours are defined, the BlueJ default colours are
089: * used.
090: *
091: * @author This method was added by Jo Wood (jwo@soi.city.ac.uk), 9th March,
092: * 2001.
093: */
094: private static Color[] getUserColors() {
095: if (colors == null) {
096: // Replace with user-defined colours.
097: int colorInt;
098:
099: // First determine default colour and background colour
100: colorInt = getPropHexInt("other", 0x000000);
101: defaultColour = new Color(colorInt);
102:
103: colorInt = getPropHexInt("background", 0x000000);
104: backgroundColour = new Color(colorInt);
105:
106: // Build colour table.
107: colors = new Color[Token.ID_COUNT];
108:
109: // Comments.
110: colorInt = getPropHexInt("comment", 0x1a1a80);
111: colors[Token.COMMENT1] = new Color(colorInt);
112:
113: // Javadoc comments.
114: colorInt = getPropHexInt("javadoc", 0x1a1a80);
115: colors[Token.COMMENT2] = new Color(colorInt);
116:
117: // Stand-out comments (/*#).
118: colorInt = getPropHexInt("stand-out", 0xee00bb);
119: colors[Token.COMMENT3] = new Color(colorInt);
120:
121: // Java keywords.
122: colorInt = getPropHexInt("keyword1", 0x660033);
123: colors[Token.KEYWORD1] = new Color(colorInt);
124:
125: // Class-based keywords.
126: colorInt = getPropHexInt("keyword2", 0xcc8033);
127: colors[Token.KEYWORD2] = new Color(colorInt);
128:
129: // Other Java keywords (true, false, this, super).
130: colorInt = getPropHexInt("keyword3", 0x006699);
131: colors[Token.KEYWORD3] = new Color(colorInt);
132:
133: // Primitives.
134: colorInt = getPropHexInt("primitive", 0xcc0000);
135: colors[Token.PRIMITIVE] = new Color(colorInt);
136:
137: // String literals.
138: colorInt = getPropHexInt("string", 0x339933);
139: colors[Token.LITERAL1] = new Color(colorInt);
140:
141: // Labels
142: colorInt = getPropHexInt("label", 0x999999);
143: colors[Token.LABEL] = new Color(colorInt);
144:
145: // Invalid (eg unclosed string literal)
146: colorInt = getPropHexInt("invalid", 0xff3300);
147: colors[Token.INVALID] = new Color(colorInt);
148:
149: // Operator is not produced by token marker
150: colors[Token.OPERATOR] = new Color(0xcc9900);
151: }
152: return colors;
153: }
154:
155: /**
156: * Get an integer value from a property whose value is hex-encoded.
157: * @param propName The name of the property
158: * @param def The default value if the property is undefined or
159: * not parseable as a hexadecimal
160: * @return The value
161: */
162: private static int getPropHexInt(String propName, int def) {
163: String strVal = Config.getPropString(propName, null,
164: Config.moeUserProps);
165: try {
166: return Integer.parseInt(strVal, 16);
167: } catch (NumberFormatException nfe) {
168: return def;
169: }
170: }
171: }
|