All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.globalmentor.swing.LinkLabel Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 1996-2009 GlobalMentor, Inc. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.globalmentor.swing;

import java.awt.*;
import java.awt.event.*;
import java.net.URI;

import javax.swing.*;

import com.globalmentor.awt.event.RolloverMouseAdapter;
import com.globalmentor.java.Objects;
import com.globalmentor.log.Log;

/**
 * A label that serves as a link to an Internet destination.
 * @author Garret Wilson
 */
public class LinkLabel extends JLabel {

	/** The mouse adapter responsible for handling rollovers for the label. */
	protected final RolloverMouseAdapter rolloverMouseAdapter;

	/** The property representing the rollover color. */
	public static final String ROLLOVER_COLOR_PROPERTY = "rolloverColor";

	/** @return The rollover color; defaults to Color.red. */
	public Color getRolloverColor() {
		return rolloverMouseAdapter.getRolloverColor();
	}

	/**
	 * Sets the rollover color property. This is a bound property, ROLLOVER_COLOR_PROPERTY.
	 * @param newRolloverColor The new rollover color, or null if the component color should not be changed for mouse rollovers.
	 */
	public void setRolloverColor(final Color newRolloverColor) {
		final Color oldRolloverColor = getRolloverColor(); //get the current rollover color
		if(!Objects.equals(oldRolloverColor, newRolloverColor)) { //if the color is really changing
			rolloverMouseAdapter.setRolloverColor(newRolloverColor); //update the value
			//show that the property has changed
			firePropertyChange(ROLLOVER_COLOR_PROPERTY, oldRolloverColor, newRolloverColor);
		}
	}

	/** The property representing the link target URL. */
	public static final String TARGET_PROPERTY = "target";

	/** The link target URL; defaults to null. */
	private String target = null;

	/** @return The link target; defaults to null. */
	public String getTarget() {
		return target;
	}

	/**
	 * Sets the link target property. This is a bound property, TARGET_PROPERTY.
	 * @param newTarget The new link target, or null if the link has no destination.
	 */
	public void setTarget(final String newTarget) {
		final String oldTarget = target; //get the current target
		if(!Objects.equals(oldTarget, newTarget)) { //if the target is really changing
			target = newTarget; //update the value
			firePropertyChange(TARGET_PROPERTY, oldTarget, newTarget); //show that the property has changed
		}
	}

	/**
	 * Creates a link label instance with the specified text, image, and horizontal alignment. The label is centered vertically in its display area. The text is
	 * on the trailing edge of the image.
	 * @param text The text to be displayed by the label.
	 * @param icon The image to be displayed by the label.
	 * @param horizontalAlignment One of the following constants defined in SwingConstants: LEFT, CENTER,
	 *          RIGHT, LEADING or TRAILING.
	 */
	public LinkLabel(final String text, final Icon icon, final int horizontalAlignment) {
		super(text, icon, horizontalAlignment); //construct the parent class
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); //use the hand cursor when the mouse is over the label
		//create a rollover adapter that not only handles mouse rollovers but also
		//  handles mouse clicks
		rolloverMouseAdapter = new RolloverMouseAdapter() {

			/**
			 * Invoked when the mouse has been clicked on a component.
			 * @param mouseEvent The mouse event.
			 */
			public void mouseClicked(final MouseEvent mouseEvent) {
				try {
					Desktop.getDesktop().browse(URI.create(getTarget())); //attempt to browse to the location designated by the target
				} catch(final Exception exception) {
					Log.warn(exception); //only warn about any errors that occur
				}
			}
		};
		//add the rollover adapter as a listener;
		//  it will now automatically handle rollovers and mouse clicks
		addMouseListener(rolloverMouseAdapter);
	}

	/**
	 * Creates a link label instance with the specified text and horizontal alignment. The label is centered vertically in its display area.
	 * @param text The text to be displayed by the label.
	 * @param horizontalAlignment One of the following constants defined in SwingConstants: LEFT, CENTER,
	 *          RIGHT, LEADING or TRAILING.
	 */
	public LinkLabel(final String text, final int horizontalAlignment) {
		this(text, null, horizontalAlignment); //do the default construction
	}

	/**
	 * Creates a link label instance with the specified text. The label is aligned against the leading edge of its display area, and centered vertically.
	 * @param text The text to be displayed by the label.
	 */
	public LinkLabel(final String text) {
		this(text, null, LEADING); //do the default construction
	}

	/**
	 * Creates a link label instance with the specified image and horizontal alignment. The label is centered vertically in its display area.
	 * @param icon The image to be displayed by the label.
	 * @param horizontalAlignment One of the following constants defined in SwingConstants: LEFT, CENTER,
	 *          RIGHT, LEADING or TRAILING.
	 */
	public LinkLabel(final Icon icon, final int horizontalAlignment) {
		this(null, icon, horizontalAlignment); //do the default construction
	}

	/**
	 * Creates a link label instance with the specified image.
	 * @param icon The image to be displayed by the label. The label is centered vertically and horizontally in its display area.
	 */
	public LinkLabel(final Icon icon) {
		this(null, icon, CENTER); //do the default construction
	}

	/**
	 * Default constructor. Creates a link label instance with no image and with an empty string for the title. The label is centered vertically in its display
	 * area. The label's contents, once set, will be displayed on the leading edge of the label's display area.
	 */
	public LinkLabel() {
		this("", null, LEADING); //do the default construction
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy