Commit | Line | Data |
---|---|---|
3649b921 KL |
1 | /* |
2 | * Jexer - Java Text User Interface | |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
3649b921 | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
3649b921 | 7 | * |
e16dda65 KL |
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: | |
3649b921 | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
3649b921 | 17 | * |
e16dda65 KL |
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. | |
3649b921 KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import java.util.List; | |
339652cc | 32 | import java.util.ResourceBundle; |
3649b921 KL |
33 | |
34 | import jexer.bits.Color; | |
35 | import jexer.bits.ColorTheme; | |
36 | import jexer.bits.CellAttributes; | |
37 | import jexer.bits.GraphicsChars; | |
38 | import jexer.event.TKeypressEvent; | |
39 | import jexer.event.TMouseEvent; | |
40 | import static jexer.TKeypress.*; | |
41 | ||
42 | /** | |
43 | * TEditColorThemeWindow provides an easy UI for users to alter the running | |
44 | * color theme. | |
45 | * | |
46 | */ | |
eb29bbb5 | 47 | public class TEditColorThemeWindow extends TWindow { |
3649b921 | 48 | |
339652cc KL |
49 | /** |
50 | * Translated strings. | |
51 | */ | |
52 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TEditColorThemeWindow.class.getName()); | |
53 | ||
d36057df KL |
54 | // ------------------------------------------------------------------------ |
55 | // Variables -------------------------------------------------------------- | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
58 | /** | |
59 | * The current editing theme. | |
60 | */ | |
61 | private ColorTheme editTheme; | |
62 | ||
63 | /** | |
64 | * The left-side list of colors pane. | |
65 | */ | |
66 | private TList colorNames; | |
67 | ||
68 | /** | |
69 | * The foreground color. | |
70 | */ | |
71 | private ForegroundPicker foreground; | |
72 | ||
73 | /** | |
74 | * The background color. | |
75 | */ | |
76 | private BackgroundPicker background; | |
77 | ||
3649b921 KL |
78 | /** |
79 | * The foreground color picker. | |
80 | */ | |
81 | class ForegroundPicker extends TWidget { | |
82 | ||
83 | /** | |
84 | * The selected color. | |
85 | */ | |
86 | Color color; | |
87 | ||
88 | /** | |
89 | * The bold flag. | |
90 | */ | |
91 | boolean bold; | |
92 | ||
93 | /** | |
94 | * Public constructor. | |
95 | * | |
96 | * @param parent parent widget | |
97 | * @param x column relative to parent | |
98 | * @param y row relative to parent | |
99 | * @param width width of text area | |
100 | * @param height height of text area | |
101 | */ | |
102 | public ForegroundPicker(final TWidget parent, final int x, | |
103 | final int y, final int width, final int height) { | |
104 | ||
105 | super(parent, x, y, width, height); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Get the X grid coordinate for this color. | |
110 | * | |
111 | * @param color the Color value | |
112 | * @return the X coordinate | |
113 | */ | |
114 | private int getXColorPosition(final Color color) { | |
115 | if (color.equals(Color.BLACK)) { | |
116 | return 2; | |
117 | } else if (color.equals(Color.BLUE)) { | |
118 | return 5; | |
119 | } else if (color.equals(Color.GREEN)) { | |
120 | return 8; | |
121 | } else if (color.equals(Color.CYAN)) { | |
122 | return 11; | |
123 | } else if (color.equals(Color.RED)) { | |
124 | return 2; | |
125 | } else if (color.equals(Color.MAGENTA)) { | |
126 | return 5; | |
127 | } else if (color.equals(Color.YELLOW)) { | |
128 | return 8; | |
129 | } else if (color.equals(Color.WHITE)) { | |
130 | return 11; | |
131 | } | |
132 | throw new IllegalArgumentException("Invalid color: " + color); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Get the Y grid coordinate for this color. | |
137 | * | |
138 | * @param color the Color value | |
139 | * @param bold if true use bold color | |
140 | * @return the Y coordinate | |
141 | */ | |
142 | private int getYColorPosition(final Color color, final boolean bold) { | |
143 | int dotY = 1; | |
144 | if (color.equals(Color.RED)) { | |
145 | dotY = 2; | |
146 | } else if (color.equals(Color.MAGENTA)) { | |
147 | dotY = 2; | |
148 | } else if (color.equals(Color.YELLOW)) { | |
149 | dotY = 2; | |
150 | } else if (color.equals(Color.WHITE)) { | |
151 | dotY = 2; | |
152 | } | |
153 | if (bold) { | |
154 | dotY += 2; | |
155 | } | |
156 | return dotY; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Get the bold value based on Y grid coordinate. | |
161 | * | |
162 | * @param dotY the Y coordinate | |
163 | * @return the bold value | |
164 | */ | |
165 | private boolean getBoldFromPosition(final int dotY) { | |
166 | if (dotY > 2) { | |
167 | return true; | |
168 | } | |
169 | return false; | |
170 | } | |
171 | ||
172 | /** | |
173 | * Get the color based on (X, Y) grid coordinate. | |
174 | * | |
175 | * @param dotX the X coordinate | |
176 | * @param dotY the Y coordinate | |
177 | * @return the Color value | |
178 | */ | |
179 | private Color getColorFromPosition(final int dotX, final int dotY) { | |
180 | int y = dotY; | |
181 | if (y > 2) { | |
182 | y -= 2; | |
183 | } | |
184 | if ((1 <= dotX) && (dotX <= 3) && (y == 1)) { | |
185 | return Color.BLACK; | |
186 | } | |
187 | if ((4 <= dotX) && (dotX <= 6) && (y == 1)) { | |
188 | return Color.BLUE; | |
189 | } | |
190 | if ((7 <= dotX) && (dotX <= 9) && (y == 1)) { | |
191 | return Color.GREEN; | |
192 | } | |
193 | if ((10 <= dotX) && (dotX <= 12) && (y == 1)) { | |
194 | return Color.CYAN; | |
195 | } | |
196 | if ((1 <= dotX) && (dotX <= 3) && (y == 2)) { | |
197 | return Color.RED; | |
198 | } | |
199 | if ((4 <= dotX) && (dotX <= 6) && (y == 2)) { | |
200 | return Color.MAGENTA; | |
201 | } | |
202 | if ((7 <= dotX) && (dotX <= 9) && (y == 2)) { | |
203 | return Color.YELLOW; | |
204 | } | |
205 | if ((10 <= dotX) && (dotX <= 12) && (y == 2)) { | |
206 | return Color.WHITE; | |
207 | } | |
208 | ||
209 | throw new IllegalArgumentException("Invalid coordinates: " | |
210 | + dotX + ", " + dotY); | |
211 | } | |
212 | ||
213 | /** | |
214 | * Draw the foreground colors grid. | |
215 | */ | |
216 | @Override | |
217 | public void draw() { | |
218 | CellAttributes border = getWindow().getBorder(); | |
219 | CellAttributes background = getWindow().getBackground(); | |
220 | CellAttributes attr = new CellAttributes(); | |
221 | ||
a69ed767 KL |
222 | drawBox(0, 0, getWidth(), getHeight(), border, background, 1, |
223 | false); | |
3649b921 KL |
224 | |
225 | attr.setTo(getTheme().getColor("twindow.background.modal")); | |
226 | if (isActive()) { | |
227 | attr.setForeColor(getTheme().getColor("tlabel").getForeColor()); | |
228 | attr.setBold(getTheme().getColor("tlabel").isBold()); | |
229 | } | |
a69ed767 | 230 | putStringXY(1, 0, i18n.getString("foregroundLabel"), attr); |
3649b921 KL |
231 | |
232 | // Have to draw the colors manually because the int value matches | |
233 | // SGR, not CGA. | |
234 | attr.reset(); | |
3649b921 | 235 | attr.setForeColor(Color.BLACK); |
a51c7937 | 236 | putStringXY(1, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 237 | attr.setForeColor(Color.BLUE); |
a51c7937 | 238 | putStringXY(4, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 239 | attr.setForeColor(Color.GREEN); |
a51c7937 | 240 | putStringXY(7, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 241 | attr.setForeColor(Color.CYAN); |
a51c7937 | 242 | putStringXY(10, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 243 | attr.setForeColor(Color.RED); |
a51c7937 | 244 | putStringXY(1, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 245 | attr.setForeColor(Color.MAGENTA); |
a51c7937 | 246 | putStringXY(4, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 247 | attr.setForeColor(Color.YELLOW); |
a51c7937 | 248 | putStringXY(7, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 249 | attr.setForeColor(Color.WHITE); |
a51c7937 | 250 | putStringXY(10, 2, "\u2588\u2588\u2588", attr); |
3649b921 KL |
251 | |
252 | attr.setBold(true); | |
253 | attr.setForeColor(Color.BLACK); | |
a51c7937 | 254 | putStringXY(1, 3, "\u2588\u2588\u2588", attr); |
3649b921 | 255 | attr.setForeColor(Color.BLUE); |
a51c7937 | 256 | putStringXY(4, 3, "\u2588\u2588\u2588", attr); |
3649b921 | 257 | attr.setForeColor(Color.GREEN); |
a51c7937 | 258 | putStringXY(7, 3, "\u2588\u2588\u2588", attr); |
3649b921 | 259 | attr.setForeColor(Color.CYAN); |
a51c7937 | 260 | putStringXY(10, 3, "\u2588\u2588\u2588", attr); |
3649b921 | 261 | attr.setForeColor(Color.RED); |
a51c7937 | 262 | putStringXY(1, 4, "\u2588\u2588\u2588", attr); |
3649b921 | 263 | attr.setForeColor(Color.MAGENTA); |
a51c7937 | 264 | putStringXY(4, 4, "\u2588\u2588\u2588", attr); |
3649b921 | 265 | attr.setForeColor(Color.YELLOW); |
a51c7937 | 266 | putStringXY(7, 4, "\u2588\u2588\u2588", attr); |
3649b921 | 267 | attr.setForeColor(Color.WHITE); |
a51c7937 | 268 | putStringXY(10, 4, "\u2588\u2588\u2588", attr); |
3649b921 KL |
269 | |
270 | // Draw the dot | |
271 | int dotX = getXColorPosition(color); | |
272 | int dotY = getYColorPosition(color, bold); | |
273 | if (color.equals(Color.BLACK) && !bold) { | |
274 | // Use white-on-black for black. All other colors use | |
275 | // black-on-whatever. | |
276 | attr.reset(); | |
a69ed767 | 277 | putCharXY(dotX, dotY, GraphicsChars.CP437[0x07], attr); |
3649b921 | 278 | } else { |
a51c7937 KL |
279 | attr.setForeColor(color); |
280 | attr.setBold(bold); | |
a69ed767 | 281 | putCharXY(dotX, dotY, '\u25D8', attr); |
3649b921 KL |
282 | } |
283 | } | |
284 | ||
285 | /** | |
286 | * Handle keystrokes. | |
287 | * | |
288 | * @param keypress keystroke event | |
289 | */ | |
290 | @Override | |
291 | public void onKeypress(final TKeypressEvent keypress) { | |
292 | if (keypress.equals(kbRight)) { | |
293 | int dotX = getXColorPosition(color); | |
294 | int dotY = getYColorPosition(color, bold); | |
295 | if (dotX < 10) { | |
296 | dotX += 3; | |
297 | } | |
298 | color = getColorFromPosition(dotX, dotY); | |
299 | } else if (keypress.equals(kbLeft)) { | |
300 | int dotX = getXColorPosition(color); | |
301 | int dotY = getYColorPosition(color, bold); | |
302 | if (dotX > 3) { | |
303 | dotX -= 3; | |
304 | } | |
305 | color = getColorFromPosition(dotX, dotY); | |
306 | } else if (keypress.equals(kbUp)) { | |
307 | int dotX = getXColorPosition(color); | |
308 | int dotY = getYColorPosition(color, bold); | |
309 | if (dotY > 1) { | |
310 | dotY--; | |
311 | } | |
312 | color = getColorFromPosition(dotX, dotY); | |
313 | bold = getBoldFromPosition(dotY); | |
314 | } else if (keypress.equals(kbDown)) { | |
315 | int dotX = getXColorPosition(color); | |
316 | int dotY = getYColorPosition(color, bold); | |
317 | if (dotY < 4) { | |
318 | dotY++; | |
319 | } | |
320 | color = getColorFromPosition(dotX, dotY); | |
321 | bold = getBoldFromPosition(dotY); | |
322 | } else { | |
323 | // Pass to my parent | |
324 | super.onKeypress(keypress); | |
325 | return; | |
326 | } | |
327 | ||
328 | // Save this update to the local theme. | |
329 | ((TEditColorThemeWindow) getWindow()).saveToEditTheme(); | |
330 | } | |
331 | ||
332 | /** | |
333 | * Handle mouse press events. | |
334 | * | |
335 | * @param mouse mouse button press event | |
336 | */ | |
337 | @Override | |
338 | public void onMouseDown(final TMouseEvent mouse) { | |
339 | if (mouse.isMouseWheelUp()) { | |
340 | // Do this like kbUp | |
341 | int dotX = getXColorPosition(color); | |
342 | int dotY = getYColorPosition(color, bold); | |
343 | if (dotY > 1) { | |
344 | dotY--; | |
345 | } | |
346 | color = getColorFromPosition(dotX, dotY); | |
347 | bold = getBoldFromPosition(dotY); | |
348 | } else if (mouse.isMouseWheelDown()) { | |
349 | // Do this like kbDown | |
350 | int dotX = getXColorPosition(color); | |
351 | int dotY = getYColorPosition(color, bold); | |
352 | if (dotY < 4) { | |
353 | dotY++; | |
354 | } | |
355 | color = getColorFromPosition(dotX, dotY); | |
356 | bold = getBoldFromPosition(dotY); | |
357 | } else if ((mouse.getX() > 0) | |
358 | && (mouse.getX() < getWidth() - 1) | |
359 | && (mouse.getY() > 0) | |
360 | && (mouse.getY() < getHeight() - 1) | |
361 | ) { | |
362 | color = getColorFromPosition(mouse.getX(), mouse.getY()); | |
363 | bold = getBoldFromPosition(mouse.getY()); | |
364 | } else { | |
365 | // Let parent class handle it. | |
366 | super.onMouseDown(mouse); | |
367 | return; | |
368 | } | |
369 | ||
370 | // Save this update to the local theme. | |
371 | ((TEditColorThemeWindow) getWindow()).saveToEditTheme(); | |
372 | } | |
373 | ||
374 | } | |
375 | ||
376 | /** | |
377 | * The background color picker. | |
378 | */ | |
379 | class BackgroundPicker extends TWidget { | |
380 | ||
381 | /** | |
382 | * The selected color. | |
383 | */ | |
384 | Color color; | |
385 | ||
386 | /** | |
387 | * Public constructor. | |
388 | * | |
389 | * @param parent parent widget | |
390 | * @param x column relative to parent | |
391 | * @param y row relative to parent | |
392 | * @param width width of text area | |
393 | * @param height height of text area | |
394 | */ | |
395 | public BackgroundPicker(final TWidget parent, final int x, | |
396 | final int y, final int width, final int height) { | |
397 | ||
398 | super(parent, x, y, width, height); | |
399 | } | |
400 | ||
401 | /** | |
402 | * Get the X grid coordinate for this color. | |
403 | * | |
404 | * @param color the Color value | |
405 | * @return the X coordinate | |
406 | */ | |
407 | private int getXColorPosition(final Color color) { | |
408 | if (color.equals(Color.BLACK)) { | |
409 | return 2; | |
410 | } else if (color.equals(Color.BLUE)) { | |
411 | return 5; | |
412 | } else if (color.equals(Color.GREEN)) { | |
413 | return 8; | |
414 | } else if (color.equals(Color.CYAN)) { | |
415 | return 11; | |
416 | } else if (color.equals(Color.RED)) { | |
417 | return 2; | |
418 | } else if (color.equals(Color.MAGENTA)) { | |
419 | return 5; | |
420 | } else if (color.equals(Color.YELLOW)) { | |
421 | return 8; | |
422 | } else if (color.equals(Color.WHITE)) { | |
423 | return 11; | |
424 | } | |
425 | throw new IllegalArgumentException("Invalid color: " + color); | |
426 | } | |
427 | ||
428 | /** | |
429 | * Get the Y grid coordinate for this color. | |
430 | * | |
431 | * @param color the Color value | |
432 | * @return the Y coordinate | |
433 | */ | |
434 | private int getYColorPosition(final Color color) { | |
435 | int dotY = 1; | |
436 | if (color.equals(Color.RED)) { | |
437 | dotY = 2; | |
438 | } else if (color.equals(Color.MAGENTA)) { | |
439 | dotY = 2; | |
440 | } else if (color.equals(Color.YELLOW)) { | |
441 | dotY = 2; | |
442 | } else if (color.equals(Color.WHITE)) { | |
443 | dotY = 2; | |
444 | } | |
445 | return dotY; | |
446 | } | |
447 | ||
448 | /** | |
449 | * Get the color based on (X, Y) grid coordinate. | |
450 | * | |
451 | * @param dotX the X coordinate | |
452 | * @param dotY the Y coordinate | |
453 | * @return the Color value | |
454 | */ | |
455 | private Color getColorFromPosition(final int dotX, final int dotY) { | |
456 | if ((1 <= dotX) && (dotX <= 3) && (dotY == 1)) { | |
457 | return Color.BLACK; | |
458 | } | |
459 | if ((4 <= dotX) && (dotX <= 6) && (dotY == 1)) { | |
460 | return Color.BLUE; | |
461 | } | |
462 | if ((7 <= dotX) && (dotX <= 9) && (dotY == 1)) { | |
463 | return Color.GREEN; | |
464 | } | |
465 | if ((10 <= dotX) && (dotX <= 12) && (dotY == 1)) { | |
466 | return Color.CYAN; | |
467 | } | |
468 | if ((1 <= dotX) && (dotX <= 3) && (dotY == 2)) { | |
469 | return Color.RED; | |
470 | } | |
471 | if ((4 <= dotX) && (dotX <= 6) && (dotY == 2)) { | |
472 | return Color.MAGENTA; | |
473 | } | |
474 | if ((7 <= dotX) && (dotX <= 9) && (dotY == 2)) { | |
475 | return Color.YELLOW; | |
476 | } | |
477 | if ((10 <= dotX) && (dotX <= 12) && (dotY == 2)) { | |
478 | return Color.WHITE; | |
479 | } | |
480 | ||
481 | throw new IllegalArgumentException("Invalid coordinates: " | |
482 | + dotX + ", " + dotY); | |
483 | } | |
484 | ||
485 | /** | |
486 | * Draw the background colors grid. | |
487 | */ | |
488 | @Override | |
489 | public void draw() { | |
490 | CellAttributes border = getWindow().getBorder(); | |
491 | CellAttributes background = getWindow().getBackground(); | |
492 | CellAttributes attr = new CellAttributes(); | |
493 | ||
a69ed767 KL |
494 | drawBox(0, 0, getWidth(), getHeight(), border, background, 1, |
495 | false); | |
3649b921 KL |
496 | |
497 | attr.setTo(getTheme().getColor("twindow.background.modal")); | |
498 | if (isActive()) { | |
499 | attr.setForeColor(getTheme().getColor("tlabel").getForeColor()); | |
500 | attr.setBold(getTheme().getColor("tlabel").isBold()); | |
501 | } | |
a69ed767 | 502 | putStringXY(1, 0, i18n.getString("backgroundLabel"), attr); |
3649b921 KL |
503 | |
504 | // Have to draw the colors manually because the int value matches | |
505 | // SGR, not CGA. | |
506 | attr.reset(); | |
3649b921 | 507 | attr.setForeColor(Color.BLACK); |
a51c7937 | 508 | putStringXY(1, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 509 | attr.setForeColor(Color.BLUE); |
a51c7937 | 510 | putStringXY(4, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 511 | attr.setForeColor(Color.GREEN); |
a51c7937 | 512 | putStringXY(7, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 513 | attr.setForeColor(Color.CYAN); |
a51c7937 | 514 | putStringXY(10, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 515 | attr.setForeColor(Color.RED); |
a51c7937 | 516 | putStringXY(1, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 517 | attr.setForeColor(Color.MAGENTA); |
a51c7937 | 518 | putStringXY(4, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 519 | attr.setForeColor(Color.YELLOW); |
a51c7937 | 520 | putStringXY(7, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 521 | attr.setForeColor(Color.WHITE); |
a51c7937 | 522 | putStringXY(10, 2, "\u2588\u2588\u2588", attr); |
3649b921 KL |
523 | |
524 | // Draw the dot | |
525 | int dotX = getXColorPosition(color); | |
526 | int dotY = getYColorPosition(color); | |
527 | if (color.equals(Color.BLACK)) { | |
528 | // Use white-on-black for black. All other colors use | |
529 | // black-on-whatever. | |
530 | attr.reset(); | |
a69ed767 | 531 | putCharXY(dotX, dotY, GraphicsChars.CP437[0x07], attr); |
3649b921 | 532 | } else { |
a51c7937 | 533 | attr.setForeColor(color); |
a69ed767 | 534 | putCharXY(dotX, dotY, '\u25D8', attr); |
3649b921 KL |
535 | } |
536 | ||
537 | } | |
538 | ||
539 | /** | |
540 | * Handle keystrokes. | |
541 | * | |
542 | * @param keypress keystroke event | |
543 | */ | |
544 | @Override | |
545 | public void onKeypress(final TKeypressEvent keypress) { | |
546 | if (keypress.equals(kbRight)) { | |
547 | int dotX = getXColorPosition(color); | |
548 | int dotY = getYColorPosition(color); | |
549 | if (dotX < 10) { | |
550 | dotX += 3; | |
551 | } | |
552 | color = getColorFromPosition(dotX, dotY); | |
553 | } else if (keypress.equals(kbLeft)) { | |
554 | int dotX = getXColorPosition(color); | |
555 | int dotY = getYColorPosition(color); | |
556 | if (dotX > 3) { | |
557 | dotX -= 3; | |
558 | } | |
559 | color = getColorFromPosition(dotX, dotY); | |
560 | } else if (keypress.equals(kbUp)) { | |
561 | int dotX = getXColorPosition(color); | |
562 | int dotY = getYColorPosition(color); | |
563 | if (dotY == 2) { | |
564 | dotY--; | |
565 | } | |
566 | color = getColorFromPosition(dotX, dotY); | |
567 | } else if (keypress.equals(kbDown)) { | |
568 | int dotX = getXColorPosition(color); | |
569 | int dotY = getYColorPosition(color); | |
570 | if (dotY == 1) { | |
571 | dotY++; | |
572 | } | |
573 | color = getColorFromPosition(dotX, dotY); | |
574 | } else { | |
575 | // Pass to my parent | |
576 | super.onKeypress(keypress); | |
577 | } | |
578 | ||
579 | // Save this update to the local theme. | |
580 | ((TEditColorThemeWindow) getWindow()).saveToEditTheme(); | |
581 | } | |
582 | ||
583 | /** | |
584 | * Handle mouse press events. | |
585 | * | |
586 | * @param mouse mouse button press event | |
587 | */ | |
588 | @Override | |
589 | public void onMouseDown(final TMouseEvent mouse) { | |
590 | if (mouse.isMouseWheelUp()) { | |
591 | // Do this like kbUp | |
592 | int dotX = getXColorPosition(color); | |
593 | int dotY = getYColorPosition(color); | |
594 | if (dotY == 2) { | |
595 | dotY--; | |
596 | } | |
597 | color = getColorFromPosition(dotX, dotY); | |
598 | } else if (mouse.isMouseWheelDown()) { | |
599 | // Do this like kbDown | |
600 | int dotX = getXColorPosition(color); | |
601 | int dotY = getYColorPosition(color); | |
602 | if (dotY == 1) { | |
603 | dotY++; | |
604 | } | |
605 | color = getColorFromPosition(dotX, dotY); | |
606 | return; | |
607 | } else if ((mouse.getX() > 0) | |
608 | && (mouse.getX() < getWidth() - 1) | |
609 | && (mouse.getY() > 0) | |
610 | && (mouse.getY() < getHeight() - 1) | |
611 | ) { | |
612 | color = getColorFromPosition(mouse.getX(), mouse.getY()); | |
613 | } else { | |
614 | // Let parent class handle it. | |
615 | super.onMouseDown(mouse); | |
616 | return; | |
617 | } | |
618 | ||
619 | // Save this update to the local theme. | |
620 | ((TEditColorThemeWindow) getWindow()).saveToEditTheme(); | |
621 | } | |
622 | ||
623 | } | |
624 | ||
d36057df KL |
625 | // ------------------------------------------------------------------------ |
626 | // Constructors ----------------------------------------------------------- | |
627 | // ------------------------------------------------------------------------ | |
3649b921 KL |
628 | |
629 | /** | |
e23ea538 | 630 | * Public constructor. The window will be centered on screen. |
3649b921 KL |
631 | * |
632 | * @param application the TApplication that manages this window | |
633 | */ | |
634 | public TEditColorThemeWindow(final TApplication application) { | |
635 | ||
636 | // Register with the TApplication | |
339652cc | 637 | super(application, i18n.getString("windowTitle"), 0, 0, 60, 18, MODAL); |
3649b921 KL |
638 | |
639 | // Initialize with the first color | |
640 | List<String> colors = getTheme().getColorNames(); | |
641 | assert (colors.size() > 0); | |
642 | editTheme = new ColorTheme(); | |
643 | for (String key: colors) { | |
644 | CellAttributes attr = new CellAttributes(); | |
645 | attr.setTo(getTheme().getColor(key)); | |
646 | editTheme.setColor(key, attr); | |
647 | } | |
648 | ||
649 | colorNames = addList(colors, 2, 2, 38, getHeight() - 7, | |
650 | new TAction() { | |
651 | // When the user presses Enter | |
652 | public void DO() { | |
653 | refreshFromTheme(colorNames.getSelected()); | |
654 | } | |
655 | }, | |
9577a9d0 KL |
656 | new TAction() { |
657 | // When the user navigates with keyboard | |
658 | public void DO() { | |
659 | refreshFromTheme(colorNames.getSelected()); | |
660 | } | |
661 | }, | |
3649b921 KL |
662 | new TAction() { |
663 | // When the user navigates with keyboard | |
664 | public void DO() { | |
665 | refreshFromTheme(colorNames.getSelected()); | |
666 | } | |
667 | } | |
668 | ); | |
669 | foreground = new ForegroundPicker(this, 42, 1, 14, 6); | |
670 | background = new BackgroundPicker(this, 42, 7, 14, 4); | |
671 | refreshFromTheme(colors.get(0)); | |
672 | colorNames.setSelectedIndex(0); | |
673 | ||
339652cc | 674 | addButton(i18n.getString("okButton"), getWidth() - 37, getHeight() - 4, |
3649b921 KL |
675 | new TAction() { |
676 | public void DO() { | |
677 | ColorTheme global = getTheme(); | |
678 | List<String> colors = editTheme.getColorNames(); | |
679 | for (String key: colors) { | |
680 | CellAttributes attr = new CellAttributes(); | |
681 | attr.setTo(editTheme.getColor(key)); | |
682 | global.setColor(key, attr); | |
683 | } | |
684 | getApplication().closeWindow(TEditColorThemeWindow.this); | |
685 | } | |
686 | } | |
687 | ); | |
688 | ||
339652cc KL |
689 | addButton(i18n.getString("cancelButton"), getWidth() - 25, |
690 | getHeight() - 4, | |
3649b921 KL |
691 | new TAction() { |
692 | public void DO() { | |
693 | getApplication().closeWindow(TEditColorThemeWindow.this); | |
694 | } | |
695 | } | |
696 | ); | |
697 | ||
698 | // Default to the color list | |
699 | activate(colorNames); | |
700 | ||
2ce6dab2 | 701 | // Add shortcut text |
339652cc | 702 | newStatusBar(i18n.getString("statusBar")); |
3649b921 KL |
703 | } |
704 | ||
d36057df KL |
705 | // ------------------------------------------------------------------------ |
706 | // Event handlers --------------------------------------------------------- | |
707 | // ------------------------------------------------------------------------ | |
708 | ||
709 | /** | |
710 | * Handle keystrokes. | |
711 | * | |
712 | * @param keypress keystroke event | |
713 | */ | |
714 | @Override | |
715 | public void onKeypress(final TKeypressEvent keypress) { | |
716 | // Escape - behave like cancel | |
717 | if (keypress.equals(kbEsc)) { | |
718 | getApplication().closeWindow(this); | |
719 | return; | |
720 | } | |
721 | ||
722 | // Pass to my parent | |
723 | super.onKeypress(keypress); | |
724 | } | |
725 | ||
726 | // ------------------------------------------------------------------------ | |
727 | // TWindow ---------------------------------------------------------------- | |
728 | // ------------------------------------------------------------------------ | |
729 | ||
3649b921 KL |
730 | /** |
731 | * Draw me on screen. | |
732 | */ | |
733 | @Override | |
734 | public void draw() { | |
735 | super.draw(); | |
736 | CellAttributes attr = new CellAttributes(); | |
737 | ||
738 | // Draw the label on colorNames | |
739 | attr.setTo(getTheme().getColor("twindow.background.modal")); | |
740 | if (colorNames.isActive()) { | |
741 | attr.setForeColor(getTheme().getColor("tlabel").getForeColor()); | |
742 | attr.setBold(getTheme().getColor("tlabel").isBold()); | |
743 | } | |
a69ed767 | 744 | putStringXY(3, 2, i18n.getString("colorName"), attr); |
3649b921 KL |
745 | |
746 | // Draw the sample text box | |
747 | attr.reset(); | |
748 | attr.setForeColor(foreground.color); | |
749 | attr.setBold(foreground.bold); | |
750 | attr.setBackColor(background.color); | |
a69ed767 | 751 | putStringXY(getWidth() - 17, getHeight() - 6, |
339652cc | 752 | i18n.getString("textTextText"), attr); |
a69ed767 | 753 | putStringXY(getWidth() - 17, getHeight() - 5, |
339652cc | 754 | i18n.getString("textTextText"), attr); |
3649b921 KL |
755 | } |
756 | ||
d36057df KL |
757 | // ------------------------------------------------------------------------ |
758 | // TEditColorThemeWindow -------------------------------------------------- | |
759 | // ------------------------------------------------------------------------ | |
760 | ||
3649b921 | 761 | /** |
d36057df | 762 | * Set various widgets/values to the editing theme color. |
3649b921 | 763 | * |
d36057df | 764 | * @param colorName name of color from theme |
3649b921 | 765 | */ |
d36057df KL |
766 | private void refreshFromTheme(final String colorName) { |
767 | CellAttributes attr = editTheme.getColor(colorName); | |
768 | foreground.color = attr.getForeColor(); | |
769 | foreground.bold = attr.isBold(); | |
770 | background.color = attr.getBackColor(); | |
771 | } | |
772 | ||
773 | /** | |
774 | * Examines foreground, background, and colorNames and sets the color in | |
775 | * editTheme. | |
776 | */ | |
777 | private void saveToEditTheme() { | |
778 | String colorName = colorNames.getSelected(); | |
779 | if (colorName == null) { | |
3649b921 KL |
780 | return; |
781 | } | |
d36057df KL |
782 | CellAttributes attr = editTheme.getColor(colorName); |
783 | attr.setForeColor(foreground.color); | |
784 | attr.setBold(foreground.bold); | |
785 | attr.setBackColor(background.color); | |
786 | editTheme.setColor(colorName, attr); | |
3649b921 KL |
787 | } |
788 | ||
789 | } |