View Javadoc

1   package org.catacomb.druid.swing;
2   
3   
4   import java.awt.CardLayout;
5   import java.awt.Color;
6   import java.awt.FlowLayout;
7   import java.awt.Font;
8   import java.awt.GridLayout;
9   import java.awt.event.ActionEvent;
10  import java.awt.event.ActionListener;
11  
12  import javax.swing.ButtonGroup;
13  import javax.swing.JRadioButton;
14  
15  import org.catacomb.druid.event.LabelActor;
16  import org.catacomb.report.E;
17  
18  
19  
20  public class DRadioButtons extends DPanel implements ActionListener {
21  
22      static final long serialVersionUID = 1001;
23  
24      LabelActor lact;
25  
26      String selected;
27  
28      String[] options;
29      String[] labels;
30  
31      JRadioButton[] buttons;
32  
33      Font labelFont;
34  
35      CardLayout cardLayout;
36  
37      boolean vertical = true;
38  
39      Color bgColor;
40  
41      public DRadioButtons(String[] opts, String layout) {
42          super();
43  
44          if (layout == null || layout.equals("vertical")) {
45              vertical = true;
46          } else if (layout.equals("horizontal")) {
47              vertical = false;
48          } else {
49              E.warning("unknown layout: " + layout);
50              vertical = false;
51          }
52  
53          labelFont = new Font("sansserif", Font.PLAIN, 12);
54  
55          setOptions(opts, opts);
56  
57      }
58  
59      public void setBg(Color c) {
60          bgColor = c;
61          super.setBg(c);
62          for (JRadioButton jrb : buttons) {
63              jrb.setBackground(c);
64          }
65      }
66  
67  
68      public void setTooltip(String s) {
69          setToolTipText(s);
70      }
71  
72  
73      public void setOptions(String[] optsin, String[] labsin) {
74          removeAll();
75  
76          String[] opts = optsin;
77          String[] labs = labsin;
78  
79          if (opts == null) {
80              opts = new String[0];
81          }
82  
83          options = opts;
84          labels = labs;
85  
86          int nopt = opts.length;
87          if (vertical) {
88              setLayout(new GridLayout(nopt, 1, 2, 2));
89          } else {
90              setLayout(new FlowLayout(FlowLayout.LEFT, 5, 2));
91          }
92  
93          ButtonGroup bgp = new ButtonGroup();
94  
95          buttons = new JRadioButton[nopt];
96  
97          for (int i = 0; i < nopt; i++) {
98              JRadioButton jrb = new JRadioButton(labels[i]);
99              jrb.setActionCommand(options[i]);
100             // jrb.setBackground(bgColor);
101             jrb.setFont(labelFont);
102 
103             bgp.add(jrb);
104             buttons[i] = jrb;
105             jrb.addActionListener(this);
106             add(jrb);
107             if (bgColor != null) {
108                 jrb.setBackground(bgColor);
109             }
110         }
111 
112 
113         revalidate();
114     }
115 
116 
117     public void setSelectedIndex(int isel) {
118         for (int i = 0; i < buttons.length; i++) {
119             if (isel != i && buttons[i].isSelected()) {
120                 buttons[i].setSelected(false);
121             } else if (isel == i && !buttons[i].isSelected()) {
122                 buttons[i].setSelected(true);
123             }
124         }
125     }
126 
127 
128     public void setSelected(String s) {
129         if (s == null) {
130             for (int i = 0; i < options.length; i++) {
131                 if (buttons[i].isSelected()) {
132                     buttons[i].setSelected(false);
133                 }
134             }
135 
136 
137         } else {
138 
139             boolean done = false;
140             for (int i = 0; i < options.length; i++) {
141                 if (options[i].equals(s)) {
142                     selected = s;
143                     if (!buttons[i].isSelected()) {
144                         buttons[i].setSelected(true); // could call doClick to have an
145                         // event generated;
146                     }
147                     done = true;
148                 }
149             }
150             if (!done) {
151                 StringBuffer sb = new StringBuffer();
152                 for (int i = 0; i < options.length; i++) {
153                     sb.append(options[i] + ", ");
154                 }
155                 E.error("cant set selected  - not an option " + s + " possibilities are: " + sb.toString());
156             }
157         }
158     }
159 
160 
161     public String getSelected() {
162         return selected;
163     }
164 
165 
166 
167     public void actionPerformed(ActionEvent aev) {
168         selected = aev.getActionCommand();
169         deliverAction(selected, true);
170 
171     }
172 
173 
174     public void setLabelActor(LabelActor bl) {
175         lact = bl;
176     }
177 
178 
179 
180     public void deliverAction(String s, boolean b) {
181         if (lact != null) {
182             lact.labelAction(s, b);
183         }
184     }
185 
186 
187     /*
188        public void setCardLayout() {
189          cardLayout = new CardLayout();
190          setLayout(cardLayout);
191        }
192 
193 
194 
195        public void nextCard() {
196           cardLayout.next(this);
197        }
198 
199 
200 
201        public void showCard(String s) {
202           cardLayout.show(this, s);
203        }
204 
205     */
206 
207 }