| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2016 Kevin Lamonte |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer.bits; |
| 30 | |
| 31 | import java.io.BufferedReader; |
| 32 | import java.io.FileReader; |
| 33 | import java.io.FileWriter; |
| 34 | import java.io.IOException; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | import java.util.Set; |
| 38 | import java.util.SortedMap; |
| 39 | import java.util.StringTokenizer; |
| 40 | import java.util.TreeMap; |
| 41 | |
| 42 | /** |
| 43 | * ColorTheme is a collection of colors keyed by string. A default theme is |
| 44 | * also provided that matches the blue-and-white theme used by Turbo Vision. |
| 45 | */ |
| 46 | public final class ColorTheme { |
| 47 | |
| 48 | /** |
| 49 | * The current theme colors. |
| 50 | */ |
| 51 | private SortedMap<String, CellAttributes> colors; |
| 52 | |
| 53 | /** |
| 54 | * Public constructor sets the theme to the default. |
| 55 | */ |
| 56 | public ColorTheme() { |
| 57 | colors = new TreeMap<String, CellAttributes>(); |
| 58 | setDefaultTheme(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Retrieve the CellAttributes for a named theme color. |
| 63 | * |
| 64 | * @param name theme color name, e.g. "twindow.border" |
| 65 | * @return color associated with name, e.g. bold yellow on blue |
| 66 | */ |
| 67 | public CellAttributes getColor(final String name) { |
| 68 | CellAttributes attr = (CellAttributes) colors.get(name); |
| 69 | return attr; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Retrieve all the names in the theme. |
| 74 | * |
| 75 | * @return a list of names |
| 76 | */ |
| 77 | public List<String> getColorNames() { |
| 78 | Set<String> keys = colors.keySet(); |
| 79 | List<String> names = new ArrayList<String>(keys.size()); |
| 80 | names.addAll(keys); |
| 81 | return names; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set the color for a named theme color. |
| 86 | * |
| 87 | * @param name theme color name, e.g. "twindow.border" |
| 88 | * @param color the new color to associate with name, e.g. bold yellow on |
| 89 | * blue |
| 90 | */ |
| 91 | public void setColor(final String name, final CellAttributes color) { |
| 92 | colors.put(name, color); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Save the color theme mappings to an ASCII file. |
| 97 | * |
| 98 | * @param filename file to write to |
| 99 | * @throws IOException if the I/O fails |
| 100 | */ |
| 101 | public void save(final String filename) throws IOException { |
| 102 | FileWriter file = new FileWriter(filename); |
| 103 | for (String key: colors.keySet()) { |
| 104 | CellAttributes color = getColor(key); |
| 105 | file.write(String.format("%s = %s\n", key, color)); |
| 106 | } |
| 107 | file.close(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Read color theme mappings from an ASCII file. |
| 112 | * |
| 113 | * @param filename file to read from |
| 114 | * @throws IOException if the I/O fails |
| 115 | */ |
| 116 | public void load(final String filename) throws IOException { |
| 117 | BufferedReader reader = new BufferedReader(new FileReader(filename)); |
| 118 | String line = reader.readLine(); |
| 119 | for (; line != null; line = reader.readLine()) { |
| 120 | String key; |
| 121 | String bold; |
| 122 | String foreColor; |
| 123 | String backColor; |
| 124 | |
| 125 | // Look for lines that resemble: |
| 126 | // "key = blah on blah" |
| 127 | // "key = bold blah on blah" |
| 128 | StringTokenizer tokenizer = new StringTokenizer(line); |
| 129 | key = tokenizer.nextToken(); |
| 130 | if (!tokenizer.nextToken().equals("=")) { |
| 131 | // Skip this line |
| 132 | continue; |
| 133 | } |
| 134 | bold = tokenizer.nextToken(); |
| 135 | if (!bold.toLowerCase().equals("bold")) { |
| 136 | // "key = blah on blah" |
| 137 | foreColor = bold; |
| 138 | } else { |
| 139 | // "key = bold blah on blah" |
| 140 | foreColor = tokenizer.nextToken().toLowerCase(); |
| 141 | } |
| 142 | if (!tokenizer.nextToken().toLowerCase().equals("on")) { |
| 143 | // Skip this line |
| 144 | continue; |
| 145 | } |
| 146 | backColor = tokenizer.nextToken().toLowerCase(); |
| 147 | |
| 148 | CellAttributes color = new CellAttributes(); |
| 149 | if (bold.equals("bold")) { |
| 150 | color.setBold(true); |
| 151 | } |
| 152 | color.setForeColor(Color.getColor(foreColor)); |
| 153 | color.setBackColor(Color.getColor(backColor)); |
| 154 | colors.put(key, color); |
| 155 | } |
| 156 | // All done. |
| 157 | reader.close(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Sets to defaults that resemble the Borland IDE colors. |
| 162 | */ |
| 163 | public void setDefaultTheme() { |
| 164 | CellAttributes color; |
| 165 | |
| 166 | // TWindow border |
| 167 | color = new CellAttributes(); |
| 168 | color.setForeColor(Color.WHITE); |
| 169 | color.setBackColor(Color.BLUE); |
| 170 | color.setBold(true); |
| 171 | colors.put("twindow.border", color); |
| 172 | |
| 173 | // TWindow background |
| 174 | color = new CellAttributes(); |
| 175 | color.setForeColor(Color.YELLOW); |
| 176 | color.setBackColor(Color.BLUE); |
| 177 | color.setBold(true); |
| 178 | colors.put("twindow.background", color); |
| 179 | |
| 180 | // TWindow border - inactive |
| 181 | color = new CellAttributes(); |
| 182 | color.setForeColor(Color.BLACK); |
| 183 | color.setBackColor(Color.BLUE); |
| 184 | color.setBold(true); |
| 185 | colors.put("twindow.border.inactive", color); |
| 186 | |
| 187 | // TWindow background - inactive |
| 188 | color = new CellAttributes(); |
| 189 | color.setForeColor(Color.YELLOW); |
| 190 | color.setBackColor(Color.BLUE); |
| 191 | color.setBold(true); |
| 192 | colors.put("twindow.background.inactive", color); |
| 193 | |
| 194 | // TWindow border - modal |
| 195 | color = new CellAttributes(); |
| 196 | color.setForeColor(Color.WHITE); |
| 197 | color.setBackColor(Color.WHITE); |
| 198 | color.setBold(true); |
| 199 | colors.put("twindow.border.modal", color); |
| 200 | |
| 201 | // TWindow background - modal |
| 202 | color = new CellAttributes(); |
| 203 | color.setForeColor(Color.BLACK); |
| 204 | color.setBackColor(Color.WHITE); |
| 205 | color.setBold(false); |
| 206 | colors.put("twindow.background.modal", color); |
| 207 | |
| 208 | // TWindow border - modal + inactive |
| 209 | color = new CellAttributes(); |
| 210 | color.setForeColor(Color.BLACK); |
| 211 | color.setBackColor(Color.WHITE); |
| 212 | color.setBold(true); |
| 213 | colors.put("twindow.border.modal.inactive", color); |
| 214 | |
| 215 | // TWindow background - modal + inactive |
| 216 | color = new CellAttributes(); |
| 217 | color.setForeColor(Color.BLACK); |
| 218 | color.setBackColor(Color.WHITE); |
| 219 | color.setBold(false); |
| 220 | colors.put("twindow.background.modal.inactive", color); |
| 221 | |
| 222 | // TWindow border - during window movement - modal |
| 223 | color = new CellAttributes(); |
| 224 | color.setForeColor(Color.GREEN); |
| 225 | color.setBackColor(Color.WHITE); |
| 226 | color.setBold(true); |
| 227 | colors.put("twindow.border.modal.windowmove", color); |
| 228 | |
| 229 | // TWindow border - during window movement |
| 230 | color = new CellAttributes(); |
| 231 | color.setForeColor(Color.GREEN); |
| 232 | color.setBackColor(Color.BLUE); |
| 233 | color.setBold(true); |
| 234 | colors.put("twindow.border.windowmove", color); |
| 235 | |
| 236 | // TWindow background - during window movement |
| 237 | color = new CellAttributes(); |
| 238 | color.setForeColor(Color.YELLOW); |
| 239 | color.setBackColor(Color.BLUE); |
| 240 | color.setBold(false); |
| 241 | colors.put("twindow.background.windowmove", color); |
| 242 | |
| 243 | // TApplication background |
| 244 | color = new CellAttributes(); |
| 245 | color.setForeColor(Color.BLUE); |
| 246 | color.setBackColor(Color.WHITE); |
| 247 | color.setBold(false); |
| 248 | colors.put("tapplication.background", color); |
| 249 | |
| 250 | // TButton text |
| 251 | color = new CellAttributes(); |
| 252 | color.setForeColor(Color.BLACK); |
| 253 | color.setBackColor(Color.GREEN); |
| 254 | color.setBold(false); |
| 255 | colors.put("tbutton.inactive", color); |
| 256 | color = new CellAttributes(); |
| 257 | color.setForeColor(Color.CYAN); |
| 258 | color.setBackColor(Color.GREEN); |
| 259 | color.setBold(true); |
| 260 | colors.put("tbutton.active", color); |
| 261 | color = new CellAttributes(); |
| 262 | color.setForeColor(Color.BLACK); |
| 263 | color.setBackColor(Color.WHITE); |
| 264 | color.setBold(true); |
| 265 | colors.put("tbutton.disabled", color); |
| 266 | color = new CellAttributes(); |
| 267 | color.setForeColor(Color.YELLOW); |
| 268 | color.setBackColor(Color.GREEN); |
| 269 | color.setBold(true); |
| 270 | colors.put("tbutton.mnemonic", color); |
| 271 | color = new CellAttributes(); |
| 272 | color.setForeColor(Color.YELLOW); |
| 273 | color.setBackColor(Color.GREEN); |
| 274 | color.setBold(true); |
| 275 | colors.put("tbutton.mnemonic.highlighted", color); |
| 276 | |
| 277 | // TLabel text |
| 278 | color = new CellAttributes(); |
| 279 | color.setForeColor(Color.WHITE); |
| 280 | color.setBackColor(Color.BLUE); |
| 281 | color.setBold(true); |
| 282 | colors.put("tlabel", color); |
| 283 | |
| 284 | // TText text |
| 285 | color = new CellAttributes(); |
| 286 | color.setForeColor(Color.WHITE); |
| 287 | color.setBackColor(Color.BLACK); |
| 288 | color.setBold(false); |
| 289 | colors.put("ttext", color); |
| 290 | |
| 291 | // TField text |
| 292 | color = new CellAttributes(); |
| 293 | color.setForeColor(Color.WHITE); |
| 294 | color.setBackColor(Color.BLUE); |
| 295 | color.setBold(false); |
| 296 | colors.put("tfield.inactive", color); |
| 297 | color = new CellAttributes(); |
| 298 | color.setForeColor(Color.YELLOW); |
| 299 | color.setBackColor(Color.BLACK); |
| 300 | color.setBold(true); |
| 301 | colors.put("tfield.active", color); |
| 302 | |
| 303 | // TCheckbox |
| 304 | color = new CellAttributes(); |
| 305 | color.setForeColor(Color.WHITE); |
| 306 | color.setBackColor(Color.BLUE); |
| 307 | color.setBold(false); |
| 308 | colors.put("tcheckbox.inactive", color); |
| 309 | color = new CellAttributes(); |
| 310 | color.setForeColor(Color.YELLOW); |
| 311 | color.setBackColor(Color.BLACK); |
| 312 | color.setBold(true); |
| 313 | colors.put("tcheckbox.active", color); |
| 314 | |
| 315 | |
| 316 | // TRadioButton |
| 317 | color = new CellAttributes(); |
| 318 | color.setForeColor(Color.WHITE); |
| 319 | color.setBackColor(Color.BLUE); |
| 320 | color.setBold(false); |
| 321 | colors.put("tradiobutton.inactive", color); |
| 322 | color = new CellAttributes(); |
| 323 | color.setForeColor(Color.YELLOW); |
| 324 | color.setBackColor(Color.BLACK); |
| 325 | color.setBold(true); |
| 326 | colors.put("tradiobutton.active", color); |
| 327 | |
| 328 | // TRadioGroup |
| 329 | color = new CellAttributes(); |
| 330 | color.setForeColor(Color.WHITE); |
| 331 | color.setBackColor(Color.BLUE); |
| 332 | color.setBold(false); |
| 333 | colors.put("tradiogroup.inactive", color); |
| 334 | color = new CellAttributes(); |
| 335 | color.setForeColor(Color.YELLOW); |
| 336 | color.setBackColor(Color.BLUE); |
| 337 | color.setBold(true); |
| 338 | colors.put("tradiogroup.active", color); |
| 339 | |
| 340 | // TMenu |
| 341 | color = new CellAttributes(); |
| 342 | color.setForeColor(Color.BLACK); |
| 343 | color.setBackColor(Color.WHITE); |
| 344 | color.setBold(false); |
| 345 | colors.put("tmenu", color); |
| 346 | color = new CellAttributes(); |
| 347 | color.setForeColor(Color.BLACK); |
| 348 | color.setBackColor(Color.GREEN); |
| 349 | color.setBold(false); |
| 350 | colors.put("tmenu.highlighted", color); |
| 351 | color = new CellAttributes(); |
| 352 | color.setForeColor(Color.RED); |
| 353 | color.setBackColor(Color.WHITE); |
| 354 | color.setBold(false); |
| 355 | colors.put("tmenu.mnemonic", color); |
| 356 | color = new CellAttributes(); |
| 357 | color.setForeColor(Color.RED); |
| 358 | color.setBackColor(Color.GREEN); |
| 359 | color.setBold(false); |
| 360 | colors.put("tmenu.mnemonic.highlighted", color); |
| 361 | color = new CellAttributes(); |
| 362 | color.setForeColor(Color.BLACK); |
| 363 | color.setBackColor(Color.WHITE); |
| 364 | color.setBold(true); |
| 365 | colors.put("tmenu.disabled", color); |
| 366 | |
| 367 | // TProgressBar |
| 368 | color = new CellAttributes(); |
| 369 | color.setForeColor(Color.BLUE); |
| 370 | color.setBackColor(Color.BLUE); |
| 371 | color.setBold(true); |
| 372 | colors.put("tprogressbar.complete", color); |
| 373 | color = new CellAttributes(); |
| 374 | color.setForeColor(Color.WHITE); |
| 375 | color.setBackColor(Color.BLUE); |
| 376 | color.setBold(false); |
| 377 | colors.put("tprogressbar.incomplete", color); |
| 378 | |
| 379 | // THScroller / TVScroller |
| 380 | color = new CellAttributes(); |
| 381 | color.setForeColor(Color.CYAN); |
| 382 | color.setBackColor(Color.BLUE); |
| 383 | color.setBold(false); |
| 384 | colors.put("tscroller.bar", color); |
| 385 | color = new CellAttributes(); |
| 386 | color.setForeColor(Color.BLUE); |
| 387 | color.setBackColor(Color.CYAN); |
| 388 | color.setBold(false); |
| 389 | colors.put("tscroller.arrows", color); |
| 390 | |
| 391 | // TTreeView |
| 392 | color = new CellAttributes(); |
| 393 | color.setForeColor(Color.WHITE); |
| 394 | color.setBackColor(Color.BLUE); |
| 395 | color.setBold(false); |
| 396 | colors.put("ttreeview", color); |
| 397 | color = new CellAttributes(); |
| 398 | color.setForeColor(Color.GREEN); |
| 399 | color.setBackColor(Color.BLUE); |
| 400 | color.setBold(true); |
| 401 | colors.put("ttreeview.expandbutton", color); |
| 402 | color = new CellAttributes(); |
| 403 | color.setForeColor(Color.BLACK); |
| 404 | color.setBackColor(Color.CYAN); |
| 405 | color.setBold(false); |
| 406 | colors.put("ttreeview.selected", color); |
| 407 | color = new CellAttributes(); |
| 408 | color.setForeColor(Color.RED); |
| 409 | color.setBackColor(Color.BLUE); |
| 410 | color.setBold(false); |
| 411 | colors.put("ttreeview.unreadable", color); |
| 412 | color = new CellAttributes(); |
| 413 | color.setForeColor(Color.BLACK); |
| 414 | color.setBackColor(Color.BLUE); |
| 415 | color.setBold(true); |
| 416 | colors.put("ttreeview.inactive", color); |
| 417 | |
| 418 | // TList |
| 419 | color = new CellAttributes(); |
| 420 | color.setForeColor(Color.WHITE); |
| 421 | color.setBackColor(Color.BLUE); |
| 422 | color.setBold(false); |
| 423 | colors.put("tlist", color); |
| 424 | color = new CellAttributes(); |
| 425 | color.setForeColor(Color.BLACK); |
| 426 | color.setBackColor(Color.CYAN); |
| 427 | color.setBold(false); |
| 428 | colors.put("tlist.selected", color); |
| 429 | color = new CellAttributes(); |
| 430 | color.setForeColor(Color.BLACK); |
| 431 | color.setBackColor(Color.CYAN); |
| 432 | color.setBold(false); |
| 433 | colors.put("tlist.unreadable", color); |
| 434 | color = new CellAttributes(); |
| 435 | color.setForeColor(Color.BLACK); |
| 436 | color.setBackColor(Color.BLUE); |
| 437 | color.setBold(true); |
| 438 | colors.put("tlist.inactive", color); |
| 439 | |
| 440 | // TEditor |
| 441 | color = new CellAttributes(); |
| 442 | color.setForeColor(Color.WHITE); |
| 443 | color.setBackColor(Color.BLACK); |
| 444 | color.setBold(false); |
| 445 | colors.put("teditor", color); |
| 446 | |
| 447 | } |
| 448 | |
| 449 | } |