package org.openslx.dozmod.gui.control; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.SystemColor; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JLabel; import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.dozmod.util.FormatHelper; import org.openslx.dozmod.util.OpenLinks; /** * A label for displaying a {@link UserInfo} object. Supports a callback event * for when the users clicks the label. */ @SuppressWarnings("serial") public class PersonLabel extends JLabel { private UserInfo user = null; private PersonLabelClickEvent callback = defaultCallback; public PersonLabel() { this(defaultCallback); } public PersonLabel(PersonLabelClickEvent cb) { setForeground(linkColor()); this.callback = cb; this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (user != null && callback != null && e.getButton() == MouseEvent.BUTTON1 && getX() >= 0 && getY() >= 0) { Dimension size = getSize(); if (size.width > e.getX() && size.height > e.getY()) callback.clicked(user); } } }); } private Color linkColor() { // Create color for links SystemColor tc = SystemColor.windowText; int r = tc.getRed() * tc.getRed(); int g = tc.getGreen() * tc.getGreen(); int b = tc.getBlue() * tc.getBlue(); b += 30000; double factor = 65535 / b; if (factor < 1) { r *= factor; g *= factor; b *= factor; } r = (int) Math.sqrt(r); g = (int) Math.sqrt(g); b = (int) Math.sqrt(b); return new Color(r, g, b); } /** * Set the callback to call when the user clicks this label using the left * mouse button. Only called if a user is set. * * @param cb The {@link PersonLabelClickEvent} to call */ public void setCallback(PersonLabelClickEvent cb) { this.callback = cb; } /** * Set the user to display. */ public void setUser(UserInfo user) { this.user = user; if (user == null) { setText(""); } else { setText(FormatHelper.userName(user)); } } /** * The callback for when this label contains a user and gets clicked. */ public static interface PersonLabelClickEvent { public void clicked(UserInfo user); } private static final PersonLabelClickEvent defaultCallback = new PersonLabelClickEvent() { @Override public void clicked(UserInfo user) { if (user == null || user.eMail == null) return; OpenLinks.sendMail(user.eMail, null); } }; }