| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 6 | * This module is licensed under the GNU Lesser General Public License |
| 7 | * Version 3. Please see the file "COPYING" in this directory for more |
| 8 | * information about the GNU Lesser General Public License Version 3. |
| 9 | * |
| 10 | * Copyright (C) 2015 Kevin Lamonte |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public License |
| 14 | * as published by the Free Software Foundation; either version 3 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, but |
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this program; if not, see |
| 24 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 26 | * 02110-1301 USA |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer.bits; |
| 32 | |
| 33 | import java.io.BufferedReader; |
| 34 | import java.io.FileReader; |
| 35 | import java.io.FileWriter; |
| 36 | import java.io.IOException; |
| 37 | import java.util.SortedMap; |
| 38 | import java.util.StringTokenizer; |
| 39 | import java.util.TreeMap; |
| 40 | |
| 41 | /** |
| 42 | * ColorTheme is a collection of colors keyed by string. A default theme is |
| 43 | * also provided that matches the blue-and-white theme used by Turbo Vision. |
| 44 | */ |
| 45 | public final class ColorTheme { |
| 46 | |
| 47 | /** |
| 48 | * The current theme colors. |
| 49 | */ |
| 50 | private SortedMap<String, CellAttributes> colors; |
| 51 | |
| 52 | /** |
| 53 | * Public constructor sets the theme to the default. |
| 54 | */ |
| 55 | public ColorTheme() { |
| 56 | colors = new TreeMap<String, CellAttributes>(); |
| 57 | setDefaultTheme(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Retrieve the CellAttributes for a named theme color. |
| 62 | * |
| 63 | * @param name theme color name, e.g. "twindow.border" |
| 64 | * @return color associated with name, e.g. bold yellow on blue |
| 65 | */ |
| 66 | public CellAttributes getColor(final String name) { |
| 67 | CellAttributes attr = (CellAttributes) colors.get(name); |
| 68 | return attr; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Save the color theme mappings to an ASCII file. |
| 73 | * |
| 74 | * @param filename file to write to |
| 75 | * @throws IOException if the I/O fails |
| 76 | */ |
| 77 | public void save(final String filename) throws IOException { |
| 78 | FileWriter file = new FileWriter(filename); |
| 79 | for (String key: colors.keySet()) { |
| 80 | CellAttributes color = getColor(key); |
| 81 | file.write(String.format("%s = %s\n", key, color)); |
| 82 | } |
| 83 | file.close(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Read color theme mappings from an ASCII file. |
| 88 | * |
| 89 | * @param filename file to read from |
| 90 | * @throws IOException if the I/O fails |
| 91 | */ |
| 92 | public void load(final String filename) throws IOException { |
| 93 | BufferedReader reader = new BufferedReader(new FileReader(filename)); |
| 94 | String line = reader.readLine(); |
| 95 | for (; line != null; line = reader.readLine()) { |
| 96 | String key; |
| 97 | String bold; |
| 98 | String foreColor; |
| 99 | String backColor; |
| 100 | |
| 101 | // Look for lines that resemble: |
| 102 | // "key = blah on blah" |
| 103 | // "key = bold blah on blah" |
| 104 | StringTokenizer tokenizer = new StringTokenizer(line); |
| 105 | key = tokenizer.nextToken(); |
| 106 | if (!tokenizer.nextToken().equals("=")) { |
| 107 | // Skip this line |
| 108 | continue; |
| 109 | } |
| 110 | bold = tokenizer.nextToken(); |
| 111 | if (!bold.toLowerCase().equals("bold")) { |
| 112 | // "key = blah on blah" |
| 113 | foreColor = bold; |
| 114 | } else { |
| 115 | // "key = bold blah on blah" |
| 116 | foreColor = tokenizer.nextToken().toLowerCase(); |
| 117 | } |
| 118 | if (!tokenizer.nextToken().toLowerCase().equals("on")) { |
| 119 | // Skip this line |
| 120 | continue; |
| 121 | } |
| 122 | backColor = tokenizer.nextToken().toLowerCase(); |
| 123 | |
| 124 | CellAttributes color = new CellAttributes(); |
| 125 | if (bold.equals("bold")) { |
| 126 | color.setBold(true); |
| 127 | } |
| 128 | color.setForeColor(Color.getColor(foreColor)); |
| 129 | color.setBackColor(Color.getColor(backColor)); |
| 130 | colors.put(key, color); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Sets to defaults that resemble the Borland IDE colors. |
| 136 | */ |
| 137 | public void setDefaultTheme() { |
| 138 | CellAttributes color; |
| 139 | |
| 140 | // TWindow border |
| 141 | color = new CellAttributes(); |
| 142 | color.setForeColor(Color.WHITE); |
| 143 | color.setBackColor(Color.BLUE); |
| 144 | color.setBold(true); |
| 145 | colors.put("twindow.border", color); |
| 146 | |
| 147 | // TWindow background |
| 148 | color = new CellAttributes(); |
| 149 | color.setForeColor(Color.YELLOW); |
| 150 | color.setBackColor(Color.BLUE); |
| 151 | color.setBold(true); |
| 152 | colors.put("twindow.background", color); |
| 153 | |
| 154 | // TWindow border - inactive |
| 155 | color = new CellAttributes(); |
| 156 | color.setForeColor(Color.BLACK); |
| 157 | color.setBackColor(Color.BLUE); |
| 158 | color.setBold(true); |
| 159 | colors.put("twindow.border.inactive", color); |
| 160 | |
| 161 | // TWindow background - inactive |
| 162 | color = new CellAttributes(); |
| 163 | color.setForeColor(Color.YELLOW); |
| 164 | color.setBackColor(Color.BLUE); |
| 165 | color.setBold(true); |
| 166 | colors.put("twindow.background.inactive", color); |
| 167 | |
| 168 | // TWindow border - modal |
| 169 | color = new CellAttributes(); |
| 170 | color.setForeColor(Color.WHITE); |
| 171 | color.setBackColor(Color.WHITE); |
| 172 | color.setBold(true); |
| 173 | colors.put("twindow.border.modal", color); |
| 174 | |
| 175 | // TWindow background - modal |
| 176 | color = new CellAttributes(); |
| 177 | color.setForeColor(Color.BLACK); |
| 178 | color.setBackColor(Color.WHITE); |
| 179 | color.setBold(false); |
| 180 | colors.put("twindow.background.modal", color); |
| 181 | |
| 182 | // TWindow border - modal + inactive |
| 183 | color = new CellAttributes(); |
| 184 | color.setForeColor(Color.BLACK); |
| 185 | color.setBackColor(Color.WHITE); |
| 186 | color.setBold(true); |
| 187 | colors.put("twindow.border.modal.inactive", color); |
| 188 | |
| 189 | // TWindow background - modal + inactive |
| 190 | color = new CellAttributes(); |
| 191 | color.setForeColor(Color.BLACK); |
| 192 | color.setBackColor(Color.WHITE); |
| 193 | color.setBold(false); |
| 194 | colors.put("twindow.background.modal.inactive", color); |
| 195 | |
| 196 | // TWindow border - during window movement - modal |
| 197 | color = new CellAttributes(); |
| 198 | color.setForeColor(Color.GREEN); |
| 199 | color.setBackColor(Color.WHITE); |
| 200 | color.setBold(true); |
| 201 | colors.put("twindow.border.modal.windowmove", color); |
| 202 | |
| 203 | // TWindow border - during window movement |
| 204 | color = new CellAttributes(); |
| 205 | color.setForeColor(Color.GREEN); |
| 206 | color.setBackColor(Color.BLUE); |
| 207 | color.setBold(true); |
| 208 | colors.put("twindow.border.windowmove", color); |
| 209 | |
| 210 | // TWindow background - during window movement |
| 211 | color = new CellAttributes(); |
| 212 | color.setForeColor(Color.YELLOW); |
| 213 | color.setBackColor(Color.BLUE); |
| 214 | color.setBold(false); |
| 215 | colors.put("twindow.background.windowmove", color); |
| 216 | |
| 217 | // TApplication background |
| 218 | color = new CellAttributes(); |
| 219 | color.setForeColor(Color.BLUE); |
| 220 | color.setBackColor(Color.WHITE); |
| 221 | color.setBold(false); |
| 222 | colors.put("tapplication.background", color); |
| 223 | |
| 224 | // TButton text |
| 225 | color = new CellAttributes(); |
| 226 | color.setForeColor(Color.BLACK); |
| 227 | color.setBackColor(Color.GREEN); |
| 228 | color.setBold(false); |
| 229 | colors.put("tbutton.inactive", color); |
| 230 | color = new CellAttributes(); |
| 231 | color.setForeColor(Color.WHITE); |
| 232 | color.setBackColor(Color.GREEN); |
| 233 | color.setBold(true); |
| 234 | colors.put("tbutton.active", color); |
| 235 | color = new CellAttributes(); |
| 236 | color.setForeColor(Color.BLACK); |
| 237 | color.setBackColor(Color.WHITE); |
| 238 | color.setBold(true); |
| 239 | colors.put("tbutton.disabled", color); |
| 240 | color = new CellAttributes(); |
| 241 | color.setForeColor(Color.YELLOW); |
| 242 | color.setBackColor(Color.GREEN); |
| 243 | color.setBold(true); |
| 244 | colors.put("tbutton.mnemonic", color); |
| 245 | color = new CellAttributes(); |
| 246 | color.setForeColor(Color.YELLOW); |
| 247 | color.setBackColor(Color.GREEN); |
| 248 | color.setBold(true); |
| 249 | colors.put("tbutton.mnemonic.highlighted", color); |
| 250 | |
| 251 | // TLabel text |
| 252 | color = new CellAttributes(); |
| 253 | color.setForeColor(Color.WHITE); |
| 254 | color.setBackColor(Color.BLUE); |
| 255 | color.setBold(true); |
| 256 | colors.put("tlabel", color); |
| 257 | |
| 258 | // TText text |
| 259 | color = new CellAttributes(); |
| 260 | color.setForeColor(Color.WHITE); |
| 261 | color.setBackColor(Color.BLACK); |
| 262 | color.setBold(false); |
| 263 | colors.put("ttext", color); |
| 264 | |
| 265 | // TField text |
| 266 | color = new CellAttributes(); |
| 267 | color.setForeColor(Color.WHITE); |
| 268 | color.setBackColor(Color.BLUE); |
| 269 | color.setBold(false); |
| 270 | colors.put("tfield.inactive", color); |
| 271 | color = new CellAttributes(); |
| 272 | color.setForeColor(Color.YELLOW); |
| 273 | color.setBackColor(Color.BLACK); |
| 274 | color.setBold(true); |
| 275 | colors.put("tfield.active", color); |
| 276 | |
| 277 | // TCheckbox |
| 278 | color = new CellAttributes(); |
| 279 | color.setForeColor(Color.WHITE); |
| 280 | color.setBackColor(Color.BLUE); |
| 281 | color.setBold(false); |
| 282 | colors.put("tcheckbox.inactive", color); |
| 283 | color = new CellAttributes(); |
| 284 | color.setForeColor(Color.YELLOW); |
| 285 | color.setBackColor(Color.BLACK); |
| 286 | color.setBold(true); |
| 287 | colors.put("tcheckbox.active", color); |
| 288 | |
| 289 | |
| 290 | // TRadioButton |
| 291 | color = new CellAttributes(); |
| 292 | color.setForeColor(Color.WHITE); |
| 293 | color.setBackColor(Color.BLUE); |
| 294 | color.setBold(false); |
| 295 | colors.put("tradiobutton.inactive", color); |
| 296 | color = new CellAttributes(); |
| 297 | color.setForeColor(Color.YELLOW); |
| 298 | color.setBackColor(Color.BLACK); |
| 299 | color.setBold(true); |
| 300 | colors.put("tradiobutton.active", color); |
| 301 | |
| 302 | // TRadioGroup |
| 303 | color = new CellAttributes(); |
| 304 | color.setForeColor(Color.WHITE); |
| 305 | color.setBackColor(Color.BLUE); |
| 306 | color.setBold(false); |
| 307 | colors.put("tradiogroup.inactive", color); |
| 308 | color = new CellAttributes(); |
| 309 | color.setForeColor(Color.YELLOW); |
| 310 | color.setBackColor(Color.BLUE); |
| 311 | color.setBold(true); |
| 312 | colors.put("tradiogroup.active", color); |
| 313 | |
| 314 | // TMenu |
| 315 | color = new CellAttributes(); |
| 316 | color.setForeColor(Color.BLACK); |
| 317 | color.setBackColor(Color.WHITE); |
| 318 | color.setBold(false); |
| 319 | colors.put("tmenu", color); |
| 320 | color = new CellAttributes(); |
| 321 | color.setForeColor(Color.BLACK); |
| 322 | color.setBackColor(Color.GREEN); |
| 323 | color.setBold(false); |
| 324 | colors.put("tmenu.highlighted", color); |
| 325 | color = new CellAttributes(); |
| 326 | color.setForeColor(Color.RED); |
| 327 | color.setBackColor(Color.WHITE); |
| 328 | color.setBold(false); |
| 329 | colors.put("tmenu.mnemonic", color); |
| 330 | color = new CellAttributes(); |
| 331 | color.setForeColor(Color.RED); |
| 332 | color.setBackColor(Color.GREEN); |
| 333 | color.setBold(false); |
| 334 | colors.put("tmenu.mnemonic.highlighted", color); |
| 335 | color = new CellAttributes(); |
| 336 | color.setForeColor(Color.BLACK); |
| 337 | color.setBackColor(Color.WHITE); |
| 338 | color.setBold(true); |
| 339 | colors.put("tmenu.disabled", color); |
| 340 | |
| 341 | // TProgressBar |
| 342 | color = new CellAttributes(); |
| 343 | color.setForeColor(Color.BLUE); |
| 344 | color.setBackColor(Color.BLUE); |
| 345 | color.setBold(true); |
| 346 | colors.put("tprogressbar.complete", color); |
| 347 | color = new CellAttributes(); |
| 348 | color.setForeColor(Color.WHITE); |
| 349 | color.setBackColor(Color.BLUE); |
| 350 | color.setBold(false); |
| 351 | colors.put("tprogressbar.incomplete", color); |
| 352 | |
| 353 | // THScroller / TVScroller |
| 354 | color = new CellAttributes(); |
| 355 | color.setForeColor(Color.CYAN); |
| 356 | color.setBackColor(Color.BLUE); |
| 357 | color.setBold(false); |
| 358 | colors.put("tscroller.bar", color); |
| 359 | color = new CellAttributes(); |
| 360 | color.setForeColor(Color.BLUE); |
| 361 | color.setBackColor(Color.CYAN); |
| 362 | color.setBold(false); |
| 363 | colors.put("tscroller.arrows", color); |
| 364 | |
| 365 | // TTreeView |
| 366 | color = new CellAttributes(); |
| 367 | color.setForeColor(Color.WHITE); |
| 368 | color.setBackColor(Color.BLUE); |
| 369 | color.setBold(false); |
| 370 | colors.put("ttreeview", color); |
| 371 | color = new CellAttributes(); |
| 372 | color.setForeColor(Color.GREEN); |
| 373 | color.setBackColor(Color.BLUE); |
| 374 | color.setBold(true); |
| 375 | colors.put("ttreeview.expandbutton", color); |
| 376 | color = new CellAttributes(); |
| 377 | color.setForeColor(Color.BLACK); |
| 378 | color.setBackColor(Color.CYAN); |
| 379 | color.setBold(false); |
| 380 | colors.put("ttreeview.selected", color); |
| 381 | color = new CellAttributes(); |
| 382 | color.setForeColor(Color.RED); |
| 383 | color.setBackColor(Color.BLUE); |
| 384 | color.setBold(false); |
| 385 | colors.put("ttreeview.unreadable", color); |
| 386 | color = new CellAttributes(); |
| 387 | color.setForeColor(Color.BLACK); |
| 388 | color.setBackColor(Color.BLUE); |
| 389 | color.setBold(true); |
| 390 | colors.put("ttreeview.inactive", color); |
| 391 | |
| 392 | // TText text |
| 393 | color = new CellAttributes(); |
| 394 | color.setForeColor(Color.WHITE); |
| 395 | color.setBackColor(Color.BLUE); |
| 396 | color.setBold(false); |
| 397 | colors.put("tdirectorylist", color); |
| 398 | color = new CellAttributes(); |
| 399 | color.setForeColor(Color.BLACK); |
| 400 | color.setBackColor(Color.CYAN); |
| 401 | color.setBold(false); |
| 402 | colors.put("tdirectorylist.selected", color); |
| 403 | color = new CellAttributes(); |
| 404 | color.setForeColor(Color.BLACK); |
| 405 | color.setBackColor(Color.CYAN); |
| 406 | color.setBold(false); |
| 407 | colors.put("tdirectorylist.unreadable", color); |
| 408 | color = new CellAttributes(); |
| 409 | color.setForeColor(Color.BLACK); |
| 410 | color.setBackColor(Color.BLUE); |
| 411 | color.setBold(true); |
| 412 | colors.put("tdirectorylist.inactive", color); |
| 413 | |
| 414 | // TEditor |
| 415 | color = new CellAttributes(); |
| 416 | color.setForeColor(Color.WHITE); |
| 417 | color.setBackColor(Color.BLACK); |
| 418 | color.setBold(false); |
| 419 | colors.put("teditor", color); |
| 420 | |
| 421 | } |
| 422 | |
| 423 | } |