01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
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;
19: import java.awt.Cursor;
20: import java.awt.Graphics;
21: import java.awt.Rectangle;
22: import java.awt.event.MouseEvent;
23: import java.awt.event.MouseListener;
24: import java.net.URL;
25:
26: import javax.swing.JLabel;
27: import javax.swing.JPopupMenu;
28:
29: public class URLLabel extends JLabel implements MouseListener {
30: private JPopupMenu popup;
31: boolean entered = false;
32: boolean mousehover;
33:
34: public URLLabel(URL url) {
35: this (url, url.toString());
36: }
37:
38: public URLLabel(URL url, String str) {
39: super (str);
40:
41: addMouseListener(this );
42: setForeground(Color.blue);
43: mousehover = false;
44:
45: URLController controller = new URLController();
46: controller.setLink(url);
47: popup = controller.createLinkMenu();
48: }
49:
50: public void mouseClicked(MouseEvent e) {
51: popup.show(e.getComponent(), e.getX(), e.getY());
52: }
53:
54: public void mouseEntered(MouseEvent e) {
55: setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
56: entered = true;
57:
58: if (mousehover) {
59: repaint();
60: }
61: }
62:
63: public void mouseExited(MouseEvent e) {
64: setCursor(Cursor.getDefaultCursor());
65: entered = false;
66:
67: if (mousehover) {
68: repaint();
69: }
70: }
71:
72: public void mousePressed(MouseEvent e) {
73: }
74:
75: public void mouseReleased(MouseEvent e) {
76: }
77:
78: public void paint(Graphics g) {
79: super .paint(g);
80:
81: if (entered || !mousehover) {
82: Rectangle r = g.getClipBounds();
83:
84: g.drawLine(0, r.height
85: - this.getFontMetrics(this.getFont()).getDescent(),
86: this.getFontMetrics(this.getFont()).stringWidth(
87: this.getText()), r.height
88: - this.getFontMetrics(this.getFont())
89: .getDescent());
90: }
91: }
92: }
|