1
1
/*
2
- * Copyright (c) 2012, 2017 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2012, 2018 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -79,6 +79,7 @@ class ColorPalette extends Region {
79
79
CustomColorDialog customColorDialog = null ;
80
80
81
81
private ColorPicker colorPicker ;
82
+ private final GridPane standardColorGrid = new GridPane ();
82
83
private final GridPane customColorGrid = new GridPane ();
83
84
private final Separator separator = new Separator ();
84
85
private final Label customColorLabel = new Label (getColorPickerString ("customColorLabel" ));
@@ -137,6 +138,10 @@ public ColorPalette(final ColorPicker colorPicker) {
137
138
});
138
139
139
140
initNavigation ();
141
+
142
+ buildStandardColors ();
143
+ standardColorGrid .getStyleClass ().add ("color-picker-grid" );
144
+ standardColorGrid .setVisible (true );
140
145
customColorGrid .getStyleClass ().add ("color-picker-grid" );
141
146
customColorGrid .setVisible (false );
142
147
buildCustomColors ();
@@ -148,7 +153,7 @@ public ColorPalette(final ColorPicker colorPicker) {
148
153
149
154
VBox paletteBox = new VBox ();
150
155
paletteBox .getStyleClass ().add ("color-palette" );
151
- paletteBox .getChildren ().addAll (colorPickerGrid , customColorLabel , customColorGrid , separator , customColorLink );
156
+ paletteBox .getChildren ().addAll (standardColorGrid , colorPickerGrid , customColorLabel , customColorGrid , separator , customColorLink );
152
157
153
158
hoverSquare .setMouseTransparent (true );
154
159
hoverSquare .getStyleClass ().addAll ("hover-square" );
@@ -193,6 +198,35 @@ private void setFocusedSquare(ColorSquare square) {
193
198
hoverSquare .setLayoutY (snapPositionY (y ) - focusedSquare .getHeight () / 2.0 + (hoverSquare .getScaleY () == 1.0 ? 0 : focusedSquare .getHeight () / 4.0 ));
194
199
}
195
200
201
+ private void buildStandardColors () {
202
+ // WARNING:
203
+ // Make sure that the number of standard colors is equal to NUM_OF_COLUMNS
204
+ // Currently, 12 standard colors are supported in a single row
205
+ // Note : Creation & access logic of standardColorGrid needs to be updated
206
+ // in case more colors are added as separate row(s) in future.
207
+
208
+ final Color [] STANDARD_COLORS = {
209
+ Color .AQUA ,
210
+ Color .TEAL ,
211
+ Color .BLUE ,
212
+ Color .NAVY ,
213
+ Color .FUCHSIA ,
214
+ Color .PURPLE ,
215
+ Color .RED ,
216
+ Color .MAROON ,
217
+ Color .YELLOW ,
218
+ Color .OLIVE ,
219
+ Color .GREEN ,
220
+ Color .LIME
221
+ };
222
+
223
+ standardColorGrid .getChildren ().clear ();
224
+
225
+ for (int i = 0 ; i < NUM_OF_COLUMNS ; i ++) {
226
+ standardColorGrid .add (new ColorSquare (STANDARD_COLORS [i ], i , ColorType .STANDARD ), i , 0 );
227
+ }
228
+ }
229
+
196
230
private void buildCustomColors () {
197
231
final ObservableList <Color > customColors = colorPicker .getCustomColors ();
198
232
customColorNumber = customColors .size ();
@@ -228,7 +262,7 @@ private void buildCustomColors() {
228
262
229
263
for (int i = 0 ; i < customColors .size (); i ++) {
230
264
Color c = customColors .get (i );
231
- ColorSquare square = new ColorSquare (c , i , true );
265
+ ColorSquare square = new ColorSquare (c , i , ColorType . CUSTOM );
232
266
square .addEventHandler (KeyEvent .KEY_PRESSED , e -> {
233
267
if (e .getCode () == KeyCode .DELETE ) {
234
268
customColors .remove (square .rectangle .getFill ());
@@ -293,24 +327,37 @@ public Node select(Node owner, Direction dir, TraversalContext context) {
293
327
}
294
328
295
329
private Node processArrow (ColorSquare owner , Direction dir ) {
296
- final int row = owner .index / NUM_OF_COLUMNS ;
297
- final int column = owner .index % NUM_OF_COLUMNS ;
330
+ int row = 0 ;
331
+ int column = 0 ;
332
+
333
+ if (owner .colorType == ColorType .STANDARD ) {
334
+ row = 0 ;
335
+ column = owner .index ;
336
+ } else {
337
+ row = owner .index / NUM_OF_COLUMNS ;
338
+ column = owner .index % NUM_OF_COLUMNS ;
339
+ }
298
340
299
341
// Adjust the direction according to color picker orientation
300
342
dir = dir .getDirectionForNodeOrientation (colorPicker .getEffectiveNodeOrientation ());
301
343
// This returns true for all the cases which we need to override
302
- if (isAtBorder (dir , row , column , owner .isCustom )) {
344
+ if (isAtBorder (dir , row , column , ( owner .colorType == ColorType . CUSTOM ) )) {
303
345
// There's no other node in the direction from the square, so we need to continue on some other row
304
346
// or cycle
305
347
int subsequentRow = row ;
306
348
int subsequentColumn = column ;
307
- boolean subSequentSquareCustom = owner .isCustom ;
349
+ boolean subSequentSquareCustom = (owner .colorType == ColorType .CUSTOM );
350
+ boolean subSequentSquareStandard = (owner .colorType == ColorType .STANDARD );
308
351
switch (dir ) {
309
352
case LEFT :
310
353
case RIGHT :
311
354
// The next row is either the first or the last, except when cycling in custom colors, the last row
312
355
// might have different number of columns
313
- if (owner .isCustom ) {
356
+ if (owner .colorType == ColorType .STANDARD ) {
357
+ subsequentRow = 0 ;
358
+ subsequentColumn = (dir == Direction .LEFT )? NUM_OF_COLUMNS - 1 : 0 ;
359
+ }
360
+ else if (owner .colorType == ColorType .CUSTOM ) {
314
361
subsequentRow = Math .floorMod (dir == Direction .LEFT ? row - 1 : row + 1 , customColorRows );
315
362
subsequentColumn = dir == Direction .LEFT ? subsequentRow == customColorRows - 1 ?
316
363
customColorLastRowLength - 1 : NUM_OF_COLUMNS - 1 : 0 ;
@@ -320,7 +367,9 @@ private Node processArrow(ColorSquare owner, Direction dir) {
320
367
}
321
368
break ;
322
369
case UP : // custom color are not handled here
323
- subsequentRow = NUM_OF_ROWS - 1 ;
370
+ if (owner .colorType == ColorType .NORMAL && row == 0 ) {
371
+ subSequentSquareStandard = true ;
372
+ }
324
373
break ;
325
374
case DOWN : // custom color are not handled here
326
375
if (customColorNumber > 0 ) {
@@ -329,12 +378,14 @@ private Node processArrow(ColorSquare owner, Direction dir) {
329
378
subsequentColumn = customColorRows > 1 ? column : Math .min (customColorLastRowLength - 1 , column );
330
379
break ;
331
380
} else {
332
- return null ; // Let the default algorith handle this
381
+ return null ; // Let the default algorithm handle this
333
382
}
334
383
335
384
}
336
385
if (subSequentSquareCustom ) {
337
386
return customColorGrid .getChildren ().get (subsequentRow * NUM_OF_COLUMNS + subsequentColumn );
387
+ } else if (subSequentSquareStandard ) {
388
+ return standardColorGrid .getChildren ().get (subsequentColumn );
338
389
} else {
339
390
return colorPickerGrid .getChildren ().get (subsequentRow * NUM_OF_COLUMNS + subsequentColumn );
340
391
}
@@ -359,7 +410,7 @@ private boolean isAtBorder(Direction dir, int row, int column, boolean custom) {
359
410
360
411
@ Override
361
412
public Node selectFirst (TraversalContext context ) {
362
- return colorPickerGrid .getChildren ().get (0 );
413
+ return standardColorGrid .getChildren ().get (0 );
363
414
}
364
415
365
416
@ Override
@@ -386,21 +437,28 @@ public boolean isCustomColorDialogShowing() {
386
437
return false ;
387
438
}
388
439
440
+
441
+ enum ColorType {
442
+ NORMAL ,
443
+ STANDARD ,
444
+ CUSTOM
445
+ };
446
+
389
447
class ColorSquare extends StackPane {
390
448
Rectangle rectangle ;
391
449
int index ;
392
450
boolean isEmpty ;
393
- boolean isCustom ;
451
+ ColorType colorType = ColorType . NORMAL ;
394
452
395
453
public ColorSquare () {
396
- this (null , -1 , false );
454
+ this (null , -1 , ColorType . NORMAL );
397
455
}
398
456
399
457
public ColorSquare (Color color , int index ) {
400
- this (color , index , false );
458
+ this (color , index , ColorType . NORMAL );
401
459
}
402
460
403
- public ColorSquare (Color color , int index , boolean isCustom ) {
461
+ public ColorSquare (Color color , int index , ColorType type ) {
404
462
// Add style class to handle selected color square
405
463
getStyleClass ().add ("color-square" );
406
464
if (color != null ) {
@@ -429,7 +487,7 @@ public ColorSquare(Color color, int index, boolean isCustom) {
429
487
colorPicker .hide ();
430
488
} else if (event .getButton () == MouseButton .SECONDARY ||
431
489
event .getButton () == MouseButton .MIDDLE ) {
432
- if (isCustom && contextMenu != null ) {
490
+ if (( colorType == ColorType . CUSTOM ) && contextMenu != null ) {
433
491
if (!contextMenu .isShowing ()) {
434
492
contextMenu .show (ColorSquare .this , Side .RIGHT , 0 , 0 );
435
493
Utils .addMnemonics (contextMenu , ColorSquare .this .getScene (), NodeHelper .isShowMnemonics (colorPicker ));
@@ -442,7 +500,7 @@ public ColorSquare(Color color, int index, boolean isCustom) {
442
500
});
443
501
}
444
502
this .index = index ;
445
- this .isCustom = isCustom ;
503
+ this .colorType = type ;
446
504
rectangle = new Rectangle (SQUARE_SIZE , SQUARE_SIZE );
447
505
if (color == null ) {
448
506
rectangle .setFill (Color .WHITE );
@@ -477,20 +535,29 @@ public void selectColor(KeyEvent event) {
477
535
public void updateSelection (Color color ) {
478
536
setFocusedSquare (null );
479
537
480
- for (ColorSquare c : colorPickerGrid .getSquares ()) {
481
- if (c .rectangle .getFill ().equals (color )) {
482
- setFocusedSquare (c );
538
+ // Check all color grids to find ColorSquare that matches color
539
+ // if found, set focus to it
540
+
541
+ List <GridPane > gridList = List .of (standardColorGrid , colorPickerGrid ,
542
+ customColorGrid );
543
+
544
+ for (GridPane grid : gridList ) {
545
+ ColorSquare sq = findColorSquare (grid , color );
546
+ if (sq != null ) {
547
+ setFocusedSquare (sq );
483
548
return ;
484
549
}
485
550
}
486
- // check custom colors
487
- for (Node n : customColorGrid .getChildren ()) {
551
+ }
552
+
553
+ private ColorSquare findColorSquare (GridPane colorGrid , Color color ) {
554
+ for (Node n : colorGrid .getChildren ()) {
488
555
ColorSquare c = (ColorSquare ) n ;
489
556
if (c .rectangle .getFill ().equals (color )) {
490
- setFocusedSquare (c );
491
- return ;
557
+ return c ;
492
558
}
493
559
}
560
+ return null ;
494
561
}
495
562
496
563
class ColorPickerGrid extends GridPane {
0 commit comments