001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing.plaf.basic;
018:
019: import java.awt.*;
020: import java.awt.event.MouseAdapter;
021: import java.awt.event.MouseEvent;
022: import java.awt.event.MouseListener;
023:
024: import javax.swing.AbstractButton;
025: import javax.swing.ButtonModel;
026: import javax.swing.JComponent;
027: import javax.swing.JToolBar;
028: import javax.swing.SwingUtilities;
029: import javax.swing.UIManager;
030: import javax.swing.plaf.ComponentUI;
031: import javax.swing.plaf.basic.BasicButtonUI;
032: import javax.swing.plaf.basic.BasicGraphicsUtils;
033: import javax.swing.plaf.basic.BasicHTML;
034: import javax.swing.text.View;
035:
036: /**
037: * Basic implementation of the <code>JLinkButton</code> UI. <br>
038: */
039: public class BasicLinkButtonUI extends BasicButtonUI {
040:
041: public static ComponentUI createUI(JComponent c) {
042: return new BasicLinkButtonUI();
043: }
044:
045: private static Rectangle viewRect = new Rectangle();
046: private static Rectangle textRect = new Rectangle();
047: private static Rectangle iconRect = new Rectangle();
048: private static MouseListener handCursorListener = new HandCursor();
049:
050: protected int dashedRectGapX;
051: protected int dashedRectGapY;
052: protected int dashedRectGapWidth;
053: protected int dashedRectGapHeight;
054: private Color focusColor;
055:
056: protected void installDefaults(AbstractButton b) {
057: super .installDefaults(b);
058:
059: b.setOpaque(false);
060: b.setBorderPainted(false);
061: b.setRolloverEnabled(true);
062:
063: dashedRectGapX = UIManager.getInt("ButtonUI.dashedRectGapX");
064: dashedRectGapY = UIManager.getInt("ButtonUI.dashedRectGapY");
065: dashedRectGapWidth = UIManager
066: .getInt("ButtonUI.dashedRectGapWidth");
067: dashedRectGapHeight = UIManager
068: .getInt("ButtonUI.dashedRectGapHeight");
069: focusColor = UIManager.getColor("ButtonUI.focus");
070:
071: b.setHorizontalAlignment(AbstractButton.LEFT);
072: }
073:
074: protected void installListeners(AbstractButton b) {
075: super .installListeners(b);
076: b.addMouseListener(handCursorListener);
077: }
078:
079: protected void uninstallListeners(AbstractButton b) {
080: super .uninstallListeners(b);
081: b.removeMouseListener(handCursorListener);
082: }
083:
084: protected Color getFocusColor() {
085: return focusColor;
086: }
087:
088: public void paint(Graphics g, JComponent c) {
089: AbstractButton b = (AbstractButton) c;
090: ButtonModel model = b.getModel();
091:
092: FontMetrics fm = g.getFontMetrics();
093:
094: Insets i = c.getInsets();
095:
096: viewRect.x = i.left;
097: viewRect.y = i.top;
098: viewRect.width = b.getWidth() - (i.right + viewRect.x);
099: viewRect.height = b.getHeight() - (i.bottom + viewRect.y);
100:
101: textRect.x = textRect.y = textRect.width = textRect.height = 0;
102: iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
103:
104: Font f = c.getFont();
105: g.setFont(f);
106:
107: // layout the text and icon
108: String text = SwingUtilities.layoutCompoundLabel(c, fm, b
109: .getText(), b.getIcon(), b.getVerticalAlignment(), b
110: .getHorizontalAlignment(), b.getVerticalTextPosition(),
111: b.getHorizontalTextPosition(), viewRect, iconRect,
112: textRect, b.getText() == null ? 0 : b.getIconTextGap());
113:
114: clearTextShiftOffset();
115:
116: // perform UI specific press action, e.g. Windows L&F shifts text
117: if (model.isArmed() && model.isPressed()) {
118: paintButtonPressed(g, b);
119: }
120:
121: // Paint the Icon
122: if (b.getIcon() != null) {
123: paintIcon(g, c, iconRect);
124: }
125:
126: Composite oldComposite = ((Graphics2D) g).getComposite();
127:
128: if (model.isRollover()) {
129: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
130: AlphaComposite.SRC_OVER, 0.5f));
131: }
132:
133: if (text != null && !text.equals("")) {
134: View v = (View) c.getClientProperty(BasicHTML.propertyKey);
135: if (v != null) {
136: textRect.x += getTextShiftOffset();
137: textRect.y += getTextShiftOffset();
138: v.paint(g, textRect);
139: textRect.x -= getTextShiftOffset();
140: textRect.y -= getTextShiftOffset();
141: } else {
142: paintText(g, b, textRect, text);
143: }
144: }
145:
146: if (b.isFocusPainted() && b.hasFocus()) {
147: // paint UI specific focus
148: paintFocus(g, b, viewRect, textRect, iconRect);
149: }
150:
151: ((Graphics2D) g).setComposite(oldComposite);
152: }
153:
154: protected void paintFocus(Graphics g, AbstractButton b,
155: Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
156: if (b.getParent() instanceof JToolBar) {
157: // Windows doesn't draw the focus rect for buttons in a toolbar.
158: return;
159: }
160:
161: // focus painted same color as text
162: int width = b.getWidth();
163: int height = b.getHeight();
164: g.setColor(getFocusColor());
165: BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX,
166: dashedRectGapY, width - dashedRectGapWidth, height
167: - dashedRectGapHeight);
168: }
169:
170: protected void paintButtonPressed(Graphics g, AbstractButton b) {
171: setTextShiftOffset();
172: }
173:
174: static class HandCursor extends MouseAdapter {
175: public void mouseEntered(MouseEvent e) {
176: e.getComponent().setCursor(
177: Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
178: }
179:
180: public void mouseExited(MouseEvent e) {
181: e.getComponent().setCursor(Cursor.getDefaultCursor());
182: }
183: }
184: }
|