001    package com.ochafik.swing.candy;
002    
003    import java.awt.Cursor;
004    import java.awt.event.MouseAdapter;
005    import java.awt.event.MouseEvent;
006    
007    import javax.swing.JLabel;
008    
009    import com.ochafik.xml.XMLUtils;
010    
011    public class JLinkLabel extends JLabel {
012            private static final long serialVersionUID = -2619802894792618038L;
013            Runnable linkAction;
014            boolean actInNewThread = false;
015            
016            public JLinkLabel(String text) {
017                    super(text);
018                    init(null);
019            }
020            
021            public JLinkLabel(String text, int orientation) {
022                    super(text, orientation);
023                    init(null);
024            }
025            
026            public JLinkLabel(String text, int orientation, Runnable linkAction) {
027                    this(text, orientation, linkAction, false);
028            }
029            public JLinkLabel(String text, int orientation, Runnable linkAction, boolean actInNewThread) {
030                    super(text, orientation);
031                    init(linkAction);
032                    setActInNewThread(actInNewThread);
033            }
034            public JLinkLabel(String text, Runnable linkAction) {
035                    this(text, linkAction, false);
036            }
037            public JLinkLabel(String text, Runnable linkAction, boolean actInNewThread) {
038                    super(text);
039                    init(linkAction);
040                    setActInNewThread(actInNewThread);
041            }
042            
043            public void setActInNewThread(boolean actInNewThread) {
044                    this.actInNewThread = actInNewThread;
045            }
046            public boolean isActInNewThread() {
047                    return actInNewThread;
048            }
049            
050            protected void enabledChanged() {
051                    String text = actualText;
052                    if (getLinkAction() == null) {
053                            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
054                    } else {
055                            if (isEnabled()) {
056                                    setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
057                                    if (!actualText.startsWith("<html>"))
058                                            text = "<html><body width='100%'><a href='#'>" + XMLUtils.escapeEntities(actualText) + "</a></body></html>";
059                                    
060                            } else {
061                                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
062                                    if (!actualText.startsWith("<html>"))
063                                            text = "<html><body width='100%'><u>" + XMLUtils.escapeEntities(actualText) + "</u></body></html>";
064                            }
065                    }
066                    super.setText(text);
067                    setMaximumSize(getMinimumSize());
068            }
069            String actualText;
070            @Override
071            public void setText(String text) {
072                    this.actualText = text;
073                    enabledChanged();
074            }
075            private void init(Runnable linkAction2) {
076                    setLinkAction(linkAction2);
077                    enabledChanged();
078                    addMouseListener(new MouseAdapter() {
079                            @Override
080                            public void mouseClicked(MouseEvent arg0) {
081                                    action();
082                            }
083                            @Override
084                            public void mouseEntered(MouseEvent arg0) {
085                                    if (isEnabled() && getLinkAction() != null)
086                                            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
087                                    else
088                                            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
089                            }
090                    });
091            }
092            
093            public boolean action() {
094                    Runnable linkAction = getLinkAction();
095                    if (!isEnabled() || linkAction == null)
096                            return false;
097                    
098                    if (isActInNewThread())
099                            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    }