OSDN Git Service

initial commit
[xdf/git-repos.git] / xdf-swing / src / main / java / jp / ac / aiit / xdf / component / swing / mappers / JRadioButtonMapper.java
1 package jp.ac.aiit.xdf.component.swing.mappers;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.swing.ButtonGroup;
7 import javax.swing.JRadioButton;
8
9 import jp.ac.aiit.xdf.component.swing.attribute.AttributeProcessingUnit;
10 import jp.ac.aiit.xdf.component.swing.attribute.AttributeSetProcessor;
11 import jp.ac.aiit.xdf.component.swing.attribute.CommonAttributeStore;
12 import jp.ac.aiit.xdf.component.swing.attribute.DoNothingAttributeProcessor;
13 import jp.ac.aiit.xdf.component.swing.attribute.GetterAttributeProcessor;
14 import jp.ac.aiit.xdf.component.swing.attribute.ObjectModelAttributeGetProcessor;
15 import jp.ac.aiit.xdf.component.swing.attribute.SetterAttributeProcessor;
16 import jp.ac.aiit.xdf.component.swing.event.ActionEventHandler;
17 import jp.ac.aiit.xdf.component.swing.event.SwingEventHandler;
18 import jp.ac.aiit.xdf.component.swing.event.SwingEventType;
19 import jp.ac.aiit.xdf.core.action.EventHandler;
20 import jp.ac.aiit.xdf.core.typeconvert.BooleanConverter;
21 import jp.ac.aiit.xdf.core.typeconvert.StringConverter;
22
23 /**
24  * radioタグをSwingのJRadioButtonにマッピングする
25  * 
26  * @author kodama
27  */
28 public class JRadioButtonMapper extends SwingComponentMapperTemplate {
29
30         /**
31          * name属性の値からButtonGroupへの対応。
32          * TODO どこへ置くべきか・・
33          */
34         private static Map<String, ButtonGroup> buttonGroups
35                 = new HashMap<String, ButtonGroup>();
36
37         @Override
38         protected Map<String, AttributeProcessingUnit> initProcessingUnits() {
39                 Map<String, AttributeProcessingUnit> result = CommonAttributeStore.commonAttributes(model);
40                 result.put("name", new AttributeProcessingUnit(new ChangeGroupProcessor(), new ObjectModelAttributeGetProcessor(model), new StringConverter()));
41                 result.put("value", new AttributeProcessingUnit(new DoNothingAttributeProcessor(), new ObjectModelAttributeGetProcessor(model), new StringConverter()));
42                 result.put("text", new AttributeProcessingUnit(new SetterAttributeProcessor("setText", false), new GetterAttributeProcessor("getText"), new StringConverter()));
43                 result.put("checked", new AttributeProcessingUnit(new SetterAttributeProcessor("setSelected", true), new GetterAttributeProcessor("isSelected"), new BooleanConverter()));
44
45                 return result;
46         }
47
48
49         @Override
50         protected Object newComponent() {
51                 return new JRadioButton();
52         }
53
54         @Override
55         protected void processingChildComponents() { }
56
57         /**
58          * radioタグのname属性の為のAttributeProcessor。
59          */
60         private class ChangeGroupProcessor implements AttributeSetProcessor {
61                 @Override
62                 public void invokeSet(Object target, String name, Object value) {
63                         JRadioButton radio = (JRadioButton)getComponent();
64                         //旧所属ButtonGroupからの引揚げ
65                         for(ButtonGroup group : buttonGroups.values()){
66                                 group.remove(radio);
67                                 //ButtonGroupがすべてのラジオボタンを失っても、それを残しておく。
68                         }
69                         //新たなButtonGroupへの登録
70                         ButtonGroup group = buttonGroups.get(value);
71                         if(group == null) buttonGroups.put((String)value, group = new ButtonGroup());
72                         group.add(radio);
73                 }
74         }
75
76         @Override
77         protected Map<String, EventHandler> intiProcessingAction() {
78                 Map<String, EventHandler> action = SwingEventHandler.commonEvent();
79                 action.put("check", new ActionEventHandler(SwingEventType.CHECK));
80                 action.put("uncheck", new ActionEventHandler(SwingEventType.UNCHECK));
81                 return action;
82         }
83 }