1 /*
   2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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;
  27 
  28 import javafx.collections.FXCollections;
  29 import javafx.collections.ObservableList;
  30 import javafx.scene.paint.Color;
  31 import javafx.scene.control.skin.ColorPickerSkin;
  32 
  33 /**
  34  * <p>ColorPicker control allows the user to select a color from either a standard
  35  * palette of colors with a simple one click selection OR define their own custom color.
  36  *
  37  * <p>The {@link #valueProperty() value} is the currently selected {@link javafx.scene.paint.Color}.
  38  * An initial color can be set by calling setColor or via the constructor. If nothing
  39  * is specified, a default initial color is used.
  40  *
  41  * <p>The ColorPicker control provides a color palette with a predefined set of colors. If
  42  * the user does not want to choose from the predefined set, they can create a custom
  43  * color by interacting with a custom color dialog. This dialog provides RGB,
  44  * HSB and Web modes of interaction, to create new colors. It also lets the opacity
  45  * of the color to be modified.
  46  *
  47  * <p>Once a new color is defined, users can choose whether they want to save it
  48  * or just use it. If the new color is saved, this color will then appear in the
  49  * custom colors area on the color palette. Also {@link #getCustomColors() getCustomColors}
  50  * returns the list of saved custom colors.
  51  *
  52  * <p>The {@link #promptTextProperty() promptText} is not supported and hence is a no-op.
  53  * But it may be supported in the future.
  54  *
  55  * <pre><code>
  56  * final ColorPicker colorPicker = new ColorPicker();
  57  * colorPicker.setOnAction(new EventHandler() {
  58  *     public void handle(Event t) {
  59  *         Color c = colorPicker.getValue();
  60  *         System.out.println("New Color's RGB = "+c.getRed()+" "+c.getGreen()+" "+c.getBlue());
  61  *     }
  62  * });
  63  * </code></pre>
  64  *
  65  * <p>The ColorPicker control's appearance can be styled in three ways: a simple Button mode,
  66  * MenuButton mode or SplitMenuButton mode. The default is MenuButton mode.
  67  * For a Button like appearance the style class to use is {@link #STYLE_CLASS_BUTTON STYLE_CLASS_BUTTON}
  68  * and for SplitMenuButton appearance and behavior, the style class to use is
  69  * {@link #STYLE_CLASS_SPLIT_BUTTON STYLE_CLASS_SPLIT_BUTTON}.
  70  *
  71  * <pre><code>
  72  * colorPicker.getStyleClass().add("button");
  73  * </code></pre>
  74  * or
  75  * <pre><code>
  76  * colorPicker.getStyleClass().add("split-button");
  77  * </pre><code>
  78  * @since JavaFX 2.2
  79  */
  80 public class ColorPicker extends ComboBoxBase<Color> {
  81 
  82     /**
  83      * The style class to specify a Button like appearance of ColorPicker control.
  84      */
  85     public static final String STYLE_CLASS_BUTTON = "button";
  86 
  87     /**
  88      * The style class to specify a SplitMenuButton like appearance of ColorPicker control.
  89      */
  90     public static final String STYLE_CLASS_SPLIT_BUTTON = "split-button";
  91 
  92     /**
  93      * The custom colors added to the Color Palette by the user.
  94      */
  95     private ObservableList<Color> customColors = FXCollections.<Color>observableArrayList();
  96     /**
  97      * Gets the list of custom colors added to the Color Palette by the user.
  98      */
  99     public final ObservableList<Color>  getCustomColors() {
 100         return customColors;
 101     }
 102 
 103     /**
 104      * Creates a default ColorPicker instance with a selected color set to white.
 105      */
 106     public ColorPicker() {
 107         this(Color.WHITE);
 108     }
 109 
 110     /**
 111      * Creates a ColorPicker instance and sets the selected color to the given color.
 112      * @param color to be set as the currently selected color of the ColorPicker.
 113      */
 114     public ColorPicker(Color color) {
 115         setValue(color);
 116         getStyleClass().add(DEFAULT_STYLE_CLASS);
 117     }
 118 
 119     /***************************************************************************
 120      *                                                                         *
 121      * Methods                                                                 *
 122      *                                                                         *
 123      **************************************************************************/
 124 
 125     /** {@inheritDoc} */
 126     @Override protected Skin<?> createDefaultSkin() {
 127         return new ColorPickerSkin(this);
 128     }
 129 
 130     /***************************************************************************
 131      *                                                                         *
 132      * Stylesheet Handling                                                     *
 133      *                                                                         *
 134      **************************************************************************/
 135 
 136     private static final String DEFAULT_STYLE_CLASS = "color-picker";
 137 }