KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > util > URLLabel


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.gui.util;
17
18 import java.awt.Color JavaDoc;
19 import java.awt.Cursor JavaDoc;
20 import java.awt.Graphics JavaDoc;
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.event.MouseEvent JavaDoc;
23 import java.awt.event.MouseListener JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JPopupMenu JavaDoc;
28
29
30
31 public class URLLabel extends JLabel JavaDoc implements MouseListener JavaDoc {
32     private JPopupMenu JavaDoc popup;
33     boolean entered = false;
34     boolean mousehover;
35
36     public URLLabel(URL JavaDoc url) {
37         this(url, url.toString());
38     }
39
40     public URLLabel(URL JavaDoc url, String JavaDoc str) {
41         super(str);
42
43         addMouseListener(this);
44         setForeground(Color.blue);
45         mousehover = false;
46
47         URLController controller = new URLController();
48         controller.setLink(url);
49         popup = controller.createLinkMenu();
50     }
51
52     public void mouseClicked(MouseEvent JavaDoc e) {
53         popup.show(e.getComponent(), e.getX(), e.getY());
54     }
55
56     public void mouseEntered(MouseEvent JavaDoc e) {
57         setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
58         entered = true;
59
60         if (mousehover) {
61             repaint();
62         }
63     }
64
65     public void mouseExited(MouseEvent JavaDoc e) {
66         setCursor(Cursor.getDefaultCursor());
67         entered = false;
68
69         if (mousehover) {
70             repaint();
71         }
72     }
73
74     public void mousePressed(MouseEvent JavaDoc e) {
75     }
76
77     public void mouseReleased(MouseEvent JavaDoc e) {
78     }
79
80     public void paint(Graphics JavaDoc g) {
81         super.paint(g);
82
83         if (entered || !mousehover) {
84             Rectangle JavaDoc r = g.getClipBounds();
85
86             g.drawLine(0,
87                 r.height - this.getFontMetrics(this.getFont()).getDescent(),
88                 this.getFontMetrics(this.getFont()).stringWidth(this.getText()),
89                 r.height - this.getFontMetrics(this.getFont()).getDescent());
90         }
91     }
92 }
93
Popular Tags