//Titre : linkLabel
//Version :
//Copyright : Copyright (c) 1999..2000
//Auteur : BERTHOU
//Société : BERTHOU
//Description : Exemple simple de creation d'un bean
package rblBeans ;
import java.awt.*;
import java.awt.event.*;
public class linkLabel extends Label
implements MouseListener
{
// URL
private String URL= null;
// Color for the link (not the text)
private Color linkColor, linkbgColor;
private Color sColor1, sColor2;
transient boolean bOn = false ;
transient ActionListener actionListener = null;
/**
* Creates a linkLabel with no specific data
*
*/
public linkLabel() {
this("", null, Color.blue, null);
}
/**
* Creates a linkLabel with no specific hyperlink
*
* @param label Text of label
*/
public linkLabel(String label)
{
this(label, null, Color.blue, null);
}
/**
* Creates a linkLabel hyperlinked to the URL.
*
* @param label Text of label
* @param URL Universal Resource Locator (URL)
*/
public linkLabel(String label, String URL)
{
this(label, URL, Color.blue, null);
}
/**
* Creates a linkLabel ,
*
* @param label Text of label
* @param URL Universal Resource Locator (URL)
* @param color Color of hyperlink
*/
public linkLabel(String label, String URL, Color color)
{
this(label, URL, color, null);
}
/**
* Creates a linkLabel with all data,
*
* @param label Text of label
* @param URL Universal Resource Locator (URL)
* @param color Color of hyperlink
* @param color BgColor of hyperlink
*/
public linkLabel(String label, String URL, Color color1, Color color2)
{
super(label);
setURL (URL);
setlinkColor(color1);
setlinkbgColor(color2);
addMouseListener(this);
}
/**
* Sets the URL destination for this hyperlink.
*
* @param URL Universal Resource Locator (URL)
*/
public void setURL(String URL)
{
this.URL=URL;
}
/**
* Sets the color for this hyperlink.
*
* @param color Color of hyperlink
*/
public void setlinkColor (Color color)
{
this.linkColor=color;
}
/**
* Returns the color for this hyperlink.
*
* @return Color of hyperlink
*/
public Color getlinkColor ()
{
return linkColor;
}
/**
* Sets the background color for this hyperlink.
*
* @param color background Color of hyperlink
*/
public void setlinkbgColor (Color color)
{
this.linkbgColor=color;
}
/**
* Returns the background color for this hyperlink.
*
* @return background Color of hyperlink
*/
public Color getlinkbgColor ()
{
return linkbgColor;
}
/**
* Returns the URL for this hyperlink.
*
* @return URL as a String
*/
public String getURL ()
{
return URL;
}
/**
* Paints the hyperlink
*
* @param g Graphics object for painting
*/
public void paint(Graphics g)
{
// Call label's paint method
super.paint(g);
// Check that a hyperlink was specified
if (URL == null) return;
// Set color for hyperlink
if (bOn) {
g.setColor ( linkColor );
}
// Get width of hyperlink label component
Dimension d = getSize();
int width = d.width;
int height= d.height;
// Get width of label text
FontMetrics metrics = getFontMetrics(getFont());
int strWidth = metrics.stringWidth( getText() );
int offset = width - strWidth ;
int align = getAlignment();
switch (align)
{
case LEFT :
g.drawLine ( 2, height-5, 2 +strWidth, height-5);
break;
case CENTER :
g.drawLine ( offset/2 + 1, height-5, width - offset/2 -1, height-5);
break;
case RIGHT :
g.drawLine ( offset - 2, height-5, width, height-5);
break;
}
}
/**
* Adds action listener to receive action events from
* this hyperlink.
*
* @param listener ActionListener to add to event queue
*/
public synchronized void addActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.add(actionListener, listener);
}
/**
* Removes action listener from the list of action listeners
* associated with this hyperlink.
*
* @param listener ActionListener to add to event queue
*/
public synchronized void removeActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.remove(actionListener, listener);
}
/**
* Event handler when the mouse is clcked on the hyperlink<br>
* send click action to all listeners
* @param MouseEvent Event
*/
public void mouseClicked(MouseEvent e) {
if (actionListener != null) {
actionListener.actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, "Clicked"));
}
}
/**
* Event handler when the cursor enters the hyperlink
* @param MouseEvent Event
*/
public void mouseEntered(MouseEvent e)
{
bOn = true ;
sColor1 = getForeground();
setForeground ( linkColor );
sColor2 = getBackground() ;
if (linkbgColor != null)
setBackground ( linkbgColor );
setCursor (new Cursor(Cursor.HAND_CURSOR));
}
/**
* Event handler for when the cursor leaves the hyperlink
* @param MouseEvent Event
*/
public void mouseExited(MouseEvent e)
{
bOn = false ;
setForeground ( sColor1 );
setBackground ( sColor2 );
setCursor (new Cursor(Cursor.DEFAULT_CURSOR));
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
/**
* Returns the preferred size of the hyperlink.
* @return Preferred dimensions
*/
public Dimension getPreferredSize()
{
// Shave a little off original width
FontMetrics metrics = getFontMetrics(getFont());
int strWidth = metrics.stringWidth( getText() );
Dimension original = super.getPreferredSize();
return new Dimension (strWidth+2, original.height);
}
}
Generated by srcColor