KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FormatTable.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: FormatTable.java,v 1.1.1.1 2002/09/24 16:09:14 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
28 public final class FormatTable
29 {
30     private static final Preferences preferences = Editor.preferences();
31
32     private String JavaDoc modeName;
33     private ArrayList JavaDoc list;
34     private FormatTableEntry[] array;
35     private boolean initialized;
36
37     public FormatTable(String JavaDoc modeName)
38     {
39         this.modeName = modeName;
40         list = new ArrayList JavaDoc();
41     }
42
43     public synchronized final void setModeName(String JavaDoc s)
44     {
45         modeName = s;
46     }
47
48     public synchronized FormatTableEntry lookup(int format)
49     {
50         if (array != null) {
51             try {
52                 return array[format];
53             }
54             catch (ArrayIndexOutOfBoundsException JavaDoc e) {
55                 Log.error(e);
56                 // Fall through...
57
}
58         }
59         if (!initialized) {
60             initialized = true;
61             boolean ok = true;
62             int largest = -1;
63             for (int i = list.size()-1; i >= 0; i--) {
64                 final FormatTableEntry entry = (FormatTableEntry) list.get(i);
65                 final int f = entry.getFormat();
66                 if (f < 0) {
67                     ok = false;
68                     break;
69                 }
70                 if (f > largest)
71                     largest = f;
72             }
73             if (ok && largest < 128) {
74                 array = new FormatTableEntry[largest+1];
75                 for (int i = list.size()-1; i >= 0; i--) {
76                     FormatTableEntry entry = (FormatTableEntry) list.get(i);
77                     array[entry.getFormat()] = entry;
78                 }
79                 list = null; // We don't need it any more.
80
try {
81                     return array[format];
82                 }
83                 catch (ArrayIndexOutOfBoundsException JavaDoc e) {
84                     Log.error(e);
85                     return null;
86                 }
87             } else
88                 Debug.bug("FormatTableEntry.lookup unable to build array");
89         }
90         if (list != null) {
91             for (int i = list.size()-1; i >= 0; i--) {
92                 FormatTableEntry entry = (FormatTableEntry) list.get(i);
93                 if (entry.getFormat() == format)
94                     return entry;
95             }
96         }
97         return null;
98     }
99
100     public synchronized void addEntryFromPrefs(int format, String JavaDoc thing)
101     {
102         addEntryFromPrefs(format, thing, null);
103     }
104
105     public synchronized void addEntryFromPrefs(int format, String JavaDoc thing, String JavaDoc fallback)
106     {
107         // Color.
108
Color JavaDoc color = null;
109         String JavaDoc key;
110         if (modeName != null) {
111             // "JavaMode.color.comment"
112
key = modeName + ".color." + thing;
113             color = preferences.getColorProperty(key);
114         }
115         if (color == null) {
116             // "color.comment"
117
key = "color." + thing;
118             color = preferences.getColorProperty(key);
119         }
120         // Use fallback if there's no entry for thing.
121
if (color == null && fallback != null) {
122             if (modeName != null) {
123                 key = modeName + ".color." + fallback;
124                 color = preferences.getColorProperty(key);
125             }
126             if (color == null) {
127                 key = "color." + fallback;
128                 color = preferences.getColorProperty(key);
129             }
130         }
131         if (color == null) {
132             color = DefaultTheme.getColor(modeName, thing);
133             if (color == null && fallback != null) {
134                 color = DefaultTheme.getColor(modeName, fallback);
135                 if (color == null)
136                     color = DefaultTheme.getColor("text");
137             }
138         }
139
140         // Style.
141
int style = -1;
142         String JavaDoc value = null;
143         if (modeName != null) {
144             // "JavaMode.style.comment"
145
key = modeName + ".style." + thing;
146             value = preferences.getStringProperty(key);
147         }
148         if (value == null) {
149             // "style.comment"
150
key = "style." + thing;
151             value = preferences.getStringProperty(key);
152         }
153         // Use fallback if there's no entry for thing.
154
if (value == null && fallback != null) {
155             if (modeName != null) {
156                 key = modeName + ".style." + fallback;
157                 value = preferences.getStringProperty(key);
158             }
159             if (value == null) {
160                 key = "style." + fallback;
161                 value = preferences.getStringProperty(key);
162             }
163         }
164         if (value != null) {
165             try {
166                 style = Integer.parseInt(value);
167             }
168             catch (NumberFormatException JavaDoc e) {}
169         }
170         if (style != Font.PLAIN && style != Font.BOLD && style != Font.ITALIC) {
171             style = DefaultTheme.getStyle(modeName, thing);
172             if (style < 0) {
173                 if (fallback != null)
174                     style = DefaultTheme.getStyle(modeName, fallback);
175                 if (style < 0)
176                     style = Font.PLAIN;
177             }
178         }
179         addEntry(format, thing, color, style);
180     }
181
182     // Only called from synchronized methods.
183
private void addEntry(int format, String JavaDoc name, Color JavaDoc color, int style)
184     {
185         FormatTableEntry entry = new FormatTableEntry(format, color, style);
186         int index = indexOf(format);
187         if (index >= 0)
188             list.set(index, entry);
189         else
190             list.add(entry);
191     }
192
193     // Only called from synchronized methods.
194
private int indexOf(int format)
195     {
196         for (int i = 0; i < list.size(); i++) {
197             FormatTableEntry entry = (FormatTableEntry) list.get(i);
198             if (entry.getFormat() == format)
199                 return i;
200         }
201         return -1;
202     }
203 }
204
Popular Tags