KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ui > JToggleGroup


1 package prefuse.util.ui;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.Font JavaDoc;
6 import java.awt.event.ActionEvent JavaDoc;
7 import java.awt.event.ActionListener JavaDoc;
8
9 import javax.swing.BoxLayout JavaDoc;
10 import javax.swing.ButtonGroup JavaDoc;
11 import javax.swing.DefaultListModel JavaDoc;
12 import javax.swing.DefaultListSelectionModel JavaDoc;
13 import javax.swing.JCheckBox JavaDoc;
14 import javax.swing.JPanel JavaDoc;
15 import javax.swing.JRadioButton JavaDoc;
16 import javax.swing.JToggleButton JavaDoc;
17 import javax.swing.ListModel JavaDoc;
18 import javax.swing.ListSelectionModel JavaDoc;
19 import javax.swing.event.ListSelectionEvent JavaDoc;
20 import javax.swing.event.ListSelectionListener JavaDoc;
21
22 /**
23  * Swing component representing a group of toggle buttons -- either checkboxes
24  * or radio buttons. This class uses a ListModel and ListSelectionModel to
25  * represent the selection state of the buttons.
26  *
27  * @author <a HREF="http://jheer.org">jeffrey heer</a>
28  */

29 public class JToggleGroup extends JPanel JavaDoc {
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 JavaDoc m_data;
40     protected ListSelectionModel JavaDoc m_sel;
41     protected String JavaDoc[] m_labels;
42     protected ButtonGroup JavaDoc m_group;
43     
44     private Listener JavaDoc m_lstnr;
45     
46     /**
47      * Create a new JToggleGroup.
48      * @param type the toggle button type to use, one of {@link #CHECKBOX}
49      * or {@link #RADIO}
50      * @param data the list data that should populate the toggle group
51      */

52     public JToggleGroup(int type, Object JavaDoc[] data) {
53         this(type, new DefaultListModel JavaDoc(),
54                 new DefaultListSelectionModel JavaDoc());
55         
56         DefaultListModel JavaDoc model = (DefaultListModel JavaDoc)m_data;
57         for ( int i=0; i<data.length; ++i ) {
58             model.addElement(data[i]);
59         }
60         initUI();
61     }
62     
63     /**
64      * Create a new JToggleGroup.
65      * @param type the toggle button type to use, one of {@link #CHECKBOX}
66      * or {@link #RADIO}
67      * @param data the list model data backing the toggle group
68      */

69     public JToggleGroup(int type, ListModel JavaDoc data) {
70         this(type, data, new DefaultListSelectionModel JavaDoc());
71     }
72     
73     /**
74      * Create a new JToggleGroup.
75      * @param type the toggle button type to use, one of {@link #CHECKBOX}
76      * or {@link #RADIO}
77      * @param data the list model data backing the toggle group
78      * @param selection the list selection model to use to monitor selection
79      * changes to the various toggle buttons.
80      */

81     public JToggleGroup(int type, ListModel JavaDoc data, ListSelectionModel JavaDoc selection)
82     {
83         setLayout(new BoxLayout JavaDoc(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 JavaDoc();
90         }
91
92         m_lstnr = new Listener JavaDoc();
93         m_sel.addListSelectionListener(m_lstnr);
94         
95         if ( m_data.getSize() > 0 )
96             initUI();
97         setFocusable(false);
98     }
99     
100     // ------------------------------------------------------------------------
101

102     /**
103      * Initialize the UI.
104      */

105     protected void initUI() {
106         // unregister all active components
107
for ( int i=0; i<getComponentCount(); ++i ) {
108             Component JavaDoc c = getComponent(i);
109             if ( !(c instanceof JToggleButton JavaDoc) ) continue;
110             JToggleButton JavaDoc tb = (JToggleButton JavaDoc)c;
111             tb.removeActionListener(m_lstnr);
112             if ( m_group != null )
113                 m_group.remove(tb);
114         }
115         
116         // clear this container and add new components
117
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 JavaDoc data = m_data.getElementAt(i);
123             String JavaDoc label = m_labels==null ? data.toString() : m_labels[i];
124             
125             JToggleButton JavaDoc tb = null;
126             if ( m_type == CHECKBOX ) {
127                 tb = new JCheckBox JavaDoc(label);
128             } else {
129                 tb = new JRadioButton JavaDoc(label);
130                 m_group.add(tb);
131             }
132             tb.putClientProperty("idx", new Integer JavaDoc(i));
133             tb.addActionListener(m_lstnr);
134             add(tb);
135         }
136         UILib.addStrut(this, m_axis, m_margin);
137         
138         // make sure the selection status shows up
139
m_lstnr.valueChanged(null);
140     }
141     
142     // ------------------------------------------------------------------------
143

144     /**
145      * Set the Box axis type used to orient the toggle group component.
146      * @param axis the axis type, one of
147      * {@link javax.swing.BoxLayout#X_AXIS},
148      * {@link javax.swing.BoxLayout#Y_AXIS},
149      * {@link javax.swing.BoxLayout#LINE_AXIS}, or
150      * {@link javax.swing.BoxLayout#PAGE_AXIS}.
151      */

152     public void setAxisType(int axis) {
153         this.setLayout(new BoxLayout JavaDoc(this, axis));
154         m_axis = axis;
155         initUI();
156     }
157     
158     /**
159      * Get the Box axis type used to orient the toggle group component.
160      * @return the axis type, one of
161      * {@link javax.swing.BoxLayout#X_AXIS},
162      * {@link javax.swing.BoxLayout#Y_AXIS},
163      * {@link javax.swing.BoxLayout#LINE_AXIS}, or
164      * {@link javax.swing.BoxLayout#PAGE_AXIS}.
165      */

166     public int getAxisType() {
167         return m_axis;
168     }
169     
170     /**
171      * Set the margin, in pixels, to use at the ends of the JToggleGroup.
172      * @param margin the margin in pixels
173      */

174     public void setMargin(int margin) {
175         if ( margin < 0 )
176             throw new IllegalArgumentException JavaDoc("Margin is less than zero.");
177         m_margin = margin;
178         initUI();
179     }
180
181     /**
182      * Get the margin, in pixels, used at the ends of the JToggleGroup.
183      * @return the margin in pixels
184      */

185     public int getMargin() {
186         return m_margin;
187     }
188     
189     /**
190      * Set the spacing between toggle group components.
191      * @param spacing the spacing, in pixels, to use between components
192      */

193     public void setSpacing(int spacing) {
194         if ( spacing < 0 )
195             throw new IllegalArgumentException JavaDoc("Spacing is less than zero.");
196         m_spacing = spacing;
197         initUI();
198     }
199     
200     /**
201      * Get the spacing between toggle group components.
202      * @return the spacing, in pixels, to use between components
203      */

204     public int getSpacing() {
205         return m_spacing;
206     }
207     
208     /**
209      * Set the ListModel backing this component.
210      * @return the list model to use
211      */

212     public void setModel(ListModel JavaDoc model) {
213         m_data = model;
214         initUI();
215     }
216     
217     /**
218      * Get the ListModel backing this component.
219      * @return the list model
220      */

221     public ListModel JavaDoc getModel() {
222         return m_data;
223     }
224     
225     /**
226      * Set the ListSelectionModel used by this component.
227      * @param sel the list selection model to use
228      */

229     public void setSelectionModel(ListSelectionModel JavaDoc 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     /**
237      * Get the ListSelectionModel used by this component.
238      * @return the list selection model to use
239      */

240     public ListSelectionModel JavaDoc getSelectionModel() {
241         return m_sel;
242     }
243     
244     /**
245      * Set the labels to use for the Objects contained in the list model.
246      * @param labels the display labels to use in the interface component
247      */

248     public void setLabels(String JavaDoc[] labels) {
249         if ( labels.length < m_data.getSize() ) {
250             throw new IllegalArgumentException JavaDoc("Alias array is too short");
251         }
252         m_labels = labels;
253         initUI();
254     }
255     
256     /**
257      * Set the background color of this toggle group.
258      * @see java.awt.Component#setBackground(java.awt.Color)
259      */

260     public void setBackground(Color JavaDoc background) {
261         for ( int i=0; i<getComponentCount(); ++i ) {
262             getComponent(i).setBackground(background);
263         }
264     }
265     
266     /**
267      * Set the foreground color of this toggle group.
268      * @see java.awt.Component#setBackground(java.awt.Color)
269      */

270     public void setForeground(Color JavaDoc foreground) {
271         for ( int i=0; i<getComponentCount(); ++i ) {
272             getComponent(i).setForeground(foreground);
273         }
274     }
275     
276     /**
277      * Set the font used by this toggle group.
278      * @see java.awt.Component#setFont(java.awt.Font)
279      */

280     public void setFont(Font JavaDoc font) {
281         for ( int i=0; i<getComponentCount(); ++i ) {
282             getComponent(i).setFont(font);
283         }
284     }
285     
286     /**
287      * Sets if the various toggle buttons can receive the keyboard focus.
288      * @param b true to set toggle buttons keyboard accessible, false to
289      * set them unaccessible.
290      */

291     public void setGroupFocusable(boolean b) {
292         for ( int i=0; i<getComponentCount(); ++i ) {
293             Component JavaDoc c = getComponent(i);
294             if ( c instanceof JToggleButton JavaDoc )
295                 c.setFocusable(b);
296         }
297     }
298     
299     // ------------------------------------------------------------------------
300

301     private class Listener implements ListSelectionListener JavaDoc, ActionListener JavaDoc {
302
303         private boolean m_ignore = false;
304         
305         public void valueChanged(ListSelectionEvent JavaDoc 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 JavaDoc tb = null;
312                 
313                 for ( int i=0, j=0; i<getComponentCount(); ++i ) {
314                     Component JavaDoc c = getComponent(i);
315                     if ( c instanceof JToggleButton JavaDoc ) {
316                         tb = (JToggleButton JavaDoc)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 JavaDoc c = getComponent(i);
326                     if ( c instanceof JCheckBox JavaDoc ) {
327                         ((JCheckBox JavaDoc)c).setSelected(m_sel.isSelectedIndex(j++));
328                     }
329                 }
330             }
331             
332             m_ignore = false;
333         }
334
335         public void actionPerformed(ActionEvent JavaDoc e) {
336             if ( m_ignore ) { return; } else { m_ignore = true; }
337             
338             JToggleButton JavaDoc tb = (JToggleButton JavaDoc)e.getSource();
339             boolean sel = tb.isSelected();
340             int idx = ((Integer JavaDoc)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 } // end of class JToggleGroup
355
Popular Tags