mirror of https://github.com/sipwise/jitsi.git
StatusSelectorBox component generalized in SIPCommSelectorBox, in order to be used as a common control in other places in the gui.
parent
48301a03f2
commit
02680e54eb
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.impl.gui.main.customcontrols;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JPopupMenu;
|
||||
|
||||
import net.java.sip.communicator.impl.gui.utils.AntialiasingManager;
|
||||
import net.java.sip.communicator.impl.gui.utils.ImageLoader;
|
||||
|
||||
/**
|
||||
* SIPCommSelectorBox is very similar to a JComboBox. It
|
||||
* allows having a list of icon-text pairs and when choosing
|
||||
* on of them, to show only the selected icon without the text.
|
||||
*
|
||||
* @author Yana Stamcheva
|
||||
*/
|
||||
public class SIPCommSelectorBox extends JLabel
|
||||
implements MouseListener {
|
||||
|
||||
private JPopupMenu popup;
|
||||
|
||||
private Object selectedItem;
|
||||
|
||||
private Image backgroundImage = ImageLoader
|
||||
.getImage(ImageLoader.STATUS_SELECTOR_BOX);
|
||||
|
||||
public SIPCommSelectorBox(){
|
||||
|
||||
this.setPreferredSize(new Dimension(
|
||||
this.backgroundImage.getWidth(this),
|
||||
this.backgroundImage.getHeight(this)));
|
||||
|
||||
this.setVerticalAlignment(JLabel.CENTER);
|
||||
|
||||
this.setHorizontalAlignment(JLabel.CENTER);
|
||||
|
||||
this.popup = new JPopupMenu();
|
||||
|
||||
this.popup.setInvoker(this);
|
||||
|
||||
this.addMouseListener(this);
|
||||
}
|
||||
|
||||
public SIPCommSelectorBox(Object selectedItem){
|
||||
this();
|
||||
|
||||
this.selectedItem = selectedItem;
|
||||
|
||||
if(selectedItem instanceof Image)
|
||||
this.setIcon(new ImageIcon((Image)selectedItem));
|
||||
else
|
||||
this.setText(selectedItem.toString());
|
||||
}
|
||||
|
||||
public void addItem(String text, Icon icon,
|
||||
ActionListener actionListener){
|
||||
|
||||
JMenuItem item = new JMenuItem(text, icon);
|
||||
|
||||
item.addActionListener(actionListener);
|
||||
|
||||
this.popup.add(item);
|
||||
}
|
||||
|
||||
public Point calculatePopupLocation(){
|
||||
|
||||
Component component = this;
|
||||
Point point = new Point();
|
||||
int x = this.getX();
|
||||
int y = this.getY();
|
||||
|
||||
while(component.getParent() != null){
|
||||
|
||||
component = component.getParent();
|
||||
|
||||
x += component.getX();
|
||||
y += component.getY();
|
||||
}
|
||||
|
||||
point.x = x;
|
||||
point.y = y + this.getHeight();
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
protected void paintComponent(Graphics g){
|
||||
|
||||
AntialiasingManager.activateAntialiasing(g);
|
||||
|
||||
g.drawImage(this.backgroundImage, 0, 0, this);
|
||||
|
||||
super.paintComponent(g);
|
||||
}
|
||||
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
|
||||
if (!this.popup.isVisible()) {
|
||||
this.popup.setLocation(calculatePopupLocation());
|
||||
this.popup.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
public void mouseExited(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
public void mousePressed(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
public void setSelected(JMenuItem menuItem){
|
||||
this.setIcon(menuItem.getIcon());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue