View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 package com.sun.xml.registry.client.browser; 11 12 import java.awt.*; 13 import java.awt.event.*; 14 import java.awt.font.TextAttribute; 15 16 import javax.swing.*; 17 import java.net.*; 18 19 /* 20 * Based upon code from: 21 * http://forum.java.sun.com/thread.jsp?thread=328882&forum=57&message=1337973 22 * 23 */ 24 public class HyperLinkLabel extends JLabel implements HyperLinkContainer { 25 26 Color stdFG = Color.BLACK; 27 Font stdFont; 28 Font urlFont; 29 Color urlNormalFG = Color.BLUE; 30 Color urlHiliteFG = Color.RED; 31 String url; 32 MouseListener mouseListener; 33 34 HyperLinkContainer linkContainer = null; 35 36 public HyperLinkLabel() { 37 this(null, null, 0); 38 } 39 40 public HyperLinkLabel(Icon image) { 41 this(null, image, 0); 42 } 43 44 public HyperLinkLabel(Icon image, int horizontalAlignment) { 45 this(null, image, horizontalAlignment); 46 } 47 48 public HyperLinkLabel(String text) { 49 this(text, null, 0); 50 } 51 52 public HyperLinkLabel(String text, int horizontalAlignment) { 53 this(text, null, horizontalAlignment); 54 } 55 56 public HyperLinkLabel(String text, Icon icon, int horizontalAlignment) { 57 super(icon, horizontalAlignment); 58 59 stdFG = this.getForeground(); 60 stdFont = this.getFont(); 61 62 //Make urlFont be bold and underlined 63 urlFont = stdFont.deriveFont(Font.BOLD); 64 //java.util.HashMap textAttributes = new java.util.HashMap(stdFont.getAttributes()); 65 //textAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); 66 //urlFont = urlFont.deriveFont(textAttributes); 67 68 setText(text); 69 70 //Create mouse listener 71 mouseListener = new MouseAdapter() { 72 public void mouseClicked(MouseEvent e){ 73 String _url = getURL(); 74 if ((_url != null) && (_url.length() > 0)) { 75 HyperLinker.displayURL(_url); 76 } 77 } 78 public void mouseEntered(MouseEvent e){ 79 String _url = getURL(); 80 if ((_url != null) && (_url.length() > 0)) { 81 HyperLinkLabel.this.setCursor(new Cursor(Cursor.HAND_CURSOR)); 82 HyperLinkLabel.this.setForeground(urlHiliteFG); 83 } 84 } 85 public void mouseExited(MouseEvent e){ 86 String _url = getURL(); 87 if ((_url != null) && (_url.length() > 0)) { 88 HyperLinkLabel.this.setForeground(urlNormalFG); 89 HyperLinkLabel.this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 90 } 91 } 92 93 }; 94 } 95 96 97 public String getURL() { 98 if (this.linkContainer != null) { 99 url = this.linkContainer.getURL(); 100 } 101 return url; 102 } 103 104 public void setURL(String urlString) throws MalformedURLException { 105 106 removeMouseListener(mouseListener); 107 this.url=null; 108 linkContainer = null; 109 110 setForeground(stdFG); 111 //setFont(stdFont); 112 113 setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 114 //Check if URL is valid 115 URL _url = null; 116 if (urlString != null) { 117 _url = new URL(urlString); 118 } 119 120 if (_url != null) { 121 //Valid URL. Use it. 122 this.url=urlString; 123 linkContainer = null; 124 125 setForeground(urlNormalFG); 126 //setFont(urlFont); 127 128 addMouseListener(mouseListener); 129 } 130 131 } 132 133 134 public HyperLinkContainer getHyperLinkContainer() { 135 HyperLinkContainer _linkContainer = linkContainer; 136 if (_linkContainer == null) { 137 _linkContainer = this; 138 } 139 140 return _linkContainer; 141 } 142 143 public void setHyperLinkContainer(HyperLinkContainer linkContainer) { 144 this.linkContainer=linkContainer; 145 146 setForeground(urlNormalFG); 147 //setFont(urlFont); 148 149 addMouseListener(mouseListener); 150 } 151 152 public void setText(String text) { 153 super.setText(text); 154 155 //Check if URL is valid 156 URL _url = null; 157 try { 158 _url = new URL(text); 159 160 //setURL if text is a valid URL 161 setURL(text); 162 } 163 catch (MalformedURLException e) { 164 //No need to do anything. It is normal for tetx to not be a URL 165 } 166 167 } 168 169 public static void main(String[] args) throws MalformedURLException { 170 JFrame f=new JFrame("HyperLinkLabel Tester"); 171 f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 172 Container c=f.getContentPane(); 173 HyperLinkLabel lb=new HyperLinkLabel("click me"); 174 lb.setURL("http://ebxmlrr.sourceforge.net"); 175 176 177 c.add(lb); 178 f.pack(); 179 f.show(); 180 181 } 182 183 }

This page was automatically generated by Maven