1 package prefuse.util.ui; 2 3 import java.awt.Color ; 4 import java.awt.Component ; 5 import java.awt.Font ; 6 import java.awt.event.ActionEvent ; 7 import java.awt.event.ActionListener ; 8 9 import javax.swing.BoxLayout ; 10 import javax.swing.ButtonGroup ; 11 import javax.swing.DefaultListModel ; 12 import javax.swing.DefaultListSelectionModel ; 13 import javax.swing.JCheckBox ; 14 import javax.swing.JPanel ; 15 import javax.swing.JRadioButton ; 16 import javax.swing.JToggleButton ; 17 import javax.swing.ListModel ; 18 import javax.swing.ListSelectionModel ; 19 import javax.swing.event.ListSelectionEvent ; 20 import javax.swing.event.ListSelectionListener ; 21 22 29 public class JToggleGroup extends JPanel { 30 31 public static final int CHECKBOX = 0; 32 public static final int RADIO = 1; 33 34 protected final int m_type; 35 protected int m_margin = 0; 36 protected int m_spacing = 0; 37 protected int m_axis = BoxLayout.X_AXIS; 38 39 protected ListModel m_data; 40 protected ListSelectionModel m_sel; 41 protected String [] m_labels; 42 protected ButtonGroup m_group; 43 44 private Listener m_lstnr; 45 46 52 public JToggleGroup(int type, Object [] data) { 53 this(type, new DefaultListModel (), 54 new DefaultListSelectionModel ()); 55 56 DefaultListModel model = (DefaultListModel )m_data; 57 for ( int i=0; i<data.length; ++i ) { 58 model.addElement(data[i]); 59 } 60 initUI(); 61 } 62 63 69 public JToggleGroup(int type, ListModel data) { 70 this(type, data, new DefaultListSelectionModel ()); 71 } 72 73 81 public JToggleGroup(int type, ListModel data, ListSelectionModel selection) 82 { 83 setLayout(new BoxLayout (this, m_axis)); 84 m_type = type; 85 m_data = data; 86 m_sel = selection; 87 88 if ( m_type == RADIO ) { 89 m_group = new ButtonGroup (); 90 } 91 92 m_lstnr = new Listener (); 93 m_sel.addListSelectionListener(m_lstnr); 94 95 if ( m_data.getSize() > 0 ) 96 initUI(); 97 setFocusable(false); 98 } 99 100 102 105 protected void initUI() { 106 for ( int i=0; i<getComponentCount(); ++i ) { 108 Component c = getComponent(i); 109 if ( !(c instanceof JToggleButton ) ) continue; 110 JToggleButton tb = (JToggleButton )c; 111 tb.removeActionListener(m_lstnr); 112 if ( m_group != null ) 113 m_group.remove(tb); 114 } 115 116 removeAll(); 118 UILib.addStrut(this, m_axis, m_margin); 119 for ( int i=0; i<m_data.getSize(); ++i ) { 120 if ( i>0 ) UILib.addStrut(this, m_axis, m_spacing); 121 122 Object data = m_data.getElementAt(i); 123 String label = m_labels==null ? data.toString() : m_labels[i]; 124 125 JToggleButton tb = null; 126 if ( m_type == CHECKBOX ) { 127 tb = new JCheckBox (label); 128 } else { 129 tb = new JRadioButton (label); 130 m_group.add(tb); 131 } 132 tb.putClientProperty("idx", new Integer (i)); 133 tb.addActionListener(m_lstnr); 134 add(tb); 135 } 136 UILib.addStrut(this, m_axis, m_margin); 137 138 m_lstnr.valueChanged(null); 140 } 141 142 144 152 public void setAxisType(int axis) { 153 this.setLayout(new BoxLayout (this, axis)); 154 m_axis = axis; 155 initUI(); 156 } 157 158 166 public int getAxisType() { 167 return m_axis; 168 } 169 170 174 public void setMargin(int margin) { 175 if ( margin < 0 ) 176 throw new IllegalArgumentException ("Margin is less than zero."); 177 m_margin = margin; 178 initUI(); 179 } 180 181 185 public int getMargin() { 186 return m_margin; 187 } 188 189 193 public void setSpacing(int spacing) { 194 if ( spacing < 0 ) 195 throw new IllegalArgumentException ("Spacing is less than zero."); 196 m_spacing = spacing; 197 initUI(); 198 } 199 200 204 public int getSpacing() { 205 return m_spacing; 206 } 207 208 212 public void setModel(ListModel model) { 213 m_data = model; 214 initUI(); 215 } 216 217 221 public ListModel getModel() { 222 return m_data; 223 } 224 225 229 public void setSelectionModel(ListSelectionModel sel) { 230 m_sel.removeListSelectionListener(m_lstnr); 231 m_sel = sel; 232 m_sel.addListSelectionListener(m_lstnr); 233 m_lstnr.valueChanged(null); 234 } 235 236 240 public ListSelectionModel getSelectionModel() { 241 return m_sel; 242 } 243 244 248 public void setLabels(String [] labels) { 249 if ( labels.length < m_data.getSize() ) { 250 throw new IllegalArgumentException ("Alias array is too short"); 251 } 252 m_labels = labels; 253 initUI(); 254 } 255 256 260 public void setBackground(Color background) { 261 for ( int i=0; i<getComponentCount(); ++i ) { 262 getComponent(i).setBackground(background); 263 } 264 } 265 266 270 public void setForeground(Color foreground) { 271 for ( int i=0; i<getComponentCount(); ++i ) { 272 getComponent(i).setForeground(foreground); 273 } 274 } 275 276 280 public void setFont(Font font) { 281 for ( int i=0; i<getComponentCount(); ++i ) { 282 getComponent(i).setFont(font); 283 } 284 } 285 286 291 public void setGroupFocusable(boolean b) { 292 for ( int i=0; i<getComponentCount(); ++i ) { 293 Component c = getComponent(i); 294 if ( c instanceof JToggleButton ) 295 c.setFocusable(b); 296 } 297 } 298 299 301 private class Listener implements ListSelectionListener , ActionListener { 302 303 private boolean m_ignore = false; 304 305 public void valueChanged(ListSelectionEvent neverUsed) { 306 if ( m_ignore ) { return; } else { m_ignore = true; } 307 308 if ( m_type == RADIO ) { 309 int idx = m_sel.getMinSelectionIndex(); 310 boolean sel = (idx >= 0); 311 JToggleButton tb = null; 312 313 for ( int i=0, j=0; i<getComponentCount(); ++i ) { 314 Component c = getComponent(i); 315 if ( c instanceof JToggleButton ) { 316 tb = (JToggleButton )c; 317 if ( (!sel && tb.isSelected()) || (sel && idx==j) ) 318 break; 319 ++j; 320 } 321 } 322 tb.setSelected(sel); 323 } else { 324 for ( int i=0, j=0; i<getComponentCount(); ++i ) { 325 Component c = getComponent(i); 326 if ( c instanceof JCheckBox ) { 327 ((JCheckBox )c).setSelected(m_sel.isSelectedIndex(j++)); 328 } 329 } 330 } 331 332 m_ignore = false; 333 } 334 335 public void actionPerformed(ActionEvent e) { 336 if ( m_ignore ) { return; } else { m_ignore = true; } 337 338 JToggleButton tb = (JToggleButton )e.getSource(); 339 boolean sel = tb.isSelected(); 340 int idx = ((Integer )tb.getClientProperty("idx")).intValue(); 341 if ( m_type == RADIO ) { 342 m_sel.setSelectionInterval(idx,idx); 343 } else if ( sel ) { 344 m_sel.addSelectionInterval(idx,idx); 345 } else { 346 m_sel.removeSelectionInterval(idx,idx); 347 } 348 349 m_ignore = false; 350 } 351 352 } 353 354 } | Popular Tags |