1 package com.ochafik.swing.candy; 2 3 import java.awt.Cursor; 4 import java.awt.event.MouseAdapter; 5 import java.awt.event.MouseEvent; 6 7 import javax.swing.JLabel; 8 9 import com.ochafik.xml.XMLUtils; 10 11 public class JLinkLabel extends JLabel { 12 private static final long serialVersionUID = -2619802894792618038L; 13 Runnable linkAction; 14 boolean actInNewThread = false; 15 16 public JLinkLabel(String text) { 17 super(text); 18 init(null); 19 } 20 21 public JLinkLabel(String text, int orientation) { 22 super(text, orientation); 23 init(null); 24 } 25 26 public JLinkLabel(String text, int orientation, Runnable linkAction) { 27 this(text, orientation, linkAction, false); 28 } 29 public JLinkLabel(String text, int orientation, Runnable linkAction, boolean actInNewThread) { 30 super(text, orientation); 31 init(linkAction); 32 setActInNewThread(actInNewThread); 33 } 34 public JLinkLabel(String text, Runnable linkAction) { 35 this(text, linkAction, false); 36 } 37 public JLinkLabel(String text, Runnable linkAction, boolean actInNewThread) { 38 super(text); 39 init(linkAction); 40 setActInNewThread(actInNewThread); 41 } 42 43 public void setActInNewThread(boolean actInNewThread) { 44 this.actInNewThread = actInNewThread; 45 } 46 public boolean isActInNewThread() { 47 return actInNewThread; 48 } 49 50 protected void enabledChanged() { 51 String text = actualText; 52 if (getLinkAction() == null) { 53 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 54 } else { 55 if (isEnabled()) { 56 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 57 if (!actualText.startsWith("<html>")) 58 text = "<html><body width='100%'><a href='#'>" + XMLUtils.escapeEntities(actualText) + "</a></body></html>"; 59 60 } else { 61 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 62 if (!actualText.startsWith("<html>")) 63 text = "<html><body width='100%'><u>" + XMLUtils.escapeEntities(actualText) + "</u></body></html>"; 64 } 65 } 66 super.setText(text); 67 setMaximumSize(getMinimumSize()); 68 } 69 String actualText; 70 @Override 71 public void setText(String text) { 72 this.actualText = text; 73 enabledChanged(); 74 } 75 private void init(Runnable linkAction2) { 76 setLinkAction(linkAction2); 77 enabledChanged(); 78 addMouseListener(new MouseAdapter() { 79 @Override 80 public void mouseClicked(MouseEvent arg0) { 81 action(); 82 } 83 @Override 84 public void mouseEntered(MouseEvent arg0) { 85 if (isEnabled() && getLinkAction() != null) 86 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 87 else 88 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 89 } 90 }); 91 } 92 93 public boolean action() { 94 Runnable linkAction = getLinkAction(); 95 if (!isEnabled() || linkAction == null) 96 return false; 97 98 if (isActInNewThread()) 99 new Thread(linkAction).start(); 100 else 101 linkAction.run(); 102 return true; 103 } 104 105 public void setLinkAction(Runnable linkAction) { 106 this.linkAction = linkAction; 107 enabledChanged(); 108 } 109 public Runnable getLinkAction() { 110 return linkAction; 111 } 112 113 }