001: /*
002: * Copyright 2005 Patrick Gotthardt
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.pagosoft.plaf;
017:
018: import com.pagosoft.swing.ColorUtils;
019:
020: import javax.swing.*;
021: import javax.swing.plaf.*;
022: import javax.swing.plaf.basic.*;
023: import javax.swing.plaf.metal.*;
024: import java.awt.*;
025: import java.awt.event.*;
026: import java.lang.ref.WeakReference;
027:
028: public class PgsButtonUI extends MetalButtonUI implements
029: ActionListener {
030: private static PgsButtonUI INSTANCE = new PgsButtonUI();
031:
032: private Timer defaultButtonTimer;
033: private WeakReference defaultButtonRef;
034: private int defaultButtonAlpha;
035: private boolean defaultButtonAlphaDir;
036:
037: public static ComponentUI createUI(JComponent c) {
038: if (c.getParent() instanceof JToolBar) {
039: return ToolBarButtonUI.createUI(c);
040: }
041: return INSTANCE;
042: }
043:
044: public PgsButtonUI() {
045: defaultButtonTimer = new Timer(50, this );
046: }
047:
048: public void installDefaults(AbstractButton b) {
049: super .installDefaults(b);
050: b.putClientProperty("rolloverBackground", UIManager
051: .getColor("Button.rolloverBackground"));
052: b.putClientProperty("pgs.isFlat", UIManager
053: .get("Button.isFlat"));
054: b.putClientProperty("gradientStart", UIManager
055: .get("Button.gradientStart"));
056: b.putClientProperty("gradientEnd", UIManager
057: .get("Button.gradientEnd"));
058: b.putClientProperty("rollover.gradientStart", UIManager
059: .get("Button.rolloverGradientStart"));
060: b.putClientProperty("rollover.gradientEnd", UIManager
061: .get("Button.rolloverGradientEnd"));
062: b.putClientProperty("selected.gradientStart", UIManager
063: .get("Button.selectedGradientStart"));
064: b.putClientProperty("selected.gradientEnd", UIManager
065: .get("Button.selectedGradientEnd"));
066: }
067:
068: protected BasicButtonListener createButtonListener(AbstractButton b) {
069: return new RolloverButtonListener(b);
070: }
071:
072: protected void paintFocus(Graphics g, AbstractButton b,
073: Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
074:
075: if (b.isFocusPainted() && !(b instanceof PgsComboBoxButtonUI)) {
076: int topLeftInset = 3;
077: int width = b.getWidth() - topLeftInset * 2;
078: int height = b.getHeight() - topLeftInset * 2;
079:
080: g.setColor(getFocusColor());
081: PgsUtils.drawRoundRect(g, topLeftInset, topLeftInset,
082: width - 1, height - 1, 3, 3);
083: }
084: }
085:
086: public void update(Graphics g, JComponent c) {
087: JButton b = (JButton) c;
088: if (c.isOpaque() && b.isContentAreaFilled()) {
089: if (Boolean.TRUE.equals(c.getClientProperty("pgs.isFlat"))
090: || !b.isEnabled()) {
091: g
092: .setColor(c.isEnabled()
093: && b.getModel().isRollover() ? (Color) c
094: .getClientProperty("rolloverBackground")
095: : c.getBackground());
096: g.fillRect(0, 0, c.getWidth(), c.getHeight());
097: } else {
098: g
099: .setColor(c.isEnabled()
100: && b.getModel().isRollover() ? (Color) c
101: .getClientProperty("rolloverBackground")
102: : c.getBackground());
103: g.fillRect(0, 0, c.getWidth(), c.getHeight());
104: if (c.isEnabled() && b.getModel().isRollover()) {
105: PgsUtils.drawGradient(g, c, "rollover");
106: } else {
107: PgsUtils.drawGradient(g, c);
108: }
109: }
110: if (b.isDefaultButton()
111: && PgsUtils.hasFocus(b.getTopLevelAncestor())) {
112: JButton defaultButton;
113: if (defaultButtonRef == null) {
114: defaultButton = null;
115: } else {
116: defaultButton = (JButton) defaultButtonRef.get();
117: }
118: // We need to re-apply our defaultButton-setup
119: if (b != defaultButton) {
120: // we had no defaultButton before
121: if (defaultButton == null) {
122: defaultButton = b;
123: defaultButtonRef = new WeakReference(b);
124: defaultButtonTimer.start();
125: } else {
126: JButton temp = defaultButton;
127: defaultButton = b;
128: defaultButtonRef = new WeakReference(b);
129: temp.repaint();
130: }
131: defaultButtonAlpha = 10;
132: defaultButtonAlphaDir = true;
133: }
134: // let's paint the button
135: PgsUtils.drawButtonBorder(g, 1, 1, b.getWidth() - 3, b
136: .getHeight() - 3,
137: PgsUtils.rolloverBorderStroke, ColorUtils
138: .getTranslucentColor(PgsLookAndFeel
139: .getPrimaryControlShadow(),
140: defaultButtonAlpha));
141: }
142: }
143: super .paint(g, c);
144: }
145:
146: protected void paintButtonPressed(Graphics g, AbstractButton b) {
147: if (b.isOpaque() && b.isContentAreaFilled()) {
148: if (Boolean.TRUE.equals(b.getClientProperty("pgs.isFlat"))) {
149: g.setColor(getSelectColor());
150: g.fillRect(0, 0, b.getWidth(), b.getHeight());
151: } else {
152: PgsUtils.drawGradient(g, b, "selected");
153: }
154: }
155: }
156:
157: protected void paintText(Graphics g, JComponent c,
158: Rectangle textRect, String text) {
159: PgsUtils.installAntialiasing(g);
160: super .paintText(g, c, textRect, text);
161: PgsUtils.uninstallAntialiasing(g);
162: }
163:
164: public void actionPerformed(ActionEvent e) {
165: JButton defaultButton = (JButton) defaultButtonRef.get();
166: // no default button, thus stop running
167: if (defaultButton == null && defaultButtonTimer.isRunning()) {
168: defaultButtonTimer.stop();
169: defaultButtonRef = null;
170: return;
171: }
172: defaultButtonAlpha += (defaultButtonAlphaDir) ? 10 : -10;
173: if (defaultButtonAlpha == 200 || defaultButtonAlpha == 10) {
174: defaultButtonAlphaDir = !defaultButtonAlphaDir;
175: }
176: defaultButton.repaint();
177: }
178: }
|