modules/controls/src/main/java/javafx/scene/control/skin/ColorPalette.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.scene.control.skin;
  27 


  28 import com.sun.javafx.scene.traversal.Algorithm;
  29 import com.sun.javafx.scene.traversal.Direction;
  30 import com.sun.javafx.scene.traversal.ParentTraversalEngine;
  31 import com.sun.javafx.scene.traversal.TraversalContext;
  32 import javafx.collections.FXCollections;
  33 import javafx.collections.ListChangeListener;
  34 import javafx.collections.ObservableList;
  35 import javafx.event.ActionEvent;
  36 import javafx.event.Event;
  37 import javafx.event.EventHandler;
  38 import javafx.geometry.Bounds;
  39 import javafx.geometry.NodeOrientation;
  40 import javafx.geometry.Pos;
  41 import javafx.geometry.Side;
  42 import javafx.scene.Node;
  43 import javafx.scene.control.ColorPicker;
  44 import javafx.scene.control.ContextMenu;
  45 import javafx.scene.control.Hyperlink;
  46 import javafx.scene.control.Label;
  47 import javafx.scene.control.MenuItem;
  48 import javafx.scene.control.PopupControl;
  49 import javafx.scene.control.Separator;
  50 import javafx.scene.control.Tooltip;
  51 import javafx.scene.input.KeyCode;
  52 import javafx.scene.input.KeyEvent;
  53 import javafx.scene.input.MouseButton;
  54 import javafx.scene.input.MouseEvent;
  55 import javafx.scene.layout.GridPane;
  56 import javafx.scene.layout.Region;
  57 import javafx.scene.layout.StackPane;
  58 import javafx.scene.layout.VBox;
  59 import javafx.scene.paint.Color;
  60 import javafx.scene.shape.Rectangle;
  61 import javafx.scene.shape.StrokeType;
  62 
  63 import java.util.List;
  64 
  65 import static com.sun.javafx.scene.control.skin.ColorPickerSkin.getString;
  66 
  67 public class ColorPalette extends Region {

  68 
  69     private static final int SQUARE_SIZE = 15;
  70 
  71     // package protected for testing purposes
  72     ColorPickerGrid colorPickerGrid;
  73     final Hyperlink customColorLink = new Hyperlink(getString("customColorLink"));
  74     CustomColorDialog customColorDialog = null;
  75 
  76     private ColorPicker colorPicker;
  77     private final GridPane customColorGrid = new GridPane();
  78     private final Separator separator = new Separator();
  79     private final Label customColorLabel = new Label(getString("customColorLabel"));
  80 
  81     private PopupControl popupControl;
  82     private ColorSquare focusedSquare;
  83     private ContextMenu contextMenu = null;
  84     
  85     private Color mouseDragColor = null;
  86     private boolean dragDetected = false;
  87 
  88     // Metrics for custom colors
  89     private int customColorNumber = 0;
  90     private int customColorRows = 0;
  91     private int customColorLastRowLength = 0;
  92 
  93     private final ColorSquare hoverSquare = new ColorSquare();
  94     
  95     public ColorPalette(final ColorPicker colorPicker) {
  96         getStyleClass().add("color-palette-region");
  97         this.colorPicker = colorPicker;
  98         colorPickerGrid = new ColorPickerGrid();
  99         colorPickerGrid.getChildren().get(0).requestFocus();


 188         hoverSquare.setLayoutY(snapPosition(y) - focusedSquare.getHeight() / 2.0 + (hoverSquare.getScaleY() == 1.0 ? 0 : focusedSquare.getHeight() / 4.0));
 189     }
 190     
 191     private void buildCustomColors() {
 192         final ObservableList<Color> customColors = colorPicker.getCustomColors();
 193         customColorNumber = customColors.size();
 194 
 195         customColorGrid.getChildren().clear();
 196         if (customColors.isEmpty()) {
 197             customColorLabel.setVisible(false);
 198             customColorLabel.setManaged(false);
 199             customColorGrid.setVisible(false);
 200             customColorGrid.setManaged(false);
 201             return;
 202         } else {
 203             customColorLabel.setVisible(true);
 204             customColorLabel.setManaged(true);
 205             customColorGrid.setVisible(true);
 206             customColorGrid.setManaged(true);
 207             if (contextMenu == null) {
 208                 MenuItem item = new MenuItem(getString("removeColor"));
 209                 item.setOnAction(e -> {
 210                     ColorSquare square = (ColorSquare)contextMenu.getOwnerNode();
 211                     customColors.remove(square.rectangle.getFill());
 212                     buildCustomColors();
 213                 });
 214                 contextMenu = new ContextMenu(item);
 215             }
 216         }
 217 
 218         int customColumnIndex = 0;
 219         int customRowIndex = 0;
 220         int remainingSquares = customColors.size() % NUM_OF_COLUMNS;
 221         int numEmpty = (remainingSquares == 0) ? 0 : NUM_OF_COLUMNS - remainingSquares;
 222         customColorLastRowLength = remainingSquares == 0 ? 12 : remainingSquares;
 223 
 224         for (int i = 0; i < customColors.size(); i++) {
 225             Color c = customColors.get(i);
 226             ColorSquare square = new ColorSquare(c, i, true);
 227             square.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
 228                 if (e.getCode() == KeyCode.DELETE) {




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control.skin;
  27 
  28 import com.sun.javafx.scene.control.CustomColorDialog;
  29 import com.sun.javafx.scene.control.skin.Utils;
  30 import com.sun.javafx.scene.traversal.Algorithm;
  31 import com.sun.javafx.scene.traversal.Direction;
  32 import com.sun.javafx.scene.traversal.ParentTraversalEngine;
  33 import com.sun.javafx.scene.traversal.TraversalContext;
  34 import javafx.collections.FXCollections;
  35 import javafx.collections.ListChangeListener;
  36 import javafx.collections.ObservableList;
  37 import javafx.event.ActionEvent;
  38 import javafx.event.Event;
  39 import javafx.event.EventHandler;
  40 import javafx.geometry.Bounds;
  41 import javafx.geometry.NodeOrientation;
  42 import javafx.geometry.Pos;
  43 import javafx.geometry.Side;
  44 import javafx.scene.Node;
  45 import javafx.scene.control.ColorPicker;
  46 import javafx.scene.control.ContextMenu;
  47 import javafx.scene.control.Hyperlink;
  48 import javafx.scene.control.Label;
  49 import javafx.scene.control.MenuItem;
  50 import javafx.scene.control.PopupControl;
  51 import javafx.scene.control.Separator;
  52 import javafx.scene.control.Tooltip;
  53 import javafx.scene.input.KeyCode;
  54 import javafx.scene.input.KeyEvent;
  55 import javafx.scene.input.MouseButton;
  56 import javafx.scene.input.MouseEvent;
  57 import javafx.scene.layout.GridPane;
  58 import javafx.scene.layout.Region;
  59 import javafx.scene.layout.StackPane;
  60 import javafx.scene.layout.VBox;
  61 import javafx.scene.paint.Color;
  62 import javafx.scene.shape.Rectangle;
  63 import javafx.scene.shape.StrokeType;
  64 
  65 import java.util.List;
  66 
  67 import static com.sun.javafx.scene.control.Properties.getColorPickerString;
  68 
  69 // Not public API - this is (presently) an implementation detail only
  70 class ColorPalette extends Region {
  71 
  72     private static final int SQUARE_SIZE = 15;
  73 
  74     // package protected for testing purposes
  75     ColorPickerGrid colorPickerGrid;
  76     final Hyperlink customColorLink = new Hyperlink(getColorPickerString("customColorLink"));
  77     CustomColorDialog customColorDialog = null;
  78 
  79     private ColorPicker colorPicker;
  80     private final GridPane customColorGrid = new GridPane();
  81     private final Separator separator = new Separator();
  82     private final Label customColorLabel = new Label(getColorPickerString("customColorLabel"));
  83 
  84     private PopupControl popupControl;
  85     private ColorSquare focusedSquare;
  86     private ContextMenu contextMenu = null;
  87     
  88     private Color mouseDragColor = null;
  89     private boolean dragDetected = false;
  90 
  91     // Metrics for custom colors
  92     private int customColorNumber = 0;
  93     private int customColorRows = 0;
  94     private int customColorLastRowLength = 0;
  95 
  96     private final ColorSquare hoverSquare = new ColorSquare();
  97     
  98     public ColorPalette(final ColorPicker colorPicker) {
  99         getStyleClass().add("color-palette-region");
 100         this.colorPicker = colorPicker;
 101         colorPickerGrid = new ColorPickerGrid();
 102         colorPickerGrid.getChildren().get(0).requestFocus();


 191         hoverSquare.setLayoutY(snapPosition(y) - focusedSquare.getHeight() / 2.0 + (hoverSquare.getScaleY() == 1.0 ? 0 : focusedSquare.getHeight() / 4.0));
 192     }
 193     
 194     private void buildCustomColors() {
 195         final ObservableList<Color> customColors = colorPicker.getCustomColors();
 196         customColorNumber = customColors.size();
 197 
 198         customColorGrid.getChildren().clear();
 199         if (customColors.isEmpty()) {
 200             customColorLabel.setVisible(false);
 201             customColorLabel.setManaged(false);
 202             customColorGrid.setVisible(false);
 203             customColorGrid.setManaged(false);
 204             return;
 205         } else {
 206             customColorLabel.setVisible(true);
 207             customColorLabel.setManaged(true);
 208             customColorGrid.setVisible(true);
 209             customColorGrid.setManaged(true);
 210             if (contextMenu == null) {
 211                 MenuItem item = new MenuItem(getColorPickerString("removeColor"));
 212                 item.setOnAction(e -> {
 213                     ColorSquare square = (ColorSquare)contextMenu.getOwnerNode();
 214                     customColors.remove(square.rectangle.getFill());
 215                     buildCustomColors();
 216                 });
 217                 contextMenu = new ContextMenu(item);
 218             }
 219         }
 220 
 221         int customColumnIndex = 0;
 222         int customRowIndex = 0;
 223         int remainingSquares = customColors.size() % NUM_OF_COLUMNS;
 224         int numEmpty = (remainingSquares == 0) ? 0 : NUM_OF_COLUMNS - remainingSquares;
 225         customColorLastRowLength = remainingSquares == 0 ? 12 : remainingSquares;
 226 
 227         for (int i = 0; i < customColors.size(); i++) {
 228             Color c = customColors.get(i);
 229             ColorSquare square = new ColorSquare(c, i, true);
 230             square.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
 231                 if (e.getCode() == KeyCode.DELETE) {