VKBImplementation.java
上传用户:haobig99
上传日期:2022-06-15
资源大小:369k
文件大小:9k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * Copyright 2008 Sun Microsystems, Inc.  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.  Sun designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or
  23.  * have any questions.
  24.  */
  25. package com.sun.lwuit.impl.midp;
  26. import com.sun.lwuit.Font;
  27. import com.sun.lwuit.TextField;
  28. import com.sun.lwuit.events.ActionEvent;
  29. import com.sun.lwuit.events.ActionListener;
  30. import com.sun.lwuit.plaf.Border;
  31. import com.sun.lwuit.plaf.Style;
  32. import com.sun.lwuit.plaf.UIManager;
  33. import java.util.Hashtable;
  34. /**
  35.  * An implementation of LWUIT that features a Light Weight Virtual Keyboard.
  36.  *
  37.  * @author Chen Fishbein
  38.  */
  39. public class VKBImplementation extends GameCanvasImplementation{
  40.     
  41.     private static Class vkbClass = VirtualKeyboard.class;
  42.     
  43.     private VirtualKeyboard vkb;
  44.     public void init(Object m) {
  45.         super.init(m);         
  46.         //installs a listener on the UIManager that updates the default styles
  47.         //for the Virtual Keyboard
  48.         UIManager.getInstance().addThemeRefreshListener(new ActionListener() {
  49.             public void actionPerformed(ActionEvent evt) {
  50.                 Hashtable themeProps = new Hashtable();
  51.                 themeProps.put("VKB.bgColor", "666666");
  52.                 themeProps.put("VKBtooltip.padding", "8,8,8,8");
  53.                 themeProps.put("VKBtooltip.font",
  54.                         Font.createSystemFont(Font.FACE_SYSTEM,
  55.                         Font.STYLE_BOLD, Font.SIZE_LARGE));
  56.                 themeProps.put("VKBtooltip.bgColor", "FFFFFF");
  57.                 themeProps.put("VKBtooltip.fgColor", "0");
  58.                 themeProps.put("VKBtooltip.border", Border.createRoundBorder(8, 8));
  59.                 themeProps.put("VKBButton.fgColor", "FFFFFF");
  60.                 themeProps.put("VKBButton.bgColor", "0");
  61.                 themeProps.put("VKBButton.sel#fgColor", "FFFFFF");
  62.                 themeProps.put("VKBButton.sel#bgColor", "0");
  63.                 themeProps.put("VKBButton.press#fgColor", "FFFFFF");
  64.                 themeProps.put("VKBButton.press#bgColor", "0");
  65.                 themeProps.put("VKBButton.border", Border.createRoundBorder(8, 8));
  66.                 themeProps.put("VKBButton.sel#border", Border.createRoundBorder(8, 8));
  67.                 themeProps.put("VKBButton.press#border", Border.createRoundBorder(8, 8));
  68. //                themeProps.put("VKBButton.bgType", new Byte(Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL));
  69.                 themeProps.put("VKBButton.sel#bgType", new Byte(Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL));
  70.                 themeProps.put("VKBButton.press#bgType", new Byte(Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL));
  71. //                themeProps.put("VKBButton.bgGradient", new Object[]{new Integer(0x666666),
  72. //                            new Integer(0), new Float(0), new Float(0), new Float(0)
  73. //                        });
  74.                 themeProps.put("VKBButton.sel#bgGradient", new Object[]{new Integer(0x666666),
  75.                             new Integer(0), new Float(0), new Float(0), new Float(0)
  76.                         });
  77.                 themeProps.put("VKBButton.press#bgGradient", new Object[]{new Integer(0),
  78.                             new Integer(0x666666), new Float(0), new Float(0), new Float(0)
  79.                         });
  80.                 themeProps.put("VKBButton.margin", "2,2,1,1");
  81.                 themeProps.put("VKBButton.sel#margin", "2,2,1,1");
  82.                 themeProps.put("VKBButton.press#margin", "2,2,1,1");
  83.                 themeProps.put("VKBButton.padding", "6,6,4,4");
  84.                 themeProps.put("VKBButton.sel#padding", "6,6,4,4");
  85.                 themeProps.put("VKBButton.press#padding", "6,6,4,4");
  86.                 themeProps.put("VKBButton.font",
  87.                         Font.createSystemFont(Font.FACE_SYSTEM,
  88.                         Font.STYLE_BOLD, Font.SIZE_MEDIUM));
  89.                 themeProps.put("VKBButton.sel#font",
  90.                         Font.createSystemFont(Font.FACE_SYSTEM,
  91.                         Font.STYLE_BOLD, Font.SIZE_MEDIUM));
  92.                 themeProps.put("VKBButton.press#font",
  93.                         Font.createSystemFont(Font.FACE_SYSTEM,
  94.                         Font.STYLE_BOLD, Font.SIZE_MEDIUM));
  95.                 themeProps.put("VKBSpecialButton.fgColor", "FFFFFF");
  96.                 themeProps.put("VKBSpecialButton.bgColor", "0");
  97.                 themeProps.put("VKBSpecialButton.sel#fgColor", "FFFFFF");
  98.                 themeProps.put("VKBSpecialButton.sel#bgColor", "0");
  99.                 themeProps.put("VKBSpecialButton.press#fgColor", "FFFFFF");
  100.                 themeProps.put("VKBSpecialButton.press#bgColor", "0");
  101.                 themeProps.put("VKBSpecialButton.border", Border.createRoundBorder(8, 8));
  102.                 themeProps.put("VKBSpecialButton.sel#border", Border.createRoundBorder(8, 8));
  103.                 themeProps.put("VKBSpecialButton.press#border", Border.createRoundBorder(8, 8));
  104.                 themeProps.put("VKBSpecialButton.bgType", new Byte(Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL));
  105.                 themeProps.put("VKBSpecialButton.sel#bgType", new Byte(Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL));
  106.                 themeProps.put("VKBSpecialButton.press#bgType", new Byte(Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL));
  107.                 themeProps.put("VKBSpecialButton.bgGradient", new Object[]{new Integer(0xcccccc),
  108.                             new Integer(0x666666), new Float(0), new Float(0), new Float(0)
  109.                         });
  110.                 themeProps.put("VKBSpecialButton.sel#bgGradient", new Object[]{new Integer(0xcccccc),
  111.                             new Integer(0x666666), new Float(0), new Float(0), new Float(0)
  112.                         });
  113.                 themeProps.put("VKBSpecialButton.press#bgGradient", new Object[]{new Integer(0x666666),
  114.                             new Integer(0xcccccc), new Float(0), new Float(0), new Float(0)
  115.                         });
  116.                 themeProps.put("VKBSpecialButton.margin", "2,2,1,1");
  117.                 themeProps.put("VKBSpecialButton.sel#margin", "2,2,1,1");
  118.                 themeProps.put("VKBSpecialButton.press#margin", "2,2,1,1");
  119.                 themeProps.put("VKBSpecialButton.padding", "6,6,4,4");
  120.                 themeProps.put("VKBSpecialButton.sel#padding", "6,6,4,4");
  121.                 themeProps.put("VKBSpecialButton.press#padding", "6,6,4,4");
  122.                 themeProps.put("VKBSpecialButton.font",
  123.                         Font.createSystemFont(Font.FACE_SYSTEM,
  124.                         Font.STYLE_BOLD, Font.SIZE_MEDIUM));
  125.                 themeProps.put("VKBSpecialButton.sel#font",
  126.                         Font.createSystemFont(Font.FACE_SYSTEM,
  127.                         Font.STYLE_BOLD, Font.SIZE_MEDIUM));
  128.                 themeProps.put("VKBSpecialButton.press#font",
  129.                         Font.createSystemFont(Font.FACE_SYSTEM,
  130.                         Font.STYLE_BOLD, Font.SIZE_MEDIUM));
  131.                 themeProps.put("VKBTextInput.sel#fgColor", "FFFFFF");
  132.                 themeProps.put("VKBTextInput.sel#bgColor", "0");
  133.                 themeProps.put("VKBTextInput.sel#font",
  134.                         Font.createSystemFont(Font.FACE_SYSTEM,
  135.                         Font.STYLE_BOLD, Font.SIZE_MEDIUM));
  136.                 themeProps.put("VKBTextInput.sel#border",
  137.                         Border.getDefaultBorder());
  138.                 UIManager.getInstance().addThemeProps(themeProps);
  139.             }
  140.         });
  141.     }
  142.     
  143.     /**
  144.      * @inheritDoc
  145.      */
  146.     public boolean isVirtualKeyboardShowing() {
  147.         return vkb != null && vkb.isVisible();
  148.     }
  149.     /**
  150.      * @inheritDoc
  151.      */
  152.     public boolean isVirtualKeyboardShowingSupported() {
  153.         return true;
  154.     }
  155.     /**
  156.      * @inheritDoc
  157.      */
  158.     public void setShowVirtualKeyboard(boolean show) {
  159.         if (show) {
  160.             TextField txtCmp = (TextField) getCurrentForm().getFocused();
  161.             if (txtCmp != null) {
  162.                 vkb = createVirtualKeyboard();
  163.                 vkb.setTextField(txtCmp);
  164.                 vkb.showDialog();
  165.             }
  166.         }
  167.     }
  168.     /**
  169.      * Sets the virtual keyboard class.
  170.      * 
  171.      * @param vkbClazz this class must extend VirtualKeyboard.
  172.      */
  173.     static void setVirtualKeyboardClass(Class vkbClazz){
  174.         vkbClass = vkbClazz;
  175.     }
  176.     
  177.     private VirtualKeyboard createVirtualKeyboard() {
  178.         try {
  179.             return (VirtualKeyboard) vkbClass.newInstance();
  180.         } catch (Exception ex) {
  181.             ex.printStackTrace();
  182.             return new VirtualKeyboard();
  183.         } 
  184.     }
  185.     
  186.     
  187. }