Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11/XButtonPeer.java
32288 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.awt.X11;2526import java.awt.*;27import java.awt.peer.*;28import java.awt.event.MouseEvent;29import java.awt.event.FocusEvent;30import java.awt.event.KeyEvent;31import java.awt.event.ActionEvent;32import javax.swing.plaf.basic.*;33import javax.swing.SwingUtilities;34import javax.swing.SwingConstants;35public class XButtonPeer extends XComponentPeer implements ButtonPeer {36private boolean pressed;37private boolean armed;38private Insets focusInsets;39private Insets borderInsets;40private Insets contentAreaInsets;4142private final static String propertyPrefix = "Button" + ".";43protected Color focusColor = SystemColor.windowText;4445private boolean disposed = false;4647String label;4849protected String getPropertyPrefix() {50return propertyPrefix;51}5253void preInit(XCreateWindowParams params) {54super.preInit(params);55borderInsets = new Insets(2,2,2,2);56focusInsets = new Insets(0,0,0,0);57contentAreaInsets = new Insets(3,3,3,3);58}596061public XButtonPeer(Button target) {62super(target);63pressed = false;64armed = false;65label = target.getLabel();66updateMotifColors(getPeerBackground());67}6869public void dispose() {70synchronized (target)71{72disposed = true;73}74super.dispose();75}7677public boolean isFocusable() {78return true;79}8081@Override82public void setLabel(String label) {83if (label == null) {84label = "";85}86if (!label.equals(this.label)) {87this.label = label;88repaint();89}90}9192public void setBackground(Color c) {93updateMotifColors(c);94super.setBackground(c);95}9697void handleJavaMouseEvent(MouseEvent e) {98super.handleJavaMouseEvent(e);99int id = e.getID();100switch (id) {101case MouseEvent.MOUSE_PRESSED:102if (XToolkit.isLeftMouseButton(e) ) {103Button b = (Button) e.getSource();104105if(b.contains(e.getX(), e.getY())) {106if (!isEnabled()) {107// Disabled buttons ignore all input...108return;109}110pressed = true;111armed = true;112repaint();113}114}115116break;117118case MouseEvent.MOUSE_RELEASED:119if (XToolkit.isLeftMouseButton(e)) {120if (armed)121{122action(e.getWhen(),e.getModifiers());123}124pressed = false;125armed = false;126repaint();127}128129break;130131case MouseEvent.MOUSE_ENTERED:132if (pressed)133armed = true;134break;135case MouseEvent.MOUSE_EXITED:136armed = false;137break;138}139}140141142// NOTE: This method is called by privileged threads.143// DO NOT INVOKE CLIENT CODE ON THIS THREAD!144public void action(final long when, final int modifiers) {145postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,146((Button)target).getActionCommand(),147when, modifiers));148}149150151public void focusGained(FocusEvent e) {152super.focusGained(e);153repaint();154}155156public void focusLost(FocusEvent e) {157super.focusLost(e);158repaint();159}160161void handleJavaKeyEvent(KeyEvent e) {162int id = e.getID();163switch (id) {164case KeyEvent.KEY_PRESSED:165if (e.getKeyCode() == KeyEvent.VK_SPACE)166{167pressed=true;168armed=true;169repaint();170action(e.getWhen(),e.getModifiers());171}172173break;174175case KeyEvent.KEY_RELEASED:176if (e.getKeyCode() == KeyEvent.VK_SPACE)177{178pressed = false;179armed = false;180repaint();181}182183break;184185186}187}188189public Dimension getMinimumSize() {190FontMetrics fm = getFontMetrics(getPeerFont());191if ( label == null ) {192label = "";193}194return new Dimension(fm.stringWidth(label) + 14,195fm.getHeight() + 8);196}197198/**199* DEPRECATED200*/201public Dimension minimumSize() {202return getMinimumSize();203}204/**205* This method is called from Toolkit Thread and so it should not call any206* client code.207*/208@Override209void paintPeer(final Graphics g) {210if (!disposed) {211Dimension size = getPeerSize();212g.setColor( getPeerBackground() ); /* erase the existing button remains */213g.fillRect(0,0, size.width , size.height);214paintBorder(g,borderInsets.left,215borderInsets.top,216size.width-(borderInsets.left+borderInsets.right),217size.height-(borderInsets.top+borderInsets.bottom));218219FontMetrics fm = g.getFontMetrics();220221Rectangle textRect,iconRect,viewRect;222223textRect = new Rectangle();224viewRect = new Rectangle();225iconRect = new Rectangle();226227228viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);229viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);230viewRect.x = contentAreaInsets.left;231viewRect.y = contentAreaInsets.top;232String llabel = (label != null) ? label : "";233// layout the text and icon234String text = SwingUtilities.layoutCompoundLabel(235fm, llabel, null,236SwingConstants.CENTER, SwingConstants.CENTER,237SwingConstants.CENTER, SwingConstants.CENTER,238viewRect, iconRect, textRect, 0);239240Font f = getPeerFont();241242g.setFont(f);243244// perform UI specific press action, e.g. Windows L&F shifts text245if (pressed && armed) {246paintButtonPressed(g,target);247}248249paintText(g, target, textRect, text);250251if (hasFocus()) {252// paint UI specific focus253paintFocus(g,focusInsets.left,254focusInsets.top,255size.width-(focusInsets.left+focusInsets.right)-1,256size.height-(focusInsets.top+focusInsets.bottom)-1);257}258}259flush();260}261262public void paintBorder(Graphics g, int x, int y, int w, int h) {263drawMotif3DRect(g, x, y, w-1, h-1, pressed);264}265266protected void paintFocus(Graphics g, int x, int y, int w, int h){267g.setColor(focusColor);268g.drawRect(x,y,w,h);269}270271protected void paintButtonPressed(Graphics g, Component b) {272Dimension size = getPeerSize();273g.setColor(selectColor);274g.fillRect(contentAreaInsets.left,275contentAreaInsets.top,276size.width-(contentAreaInsets.left+contentAreaInsets.right),277size.height-(contentAreaInsets.top+contentAreaInsets.bottom));278279}280protected void paintText(Graphics g, Component c, Rectangle textRect, String text) {281FontMetrics fm = g.getFontMetrics();282283int mnemonicIndex = -1;284285/* Draw the Text */286if(isEnabled()) {287/*** paint the text normally */288g.setColor(getPeerForeground());289BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );290}291else {292/*** paint the text disabled ***/293g.setColor(getPeerBackground().brighter());294BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,295textRect.x, textRect.y + fm.getAscent());296g.setColor(getPeerBackground().darker());297BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,298textRect.x - 1, textRect.y + fm.getAscent() - 1);299}300}301}302303304