1 /*
   2  * Copyright (c) 2010, 2013, 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.javafx.scene.control.skin;
  27 
  28 import javafx.application.ConditionalFeature;
  29 import javafx.application.Platform;
  30 import javafx.beans.property.ObjectProperty;
  31 import javafx.beans.property.ObjectPropertyBase;
  32 import javafx.beans.property.SimpleObjectProperty;
  33 import javafx.geometry.NodeOrientation;
  34 import javafx.scene.Node;
  35 import javafx.scene.control.*;
  36 
  37 import javafx.event.EventHandler;
  38 import javafx.scene.control.Control;
  39 import javafx.scene.input.KeyEvent;
  40 
  41 
  42 public class FXVK extends Control {
  43 
  44     public enum Type {
  45         TEXT,
  46         NUMERIC,
  47         EMAIL,
  48     }
  49 
  50 
  51     private final ObjectProperty<EventHandler<KeyEvent>> onAction =
  52             new SimpleObjectProperty<EventHandler<KeyEvent>>(this, "onAction");
  53     public final void setOnAction(EventHandler<KeyEvent> value) { onAction.set(value); }
  54     public final EventHandler<KeyEvent> getOnAction() { return onAction.get(); }
  55     public final ObjectProperty<EventHandler<KeyEvent>> onActionProperty() { return onAction; }
  56 
  57 
  58     public final static String[] VK_TYPE_NAMES = new String[] { "text", "numeric", "url", "email" };
  59     public final static String VK_TYPE_PROP_KEY = "vkType";
  60 
  61     String[] chars;
  62 
  63     public FXVK() {
  64         setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
  65         getStyleClass().add(DEFAULT_STYLE_CLASS);
  66     }
  67 
  68     final ObjectProperty<Node> attachedNodeProperty() {
  69         if (attachedNode == null) {
  70             attachedNode = new ObjectPropertyBase<Node>() {
  71                 @Override public Object getBean() {
  72                     return FXVK.this;
  73                 }
  74 
  75                 @Override public String getName() {
  76                     return "attachedNode";
  77                 }
  78             };
  79         }
  80         return attachedNode;
  81     }
  82 
  83     private ObjectProperty<Node> attachedNode;
  84     final void setAttachedNode(Node value) { attachedNodeProperty().setValue(value); }
  85     final Node getAttachedNode() { return attachedNode == null ? null : attachedNode.getValue(); }
  86     static FXVK vk;
  87 
  88     public static void init(Node textInput) {
  89         if (vk != null) return;
  90         vk = new FXVK();
  91         FXVKSkin vkskin = new FXVKSkin(vk);
  92         vk.setSkin(vkskin);
  93         vkskin.prerender(textInput);
  94     }
  95  
  96     public static void attach(final Node textInput) {
  97 
  98         if (vk == null) {
  99             vk = new FXVK();
 100             vk.setSkin(new FXVKSkin(vk));
 101         }
 102         vk.setAttachedNode(textInput);
 103     }
 104 
 105     public static void detach() {
 106         if (vk != null) {
 107             vk.setAttachedNode(null);
 108         }
 109     }
 110 
 111     private final static boolean IS_FXVK_SUPPORTED = Platform.isSupported(ConditionalFeature.VIRTUAL_KEYBOARD);
 112     private static boolean USE_FXVK = IS_FXVK_SUPPORTED;
 113 
 114     public static boolean useFXVK() {
 115         return USE_FXVK;
 116     }
 117 
 118     public static void toggleUseVK(TextInputControl textInput) {
 119         Integer vkType = (Integer)textInput.getProperties().get(VK_TYPE_PROP_KEY);
 120         if (vkType == null) {
 121             vkType = -1;
 122         }
 123         vkType++;
 124         if (vkType < 4) {
 125             USE_FXVK = true;
 126             textInput.getProperties().put(VK_TYPE_PROP_KEY, vkType);
 127             attach(textInput);
 128         } else {
 129             detach();
 130             textInput.getProperties().put(VK_TYPE_PROP_KEY, null);
 131             USE_FXVK = false;
 132         }
 133     }
 134 
 135     /***************************************************************************
 136      *                                                                         *
 137      * Methods                                                                 *
 138      *                                                                         *
 139      **************************************************************************/
 140 
 141     /** {@inheritDoc} */
 142     @Override protected Skin<?> createDefaultSkin() {
 143         return new FXVKSkin(this);
 144     }
 145 
 146     /***************************************************************************
 147      *                                                                         *
 148      * Stylesheet Handling                                                     *
 149      *                                                                         *
 150      **************************************************************************/
 151 
 152     private static final String DEFAULT_STYLE_CLASS = "fxvk";
 153 }
 154