| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.ui; |
| 17 | |
| 18 | import java.awt.GridLayout; |
| 19 | import java.util.Collections; |
| 20 | import java.util.Iterator; |
| 21 | |
| 22 | import javax.swing.AbstractButton; |
| 23 | import javax.swing.Action; |
| 24 | import javax.swing.ButtonGroup; |
| 25 | import javax.swing.JPanel; |
| 26 | import javax.swing.JRadioButton; |
| 27 | |
| 28 | import net.sf.jomic.common.Settings; |
| 29 | |
| 30 | /** |
| 31 | * ButtonGroup of <code>JRadioButtons</code> that when clicked will set a setting to the value |
| 32 | * assigned to the specific button. |
| 33 | * |
| 34 | * @see LimitedIntSettingSlider |
| 35 | * @see BooleanSettingCheckBox |
| 36 | * @see BooleanSettingMenuItem |
| 37 | */ |
| 38 | public class ChoiceSettingButtonGroup extends ButtonGroup |
| 39 | { |
| 40 | private Settings settings; |
| 41 | |
| 42 | public ChoiceSettingButtonGroup(String newPropertyName, String[] choices) { |
| 43 | super(); |
| 44 | settings = Settings.instance(); |
| 45 | |
| 46 | String currentChoice = settings.getChoiceProperty(newPropertyName, choices); |
| 47 | |
| 48 | for (int i = 0; i < choices.length; i += 1) { |
| 49 | String choice = choices[i]; |
| 50 | Action action = new SetSettingAction(newPropertyName, choice); |
| 51 | JRadioButton button = new JRadioButton(action); |
| 52 | |
| 53 | button.setText(choice); |
| 54 | if (currentChoice.equals(choice)) { |
| 55 | button.setSelected(true); |
| 56 | } |
| 57 | add(button); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public JPanel asJPanel() { |
| 62 | JPanel result = new JPanel(new GridLayout(0, 1)); |
| 63 | Iterator buttonRider = Collections.list(getElements()).iterator(); |
| 64 | |
| 65 | while (buttonRider.hasNext()) { |
| 66 | AbstractButton button = (AbstractButton) buttonRider.next(); |
| 67 | |
| 68 | result.add(button); |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | } |