001: /*
002: * FormatTable.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: FormatTable.java,v 1.1.1.1 2002/09/24 16:09:14 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:
028: public final class FormatTable {
029: private static final Preferences preferences = Editor.preferences();
030:
031: private String modeName;
032: private ArrayList list;
033: private FormatTableEntry[] array;
034: private boolean initialized;
035:
036: public FormatTable(String modeName) {
037: this .modeName = modeName;
038: list = new ArrayList();
039: }
040:
041: public synchronized final void setModeName(String s) {
042: modeName = s;
043: }
044:
045: public synchronized FormatTableEntry lookup(int format) {
046: if (array != null) {
047: try {
048: return array[format];
049: } catch (ArrayIndexOutOfBoundsException e) {
050: Log.error(e);
051: // Fall through...
052: }
053: }
054: if (!initialized) {
055: initialized = true;
056: boolean ok = true;
057: int largest = -1;
058: for (int i = list.size() - 1; i >= 0; i--) {
059: final FormatTableEntry entry = (FormatTableEntry) list
060: .get(i);
061: final int f = entry.getFormat();
062: if (f < 0) {
063: ok = false;
064: break;
065: }
066: if (f > largest)
067: largest = f;
068: }
069: if (ok && largest < 128) {
070: array = new FormatTableEntry[largest + 1];
071: for (int i = list.size() - 1; i >= 0; i--) {
072: FormatTableEntry entry = (FormatTableEntry) list
073: .get(i);
074: array[entry.getFormat()] = entry;
075: }
076: list = null; // We don't need it any more.
077: try {
078: return array[format];
079: } catch (ArrayIndexOutOfBoundsException e) {
080: Log.error(e);
081: return null;
082: }
083: } else
084: Debug
085: .bug("FormatTableEntry.lookup unable to build array");
086: }
087: if (list != null) {
088: for (int i = list.size() - 1; i >= 0; i--) {
089: FormatTableEntry entry = (FormatTableEntry) list.get(i);
090: if (entry.getFormat() == format)
091: return entry;
092: }
093: }
094: return null;
095: }
096:
097: public synchronized void addEntryFromPrefs(int format, String thing) {
098: addEntryFromPrefs(format, thing, null);
099: }
100:
101: public synchronized void addEntryFromPrefs(int format,
102: String thing, String fallback) {
103: // Color.
104: Color color = null;
105: String key;
106: if (modeName != null) {
107: // "JavaMode.color.comment"
108: key = modeName + ".color." + thing;
109: color = preferences.getColorProperty(key);
110: }
111: if (color == null) {
112: // "color.comment"
113: key = "color." + thing;
114: color = preferences.getColorProperty(key);
115: }
116: // Use fallback if there's no entry for thing.
117: if (color == null && fallback != null) {
118: if (modeName != null) {
119: key = modeName + ".color." + fallback;
120: color = preferences.getColorProperty(key);
121: }
122: if (color == null) {
123: key = "color." + fallback;
124: color = preferences.getColorProperty(key);
125: }
126: }
127: if (color == null) {
128: color = DefaultTheme.getColor(modeName, thing);
129: if (color == null && fallback != null) {
130: color = DefaultTheme.getColor(modeName, fallback);
131: if (color == null)
132: color = DefaultTheme.getColor("text");
133: }
134: }
135:
136: // Style.
137: int style = -1;
138: String value = null;
139: if (modeName != null) {
140: // "JavaMode.style.comment"
141: key = modeName + ".style." + thing;
142: value = preferences.getStringProperty(key);
143: }
144: if (value == null) {
145: // "style.comment"
146: key = "style." + thing;
147: value = preferences.getStringProperty(key);
148: }
149: // Use fallback if there's no entry for thing.
150: if (value == null && fallback != null) {
151: if (modeName != null) {
152: key = modeName + ".style." + fallback;
153: value = preferences.getStringProperty(key);
154: }
155: if (value == null) {
156: key = "style." + fallback;
157: value = preferences.getStringProperty(key);
158: }
159: }
160: if (value != null) {
161: try {
162: style = Integer.parseInt(value);
163: } catch (NumberFormatException e) {
164: }
165: }
166: if (style != Font.PLAIN && style != Font.BOLD
167: && style != Font.ITALIC) {
168: style = DefaultTheme.getStyle(modeName, thing);
169: if (style < 0) {
170: if (fallback != null)
171: style = DefaultTheme.getStyle(modeName, fallback);
172: if (style < 0)
173: style = Font.PLAIN;
174: }
175: }
176: addEntry(format, thing, color, style);
177: }
178:
179: // Only called from synchronized methods.
180: private void addEntry(int format, String name, Color color,
181: int style) {
182: FormatTableEntry entry = new FormatTableEntry(format, color,
183: style);
184: int index = indexOf(format);
185: if (index >= 0)
186: list.set(index, entry);
187: else
188: list.add(entry);
189: }
190:
191: // Only called from synchronized methods.
192: private int indexOf(int format) {
193: for (int i = 0; i < list.size(); i++) {
194: FormatTableEntry entry = (FormatTableEntry) list.get(i);
195: if (entry.getFormat() == format)
196: return i;
197: }
198: return -1;
199: }
200: }
|