1 package org.columba.core.gui.base; 19 20 import java.awt.Insets ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.awt.event.ItemEvent ; 24 import java.awt.event.ItemListener ; 25 import java.util.Iterator ; 26 import java.util.Vector ; 27 28 import javax.swing.ButtonGroup ; 29 import javax.swing.JButton ; 30 import javax.swing.JPopupMenu ; 31 import javax.swing.JRadioButtonMenuItem ; 32 import javax.swing.SwingConstants ; 33 34 44 45 public class ComboMenu extends JButton implements ActionListener { 46 47 protected JPopupMenu popupMenu; 48 49 protected Vector listeners; 50 51 private ButtonGroup group; 52 53 public ComboMenu() { 54 super(); 55 56 setIcon(new AscendingIcon()); 58 setMargin(new Insets (1, 3, 1, 3)); 59 setIconTextGap(12); 60 61 setHorizontalTextPosition(SwingConstants.LEFT); 62 63 listeners = new Vector (); 64 65 group = new ButtonGroup (); 66 67 popupMenu = new JPopupMenu (); 68 69 addActionListener(new ActionListener () { 70 public void actionPerformed(ActionEvent evt) { 71 popupMenu.show(ComboMenu.this, 0, getHeight() - 2); 72 } 73 }); 74 } 75 76 80 public ComboMenu(String [] list) { 81 this(); 82 83 for (int i = 0; i < list.length; i++) { 84 85 if (i == 0) 86 setText(list[i]); 87 88 if (list[i].equalsIgnoreCase("separator")) { 89 popupMenu.addSeparator(); 90 } else { 91 addMenuItem(list[i], list[i]); 92 } 93 } 94 95 } 96 97 public JRadioButtonMenuItem addMenuItem(String name, String localizedName) { 98 99 JRadioButtonMenuItem m = new JRadioButtonMenuItem (localizedName); 100 101 m.setActionCommand(name); 102 103 m.addActionListener(this); 104 105 group.add(m); 106 107 popupMenu.add(m); 108 109 if (popupMenu.getComponentCount() == 1) { 110 setText(localizedName); 111 m.setSelected(true); 112 } 113 114 return m; 115 } 116 117 public void addSeparator() { 118 popupMenu.addSeparator(); 119 } 120 121 124 public void actionPerformed(ActionEvent arg0) { 125 String action = arg0.getActionCommand(); 126 JRadioButtonMenuItem m = (JRadioButtonMenuItem ) arg0.getSource(); 127 128 setText(m.getText()); 129 130 fireItemStateChanged(new ItemEvent (this, 0, action, ItemEvent.SELECTED)); 131 132 } 133 134 public void addItemListener(ItemListener l) { 135 listeners.add(l); 136 } 137 138 public void setSelectedItem( int nr ) { 139 JRadioButtonMenuItem item = (JRadioButtonMenuItem )popupMenu.getComponent(0); 140 141 setText(item.getText()); 142 fireItemStateChanged(new ItemEvent (this, 0, item.getActionCommand(), ItemEvent.SELECTED)); 143 } 144 145 protected void fireItemStateChanged(ItemEvent event) { 146 Iterator it = listeners.iterator(); 147 while (it.hasNext()) { 148 ItemListener l = (ItemListener ) it.next(); 149 l.itemStateChanged(event); 150 } 151 } 152 } | Popular Tags |