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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXHyperlink.java,v 1.7 2005/10/12 11:26:54 kleopatra 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;
  22. import java.awt.Color;
  23. import java.beans.PropertyChangeEvent;
  24. import java.beans.PropertyChangeListener;
  25. import javax.swing.Action;
  26. import javax.swing.JButton;
  27. import org.jdesktop.swingx.plaf.JXHyperlinkAddon;
  28. import org.jdesktop.swingx.plaf.LookAndFeelAddons;
  29. /**
  30.  * A hyperlink component that derives from JButton to provide compatibility
  31.  * mostly for binding actions enabled/disabled behavior accesility i18n etc...
  32.  * 
  33.  * @author Richard Bair
  34.  * @author Shai Almog
  35.  * @author Jeanette Winzenburg
  36.  */
  37. public class JXHyperlink extends JButton {
  38.     /**
  39.      * @see #getUIClassID
  40.      * @see #readObject
  41.      */
  42.     public static final String uiClassID = "HyperlinkUI";
  43.     // ensure at least the default ui is registered
  44.     static {
  45.       LookAndFeelAddons.contribute(new JXHyperlinkAddon());
  46.     }
  47.     private boolean hasBeenVisited = false;
  48.     /**
  49.      * Color for the hyper link if it has not yet been clicked. This color can
  50.      * be set both in code, and through the UIManager with the property
  51.      * "JXHyperlink.unclickedColor".
  52.      */
  53.     private Color unclickedColor = new Color(0, 0x33, 0xFF);
  54.     /**
  55.      * Color for the hyper link if it has already been clicked. This color can
  56.      * be set both in code, and through the UIManager with the property
  57.      * "JXHyperlink.clickedColor".
  58.      */
  59.     private Color clickedColor = new Color(0x99, 0, 0x99);
  60.     /** Creates a new instance of JXHyperlink */
  61.     public JXHyperlink() {
  62.         super();
  63.     }
  64.     public JXHyperlink(Action action) {
  65.         super(action);
  66.         init();
  67.     }
  68.     /**
  69.      * @return
  70.      */
  71.     public Color getUnclickedColor() {
  72.         return unclickedColor;
  73.     }
  74.     /**
  75.      * @param color
  76.      */
  77.     public void setClickedColor(Color color) {
  78.         Color old = getClickedColor();
  79.         clickedColor = color;
  80.         if (isVisited()) {
  81.             setForeground(getClickedColor());
  82.         }
  83.         firePropertyChange("clickedColor", old, getClickedColor());
  84.     }
  85.     /**
  86.      * @return
  87.      */
  88.     public Color getClickedColor() {
  89.         return clickedColor;
  90.     }
  91.     /**
  92.      * @param color
  93.      */
  94.     public void setUnclickedColor(Color color) {
  95.         Color old = getUnclickedColor();
  96.         unclickedColor = color;
  97.         if (!isVisited()) {
  98.             setForeground(getUnclickedColor());
  99.         }
  100.         firePropertyChange("unclickedColor", old, getUnclickedColor());
  101.     }
  102.     protected void setVisited(boolean visited) {
  103.         boolean old = isVisited();
  104.         hasBeenVisited = visited;
  105.         setForeground(isVisited() ? getClickedColor() : getUnclickedColor());
  106.         firePropertyChange("visited", old, isVisited());
  107.     }
  108.     protected boolean isVisited() {
  109.         return hasBeenVisited;
  110.     }
  111.     protected PropertyChangeListener createActionPropertyChangeListener(
  112.             final Action a) {
  113.         final PropertyChangeListener superListener = super
  114.                 .createActionPropertyChangeListener(a);
  115.         // JW: need to do something better - only weak refs allowed!
  116.         // no way to hook into super
  117.         PropertyChangeListener l = new PropertyChangeListener() {
  118.             public void propertyChange(PropertyChangeEvent evt) {
  119.                 if (LinkModel.VISITED_PROPERTY.equals(evt.getPropertyName())) {
  120.                     setVisitedFromActionProperty(a);
  121.                 } else {
  122.                     superListener.propertyChange(evt);
  123.                 }
  124.             }
  125.         };
  126.         return l;
  127.     }
  128.     protected void configurePropertiesFromAction(Action a) {
  129.         super.configurePropertiesFromAction(a);
  130.         setVisitedFromActionProperty(a);
  131.     }
  132.     private void setVisitedFromActionProperty(Action a) {
  133.         Boolean visited = (Boolean) a.getValue(LinkModel.VISITED_PROPERTY);
  134.         setVisited(visited != null ? visited.booleanValue() : false);
  135.     }
  136.     private void init() {
  137.         setForeground(isVisited() ? getClickedColor() : getUnclickedColor());
  138.     }
  139.     public String getUIClassID() {
  140.         return uiClassID;
  141.     }
  142. }