BasicHyperlinkUI.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: BasicHyperlinkUI.java,v 1.5 2005/10/10 18:02:52 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.plaf.basic;
  22. import java.awt.Color;
  23. import java.awt.Cursor;
  24. import java.awt.Font;
  25. import java.awt.FontMetrics;
  26. import java.awt.Graphics;
  27. import java.awt.Insets;
  28. import java.awt.Rectangle;
  29. import java.awt.event.MouseAdapter;
  30. import java.awt.event.MouseEvent;
  31. import java.awt.event.MouseListener;
  32. import javax.swing.AbstractButton;
  33. import javax.swing.BorderFactory;
  34. import javax.swing.ButtonModel;
  35. import javax.swing.JComponent;
  36. import javax.swing.JToolBar;
  37. import javax.swing.SwingUtilities;
  38. import javax.swing.UIManager;
  39. import javax.swing.plaf.ComponentUI;
  40. import javax.swing.plaf.basic.BasicButtonUI;
  41. import javax.swing.plaf.basic.BasicGraphicsUtils;
  42. import javax.swing.plaf.basic.BasicHTML;
  43. import javax.swing.text.View;
  44. /**
  45.  * Basic implementation of the <code>JXHyperlink</code> UI. <br>
  46.  * This is copied from org.jdesktop.jdnc.plaf.basic.BasicLinkButtonUI
  47.  */
  48. public class BasicHyperlinkUI extends BasicButtonUI {
  49.     public static ComponentUI createUI(JComponent c) {
  50.         return new BasicHyperlinkUI();
  51.     }
  52.     private static Rectangle viewRect = new Rectangle();
  53.     private static Rectangle textRect = new Rectangle();
  54.     private static Rectangle iconRect = new Rectangle();
  55.     private static MouseListener handCursorListener = new HandCursor();
  56.     protected int dashedRectGapX;
  57.     protected int dashedRectGapY;
  58.     protected int dashedRectGapWidth;
  59.     protected int dashedRectGapHeight;
  60.     private Color focusColor;
  61.     protected void installDefaults(AbstractButton b) {
  62.         super.installDefaults(b);
  63.         b.setOpaque(false);
  64.         b.setBorderPainted(false);
  65.         b.setRolloverEnabled(true);
  66.         b.setBorder(BorderFactory.createEmptyBorder());
  67.         dashedRectGapX = UIManager.getInt("ButtonUI.dashedRectGapX");
  68.         dashedRectGapY = UIManager.getInt("ButtonUI.dashedRectGapY");
  69.         dashedRectGapWidth = UIManager.getInt("ButtonUI.dashedRectGapWidth");
  70.         dashedRectGapHeight = UIManager.getInt("ButtonUI.dashedRectGapHeight");
  71.         focusColor = UIManager.getColor("ButtonUI.focus");
  72.         b.setHorizontalAlignment(AbstractButton.LEADING);
  73.     }
  74.     protected void installListeners(AbstractButton b) {
  75.         super.installListeners(b);
  76.         b.addMouseListener(handCursorListener);
  77.     }
  78.     protected void uninstallListeners(AbstractButton b) {
  79.         super.uninstallListeners(b);
  80.         b.removeMouseListener(handCursorListener);
  81.     }
  82.     protected Color getFocusColor() {
  83.         return focusColor;
  84.     }
  85.     public void paint(Graphics g, JComponent c) {
  86.         AbstractButton b = (AbstractButton) c;
  87.         ButtonModel model = b.getModel();
  88.         FontMetrics fm = g.getFontMetrics();
  89.         Insets i = c.getInsets();
  90.         viewRect.x = i.left;
  91.         viewRect.y = i.top;
  92.         viewRect.width = b.getWidth() - (i.right + viewRect.x);
  93.         viewRect.height = b.getHeight() - (i.bottom + viewRect.y);
  94.         textRect.x = textRect.y = textRect.width = textRect.height = 0;
  95.         iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
  96.         Font f = c.getFont();
  97.         g.setFont(f);
  98.         // layout the text and icon
  99.         String text = SwingUtilities.layoutCompoundLabel(c, fm, b.getText(), b
  100.                 .getIcon(), b.getVerticalAlignment(), b
  101.                 .getHorizontalAlignment(), b.getVerticalTextPosition(), b
  102.                 .getHorizontalTextPosition(), viewRect, iconRect, textRect, b
  103.                 .getText() == null ? 0 : b.getIconTextGap());
  104.         clearTextShiftOffset();
  105.         // perform UI specific press action, e.g. Windows L&F shifts text
  106.         if (model.isArmed() && model.isPressed()) {
  107.             paintButtonPressed(g, b);
  108.         }
  109.         // Paint the Icon
  110.         if (b.getIcon() != null) {
  111.             paintIcon(g, c, iconRect);
  112.         }
  113. //        Composite oldComposite = ((Graphics2D) g).getComposite();
  114. //
  115. //        if (model.isRollover()) {
  116. //            ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
  117. //                    AlphaComposite.SRC_OVER, 0.5f));
  118. //        }
  119.         if (text != null && !text.equals("")) {
  120.             View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  121.             if (v != null) {
  122.                 textRect.x += getTextShiftOffset();
  123.                 textRect.y += getTextShiftOffset();
  124.                 v.paint(g, textRect);
  125.                 textRect.x -= getTextShiftOffset();
  126.                 textRect.y -= getTextShiftOffset();
  127.             } else {
  128.                 paintText(g, b, textRect, text);
  129.             }
  130.         }
  131.         if (b.isFocusPainted() && b.hasFocus()) {
  132.             // paint UI specific focus
  133.             paintFocus(g, b, viewRect, textRect, iconRect);
  134.         }
  135. //        ((Graphics2D) g).setComposite(oldComposite);
  136.     }
  137.     protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
  138.             String text) {
  139.         super.paintText(g, b, textRect, text);
  140.         if (b.getModel().isRollover()) {
  141.             paintUnderline(g, b, textRect, text);
  142.         }
  143.     }
  144.     private void paintUnderline(Graphics g, AbstractButton b, Rectangle rect,
  145.             String text) {
  146.         // JW: copied from JXTable.LinkRenderer
  147.         FontMetrics fm = g.getFontMetrics();
  148.         int descent = fm.getDescent();
  149.         // REMIND(aim): should we be basing the underline on
  150.         // the font's baseline instead of the text bounds?
  151.         g.drawLine(rect.x + getTextShiftOffset(),
  152.           (rect.y + rect.height) - descent + 1 + getTextShiftOffset(),
  153.           rect.x + rect.width + getTextShiftOffset(),
  154.           (rect.y + rect.height) - descent + 1 + getTextShiftOffset());
  155.     }
  156.     protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
  157.             Rectangle textRect, Rectangle iconRect) {
  158.         if (b.getParent() instanceof JToolBar) {
  159.             // Windows doesn't draw the focus rect for buttons in a toolbar.
  160.             return;
  161.         }
  162.         // focus painted same color as text
  163.         int width = b.getWidth();
  164.         int height = b.getHeight();
  165.         g.setColor(getFocusColor());
  166.         BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
  167.                 width - dashedRectGapWidth, height - dashedRectGapHeight);
  168.     }
  169.     protected void paintButtonPressed(Graphics g, AbstractButton b) {
  170.         // setTextShiftOffset();
  171.     }
  172.     static class HandCursor extends MouseAdapter {
  173.         public void mouseEntered(MouseEvent e) {
  174.             e.getComponent().setCursor(
  175.                     Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  176.         }
  177.         public void mouseExited(MouseEvent e) {
  178.             e.getComponent().setCursor(Cursor.getDefaultCursor());
  179.         }
  180.     }
  181. }