KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Formatter


1 /*
2  * Formatter.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: Formatter.java,v 1.2 2004/04/02 03:30:05 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 public abstract class Formatter implements Constants
30 {
31     protected Buffer buffer;
32
33     protected FormatTable formatTable;
34     protected Color JavaDoc colorCaret;
35     protected Color JavaDoc colorBackground;
36     protected Color JavaDoc colorCurrentLineBackground;
37     protected Color JavaDoc colorSelectionBackground;
38     protected Color JavaDoc colorMatchingBracketBackground;
39
40     protected LineSegmentList segmentList = new LineSegmentList();
41
42     public abstract LineSegmentList formatLine(Line line);
43     public abstract FormatTable getFormatTable();
44
45     public boolean parseBuffer()
46     {
47         buffer.setNeedsParsing(false);
48         return false;
49     }
50
51     protected final boolean isKeyword(String JavaDoc s)
52     {
53         return buffer.isKeyword(s);
54     }
55
56     public Color JavaDoc getCaretColor()
57     {
58         if (colorCaret == null) {
59              colorCaret = buffer.getMode().getColorProperty(Property.COLOR_CARET);
60              if (colorCaret == null) {
61                  colorCaret = buffer.getMode().getColorProperty(Property.COLOR_TEXT);
62                  if (colorCaret == null)
63                      colorCaret = DefaultTheme.getColor("caret");
64              }
65         }
66         return colorCaret;
67     }
68
69     public Color JavaDoc getBackgroundColor()
70     {
71         if (colorBackground == null) {
72             colorBackground = buffer.getMode().getColorProperty(Property.COLOR_BACKGROUND);
73             if (colorBackground == null)
74                 colorBackground = DefaultTheme.getColor("background");
75         }
76         return colorBackground;
77     }
78
79     public Color JavaDoc getCurrentLineBackgroundColor()
80     {
81         if (colorCurrentLineBackground == null) {
82             colorCurrentLineBackground = buffer.getMode().getColorProperty(Property.COLOR_CURRENT_LINE_BACKGROUND);
83             if (colorCurrentLineBackground == null)
84                 colorCurrentLineBackground = DefaultTheme.getColor("currentLineBackground");
85         }
86         return colorCurrentLineBackground;
87     }
88
89     public Color JavaDoc getSelectionBackgroundColor()
90     {
91         if (colorSelectionBackground == null) {
92             colorSelectionBackground = buffer.getMode().getColorProperty(Property.COLOR_SELECTION_BACKGROUND);
93             if (colorSelectionBackground == null)
94                 colorSelectionBackground = DefaultTheme.getColor("selectionBackground");
95         }
96         return colorSelectionBackground;
97     }
98
99     public Color JavaDoc getMatchingBracketBackgroundColor()
100     {
101         if (colorMatchingBracketBackground == null) {
102             colorMatchingBracketBackground = buffer.getMode().getColorProperty(Property.COLOR_MATCHING_BRACKET_BACKGROUND);
103             if (colorMatchingBracketBackground == null)
104                 colorMatchingBracketBackground = DefaultTheme.getColor("matchingBracketBackground");
105         }
106         return colorMatchingBracketBackground;
107     }
108
109     public Color JavaDoc getColor(int format)
110     {
111         FormatTableEntry entry = getFormatTable().lookup(format);
112         if (entry != null)
113             return entry.getColor();
114         return DefaultTheme.getColor("text");
115     }
116
117     public int getStyle(int format)
118     {
119         FormatTableEntry entry = getFormatTable().lookup(format);
120         if (entry != null)
121             return entry.getStyle();
122         return Font.PLAIN;
123     }
124
125     public FormatTableEntry getFormatTableEntry(int format)
126     {
127         return getFormatTable().lookup(format);
128     }
129
130     public boolean getUnderline(int format)
131     {
132         return false;
133     }
134
135     public void reset()
136     {
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 JavaDoc text, int begin, int end, int format)
146     {
147         segmentList.addSegment(new LineSegment(text, begin, end, format));
148     }
149
150     protected final void addSegment(String JavaDoc text, int begin, int format)
151     {
152         segmentList.addSegment(new LineSegment(text, begin, text.length(), format));
153     }
154
155     protected final void addSegment(String JavaDoc text, int format)
156     {
157         segmentList.addSegment(new LineSegment(text, format));
158     }
159
160     protected final LineSegment getLastSegment()
161     {
162         return segmentList.getLastSegment();
163     }
164
165     protected final void clearSegmentList()
166     {
167         segmentList.clear();
168     }
169
170     protected final String JavaDoc getDetabbedText(Line line)
171     {
172         if (Editor.tabsAreVisible())
173             return Utilities.makeTabsVisible(line.getText(), buffer.getTabWidth());
174         return Utilities.detab(line.getText(), buffer.getTabWidth());
175     }
176 }
177
Popular Tags