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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: LinkModel.java,v 1.6 2005/10/10 18:02:05 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;
  22. import java.beans.PropertyChangeListener;
  23. import java.beans.PropertyChangeSupport;
  24. import java.net.MalformedURLException;
  25. import java.net.URL;
  26. /**
  27.  * An bean which represents an URL link.
  28.  * 
  29.  * Text, URL and visited are bound properties. Compares by Text.
  30.  * 
  31.  * @author Mark Davidson
  32.  * @author Jeanette Winzenburg
  33.  */
  34. public class LinkModel implements Comparable {
  35.     private String text; // display text
  36.     private URL url; // url of the link
  37.     private String target; // url target frame
  38.     private boolean visited = false;
  39.     private PropertyChangeSupport propertyChangeSupport;
  40.     public static final String VISITED_PROPERTY = "visited";
  41.     // hack - this class assumes that the url always != null
  42.     // need to cleanup
  43.     private static String defaultURLString = "https://jdnc.dev.java.net";
  44.     private static URL defaultURL;
  45.     
  46.     /**
  47.      * 
  48.      * @param text
  49.      * @param target
  50.      * @param url
  51.      */
  52.     public LinkModel(String text, String target, URL url) {
  53.         setText(text);
  54.         setTarget(target);
  55.         setURL(url != null ? url : getDefaultURL());
  56.     }
  57.     public LinkModel(String text) {
  58.         this(text, null, null);
  59.     }
  60.     /**
  61.      * @param text
  62.      *            text to that a renderer would display
  63.      * @param target
  64.      *            the target that a URL should load into.
  65.      * @param template
  66.      *            a string that represents a URL with
  67.      * @{N} place holders for string substitution
  68.      * @param args
  69.      *            an array of strings which will be used for substitition
  70.      */
  71.     public LinkModel(String text, String target, String template, String[] args) {
  72.         setText(text);
  73.         setTarget(target);
  74.         setURL(createURL(template, args));
  75.     }
  76.     /**
  77.      * Set the display text.
  78.      */
  79.     public void setText(String text) {
  80.         String old = getText();
  81.         this.text = text;
  82.         firePropertyChange("text", old, getText());
  83.     }
  84.     public String getText() {
  85.         if (text != null) {
  86.             return text;
  87.         } else if (url != null) {
  88.             return getURL().toString();
  89.         }
  90.         return null;
  91.     }
  92.     public void setURLString(String howToURLString) {
  93.         URL url = null;
  94.         try {
  95.             url = new URL(howToURLString);
  96.         } catch (MalformedURLException e) {
  97.             url = getDefaultURL();
  98.             e.printStackTrace();
  99.         }
  100.         setURL(url);
  101.     }
  102.     private URL getDefaultURL() {
  103.         if (defaultURL == null) {
  104.             try {
  105.                 defaultURL = new URL(defaultURLString);
  106.             } catch (MalformedURLException e) {
  107.                 // TODO Auto-generated catch block
  108.                 e.printStackTrace();
  109.             }
  110.         }
  111.         return defaultURL;
  112.     }
  113.     /**
  114.      * Set the url and resets the visited flag.
  115.      * 
  116.      * Think: keep list of visited urls here?
  117.      */
  118.     public void setURL(URL url) {
  119.          if (url == null) {
  120.             throw new IllegalArgumentException("URL for link cannot be null");
  121.         }
  122.          if (url.equals(getURL())) return;
  123.          URL old = getURL();
  124.          this.url = url;
  125.         firePropertyChange("URL", old, url);
  126.         setVisited(false);
  127.     }
  128.     public URL getURL() {
  129.         return url;
  130.     }
  131.     /**
  132.      * Create a URL from a template string that has place holders and an array
  133.      * of strings which will be substituted into the place holders. The place
  134.      * holders are represented as
  135.      * 
  136.      * @{N} where N = { 1..n }
  137.      *      <p>
  138.      *      For example, if the template contains a string like:
  139.      *      http://bugz.sfbay/cgi-bin/showbug?cat=@{1}&sub_cat=@{2} and a two
  140.      *      arg array contains: java, classes_swing The resulting URL will be:
  141.      *      http://bugz.sfbay/cgi-bin/showbug?cat=java&sub_cat=classes_swing
  142.      *      <p>
  143.      * @param template
  144.      *            a url string that contains the placeholders
  145.      * @param args
  146.      *            an array of strings that will be substituted
  147.      */
  148.     private URL createURL(String template, String[] args) {
  149.         URL url = null;
  150.         try {
  151.             String urlStr = template;
  152.             for (int i = 0; i < args.length; i++) {
  153.                 urlStr = urlStr.replaceAll("@\{" + (i + 1) + "\}", args[i]);
  154.             }
  155.             url = new URL(urlStr);
  156.         } catch (MalformedURLException ex) {
  157.             //
  158.         }
  159.         return url;
  160.     }
  161.     /**
  162.      * Set the target that the URL should load into. This can be a uri
  163.      * representing another control or the name of a window or special targets.
  164.      * See: http://www.w3c.org/TR/html401/present/frames.html#adef-target
  165.      */
  166.     public void setTarget(String target) {
  167.         this.target = target;
  168.     }
  169.     /**
  170.      * Return the target for the URL.
  171.      * 
  172.      * @return value of the target. If null then "_blank" will be returned.
  173.      */
  174.     public String getTarget() {
  175.         if (target != null) {
  176.             return target;
  177.         } else {
  178.             return "_blank";
  179.         }
  180.     }
  181.     /**
  182.      * Sets a flag to indicate if the link has been visited. The state of this
  183.      * flag can be used to render the color of the link.
  184.      */
  185.     public void setVisited(boolean visited) {
  186.         boolean old = getVisited();
  187.         this.visited = visited;
  188.         firePropertyChange(VISITED_PROPERTY , old, getVisited());
  189.     }
  190.     public boolean getVisited() {
  191.         return visited;
  192.     }
  193. //---------------------- property change notification
  194.     
  195.     public void addPropertyChangeListener(PropertyChangeListener l) {
  196.         getPropertyChangeSupport().addPropertyChangeListener(l);
  197.         
  198.     }
  199.     
  200.     public void removePropertyChangeListener(PropertyChangeListener l) {
  201.         if (propertyChangeSupport == null) return;
  202.         propertyChangeSupport.removePropertyChangeListener(l);
  203.     }
  204.     
  205.     protected void firePropertyChange(String property, Object oldValue, Object newValue) {
  206.         if (propertyChangeSupport == null) return;
  207.         propertyChangeSupport.firePropertyChange(property, oldValue, newValue);
  208.     }
  209.     
  210.     protected void firePropertyChange(String property, boolean oldValue, boolean newValue) {
  211.         if (propertyChangeSupport == null) return;
  212.         propertyChangeSupport.firePropertyChange(property, oldValue, newValue);
  213.         
  214.     }
  215.     private PropertyChangeSupport getPropertyChangeSupport() {
  216.      if (propertyChangeSupport == null) {
  217.          propertyChangeSupport = new PropertyChangeSupport(this);
  218.      }
  219.     return propertyChangeSupport;
  220. }
  221.     // Comparable interface for sorting.
  222.     public int compareTo(Object obj) {
  223.         if (obj == null) {
  224.             return 1;
  225.         }
  226.         if (obj == this) {
  227.             return 0;
  228.         }
  229.         return text.compareTo(((LinkModel) obj).text);
  230.     }
  231.     public boolean equals(Object obj) {
  232.         if (this == obj) {
  233.             return true;
  234.         }
  235.         if (obj != null && obj instanceof LinkModel) {
  236.             LinkModel other = (LinkModel) obj;
  237.             if (!getText().equals(other.getText())) {
  238.                 return false;
  239.             }
  240.             if (!getTarget().equals(other.getTarget())) {
  241.                 return false;
  242.             }
  243.             if (!getURL().equals(other.getURL())) {
  244.                 return false;
  245.             }
  246.             return true;
  247.         }
  248.         return false;
  249.     }
  250.     public int hashCode() {
  251.         int result = 7;
  252.         result = 37 * result + ((getText() == null) ? 0 : getText().hashCode());
  253.         result = 37 * result
  254.                 + ((getTarget() == null) ? 1 : getTarget().hashCode());
  255.         result = 37 * result + ((getURL() == null) ? 2 : getURL().hashCode());
  256.         return result;
  257.     }
  258.     public String toString() {
  259.         StringBuffer buffer = new StringBuffer("[");
  260.         // RG: Fix for J2SE 5.0; Can't cascade append() calls because
  261.         // return type in StringBuffer and AbstractStringBuilder are different
  262.         buffer.append("url=");
  263.         buffer.append(url);
  264.         buffer.append(", target=");
  265.         buffer.append(target);
  266.         buffer.append(", text=");
  267.         buffer.append(text);
  268.         buffer.append("]");
  269.         return buffer.toString();
  270.     }
  271. }