/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2004, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: ActionComboBox.java,v 1.3 2005/07/08 13:13:52 cazenave Exp $
*
* Changes
* -------
* 29 avr. 2005 : Initial public release (CC);
*
*/
package simtools.ui;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.Action;
import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
/**
* A combobox to manage a set of actions. Only the actions icons are displayed.
* A click on the displayed item or a selection in the list generates the action.
*
* @author cazenave_c
*/
public class ActionComboBox extends JComboBox implements ComboBoxEditor,
ListCellRenderer, ItemListener, PropertyChangeListener {
/** the map between actions and buttons */
HashMap map;
/** the editor button, i.e the one displayed close to the arrow button */
JButton beditor;
/**
* Create a new action combo box with a list of actions
* @param actions a vector of actions
* @param actions a vector of string tips
*/
public ActionComboBox(Vector actions, Vector tips) {
super(actions);
map = new HashMap();
for (int i = 0; i < actions.size(); i++) {
Action a = (Action) actions.get(i);
JButton b = new JButton(a);
b.setText("");
if(tips!=null && tips.get(i)!=null){
b.setToolTipText((String)tips.get(i));
}
map.put(a, b);
}
beditor=new JButton((Action)actions.get(0));
if(tips!=null && tips.get(0)!=null){
beditor.setToolTipText((String)tips.get(0));
}
setRenderer(this);
setEditor(this);
addItemListener(this);
setEditable(true);
setOpaque(true);
}
/**
* Create a new action combo box with a list of actions
* @param actions a vector of actions
*/
public ActionComboBox(Vector actions) {
this(actions,null);
}
/* (non-Javadoc)
* @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent evt) {
if(evt.getPropertyName().equals("background")){
JButton b=(JButton)map.get(evt.getSource());
b.setBackground((Color)evt.getNewValue());
// the editor background color button is not modified
// to keep toolbar L&F.
// To change that uncomment the following lines
// if(beditor.getAction()==evt.getSource()){
// beditor.setBackground((Color)evt.getNewValue());
// }
}
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
*/
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
Object o = e.getItem();
((JButton) map.get(o)).doClick();
}
}
/*
* (non-Javadoc)
*
* @see javax.swing.ComboBoxEditor#selectAll()
*/
public void selectAll() {
}
/*
* (non-Javadoc)
*
* @see javax.swing.ComboBoxEditor#getEditorComponent()
*/
public Component getEditorComponent() {
return beditor;
}
/*
* (non-Javadoc)
*
* @see javax.swing.ComboBoxEditor#addActionListener(java.awt.event.ActionListener)
*/
public void addActionListener(ActionListener l) {
beditor.addActionListener(l);
}
/*
* (non-Javadoc)
*
* @see javax.swing.ComboBoxEditor#removeActionListener(java.awt.event.ActionListener)
*/
public void removeActionListener(ActionListener l) {
beditor.removeActionListener(l);
}
/*
* (non-Javadoc)
*
* @see javax.swing.ComboBoxEditor#getItem()
*/
public Object getItem() {
return null; // nothing edited !!!
}
/*
* (non-Javadoc)
*
* @see javax.swing.ComboBoxEditor#setItem(java.lang.Object)
*/
public void setItem(Object anObject) {
if(anObject==null){
return;
}
Action a=(Action)anObject;
beditor.setAction(a);
beditor.setText("");
JButton b=(JButton)map.get(a);
String t=b.getToolTipText();
if(t!=null){
beditor.setToolTipText(t);
}
}
/*
* (non-Javadoc)
*
* @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList,
* java.lang.Object, int, boolean, boolean)
*/
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
return (JButton) map.get(value);
}
}