1 19 20 package org.netbeans.modules.tasklist.usertasks.util; 21 22 import java.awt.Color ; 23 import java.awt.Cursor ; 24 import java.awt.FontMetrics ; 25 import java.awt.Graphics ; 26 import java.awt.Insets ; 27 import java.awt.Rectangle ; 28 import java.net.URL ; 29 30 import javax.swing.Action ; 31 import javax.swing.ButtonModel ; 32 import javax.swing.Icon ; 33 import javax.swing.JButton ; 34 import javax.swing.JComponent ; 35 import javax.swing.UIManager ; 36 import javax.swing.plaf.ComponentUI ; 37 import javax.swing.plaf.metal.MetalButtonUI ; 38 39 47 public class JLinkButton extends JButton { 48 private static final String uiString = "LinkButtonUI"; 49 50 51 public static final int ALWAYS_UNDERLINE = 0; 52 53 54 public static final int HOVER_UNDERLINE = 1; 55 56 57 public static final int NEVER_UNDERLINE = 2; 58 59 62 public static void registerUI() { 63 UIManager.getDefaults().put("LinkButtonUI", "BasicLinkButtonUI"); 64 } 65 66 private int linkBehavior; 67 private Color linkColor; 68 private Color colorPressed; 69 private Color visitedLinkColor; 70 private Color disabledLinkColor; 71 private URL buttonURL; 72 private boolean isLinkVisited; 73 74 77 public JLinkButton() { 78 this(null, null, null); 79 } 80 81 86 public JLinkButton(Action action) { 87 this(); 88 setAction(action); 89 } 90 91 96 public JLinkButton(Icon icon) { 97 this(null, icon, null); 98 } 99 100 105 public JLinkButton(String s) { 106 this(s, null, null); 107 } 108 109 114 public JLinkButton(URL url) { 115 this(null, null, url); 116 } 117 118 124 public JLinkButton(String s, URL url) { 125 this(s, null, url); 126 } 127 128 134 public JLinkButton(Icon icon, URL url) { 135 this(null, icon, url); 136 } 137 138 145 public JLinkButton(String text, Icon icon, URL url) { 146 super(text, icon); 147 linkBehavior = ALWAYS_UNDERLINE; 148 linkColor = Color.blue; 149 colorPressed = Color.red; 150 visitedLinkColor = new Color (128, 0, 128); 151 if (text == null && url != null) 152 setText(url.toExternalForm()); 153 setLinkURL(url); 154 setCursor(Cursor.getPredefinedCursor(12)); 155 setBorderPainted(false); 156 setContentAreaFilled(false); 157 setRolloverEnabled(true); 158 setMargin(new Insets (0, 0, 0, 0)); 159 } 160 161 public void updateUI() { 162 setUI(BasicLinkButtonUI.createUI(this)); 163 } 164 165 166 public String getUIClassID() { 167 return "LinkButtonUI"; 168 } 169 170 173 private void setupToolTipText() { 174 String tip = null; 175 if (buttonURL != null) 176 tip = buttonURL.toExternalForm(); 177 setToolTipText(tip); 178 } 179 180 185 public void setLinkBehavior(int bnew) { 186 if (bnew != ALWAYS_UNDERLINE && bnew != HOVER_UNDERLINE 187 && bnew != NEVER_UNDERLINE) 188 throw new IllegalArgumentException ("Not a legal LinkBehavior"); 189 190 int old = linkBehavior; 191 linkBehavior = bnew; 192 firePropertyChange("linkBehavior", old, bnew); 193 repaint(); 194 } 195 196 201 public int getLinkBehavior() { 202 return linkBehavior; 203 } 204 205 210 public void setLinkColor(Color color) { 211 Color colorOld = linkColor; 212 linkColor = color; 213 firePropertyChange("linkColor", colorOld, color); 214 repaint(); 215 } 216 217 222 public Color getLinkColor() { 223 return linkColor; 224 } 225 226 231 public void setActiveLinkColor(Color colorNew) { 232 Color colorOld = colorPressed; 233 colorPressed = colorNew; 234 firePropertyChange("activeLinkColor", colorOld, colorNew); 235 repaint(); 236 } 237 238 243 public Color getActiveLinkColor() { 244 return colorPressed; 245 } 246 247 252 public void setDisabledLinkColor(Color color) { 253 Color colorOld = disabledLinkColor; 254 disabledLinkColor = color; 255 firePropertyChange("disabledLinkColor", colorOld, color); 256 if (!isEnabled()) 257 repaint(); 258 } 259 260 265 public Color getDisabledLinkColor() { 266 return disabledLinkColor; 267 } 268 269 274 public void setVisitedLinkColor(Color colorNew) { 275 Color colorOld = visitedLinkColor; 276 visitedLinkColor = colorNew; 277 firePropertyChange("visitedLinkColor", colorOld, colorNew); 278 repaint(); 279 } 280 281 286 public Color getVisitedLinkColor() { 287 return visitedLinkColor; 288 } 289 290 295 public URL getLinkURL() { 296 return buttonURL; 297 } 298 299 304 public void setLinkURL(URL url) { 305 URL urlOld = buttonURL; 306 buttonURL = url; 307 setupToolTipText(); 308 firePropertyChange("linkURL", urlOld, url); 309 revalidate(); 310 repaint(); 311 } 312 313 318 public void setLinkVisited(boolean flagNew) { 319 boolean flagOld = isLinkVisited; 320 isLinkVisited = flagNew; 321 firePropertyChange("linkVisited", flagOld, flagNew); 322 repaint(); 323 } 324 325 330 public boolean isLinkVisited() { 331 return isLinkVisited; 332 } 333 334 protected String paramString() { 335 String str; 336 if (linkBehavior == ALWAYS_UNDERLINE) 337 str = "ALWAYS_UNDERLINE"; 338 else if (linkBehavior == HOVER_UNDERLINE) 339 str = "HOVER_UNDERLINE"; 340 else if (linkBehavior == NEVER_UNDERLINE) 341 str = "NEVER_UNDERLINE"; 342 else 343 str = "SYSTEM_DEFAULT"; 344 String colorStr = linkColor == null ? "" : linkColor.toString(); 345 String colorPressStr = colorPressed == null ? "" : colorPressed 346 .toString(); 347 String disabledLinkColorStr = disabledLinkColor == null ? "" 348 : disabledLinkColor.toString(); 349 String visitedLinkColorStr = visitedLinkColor == null ? "" 350 : visitedLinkColor.toString(); 351 String buttonURLStr = buttonURL == null ? "" : buttonURL.toString(); 352 String isLinkVisitedStr = isLinkVisited ? "true" : "false"; 353 return super.paramString() + ",linkBehavior=" + str + ",linkURL=" 354 + buttonURLStr + ",linkColor=" + colorStr + ",activeLinkColor=" 355 + colorPressStr + ",disabledLinkColor=" + disabledLinkColorStr 356 + ",visitedLinkColor=" + visitedLinkColorStr 357 + ",linkvisitedString=" + isLinkVisitedStr; 358 } 359 } 360 361 364 class BasicLinkButtonUI extends MetalButtonUI { 365 private static final BasicLinkButtonUI ui = new BasicLinkButtonUI(); 366 367 373 public static ComponentUI createUI(JComponent jcomponent) { 374 return ui; 375 } 376 377 protected void paintText(Graphics g, JComponent com, Rectangle rect, 378 String s) { 379 JLinkButton bn = (JLinkButton) com; 380 ButtonModel bnModel = bn.getModel(); 381 Color color = bn.getForeground(); 382 Object obj = null; 383 if (bnModel.isEnabled()) { 384 if (bnModel.isPressed()) 385 bn.setForeground(bn.getActiveLinkColor()); 386 else if (bn.isLinkVisited()) 387 bn.setForeground(bn.getVisitedLinkColor()); 388 389 else 390 bn.setForeground(bn.getLinkColor()); 391 } else { 392 if (bn.getDisabledLinkColor() != null) 393 bn.setForeground(bn.getDisabledLinkColor()); 394 } 395 super.paintText(g, com, rect, s); 396 int behaviour = bn.getLinkBehavior(); 397 boolean drawLine = false; 398 if (behaviour == JLinkButton.HOVER_UNDERLINE) { 399 if (bnModel.isRollover()) 400 drawLine = true; 401 } else if (behaviour == JLinkButton.ALWAYS_UNDERLINE) 402 drawLine = true; 403 if (!drawLine) 404 return; 405 FontMetrics fm = g.getFontMetrics(); 406 int x = rect.x + getTextShiftOffset(); 407 int y = (rect.y + fm.getAscent() + fm.getDescent() + 408 getTextShiftOffset()) - 1; 409 if (bnModel.isEnabled()) { 410 g.setColor(bn.getForeground()); 411 g.drawLine(x, y, (x + rect.width) - 1, y); 412 } else { 413 g.setColor(bn.getBackground().brighter()); 414 g.drawLine(x, y, (x + rect.width) - 1, y); 415 } 416 } 417 } | Popular Tags |