1 package org.xnap.commons.gui.settings; 2 3 import java.util.Iterator; 4 import java.util.LinkedList; 5 import java.util.List; 6 import javax.swing.AbstractButton; 7 import javax.swing.ComboBoxModel; 8 import javax.swing.SpinnerNumberModel; 9 import javax.swing.text.JTextComponent; 10 import org.xnap.commons.settings.BooleanSetting; 11 import org.xnap.commons.settings.IntSetting; 12 import org.xnap.commons.settings.Setting; 13 import org.xnap.commons.settings.StringSetting; 14 15 /*** 16 * Provides a mediator between {@link org.xnap.commons.settings.Setting} objects 17 * and gui components. This class can be used to easily implement dialogs that 18 * configure application settings. 19 * 20 * @author Steffen Pingel 21 */ 22 public class SettingComponentMediator { 23 24 private List<SettingMapper> mappers = new LinkedList<SettingMapper>(); 25 26 public void add(BooleanSetting setting, AbstractButton button) 27 { 28 mappers.add(new BooleanSettingMapper(setting, button)); 29 } 30 31 public void add(IntSetting setting, SpinnerNumberModel spinnerModel) 32 { 33 mappers.add(new IntSettingMapper(setting, spinnerModel)); 34 } 35 36 public void add(StringSetting setting, JTextComponent textComponent) 37 { 38 mappers.add(new StringSettingMapper(setting, textComponent)); 39 } 40 41 public <T> void add(Setting<T> setting, ComboBoxModel model) 42 { 43 mappers.add(new SelectionMapper<T>(setting, model)); 44 } 45 46 public void add(SettingMapper mapper) 47 { 48 mappers.add(mapper); 49 } 50 51 public void remove(SettingMapper mapper) 52 { 53 mappers.remove(mapper); 54 } 55 56 public Iterator<SettingMapper> iterator() 57 { 58 return mappers.iterator(); 59 } 60 61 public void apply() 62 { 63 for (SettingMapper mapper : mappers) { 64 mapper.apply(); 65 } 66 } 67 68 public void revert() 69 { 70 for (SettingMapper mapper : mappers) { 71 mapper.revert(); 72 } 73 } 74 75 public void revertToDefaults() 76 { 77 for (SettingMapper mapper : mappers) { 78 mapper.revertToDefaults(); 79 } 80 } 81 82 public static interface SettingMapper { 83 84 void apply(); 85 86 void revert(); 87 88 void revertToDefaults(); 89 90 } 91 92 public static class BooleanSettingMapper implements SettingMapper { 93 94 private AbstractButton button; 95 private BooleanSetting setting; 96 97 public BooleanSettingMapper(BooleanSetting setting, AbstractButton button) 98 { 99 this.setting = setting; 100 this.button = button; 101 } 102 103 public void apply() 104 { 105 setting.setValue(button.isSelected()); 106 } 107 108 public void revert() 109 { 110 button.setSelected(setting.getValue()); 111 } 112 113 public void revertToDefaults() 114 { 115 button.setSelected(setting.getDefaultValue()); 116 } 117 118 } 119 120 public static class StringSettingMapper implements SettingMapper { 121 122 private StringSetting setting; 123 private JTextComponent textComponent; 124 125 public StringSettingMapper(StringSetting setting, JTextComponent textComponent) 126 { 127 this.setting = setting; 128 this.textComponent = textComponent; 129 } 130 131 public void apply() 132 { 133 setting.setValue(textComponent.getText()); 134 } 135 136 public void revert() 137 { 138 textComponent.setText(setting.getValue()); 139 } 140 141 public void revertToDefaults() 142 { 143 textComponent.setText(setting.getDefaultValue()); 144 } 145 146 } 147 148 public static class IntSettingMapper implements SettingMapper { 149 150 private IntSetting setting; 151 private SpinnerNumberModel spinnerModel; 152 153 public IntSettingMapper(IntSetting setting, SpinnerNumberModel spinnerModel) 154 { 155 this.setting = setting; 156 this.spinnerModel = spinnerModel; 157 } 158 159 public void apply() 160 { 161 setting.setValue(spinnerModel.getNumber().intValue()); 162 } 163 164 public void revert() 165 { 166 spinnerModel.setValue(setting.getValue()); 167 } 168 169 public void revertToDefaults() 170 { 171 spinnerModel.setValue(setting.getDefaultValue()); 172 } 173 174 } 175 176 public static class SelectionMapper<T> implements SettingMapper { 177 178 private Setting<T> setting; 179 private ComboBoxModel model; 180 181 public SelectionMapper(Setting<T> setting, ComboBoxModel model) 182 { 183 this.setting = setting; 184 this.model = model; 185 } 186 187 public void apply() 188 { 189 setting.setValue((T)model.getSelectedItem()); 190 } 191 192 public void revert() 193 { 194 model.setSelectedItem(setting.getValue()); 195 } 196 197 public void revertToDefaults() 198 { 199 model.setSelectedItem(setting.getDefaultValue()); 200 } 201 202 } 203 204 }