KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > Link


1 /*
2  * $Id: Link.java,v 1.2 2004/07/16 18:43:57 rameshgupta Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.data;
9
10 import java.net.MalformedURLException JavaDoc;
11 import java.net.URL JavaDoc;
12
13 /**
14  * An object which represents an URL link in a table cell, tree cell or
15  * list item.
16  *
17  * @author Mark Davidson
18  */

19 public class Link implements Comparable JavaDoc {
20
21     private String JavaDoc text; // display text
22
private URL JavaDoc url; // url of the link
23
private String JavaDoc target; // url target frame
24

25     private boolean visited = false;
26
27     public Link(String JavaDoc text, String JavaDoc target, URL JavaDoc url) {
28     setText(text);
29     setTarget(target);
30     setURL(url);
31     }
32
33     /**
34      * @param text text to that a renderer would display
35      * @param target the target that a URL should load into.
36      * @param template a string that represents a URL with @{N} place holders
37      * for string substitution
38      * @param args an array of strings which will be used for substitition
39      */

40     public Link(String JavaDoc text, String JavaDoc target, String JavaDoc template, String JavaDoc[] args) {
41     setText(text);
42     setTarget(target);
43     setURL(createURL(template, args));
44     }
45
46     /**
47      * Set the display text.
48      */

49     public void setText(String JavaDoc text) { this.text = text; }
50     public String JavaDoc getText() {
51     if (text != null) {
52         return text;
53     } else {
54         return getURL().toString();
55     }
56     }
57
58     /**
59      * Set the url.
60      */

61     public void setURL(URL JavaDoc url) {
62     if (url == null) {
63         throw new IllegalArgumentException JavaDoc("URL for link cannot be null");
64     }
65     this.url = url;
66     }
67     public URL JavaDoc getURL() { return url; }
68
69     /**
70      * Create a URL from a template string that has place holders and
71      * an array of strings which will be substituted into the place holders.
72      * The place holders are represented as @{N} where N = { 1..n }
73      * <p>
74      * For example, if the template contains a string like:
75      * http://bugz.sfbay/cgi-bin/showbug?cat=@{1}&sub_cat=@{2}
76      * and a two arg array contains:
77      * java, classes_swing
78      * The resulting URL will be:
79      * http://bugz.sfbay/cgi-bin/showbug?cat=java&sub_cat=classes_swing
80      * <p>
81      * @param template a url string that contains the placeholders
82      * @param args an array of strings that will be substituted
83      */

84     private URL JavaDoc createURL(String JavaDoc template, String JavaDoc[] args) {
85     URL JavaDoc url = null;
86     try {
87         String JavaDoc urlStr = template;
88         for (int i = 0; i < args.length; i++) {
89         urlStr = urlStr.replaceAll("@\\{" + (i + 1) + "\\}", args[i]);
90         }
91         url = new URL JavaDoc(urlStr);
92     } catch (MalformedURLException JavaDoc ex) {
93         //
94
}
95     return url;
96     }
97
98     /**
99      * Set the target that the URL should load into. This can be a
100      * uri representing another control or the name of a window or
101      * special targets. See:
102      * http://www.w3c.org/TR/html401/present/frames.html#adef-target
103      */

104     public void setTarget(String JavaDoc target) { this.target = target; }
105
106     /**
107      * Return the target for the URL.
108      *
109      * @return value of the target. If null then "_blank" will be returned.
110      */

111     public String JavaDoc getTarget() {
112     if (target != null) {
113         return target;
114     } else {
115         return "_blank";
116     }
117     }
118
119     /**
120      * Sets a flag to indicate if the link has been visited. The state
121      * of this flag can be used to render the color of the link.
122      */

123     public void setVisited(boolean visited) { this.visited = visited; }
124     public boolean getVisited() { return visited; }
125
126
127     // Comparable interface for sorting.
128
public int compareTo(Object JavaDoc obj) {
129     if (obj == null) {
130         return 1;
131     }
132     if (obj == this) {
133         return 0;
134     }
135     return text.compareTo(((Link)obj).text);
136     }
137
138     public boolean equals(Object JavaDoc obj) {
139     if (this == obj) {
140         return true;
141     }
142     if (obj != null && obj instanceof Link) {
143         Link other = (Link)obj;
144         if (!getText().equals(other.getText())) {
145         return false;
146         }
147
148         if (!getTarget().equals(other.getTarget())) {
149         return false;
150         }
151
152         if (!getURL().equals(other.getURL())) {
153         return false;
154         }
155         return true;
156     }
157     return false;
158     }
159
160     public int hashCode() {
161     int result = 7;
162
163     result = 37 * result + ((getText() == null) ? 0: getText().hashCode());
164     result = 37 * result + ((getTarget() == null) ? 1: getTarget().hashCode());
165     result = 37 * result + ((getURL() == null) ? 2: getURL().hashCode());
166
167     return result;
168     }
169
170     public String JavaDoc toString() {
171
172     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("[");
173     // RG: Fix for J2SE 5.0; Can't cascade append() calls because
174
// return type in StringBuffer and AbstractStringBuilder are different
175
buffer.append("url=");
176     buffer.append(url);
177     buffer.append(", target=");
178     buffer.append(target);
179     buffer.append(", text=");
180     buffer.append(text);
181     buffer.append("]");
182
183     return buffer.toString();
184     }
185 }
186
Popular Tags