summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/PersonLabel.java
blob: 7c53bf96fce2160235f29561cf6b2b60dc8ca180 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                       
                      

                          


                                   
                             
 
                                              
                                               
                                            
                                                  




                                                                              
                           
                                         


                                     
                                                                 
 
                                                                                                                 
                                 
 
                              



                                                      
                                   
                                                     
                                 
                                                                




                                                                                                                          

                                 













                                                                                  



                                            
                       
                                   
                                      
                        
                                                             
                 





                                                                                     
                                                                                          
                                                                                  
                                                 
                 








                                                                             




                                                                                                  
                                                                      


                 
 
package org.openslx.dozmod.gui.control;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.UIManager;

import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.gui.helper.ColorUtil;
import org.openslx.dozmod.util.FormatHelper;
import org.openslx.dozmod.util.DesktopEnvironment;

/**
 * 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 QLabel {

	private UserInfo user = null;

	private PersonLabelClickEvent callback = defaultCallback;

	private static final Color linkColor = ColorUtil.add(UIManager.getColor("Label.foreground"), new Color(0,
			0, .9f));

	public PersonLabel() {
		this(defaultCallback);
	}

	public PersonLabel(PersonLabelClickEvent cb) {
		this.callback = cb;
		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);
				}
			}
		});
	}

	/**
	 * 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;
		// Text
		if (user == null) {
			setText(null);
		} else {
			setText(FormatHelper.userName(user));
		}
		// Tooltip, color, cursor
		if (user == null || user.eMail == null || user.eMail.isEmpty()) {
			setToolTipText(null);
			setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			setForeground(UIManager.getColor("Label.foreground"));
		} else {
			setToolTipText("Klicken, um eine Mail an diese Person zu senden");
			setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			setForeground(linkColor);
		}
	}

	/**
	 * 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;
			DesktopEnvironment.sendMail(user.eMail, null);
		}
	};

}