KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > HyperlinkLabel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * HyperlinkLabel.java
22  *
23  * Created on September 18, 2006, 7:31 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.schema.abe;
30
31 import java.awt.Color JavaDoc;
32 import java.awt.Cursor JavaDoc;
33 import java.awt.Font JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Rectangle JavaDoc;
36 import java.awt.event.MouseEvent JavaDoc;
37 import java.awt.event.MouseListener JavaDoc;
38 import javax.swing.JLabel JavaDoc;
39
40 /**
41  *
42  * @author girix
43  */

44 public class HyperlinkLabel extends JLabel JavaDoc{
45     private static final long serialVersionUID = -483941387931729295L;
46     /** Creates a new instance of HyperlinkLabel */
47     public HyperlinkLabel() {
48         super();
49         initialize();
50     }
51     
52     private void initialize(){
53         initMouseListener();
54     }
55     
56     boolean mouseIn = false;
57     private void initMouseListener() {
58         addMouseListener(new MouseListener JavaDoc() {
59             public void mouseClicked(MouseEvent JavaDoc e) {
60                 if(hyperlinkClickHandler != null)
61                     hyperlinkClickHandler.handleClick();
62             }
63             public void mouseEntered(MouseEvent JavaDoc e) {
64                 if(hyperlinkClickHandler != null){
65                     mouseIn = true;
66                     HyperlinkLabel.this.setCursor(new Cursor JavaDoc(Cursor.HAND_CURSOR));
67                     repaint();
68                 }
69             }
70             public void mouseExited(MouseEvent JavaDoc e) {
71                 if(hyperlinkClickHandler != null){
72                     mouseIn = false;
73                     HyperlinkLabel.this.setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
74                     repaint();
75                 }
76             }
77             public void mousePressed(MouseEvent JavaDoc e) {
78             }
79             public void mouseReleased(MouseEvent JavaDoc e) {
80             }
81         });
82     }
83     
84     protected void paintComponent(Graphics JavaDoc g) {
85         if(mouseIn){
86             //draw diff color and underline
87
Color JavaDoc origC = getForeground();
88             Font JavaDoc origF = getFont();
89             Rectangle JavaDoc bounds = g.getClipBounds();
90             Color JavaDoc bak = getForeground();
91             setForeground(Color.BLUE);
92             Color JavaDoc gbak = g.getColor();
93             g.setColor(Color.blue);
94             super.paintComponent(g);
95             int width = bounds.width;
96             g.drawLine(bounds.x+5, bounds.y + bounds.height -1,
97                     bounds.x+5 + width - 5, bounds.y + bounds.height -1);
98             setForeground(bak);
99             g.setColor(gbak);
100         }else{
101             super.paintComponent(g);
102         }
103     }
104     
105     
106     HyperlinkClickHandler hyperlinkClickHandler;
107     public void setHyperlinkClickHandler(HyperlinkClickHandler hyperlinkClickHandler){
108         this.hyperlinkClickHandler = hyperlinkClickHandler;
109         
110     }
111     
112     public interface HyperlinkClickHandler{
113         public void handleClick();
114     }
115     
116 }
117
Popular Tags