source: trunk/gli/src/org/greenstone/gatherer/util/XORToggleButtonGroup.java@ 4364

Last change on this file since 4364 was 4364, checked in by mdewsnip, 21 years ago

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 1.8 KB
Line 
1package org.greenstone.gatherer.util;
2import java.awt.event.*;
3import javax.swing.*;
4/** Just like any other button group, this object encapsulates several checkbox listeners, but instead of recording which are selected, or indeed what buttons it has assigned to it, this group has but one purpose: to ensure that at least one check button is always selected. */
5public class XORToggleButtonGroup
6 implements ActionListener {
7 /** true if we are to ignore any further change events we recieve. */
8 private boolean ignore = false;
9 /** The current number of checkboxes that are selected. Never < 1. */
10 private int selected_count = 0;
11
12 private JToggleButton last_button;
13
14 /** Any implementation of ActionListener includes this method so that we can be informed when an action has occured on any registered checkboxes, allowing us to roll back the action if this is the only selected checkbox remaining.
15 * @param event An <strong>ActionEvent</strong> containing information about the event.
16 */
17 public void actionPerformed(ActionEvent event) {
18 if(!ignore) {
19 ignore = true;
20 JToggleButton toggle_button = (JToggleButton) event.getSource();
21 if(toggle_button.isSelected()) {
22 selected_count++;
23 }
24 else {
25 if(selected_count > 1) {
26 selected_count--;
27 }
28 else {
29 toggle_button.setSelected(true);
30 }
31 }
32 ignore = false;
33 }
34 }
35 public void add(JToggleButton toggle_button) {
36 last_button = toggle_button;
37 // If the checkbox is selected, increase our count.
38 if(toggle_button.isSelected()) {
39 selected_count++;
40 }
41 toggle_button.addActionListener(this);
42 }
43
44 /** Ensures that at least one option is selected. */
45 public void establish() {
46 if(selected_count == 0 && last_button != null) {
47 last_button.setSelected(true);
48 }
49 }
50}
Note: See TracBrowser for help on using the repository browser.