1 21 22 package org.armedbear.j; 23 24 import java.awt.Color ; 25 import java.awt.Font ; 26 import java.util.ArrayList ; 27 28 public final class FormatTable 29 { 30 private static final Preferences preferences = Editor.preferences(); 31 32 private String modeName; 33 private ArrayList list; 34 private FormatTableEntry[] array; 35 private boolean initialized; 36 37 public FormatTable(String modeName) 38 { 39 this.modeName = modeName; 40 list = new ArrayList (); 41 } 42 43 public synchronized final void setModeName(String 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 e) { 55 Log.error(e); 56 } 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; try { 81 return array[format]; 82 } 83 catch (ArrayIndexOutOfBoundsException 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 thing) 101 { 102 addEntryFromPrefs(format, thing, null); 103 } 104 105 public synchronized void addEntryFromPrefs(int format, String thing, String fallback) 106 { 107 Color color = null; 109 String key; 110 if (modeName != null) { 111 key = modeName + ".color." + thing; 113 color = preferences.getColorProperty(key); 114 } 115 if (color == null) { 116 key = "color." + thing; 118 color = preferences.getColorProperty(key); 119 } 120 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 int style = -1; 142 String value = null; 143 if (modeName != null) { 144 key = modeName + ".style." + thing; 146 value = preferences.getStringProperty(key); 147 } 148 if (value == null) { 149 key = "style." + thing; 151 value = preferences.getStringProperty(key); 152 } 153 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 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 private void addEntry(int format, String name, Color 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 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 |