summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org
diff options
context:
space:
mode:
authorSimon Rettberg2015-08-03 15:13:13 +0200
committerSimon Rettberg2015-08-03 15:13:13 +0200
commit522998178bfd232c208009483dcf65a532b1464b (patch)
tree69396759437165c592cb0a8d7e07d449b0950473 /dozentenmodul/src/main/java/org
parent[client] Cleaned up "OpenLinks" class (diff)
downloadtutor-module-522998178bfd232c208009483dcf65a532b1464b.tar.gz
tutor-module-522998178bfd232c208009483dcf65a532b1464b.tar.xz
tutor-module-522998178bfd232c208009483dcf65a532b1464b.zip
[client] Add eMail feature to PersonLabel
Diffstat (limited to 'dozentenmodul/src/main/java/org')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/PersonLabel.java53
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java58
2 files changed, 88 insertions, 23 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/PersonLabel.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/PersonLabel.java
index a9cc1c84..b5ef87d6 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/PersonLabel.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/PersonLabel.java
@@ -1,6 +1,9 @@
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;
@@ -8,32 +11,59 @@ 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 = null;
+ private PersonLabelClickEvent callback = defaultCallback;
public PersonLabel() {
- // could not find a way to query the system-wide color for hyperlinks :(
- setForeground(Color.BLUE.darker());
+ 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
- && getBounds().contains(e.getX(), e.getY())) {
- callback.clicked(user);
+ 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.
@@ -63,4 +93,13 @@ public class PersonLabel extends JLabel {
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);
+ }
+ };
+
}
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
index 3263ee70..0ee446ed 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
@@ -2,7 +2,7 @@ package org.openslx.dozmod.util;
import java.awt.Desktop;
import java.net.URI;
-import java.net.URISyntaxException;
+import java.net.URLEncoder;
import org.apache.log4j.Logger;
@@ -21,30 +21,56 @@ public class OpenLinks {
INTRO(
"http://www.hs-offenburg.de/fileadmin/Einrichtungen/hrz/Projekte/bwLehrpool/3_bwLehrpool_-_Image_einbinden_und_starten.pdf");
- // TODO use enum not map
private final URI uri;
private Link(String uri) {
- URI tmp;
+ this.uri = URI.create(uri);
+ }
+ }
+
+ /**
+ * Open the URI corresponding to the given enum constant in the user's
+ * default web browser.
+ *
+ * @param location web site to open, passed as a member from the
+ * {@link Link} enum.
+ * @return success on true
+ */
+ public static boolean openWebpage(Link location) {
+ if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
- tmp = new URI(uri);
- } catch (URISyntaxException e) {
- LOGGER.error("Hard-Coded URI contains syntax error!", e);
- tmp = null;
+ desktop.browse(location.uri);
+ return true;
+ } catch (Exception e) {
+ LOGGER.error("Got exception in openWebpage: ", e);
}
- this.uri = tmp;
}
+ return false;
}
- public static void openWebpage(Link location) {
-
- if (desktop == null || !desktop.isSupported(Desktop.Action.BROWSE))
- return;
- try {
- desktop.browse(location.uri);
- } catch (Exception e) {
- LOGGER.error("Got exception in openWebpage: ", e);
+ /**
+ * Send an email to the given email address, with an optional subject. This
+ * will open the user's default mail application.
+ *
+ * @param address receiver's address
+ * @param subject default subject to use, <code>null</code> for no subject
+ * @return false on not-success
+ */
+ public static boolean sendMail(String address, String subject) {
+ if (desktop != null && desktop.isSupported(Desktop.Action.MAIL)) {
+ try {
+ if (subject != null) {
+ desktop.mail(new URI("mailto:" + address + "?subject="
+ + URLEncoder.encode(subject, "UTF-8")));
+ } else {
+ desktop.mail(new URI("mailto:" + address));
+ }
+ return true;
+ } catch (Exception e) {
+ LOGGER.error("Got exception in sendMail: ", e);
+ }
}
+ return false;
}
}