blob: b5ef87d6a93d697389625d362553584f1cde766d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
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);
}
};
}
|