1 /*
   2  * Copyright (c) 2016, 2018, 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 com.sun.webkit;
  27 
  28 import com.sun.javafx.scene.control.CustomColorDialog;
  29 import javafx.scene.paint.Color;
  30 import javafx.scene.web.WebView;
  31 
  32 public final class ColorChooser {
  33     /**
  34      * Color conversion from double (0.0 to 1.0) to uchar (0 to 255)
  35      */
  36     private static final double COLOR_DOUBLE_TO_UCHAR_FACTOR = 255.0;
  37 
  38     /**
  39      * Color chooser dialog
  40      */
  41     private final CustomColorDialog colorChooserDialog;
  42 
  43     /**
  44      * Handle / pointer to native object
  45      */
  46     private final long pdata;
  47 
  48     private ColorChooser(WebPage webPage, Color color, long data) {
  49         this.pdata = data;
  50 
  51         WebPageClient<WebView> client = webPage.getPageClient();
  52         assert (client != null);
  53         colorChooserDialog = new CustomColorDialog(client.getContainer().getScene().getWindow());
  54         colorChooserDialog.setSaveBtnToOk();
  55         colorChooserDialog.setShowUseBtn(false);
  56         colorChooserDialog.setShowOpacitySlider(false);
  57 
  58         colorChooserDialog.setOnSave(() -> {
  59             twkSetSelectedColor(pdata,
  60                     (int) Math.round(colorChooserDialog.getCustomColor().getRed() * COLOR_DOUBLE_TO_UCHAR_FACTOR),
  61                     (int) Math.round(colorChooserDialog.getCustomColor().getGreen() * COLOR_DOUBLE_TO_UCHAR_FACTOR),
  62                     (int) Math.round(colorChooserDialog.getCustomColor().getBlue() * COLOR_DOUBLE_TO_UCHAR_FACTOR));
  63         });
  64 
  65         colorChooserDialog.setCurrentColor(color);
  66         colorChooserDialog.show();
  67     }
  68 
  69     private static ColorChooser fwkCreateAndShowColorChooser(WebPage webPage, int r, int g, int b, long pdata) {
  70         return new ColorChooser(webPage, Color.rgb(r,g,b), pdata);
  71     }
  72 
  73     private void fwkShowColorChooser(int r, int g, int b) {
  74         colorChooserDialog.setCurrentColor(Color.rgb(r,g,b));
  75         colorChooserDialog.show();
  76     }
  77 
  78     private void fwkHideColorChooser() {
  79         colorChooserDialog.hide();
  80     }
  81 
  82     private native void twkSetSelectedColor(long data, int r, int g, int b);
  83 }