1 /*
   2  * Copyright (c) 1996, 2014, 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 package sun.awt.windows;
  26 
  27 import java.awt.*;
  28 import java.awt.peer.*;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.KeyEvent;
  31 
  32 final class WButtonPeer extends WComponentPeer implements ButtonPeer {
  33 
  34     static {
  35         initIDs();
  36     }
  37 
  38     // ComponentPeer overrides
  39 
  40     @Override
  41     public Dimension getMinimumSize() {
  42         FontMetrics fm = getFontMetrics(((Button)target).getFont());
  43         String label = ((Button)target).getLabel();
  44         if ( label == null ) {
  45             label = "";
  46         }
  47         return new Dimension(fm.stringWidth(label) + 14,
  48                              fm.getHeight() + 8);
  49     }
  50     @Override
  51     public boolean isFocusable() {
  52         return true;
  53     }
  54 
  55     // ButtonPeer implementation
  56 
  57     @Override
  58     public native void setLabel(String label);
  59 
  60     // Toolkit & peer internals
  61 
  62     WButtonPeer(Button target) {
  63         super(target);
  64     }
  65 
  66     @Override
  67     native void create(WComponentPeer peer);
  68 
  69     // native callbacks
  70 
  71     // NOTE: This is called on the privileged toolkit thread. Do not
  72     //       call directly into user code using this thread!
  73     public void handleAction(final long when, final int modifiers) {
  74         // Fixed 5064013: the InvocationEvent time should be equals
  75         // the time of the ActionEvent
  76         WToolkit.executeOnEventHandlerThread(target, new Runnable() {
  77             @Override
  78             public void run() {
  79                 postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
  80                                           ((Button)target).getActionCommand(),
  81                                           when, modifiers));
  82             }
  83         }, when);
  84     }
  85 
  86 
  87     @Override
  88     public boolean shouldClearRectBeforePaint() {
  89         return false;
  90     }
  91 
  92     /**
  93      * Initialize JNI field and method IDs
  94      */
  95     private static native void initIDs();
  96 
  97     @Override
  98     public boolean handleJavaKeyEvent(KeyEvent e) {
  99          switch (e.getID()) {
 100             case KeyEvent.KEY_RELEASED:
 101                 if (e.getKeyCode() == KeyEvent.VK_SPACE){
 102                     handleAction(e.getWhen(), e.getModifiers());
 103                 }
 104             break;
 105          }
 106          return false;
 107     }
 108 }