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.beans.property.ObjectProperty;
  29 import javafx.beans.property.ObjectPropertyBase;
  30 import javafx.beans.property.SimpleObjectProperty;
  31 import javafx.geometry.NodeOrientation;
  32 import javafx.scene.Node;
  33 import javafx.scene.control.*;
  34 
  35 import javafx.event.EventHandler;
  36 import javafx.scene.control.Control;
  37 import javafx.scene.input.KeyEvent;
  38 
  39 
  40 public class FXVK extends Control {
  41 
  42     public enum Type {
  43         TEXT,
  44         NUMERIC,
  45         EMAIL,
  46     }
  47 
  48 
  49     private final ObjectProperty<EventHandler<KeyEvent>> onAction =
  50             new SimpleObjectProperty<EventHandler<KeyEvent>>(this, "onAction");
  51     public final void setOnAction(EventHandler<KeyEvent> value) { onAction.set(value); }
  52     public final EventHandler<KeyEvent> getOnAction() { return onAction.get(); }
  53     public final ObjectProperty<EventHandler<KeyEvent>> onActionProperty() { return onAction; }
  54 
  55 
  56     final static String[] VK_TYPE_NAMES = new String[] { "text", "numeric", "url", "email" };
  57     public final static String VK_TYPE_PROP_KEY = "vkType";
  58 
  59     String[] chars;
  60 
  61     public FXVK() {
  62         setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
  63         getStyleClass().add(DEFAULT_STYLE_CLASS);
  64     }
  65 
  66     final ObjectProperty<Node> attachedNodeProperty() {
  67         if (attachedNode == null) {
  68             attachedNode = new ObjectPropertyBase<Node>() {
  69                 @Override public Object getBean() {
  70                     return FXVK.this;
  71                 }
  72 
  73                 @Override public String getName() {
  74                     return "attachedNode";
  75                 }
  76             };
  77         }
  78         return attachedNode;
  79     }
  80 
  81     private ObjectProperty<Node> attachedNode;
  82     final void setAttachedNode(Node value) { attachedNodeProperty().setValue(value); }
  83     final Node getAttachedNode() { return attachedNode == null ? null : attachedNode.getValue(); }
  84     static FXVK vk;
  85 
  86     public static void init(Node textInput) {
  87         if (vk != null) return;
  88         vk = new FXVK();
  89         FXVKSkin vkskin = new FXVKSkin(vk);
  90         vk.setSkin(vkskin);
  91         vkskin.prerender(textInput);
  92     }
  93  
  94     public static void attach(final Node textInput) {
  95 
  96         if (vk == null) {
  97             vk = new FXVK();
  98             vk.setSkin(new FXVKSkin(vk));
  99         }
 100         vk.setAttachedNode(textInput);
 101     }
 102 
 103     public static void detach() {
 104         if (vk != null) {
 105             vk.setAttachedNode(null);
 106         }
 107     }
 108 
 109     /***************************************************************************
 110      *                                                                         *
 111      * Methods                                                                 *
 112      *                                                                         *
 113      **************************************************************************/
 114 
 115     /** {@inheritDoc} */
 116     @Override protected Skin<?> createDefaultSkin() {
 117         return new FXVKSkin(this);
 118     }
 119 
 120     /***************************************************************************
 121      *                                                                         *
 122      * Stylesheet Handling                                                     *
 123      *                                                                         *
 124      **************************************************************************/
 125 
 126     private static final String DEFAULT_STYLE_CLASS = "fxvk";
 127 }
 128