Commit | Line | Data |
---|---|---|
3649b921 KL |
1 | /* |
2 | * Jexer - Java Text User Interface | |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
3649b921 | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 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 | ||
222 | getScreen().drawBox(0, 0, getWidth(), getHeight(), border, | |
223 | background, 1, false); | |
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 | } | |
339652cc KL |
230 | getScreen().putStringXY(1, 0, i18n.getString("foregroundLabel"), |
231 | attr); | |
3649b921 KL |
232 | |
233 | // Have to draw the colors manually because the int value matches | |
234 | // SGR, not CGA. | |
235 | attr.reset(); | |
3649b921 | 236 | attr.setForeColor(Color.BLACK); |
a51c7937 | 237 | putStringXY(1, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 238 | attr.setForeColor(Color.BLUE); |
a51c7937 | 239 | putStringXY(4, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 240 | attr.setForeColor(Color.GREEN); |
a51c7937 | 241 | putStringXY(7, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 242 | attr.setForeColor(Color.CYAN); |
a51c7937 | 243 | putStringXY(10, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 244 | attr.setForeColor(Color.RED); |
a51c7937 | 245 | putStringXY(1, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 246 | attr.setForeColor(Color.MAGENTA); |
a51c7937 | 247 | putStringXY(4, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 248 | attr.setForeColor(Color.YELLOW); |
a51c7937 | 249 | putStringXY(7, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 250 | attr.setForeColor(Color.WHITE); |
a51c7937 | 251 | putStringXY(10, 2, "\u2588\u2588\u2588", attr); |
3649b921 KL |
252 | |
253 | attr.setBold(true); | |
254 | attr.setForeColor(Color.BLACK); | |
a51c7937 | 255 | putStringXY(1, 3, "\u2588\u2588\u2588", attr); |
3649b921 | 256 | attr.setForeColor(Color.BLUE); |
a51c7937 | 257 | putStringXY(4, 3, "\u2588\u2588\u2588", attr); |
3649b921 | 258 | attr.setForeColor(Color.GREEN); |
a51c7937 | 259 | putStringXY(7, 3, "\u2588\u2588\u2588", attr); |
3649b921 | 260 | attr.setForeColor(Color.CYAN); |
a51c7937 | 261 | putStringXY(10, 3, "\u2588\u2588\u2588", attr); |
3649b921 | 262 | attr.setForeColor(Color.RED); |
a51c7937 | 263 | putStringXY(1, 4, "\u2588\u2588\u2588", attr); |
3649b921 | 264 | attr.setForeColor(Color.MAGENTA); |
a51c7937 | 265 | putStringXY(4, 4, "\u2588\u2588\u2588", attr); |
3649b921 | 266 | attr.setForeColor(Color.YELLOW); |
a51c7937 | 267 | putStringXY(7, 4, "\u2588\u2588\u2588", attr); |
3649b921 | 268 | attr.setForeColor(Color.WHITE); |
a51c7937 | 269 | putStringXY(10, 4, "\u2588\u2588\u2588", attr); |
3649b921 KL |
270 | |
271 | // Draw the dot | |
272 | int dotX = getXColorPosition(color); | |
273 | int dotY = getYColorPosition(color, bold); | |
274 | if (color.equals(Color.BLACK) && !bold) { | |
275 | // Use white-on-black for black. All other colors use | |
276 | // black-on-whatever. | |
277 | attr.reset(); | |
278 | getScreen().putCharXY(dotX, dotY, GraphicsChars.CP437[0x07], | |
279 | attr); | |
280 | } else { | |
a51c7937 KL |
281 | attr.setForeColor(color); |
282 | attr.setBold(bold); | |
283 | getScreen().putCharXY(dotX, dotY, '\u25D8', attr); | |
3649b921 KL |
284 | } |
285 | } | |
286 | ||
287 | /** | |
288 | * Handle keystrokes. | |
289 | * | |
290 | * @param keypress keystroke event | |
291 | */ | |
292 | @Override | |
293 | public void onKeypress(final TKeypressEvent keypress) { | |
294 | if (keypress.equals(kbRight)) { | |
295 | int dotX = getXColorPosition(color); | |
296 | int dotY = getYColorPosition(color, bold); | |
297 | if (dotX < 10) { | |
298 | dotX += 3; | |
299 | } | |
300 | color = getColorFromPosition(dotX, dotY); | |
301 | } else if (keypress.equals(kbLeft)) { | |
302 | int dotX = getXColorPosition(color); | |
303 | int dotY = getYColorPosition(color, bold); | |
304 | if (dotX > 3) { | |
305 | dotX -= 3; | |
306 | } | |
307 | color = getColorFromPosition(dotX, dotY); | |
308 | } else if (keypress.equals(kbUp)) { | |
309 | int dotX = getXColorPosition(color); | |
310 | int dotY = getYColorPosition(color, bold); | |
311 | if (dotY > 1) { | |
312 | dotY--; | |
313 | } | |
314 | color = getColorFromPosition(dotX, dotY); | |
315 | bold = getBoldFromPosition(dotY); | |
316 | } else if (keypress.equals(kbDown)) { | |
317 | int dotX = getXColorPosition(color); | |
318 | int dotY = getYColorPosition(color, bold); | |
319 | if (dotY < 4) { | |
320 | dotY++; | |
321 | } | |
322 | color = getColorFromPosition(dotX, dotY); | |
323 | bold = getBoldFromPosition(dotY); | |
324 | } else { | |
325 | // Pass to my parent | |
326 | super.onKeypress(keypress); | |
327 | return; | |
328 | } | |
329 | ||
330 | // Save this update to the local theme. | |
331 | ((TEditColorThemeWindow) getWindow()).saveToEditTheme(); | |
332 | } | |
333 | ||
334 | /** | |
335 | * Handle mouse press events. | |
336 | * | |
337 | * @param mouse mouse button press event | |
338 | */ | |
339 | @Override | |
340 | public void onMouseDown(final TMouseEvent mouse) { | |
341 | if (mouse.isMouseWheelUp()) { | |
342 | // Do this like kbUp | |
343 | int dotX = getXColorPosition(color); | |
344 | int dotY = getYColorPosition(color, bold); | |
345 | if (dotY > 1) { | |
346 | dotY--; | |
347 | } | |
348 | color = getColorFromPosition(dotX, dotY); | |
349 | bold = getBoldFromPosition(dotY); | |
350 | } else if (mouse.isMouseWheelDown()) { | |
351 | // Do this like kbDown | |
352 | int dotX = getXColorPosition(color); | |
353 | int dotY = getYColorPosition(color, bold); | |
354 | if (dotY < 4) { | |
355 | dotY++; | |
356 | } | |
357 | color = getColorFromPosition(dotX, dotY); | |
358 | bold = getBoldFromPosition(dotY); | |
359 | } else if ((mouse.getX() > 0) | |
360 | && (mouse.getX() < getWidth() - 1) | |
361 | && (mouse.getY() > 0) | |
362 | && (mouse.getY() < getHeight() - 1) | |
363 | ) { | |
364 | color = getColorFromPosition(mouse.getX(), mouse.getY()); | |
365 | bold = getBoldFromPosition(mouse.getY()); | |
366 | } else { | |
367 | // Let parent class handle it. | |
368 | super.onMouseDown(mouse); | |
369 | return; | |
370 | } | |
371 | ||
372 | // Save this update to the local theme. | |
373 | ((TEditColorThemeWindow) getWindow()).saveToEditTheme(); | |
374 | } | |
375 | ||
376 | } | |
377 | ||
378 | /** | |
379 | * The background color picker. | |
380 | */ | |
381 | class BackgroundPicker extends TWidget { | |
382 | ||
383 | /** | |
384 | * The selected color. | |
385 | */ | |
386 | Color color; | |
387 | ||
388 | /** | |
389 | * Public constructor. | |
390 | * | |
391 | * @param parent parent widget | |
392 | * @param x column relative to parent | |
393 | * @param y row relative to parent | |
394 | * @param width width of text area | |
395 | * @param height height of text area | |
396 | */ | |
397 | public BackgroundPicker(final TWidget parent, final int x, | |
398 | final int y, final int width, final int height) { | |
399 | ||
400 | super(parent, x, y, width, height); | |
401 | } | |
402 | ||
403 | /** | |
404 | * Get the X grid coordinate for this color. | |
405 | * | |
406 | * @param color the Color value | |
407 | * @return the X coordinate | |
408 | */ | |
409 | private int getXColorPosition(final Color color) { | |
410 | if (color.equals(Color.BLACK)) { | |
411 | return 2; | |
412 | } else if (color.equals(Color.BLUE)) { | |
413 | return 5; | |
414 | } else if (color.equals(Color.GREEN)) { | |
415 | return 8; | |
416 | } else if (color.equals(Color.CYAN)) { | |
417 | return 11; | |
418 | } else if (color.equals(Color.RED)) { | |
419 | return 2; | |
420 | } else if (color.equals(Color.MAGENTA)) { | |
421 | return 5; | |
422 | } else if (color.equals(Color.YELLOW)) { | |
423 | return 8; | |
424 | } else if (color.equals(Color.WHITE)) { | |
425 | return 11; | |
426 | } | |
427 | throw new IllegalArgumentException("Invalid color: " + color); | |
428 | } | |
429 | ||
430 | /** | |
431 | * Get the Y grid coordinate for this color. | |
432 | * | |
433 | * @param color the Color value | |
434 | * @return the Y coordinate | |
435 | */ | |
436 | private int getYColorPosition(final Color color) { | |
437 | int dotY = 1; | |
438 | if (color.equals(Color.RED)) { | |
439 | dotY = 2; | |
440 | } else if (color.equals(Color.MAGENTA)) { | |
441 | dotY = 2; | |
442 | } else if (color.equals(Color.YELLOW)) { | |
443 | dotY = 2; | |
444 | } else if (color.equals(Color.WHITE)) { | |
445 | dotY = 2; | |
446 | } | |
447 | return dotY; | |
448 | } | |
449 | ||
450 | /** | |
451 | * Get the color based on (X, Y) grid coordinate. | |
452 | * | |
453 | * @param dotX the X coordinate | |
454 | * @param dotY the Y coordinate | |
455 | * @return the Color value | |
456 | */ | |
457 | private Color getColorFromPosition(final int dotX, final int dotY) { | |
458 | if ((1 <= dotX) && (dotX <= 3) && (dotY == 1)) { | |
459 | return Color.BLACK; | |
460 | } | |
461 | if ((4 <= dotX) && (dotX <= 6) && (dotY == 1)) { | |
462 | return Color.BLUE; | |
463 | } | |
464 | if ((7 <= dotX) && (dotX <= 9) && (dotY == 1)) { | |
465 | return Color.GREEN; | |
466 | } | |
467 | if ((10 <= dotX) && (dotX <= 12) && (dotY == 1)) { | |
468 | return Color.CYAN; | |
469 | } | |
470 | if ((1 <= dotX) && (dotX <= 3) && (dotY == 2)) { | |
471 | return Color.RED; | |
472 | } | |
473 | if ((4 <= dotX) && (dotX <= 6) && (dotY == 2)) { | |
474 | return Color.MAGENTA; | |
475 | } | |
476 | if ((7 <= dotX) && (dotX <= 9) && (dotY == 2)) { | |
477 | return Color.YELLOW; | |
478 | } | |
479 | if ((10 <= dotX) && (dotX <= 12) && (dotY == 2)) { | |
480 | return Color.WHITE; | |
481 | } | |
482 | ||
483 | throw new IllegalArgumentException("Invalid coordinates: " | |
484 | + dotX + ", " + dotY); | |
485 | } | |
486 | ||
487 | /** | |
488 | * Draw the background colors grid. | |
489 | */ | |
490 | @Override | |
491 | public void draw() { | |
492 | CellAttributes border = getWindow().getBorder(); | |
493 | CellAttributes background = getWindow().getBackground(); | |
494 | CellAttributes attr = new CellAttributes(); | |
495 | ||
496 | getScreen().drawBox(0, 0, getWidth(), getHeight(), border, | |
497 | background, 1, false); | |
498 | ||
499 | attr.setTo(getTheme().getColor("twindow.background.modal")); | |
500 | if (isActive()) { | |
501 | attr.setForeColor(getTheme().getColor("tlabel").getForeColor()); | |
502 | attr.setBold(getTheme().getColor("tlabel").isBold()); | |
503 | } | |
339652cc KL |
504 | getScreen().putStringXY(1, 0, i18n.getString("backgroundLabel"), |
505 | attr); | |
3649b921 KL |
506 | |
507 | // Have to draw the colors manually because the int value matches | |
508 | // SGR, not CGA. | |
509 | attr.reset(); | |
3649b921 | 510 | attr.setForeColor(Color.BLACK); |
a51c7937 | 511 | putStringXY(1, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 512 | attr.setForeColor(Color.BLUE); |
a51c7937 | 513 | putStringXY(4, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 514 | attr.setForeColor(Color.GREEN); |
a51c7937 | 515 | putStringXY(7, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 516 | attr.setForeColor(Color.CYAN); |
a51c7937 | 517 | putStringXY(10, 1, "\u2588\u2588\u2588", attr); |
3649b921 | 518 | attr.setForeColor(Color.RED); |
a51c7937 | 519 | putStringXY(1, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 520 | attr.setForeColor(Color.MAGENTA); |
a51c7937 | 521 | putStringXY(4, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 522 | attr.setForeColor(Color.YELLOW); |
a51c7937 | 523 | putStringXY(7, 2, "\u2588\u2588\u2588", attr); |
3649b921 | 524 | attr.setForeColor(Color.WHITE); |
a51c7937 | 525 | putStringXY(10, 2, "\u2588\u2588\u2588", attr); |
3649b921 KL |
526 | |
527 | // Draw the dot | |
528 | int dotX = getXColorPosition(color); | |
529 | int dotY = getYColorPosition(color); | |
530 | if (color.equals(Color.BLACK)) { | |
531 | // Use white-on-black for black. All other colors use | |
532 | // black-on-whatever. | |
533 | attr.reset(); | |
534 | getScreen().putCharXY(dotX, dotY, GraphicsChars.CP437[0x07], | |
535 | attr); | |
536 | } else { | |
a51c7937 KL |
537 | attr.setForeColor(color); |
538 | getScreen().putCharXY(dotX, dotY, '\u25D8', attr); | |
3649b921 KL |
539 | } |
540 | ||
541 | } | |
542 | ||
543 | /** | |
544 | * Handle keystrokes. | |
545 | * | |
546 | * @param keypress keystroke event | |
547 | */ | |
548 | @Override | |
549 | public void onKeypress(final TKeypressEvent keypress) { | |
550 | if (keypress.equals(kbRight)) { | |
551 | int dotX = getXColorPosition(color); | |
552 | int dotY = getYColorPosition(color); | |
553 | if (dotX < 10) { | |
554 | dotX += 3; | |
555 | } | |
556 | color = getColorFromPosition(dotX, dotY); | |
557 | } else if (keypress.equals(kbLeft)) { | |
558 | int dotX = getXColorPosition(color); | |
559 | int dotY = getYColorPosition(color); | |
560 | if (dotX > 3) { | |
561 | dotX -= 3; | |
562 | } | |
563 | color = getColorFromPosition(dotX, dotY); | |
564 | } else if (keypress.equals(kbUp)) { | |
565 | int dotX = getXColorPosition(color); | |
566 | int dotY = getYColorPosition(color); | |
567 | if (dotY == 2) { | |
568 | dotY--; | |
569 | } | |
570 | color = getColorFromPosition(dotX, dotY); | |
571 | } else if (keypress.equals(kbDown)) { | |
572 | int dotX = getXColorPosition(color); | |
573 | int dotY = getYColorPosition(color); | |
574 | if (dotY == 1) { | |
575 | dotY++; | |
576 | } | |
577 | color = getColorFromPosition(dotX, dotY); | |
578 | } else { | |
579 | // Pass to my parent | |
580 | super.onKeypress(keypress); | |
581 | } | |
582 | ||
583 | // Save this update to the local theme. | |
584 | ((TEditColorThemeWindow) getWindow()).saveToEditTheme(); | |
585 | } | |
586 | ||
587 | /** | |
588 | * Handle mouse press events. | |
589 | * | |
590 | * @param mouse mouse button press event | |
591 | */ | |
592 | @Override | |
593 | public void onMouseDown(final TMouseEvent mouse) { | |
594 | if (mouse.isMouseWheelUp()) { | |
595 | // Do this like kbUp | |
596 | int dotX = getXColorPosition(color); | |
597 | int dotY = getYColorPosition(color); | |
598 | if (dotY == 2) { | |
599 | dotY--; | |
600 | } | |
601 | color = getColorFromPosition(dotX, dotY); | |
602 | } else if (mouse.isMouseWheelDown()) { | |
603 | // Do this like kbDown | |
604 | int dotX = getXColorPosition(color); | |
605 | int dotY = getYColorPosition(color); | |
606 | if (dotY == 1) { | |
607 | dotY++; | |
608 | } | |
609 | color = getColorFromPosition(dotX, dotY); | |
610 | return; | |
611 | } else if ((mouse.getX() > 0) | |
612 | && (mouse.getX() < getWidth() - 1) | |
613 | && (mouse.getY() > 0) | |
614 | && (mouse.getY() < getHeight() - 1) | |
615 | ) { | |
616 | color = getColorFromPosition(mouse.getX(), mouse.getY()); | |
617 | } else { | |
618 | // Let parent class handle it. | |
619 | super.onMouseDown(mouse); | |
620 | return; | |
621 | } | |
622 | ||
623 | // Save this update to the local theme. | |
624 | ((TEditColorThemeWindow) getWindow()).saveToEditTheme(); | |
625 | } | |
626 | ||
627 | } | |
628 | ||
d36057df KL |
629 | // ------------------------------------------------------------------------ |
630 | // Constructors ----------------------------------------------------------- | |
631 | // ------------------------------------------------------------------------ | |
3649b921 KL |
632 | |
633 | /** | |
634 | * Public constructor. The file open box will be centered on screen. | |
635 | * | |
636 | * @param application the TApplication that manages this window | |
637 | */ | |
638 | public TEditColorThemeWindow(final TApplication application) { | |
639 | ||
640 | // Register with the TApplication | |
339652cc | 641 | super(application, i18n.getString("windowTitle"), 0, 0, 60, 18, MODAL); |
3649b921 KL |
642 | |
643 | // Initialize with the first color | |
644 | List<String> colors = getTheme().getColorNames(); | |
645 | assert (colors.size() > 0); | |
646 | editTheme = new ColorTheme(); | |
647 | for (String key: colors) { | |
648 | CellAttributes attr = new CellAttributes(); | |
649 | attr.setTo(getTheme().getColor(key)); | |
650 | editTheme.setColor(key, attr); | |
651 | } | |
652 | ||
653 | colorNames = addList(colors, 2, 2, 38, getHeight() - 7, | |
654 | new TAction() { | |
655 | // When the user presses Enter | |
656 | public void DO() { | |
657 | refreshFromTheme(colorNames.getSelected()); | |
658 | } | |
659 | }, | |
660 | new TAction() { | |
661 | // When the user navigates with keyboard | |
662 | public void DO() { | |
663 | refreshFromTheme(colorNames.getSelected()); | |
664 | } | |
665 | } | |
666 | ); | |
667 | foreground = new ForegroundPicker(this, 42, 1, 14, 6); | |
668 | background = new BackgroundPicker(this, 42, 7, 14, 4); | |
669 | refreshFromTheme(colors.get(0)); | |
670 | colorNames.setSelectedIndex(0); | |
671 | ||
339652cc | 672 | addButton(i18n.getString("okButton"), getWidth() - 37, getHeight() - 4, |
3649b921 KL |
673 | new TAction() { |
674 | public void DO() { | |
675 | ColorTheme global = getTheme(); | |
676 | List<String> colors = editTheme.getColorNames(); | |
677 | for (String key: colors) { | |
678 | CellAttributes attr = new CellAttributes(); | |
679 | attr.setTo(editTheme.getColor(key)); | |
680 | global.setColor(key, attr); | |
681 | } | |
682 | getApplication().closeWindow(TEditColorThemeWindow.this); | |
683 | } | |
684 | } | |
685 | ); | |
686 | ||
339652cc KL |
687 | addButton(i18n.getString("cancelButton"), getWidth() - 25, |
688 | getHeight() - 4, | |
3649b921 KL |
689 | new TAction() { |
690 | public void DO() { | |
691 | getApplication().closeWindow(TEditColorThemeWindow.this); | |
692 | } | |
693 | } | |
694 | ); | |
695 | ||
696 | // Default to the color list | |
697 | activate(colorNames); | |
698 | ||
2ce6dab2 | 699 | // Add shortcut text |
339652cc | 700 | newStatusBar(i18n.getString("statusBar")); |
3649b921 KL |
701 | } |
702 | ||
d36057df KL |
703 | // ------------------------------------------------------------------------ |
704 | // Event handlers --------------------------------------------------------- | |
705 | // ------------------------------------------------------------------------ | |
706 | ||
707 | /** | |
708 | * Handle keystrokes. | |
709 | * | |
710 | * @param keypress keystroke event | |
711 | */ | |
712 | @Override | |
713 | public void onKeypress(final TKeypressEvent keypress) { | |
714 | // Escape - behave like cancel | |
715 | if (keypress.equals(kbEsc)) { | |
716 | getApplication().closeWindow(this); | |
717 | return; | |
718 | } | |
719 | ||
720 | // Pass to my parent | |
721 | super.onKeypress(keypress); | |
722 | } | |
723 | ||
724 | // ------------------------------------------------------------------------ | |
725 | // TWindow ---------------------------------------------------------------- | |
726 | // ------------------------------------------------------------------------ | |
727 | ||
3649b921 KL |
728 | /** |
729 | * Draw me on screen. | |
730 | */ | |
731 | @Override | |
732 | public void draw() { | |
733 | super.draw(); | |
734 | CellAttributes attr = new CellAttributes(); | |
735 | ||
736 | // Draw the label on colorNames | |
737 | attr.setTo(getTheme().getColor("twindow.background.modal")); | |
738 | if (colorNames.isActive()) { | |
739 | attr.setForeColor(getTheme().getColor("tlabel").getForeColor()); | |
740 | attr.setBold(getTheme().getColor("tlabel").isBold()); | |
741 | } | |
339652cc | 742 | getScreen().putStringXY(3, 2, i18n.getString("colorName"), attr); |
3649b921 KL |
743 | |
744 | // Draw the sample text box | |
745 | attr.reset(); | |
746 | attr.setForeColor(foreground.color); | |
747 | attr.setBold(foreground.bold); | |
748 | attr.setBackColor(background.color); | |
749 | getScreen().putStringXY(getWidth() - 17, getHeight() - 6, | |
339652cc | 750 | i18n.getString("textTextText"), attr); |
3649b921 | 751 | getScreen().putStringXY(getWidth() - 17, getHeight() - 5, |
339652cc | 752 | i18n.getString("textTextText"), attr); |
3649b921 KL |
753 | } |
754 | ||
d36057df KL |
755 | // ------------------------------------------------------------------------ |
756 | // TEditColorThemeWindow -------------------------------------------------- | |
757 | // ------------------------------------------------------------------------ | |
758 | ||
3649b921 | 759 | /** |
d36057df | 760 | * Set various widgets/values to the editing theme color. |
3649b921 | 761 | * |
d36057df | 762 | * @param colorName name of color from theme |
3649b921 | 763 | */ |
d36057df KL |
764 | private void refreshFromTheme(final String colorName) { |
765 | CellAttributes attr = editTheme.getColor(colorName); | |
766 | foreground.color = attr.getForeColor(); | |
767 | foreground.bold = attr.isBold(); | |
768 | background.color = attr.getBackColor(); | |
769 | } | |
770 | ||
771 | /** | |
772 | * Examines foreground, background, and colorNames and sets the color in | |
773 | * editTheme. | |
774 | */ | |
775 | private void saveToEditTheme() { | |
776 | String colorName = colorNames.getSelected(); | |
777 | if (colorName == null) { | |
3649b921 KL |
778 | return; |
779 | } | |
d36057df KL |
780 | CellAttributes attr = editTheme.getColor(colorName); |
781 | attr.setForeColor(foreground.color); | |
782 | attr.setBold(foreground.bold); | |
783 | attr.setBackColor(background.color); | |
784 | editTheme.setColor(colorName, attr); | |
3649b921 KL |
785 | } |
786 | ||
787 | } |