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