OSDN Git Service

initial commit
[xdf/git-repos.git] / xdf-swing / src / main / java / jp / ac / aiit / xdf / component / swing / mappers / SwingComponentMapperTemplate.java
1 package jp.ac.aiit.xdf.component.swing.mappers;
2
3
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Map.Entry;
7
8 import jp.ac.aiit.xdf.component.ComponentMapper;
9 import jp.ac.aiit.xdf.component.swing.attribute.AttributeProcessingUnit;
10 import jp.ac.aiit.xdf.component.swing.event.SwingEventHandler;
11 import jp.ac.aiit.xdf.core.action.Action;
12 import jp.ac.aiit.xdf.core.action.EventHandler;
13 import jp.ac.aiit.xdf.core.model.ObjectModel;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Swing用のコンポーネントマッパのテンプレートクラス
20  * このテンプレートクラスでは、Swingコンポーネントを生成するために必要な共通部分をテンプレート化し、
21  * 非共通部分をサブクラスで実装させる構成(テンプレートメソッドパターン)になっている。
22  * 
23  * @author Shunichi Takagi
24  */
25 public abstract class SwingComponentMapperTemplate implements ComponentMapper {
26         private static final Logger log = LoggerFactory.getLogger(SwingComponentMapperTemplate.class);
27         protected Map<String, AttributeProcessingUnit> processingUnits;
28         protected Map<String, EventHandler> processingAction;
29
30         protected ObjectModel model;
31         
32         private boolean realized;
33         protected Object component;
34
35         protected List<SwingEventHandler> eventhandlers;
36
37         /**
38          * 
39          */
40         public SwingComponentMapperTemplate() {
41                 this.processingUnits = initProcessingUnits();
42                 this.processingAction = intiProcessingAction();
43         }
44         
45         @Override
46         public void setModel(ObjectModel model) {
47                 this.model = model;
48         }
49         
50         @Override
51         public ObjectModel getModel() {
52                 return model;
53         }
54
55         @Override
56         public Object getComponent() {
57                 return component;
58         }
59
60         @Override
61         public boolean isRealized() {
62                 return realized;
63         }
64
65         @Override
66         public void realize() {
67                 this.component = newComponent();
68                 
69                 //ObjectModelに設定された属性を実コンポーネントに反映
70                 Map<String, Object> map = model.attrMaps();
71                 for(Entry<String, Object> entry : map.entrySet()) {
72                         AttributeProcessingUnit unit = processingUnits.get(entry.getKey());
73                         if( unit != null ) {
74                                 unit.invokeSet(component, entry.getKey(), entry.getValue());
75                         }
76                 }
77                 
78                 // アクションの実コンポーネントに反映
79                 Map<String, Action> actions = model.getActions();
80                 for (Entry<String, Action> action : actions.entrySet()){
81                         EventHandler handler = processingAction.get(action.getKey());
82                         if (handler != null){
83                                 handler.setEvent(this.model, this.component, action.getValue());
84                         } else {
85                                 log.debug("アクション登録失敗です。 <タグ名:"+ this.model.tagname() + "> <ID:" + this.model.id() + "> <アクション:"+ action.getKey() +">");
86                         }
87                 }
88
89                 processingChildComponents();
90                 //SwingEventHandler.setEvent(model, (Component) component);
91         
92                 this.realized = true;
93         }
94         
95         @Override
96         public Object typeConvert(String name, String value) {
97                 AttributeProcessingUnit unit = processingUnits.get(name);
98                 if( unit == null ) {
99                         return value;
100                 } else {
101                         return unit.typeConvert(value);
102                 }
103         }
104         
105         @Override
106         public Object getAttr(String name) {
107                 AttributeProcessingUnit unit = processingUnits.get(name);
108                 if( unit == null ) {
109                         return null;
110                 } else {
111                         return unit.invokeGet(this.component, name);
112                 }
113         }
114         
115         @Override
116         public void setAttr(String name, Object value){
117                 AttributeProcessingUnit unit = processingUnits.get(name);
118                 if( unit != null ) {
119                         unit.invokeSet(component, name, value);
120                 }
121         }
122
123         @Override
124         public void setRealize(boolean realize){
125                 this.realized = realize;
126         }
127         
128         protected abstract Object newComponent();
129         protected abstract void processingChildComponents();
130         protected abstract Map<String, AttributeProcessingUnit> initProcessingUnits();
131         protected abstract Map<String, EventHandler> intiProcessingAction();
132 }