1 7 8 package org.jdesktop.swing.data; 9 10 import java.net.MalformedURLException ; 11 import java.net.URL ; 12 13 19 public class Link implements Comparable { 20 21 private String text; private URL url; private String target; 25 private boolean visited = false; 26 27 public Link(String text, String target, URL url) { 28 setText(text); 29 setTarget(target); 30 setURL(url); 31 } 32 33 40 public Link(String text, String target, String template, String [] args) { 41 setText(text); 42 setTarget(target); 43 setURL(createURL(template, args)); 44 } 45 46 49 public void setText(String text) { this.text = text; } 50 public String getText() { 51 if (text != null) { 52 return text; 53 } else { 54 return getURL().toString(); 55 } 56 } 57 58 61 public void setURL(URL url) { 62 if (url == null) { 63 throw new IllegalArgumentException ("URL for link cannot be null"); 64 } 65 this.url = url; 66 } 67 public URL getURL() { return url; } 68 69 84 private URL createURL(String template, String [] args) { 85 URL url = null; 86 try { 87 String urlStr = template; 88 for (int i = 0; i < args.length; i++) { 89 urlStr = urlStr.replaceAll("@\\{" + (i + 1) + "\\}", args[i]); 90 } 91 url = new URL (urlStr); 92 } catch (MalformedURLException ex) { 93 } 95 return url; 96 } 97 98 104 public void setTarget(String target) { this.target = target; } 105 106 111 public String getTarget() { 112 if (target != null) { 113 return target; 114 } else { 115 return "_blank"; 116 } 117 } 118 119 123 public void setVisited(boolean visited) { this.visited = visited; } 124 public boolean getVisited() { return visited; } 125 126 127 public int compareTo(Object 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 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 toString() { 171 172 StringBuffer buffer = new StringBuffer ("["); 173 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 |