1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public Map parcourir(Component c) {
Map map = new HashMap();
parcourir(c, map, new ValuerImpl());
return map;
}
private void parcourir(Component c, Map map, Valuer v) {
Object value = v.valueOf(c));
if (value != null) {
map.put(c, value);
}
if (c instanceof Container) {
Component[] children = ((Container) c).getComponents();
for (int i = 0; i < children.length; i++) {
parcourir(children[i], map, v);
}
}
}
interface Valuer {
Object valueOf(Component c);
}
class ValuerImpl implements Valuer {
static Map valuers = new HashMap();
static {
valuers.put(JTextField.class,
new Valuer() {
public Object valueOf(Component c) {
return ((JTextField) c).getText();
}
});
valuers.put(JCheckBox.class,
new Valuer() {
public Object valueOf(Component c) {
return Boolean.valueOf(((JCheckBox) c).isSelected());
}
});
...
}
public Object valueOf(Component c) {
Valuer v= valuers.get(c.getClass());
return v!= null ? v.valueOf(c) : null;
}
} |