001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.Rectangle;
026: import java.util.EventListener;
027:
028: import javax.swing.AbstractButton;
029: import javax.swing.Icon;
030: import javax.swing.InputMap;
031: import javax.swing.JComponent;
032: import javax.swing.LookAndFeel;
033: import javax.swing.SwingUtilities;
034: import javax.swing.UIManager;
035: import javax.swing.plaf.ButtonUI;
036: import javax.swing.plaf.ComponentUI;
037:
038: import org.apache.harmony.x.swing.ButtonCommons;
039: import org.apache.harmony.x.swing.Utilities;
040:
041: public class BasicButtonUI extends ButtonUI {
042:
043: private static final String PROPERTY_PREFIX = "Button.";
044: private static BasicButtonUI basicButtonUI;
045:
046: protected int defaultTextIconGap;
047:
048: protected int defaultTextShiftOffset;
049:
050: private int textShiftOffset = 0;
051:
052: private Color focusColor;
053: private Color disabledTextColor;
054:
055: private final Rectangle viewR = new Rectangle();
056: private final Rectangle iconR = new Rectangle();
057: private final Rectangle textR = new Rectangle();
058:
059: public static ComponentUI createUI(final JComponent c) {
060: if (basicButtonUI == null) {
061: basicButtonUI = new BasicButtonUI();
062: }
063: return basicButtonUI;
064: }
065:
066: public void installUI(final JComponent c) {
067: final AbstractButton button = (AbstractButton) c;
068: installDefaults(button);
069: installListeners(button);
070: installKeyboardActions(button);
071: }
072:
073: public void uninstallUI(final JComponent c) {
074: final AbstractButton button = (AbstractButton) c;
075: uninstallKeyboardActions(button);
076: uninstallListeners(button);
077: uninstallDefaults(button);
078: }
079:
080: public Dimension getPreferredSize(final JComponent c) {
081: return ButtonCommons.getPreferredSize((AbstractButton) c, null);
082: }
083:
084: public int getDefaultTextIconGap(final AbstractButton button) {
085: return defaultTextIconGap;
086: }
087:
088: public void paint(final Graphics g, final JComponent c) {
089: final AbstractButton button = (AbstractButton) c;
090: viewR.setBounds(0, 0, 0, 0);
091: String clippedText = ButtonCommons.getPaintingParameters(
092: button, viewR, iconR, textR, button.getIcon());
093:
094: paintButtonPressed(g, button);
095: paintIcon(g, button, iconR);
096: paintText(g, button, textR, clippedText);
097: if (isFocusPainted(button)) {
098: paintFocus(g, button, viewR, textR, iconR);
099: }
100: }
101:
102: protected void paintButtonPressed(final Graphics g,
103: final AbstractButton button) {
104: }
105:
106: protected void paintIcon(final Graphics g, final JComponent c,
107: final Rectangle iconRect) {
108: final Icon icon = ButtonCommons
109: .getCurrentIcon((AbstractButton) c);
110: if (icon != null) {
111: icon.paintIcon(c, g, iconRect.x, iconRect.y);
112: }
113: }
114:
115: protected void paintText(final Graphics g, final AbstractButton b,
116: final Rectangle textRect, final String text) {
117: paintText(g, (JComponent) b, textRect, text);
118: }
119:
120: protected void paintText(final Graphics g, final JComponent c,
121: final Rectangle textRect, final String text) {
122: final AbstractButton b = (AbstractButton) c;
123: final Color color = b.isEnabled() ? b.getForeground()
124: : disabledTextColor;
125:
126: final int currentTextShiftOffset = getTextShiftOffset();
127: textRect.translate(currentTextShiftOffset,
128: currentTextShiftOffset);
129: ButtonCommons.paintText(g, b, textRect, text, color);
130: }
131:
132: protected void paintFocus(final Graphics g, final AbstractButton b,
133: final Rectangle viewRect, final Rectangle textRect,
134: final Rectangle iconRect) {
135:
136: ButtonCommons.paintFocus(g, ButtonCommons.getFocusRect(
137: viewRect, textRect, iconRect), focusColor);
138: }
139:
140: protected BasicButtonListener createButtonListener(
141: final AbstractButton button) {
142: return new BasicButtonListener(button);
143: }
144:
145: protected void installListeners(final AbstractButton button) {
146: BasicButtonListener listener = createButtonListener(button);
147: button.addPropertyChangeListener(listener);
148: button.addChangeListener(listener);
149: button.addMouseListener(listener);
150: button.addMouseMotionListener(listener);
151: button.addFocusListener(listener);
152: }
153:
154: protected void uninstallListeners(final AbstractButton button) {
155: BasicButtonListener listener = getBasicButtonListener(button);
156: button.removePropertyChangeListener(listener);
157: button.removeChangeListener(listener);
158: button.removeMouseListener(listener);
159: button.removeMouseMotionListener(listener);
160: button.removeFocusListener(listener);
161: }
162:
163: protected void installKeyboardActions(final AbstractButton button) {
164: BasicButtonListener listener = getBasicButtonListener(button);
165: if (listener != null) {
166: listener.installKeyboardActions(button);
167: }
168: SwingUtilities.replaceUIInputMap(button,
169: JComponent.WHEN_FOCUSED, (InputMap) UIManager
170: .get(getPropertyPrefix() + "focusInputMap"));
171: }
172:
173: protected void uninstallKeyboardActions(final AbstractButton button) {
174: BasicButtonListener listener = getBasicButtonListener(button);
175: if (listener != null) {
176: listener.uninstallKeyboardActions(button);
177: }
178: SwingUtilities.replaceUIInputMap(button,
179: JComponent.WHEN_FOCUSED, null);
180: }
181:
182: protected void installDefaults(final AbstractButton button) {
183: LookAndFeel.installColorsAndFont(button, getPropertyPrefix()
184: + "background", getPropertyPrefix() + "foreground",
185: getPropertyPrefix() + "font");
186:
187: LookAndFeel.installProperty(button, "opaque", Boolean.TRUE);
188: LookAndFeel.installBorder(button, getPropertyPrefix()
189: + "border");
190: LookAndFeel.installProperty(button, "alignmentX", new Float(0));
191: defaultTextIconGap = UIManager.getInt(getPropertyPrefix()
192: + "textIconGap");
193: defaultTextShiftOffset = UIManager.getInt(getPropertyPrefix()
194: + "textShiftOffset");
195:
196: button.setMargin(UIManager.getInsets(getPropertyPrefix()
197: + "margin"));
198:
199: disabledTextColor = button.getBackground().darker();
200: focusColor = UIManager.getColor("activeCaptionBorder");
201: }
202:
203: protected void uninstallDefaults(final AbstractButton button) {
204: LookAndFeel.uninstallBorder(button);
205: Utilities.uninstallColorsAndFont(button);
206: if (Utilities.isUIResource(button.getMargin())) {
207: button.setMargin(null);
208: }
209: }
210:
211: protected String getPropertyPrefix() {
212: return PROPERTY_PREFIX;
213: }
214:
215: protected void setTextShiftOffset() {
216: textShiftOffset = defaultTextShiftOffset;
217: }
218:
219: protected void clearTextShiftOffset() {
220: textShiftOffset = 0;
221: }
222:
223: protected int getTextShiftOffset() {
224: return textShiftOffset;
225: }
226:
227: private boolean isFocusPainted(final AbstractButton button) {
228: return (button.isEnabled() && button.isFocusPainted()
229: && button.isFocusOwner() && (button.getIcon() != null || !Utilities
230: .isEmptyString(button.getText())));
231: }
232:
233: private BasicButtonListener getBasicButtonListener(
234: final AbstractButton button) {
235: EventListener[] listeners = button.getPropertyChangeListeners();
236: for (int i = 0; i < listeners.length; i++) {
237: if (listeners[i] instanceof BasicButtonListener) {
238: return (BasicButtonListener) listeners[i];
239: }
240: }
241:
242: return null;
243: }
244: }
|