1/* 2 * Copyright (c) 1997, 2003, 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 26package com.sun.java.swing.plaf.motif; 27 28import sun.awt.AppContext; 29 30import javax.swing.*; 31import javax.swing.border.*; 32import javax.swing.plaf.basic.*; 33import java.awt.*; 34import java.awt.event.*; 35import javax.swing.plaf.*; 36 37/** 38 * MotifButton implementation 39 * <p> 40 * <strong>Warning:</strong> 41 * Serialized objects of this class will not be compatible with 42 * future Swing releases. The current serialization support is appropriate 43 * for short term storage or RMI between applications running the same 44 * version of Swing. A future release of Swing will provide support for 45 * long term persistence. 46 * 47 * @author Rich Schiavi 48 */ 49public class MotifButtonUI extends BasicButtonUI { 50 51 protected Color selectColor; 52 53 private boolean defaults_initialized = false; 54 55 private static final Object MOTIF_BUTTON_UI_KEY = new Object(); 56 57 // ******************************** 58 // Create PLAF 59 // ******************************** 60 public static ComponentUI createUI(JComponent c) { 61 AppContext appContext = AppContext.getAppContext(); 62 MotifButtonUI motifButtonUI = 63 (MotifButtonUI) appContext.get(MOTIF_BUTTON_UI_KEY); 64 if (motifButtonUI == null) { 65 motifButtonUI = new MotifButtonUI(); 66 appContext.put(MOTIF_BUTTON_UI_KEY, motifButtonUI); 67 } 68 return motifButtonUI; 69 } 70 71 // ******************************** 72 // Create Listeners 73 // ******************************** 74 protected BasicButtonListener createButtonListener(AbstractButton b){ 75 return new MotifButtonListener(b); 76 } 77 78 // ******************************** 79 // Install Defaults 80 // ******************************** 81 public void installDefaults(AbstractButton b) { 82 super.installDefaults(b); 83 if(!defaults_initialized) { 84 selectColor = UIManager.getColor(getPropertyPrefix() + "select"); 85 defaults_initialized = true; 86 } 87 LookAndFeel.installProperty(b, "opaque", Boolean.FALSE); 88 } 89 90 protected void uninstallDefaults(AbstractButton b) { 91 super.uninstallDefaults(b); 92 defaults_initialized = false; 93 } 94 95 // ******************************** 96 // Default Accessors 97 // ******************************** 98 99 protected Color getSelectColor() { 100 return selectColor; 101 } 102 103 // ******************************** 104 // Paint Methods 105 // ******************************** 106 public void paint(Graphics g, JComponent c) { 107 fillContentArea( g, (AbstractButton)c , c.getBackground() ); 108 super.paint(g,c); 109 } 110 111 // Overridden to ensure we don't paint icon over button borders. 112 protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) { 113 Shape oldClip = g.getClip(); 114 Rectangle newClip = 115 AbstractBorder.getInteriorRectangle(c, c.getBorder(), 0, 0, 116 c.getWidth(), c.getHeight()); 117 118 Rectangle r = oldClip.getBounds(); 119 newClip = 120 SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, 121 newClip); 122 g.setClip(newClip); 123 super.paintIcon(g, c, iconRect); 124 g.setClip(oldClip); 125 } 126 127 protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect){ 128 // focus painting is handled by the border 129 } 130 131 protected void paintButtonPressed(Graphics g, AbstractButton b) { 132 133 fillContentArea( g, b , selectColor ); 134 135 } 136 137 protected void fillContentArea( Graphics g, AbstractButton b, Color fillColor) { 138 139 if (b.isContentAreaFilled()) { 140 Insets margin = b.getMargin(); 141 Insets insets = b.getInsets(); 142 Dimension size = b.getSize(); 143 g.setColor(fillColor); 144 g.fillRect(insets.left - margin.left, 145 insets.top - margin.top, 146 size.width - (insets.left-margin.left) - (insets.right - margin.right), 147 size.height - (insets.top-margin.top) - (insets.bottom - margin.bottom)); 148 } 149 } 150} 151