View Javadoc
1 /* 2 * (C) 2002 David Carr david@carr.name 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package net.sourceforge.mflow.xul; 20 21 import java.awt.Cursor; 22 import java.awt.event.MouseAdapter; 23 import java.awt.event.MouseEvent; 24 import java.net.MalformedURLException; 25 import java.net.URL; 26 import java.util.logging.Logger; 27 import java.util.regex.Matcher; 28 import java.util.regex.Pattern; 29 30 import javax.swing.JLabel; 31 32 import apollo.ServiceManager; 33 34 /*** 35 * Label to display a URL and handle opening it in a browser Uses the 36 * BrowserLauncher component 37 * 38 * @author <a href="mailto:david@carr.name">David Carr</a> 39 * @version $Revision: 1.3 $ 40 */ 41 public class URLLabel extends JLabel { 42 protected final Logger log = Logger.getLogger(getClass().getName()); 43 protected String strURL; 44 45 /*** 46 * Default constructor, makes a new instance with no value or url 47 */ 48 public URLLabel() { 49 super(); 50 addMouseListener(new ClickListener()); 51 } 52 53 /*** 54 * @param value the new value for the label 55 */ 56 public void setValue(final String value) { 57 setText(buildHTML(value)); 58 } 59 60 /*** 61 * @param url the new URL for the link 62 */ 63 public void setUrl(final String url) { 64 this.strURL = url; 65 } 66 67 private static String buildHTML(final String linkText) { 68 StringBuffer sb = new StringBuffer(); 69 sb.append("<html>"); 70 sb.append("<font color=\"blue\"><u>"); 71 sb.append(escapeChars(linkText)); 72 sb.append("</u></font>"); 73 sb.append("</html>"); 74 return sb.toString(); 75 } 76 77 /*** 78 * Escapes chars for HTML (<, >) 79 * 80 * @param str the string to escape 81 * @return the escaped string 82 */ 83 private static String escapeChars(final String str) { 84 Pattern p1 = Pattern.compile("<"); 85 Pattern p2 = Pattern.compile(">"); 86 Matcher m1 = p1.matcher(str); 87 Matcher m2 = p2.matcher(m1.replaceAll("<")); 88 return m2.replaceAll(">"); 89 } 90 91 private class ClickListener extends MouseAdapter { 92 /*** 93 * Handles clicks by opening the URL in the default browser 94 * 95 * @param e the click event 96 */ 97 public void mouseClicked(MouseEvent e) { 98 try { 99 URL url = new URL(URLLabel.this.strURL); 100 ServiceManager.lookupBasicService().showDocument(url); 101 } catch (MalformedURLException mue) { 102 URLLabel.this.log.throwing(getClass().getName(), "mouseClicked", mue); 103 } 104 } 105 106 /*** 107 * @param e the event to process 108 */ 109 public void mouseEntered(final MouseEvent e) { 110 setCursor(new Cursor(Cursor.HAND_CURSOR)); 111 } 112 113 /*** 114 * @param e the event to process 115 */ 116 public void mouseExited(final MouseEvent e) { 117 setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 118 } 119 } 120 }

This page was automatically generated by Maven