001: /*
002: * Formatter.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves
005: * $Id: Formatter.java,v 1.2 2004/04/02 03:30:05 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Color;
025: import java.awt.Font;
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: public abstract class Formatter implements Constants {
030: protected Buffer buffer;
031:
032: protected FormatTable formatTable;
033: protected Color colorCaret;
034: protected Color colorBackground;
035: protected Color colorCurrentLineBackground;
036: protected Color colorSelectionBackground;
037: protected Color colorMatchingBracketBackground;
038:
039: protected LineSegmentList segmentList = new LineSegmentList();
040:
041: public abstract LineSegmentList formatLine(Line line);
042:
043: public abstract FormatTable getFormatTable();
044:
045: public boolean parseBuffer() {
046: buffer.setNeedsParsing(false);
047: return false;
048: }
049:
050: protected final boolean isKeyword(String s) {
051: return buffer.isKeyword(s);
052: }
053:
054: public Color getCaretColor() {
055: if (colorCaret == null) {
056: colorCaret = buffer.getMode().getColorProperty(
057: Property.COLOR_CARET);
058: if (colorCaret == null) {
059: colorCaret = buffer.getMode().getColorProperty(
060: Property.COLOR_TEXT);
061: if (colorCaret == null)
062: colorCaret = DefaultTheme.getColor("caret");
063: }
064: }
065: return colorCaret;
066: }
067:
068: public Color getBackgroundColor() {
069: if (colorBackground == null) {
070: colorBackground = buffer.getMode().getColorProperty(
071: Property.COLOR_BACKGROUND);
072: if (colorBackground == null)
073: colorBackground = DefaultTheme.getColor("background");
074: }
075: return colorBackground;
076: }
077:
078: public Color getCurrentLineBackgroundColor() {
079: if (colorCurrentLineBackground == null) {
080: colorCurrentLineBackground = buffer.getMode()
081: .getColorProperty(
082: Property.COLOR_CURRENT_LINE_BACKGROUND);
083: if (colorCurrentLineBackground == null)
084: colorCurrentLineBackground = DefaultTheme
085: .getColor("currentLineBackground");
086: }
087: return colorCurrentLineBackground;
088: }
089:
090: public Color getSelectionBackgroundColor() {
091: if (colorSelectionBackground == null) {
092: colorSelectionBackground = buffer.getMode()
093: .getColorProperty(
094: Property.COLOR_SELECTION_BACKGROUND);
095: if (colorSelectionBackground == null)
096: colorSelectionBackground = DefaultTheme
097: .getColor("selectionBackground");
098: }
099: return colorSelectionBackground;
100: }
101:
102: public Color getMatchingBracketBackgroundColor() {
103: if (colorMatchingBracketBackground == null) {
104: colorMatchingBracketBackground = buffer.getMode()
105: .getColorProperty(
106: Property.COLOR_MATCHING_BRACKET_BACKGROUND);
107: if (colorMatchingBracketBackground == null)
108: colorMatchingBracketBackground = DefaultTheme
109: .getColor("matchingBracketBackground");
110: }
111: return colorMatchingBracketBackground;
112: }
113:
114: public Color getColor(int format) {
115: FormatTableEntry entry = getFormatTable().lookup(format);
116: if (entry != null)
117: return entry.getColor();
118: return DefaultTheme.getColor("text");
119: }
120:
121: public int getStyle(int format) {
122: FormatTableEntry entry = getFormatTable().lookup(format);
123: if (entry != null)
124: return entry.getStyle();
125: return Font.PLAIN;
126: }
127:
128: public FormatTableEntry getFormatTableEntry(int format) {
129: return getFormatTable().lookup(format);
130: }
131:
132: public boolean getUnderline(int format) {
133: return false;
134: }
135:
136: public void reset() {
137: colorCaret = null;
138: colorBackground = null;
139: colorCurrentLineBackground = null;
140: colorSelectionBackground = null;
141: colorMatchingBracketBackground = null;
142: formatTable = null;
143: }
144:
145: protected final void addSegment(String text, int begin, int end,
146: int format) {
147: segmentList
148: .addSegment(new LineSegment(text, begin, end, format));
149: }
150:
151: protected final void addSegment(String text, int begin, int format) {
152: segmentList.addSegment(new LineSegment(text, begin, text
153: .length(), format));
154: }
155:
156: protected final void addSegment(String text, int format) {
157: segmentList.addSegment(new LineSegment(text, format));
158: }
159:
160: protected final LineSegment getLastSegment() {
161: return segmentList.getLastSegment();
162: }
163:
164: protected final void clearSegmentList() {
165: segmentList.clear();
166: }
167:
168: protected final String getDetabbedText(Line line) {
169: if (Editor.tabsAreVisible())
170: return Utilities.makeTabsVisible(line.getText(), buffer
171: .getTabWidth());
172: return Utilities.detab(line.getText(), buffer.getTabWidth());
173: }
174: }
|