OSDN Git Service

srcディレクトリとdocディレクトリを作成
[xdf/git-repos.git] / src / xdf / src / main / java / jp / ac / aiit / xdf / core / model / ObjectModelSupport.java
1 package jp.ac.aiit.xdf.core.model;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 import jp.ac.aiit.xdf.component.ComponentMapper;
11 import jp.ac.aiit.xdf.core.action.Action;
12 import jp.ac.aiit.xdf.core.action.EventType;
13
14 /**
15  * オブジェクトモデルのデフォルト実装を提供するクラス。
16  * オブジェクトモデル拡張のためのサポートクラスとして存在する。ただ実装として利用したい場合はDefaultObjectModelを利用すること。
17  * 
18  * @author Shunichi Takagi
19  */
20 public abstract class ObjectModelSupport implements ObjectModel {
21         private String tagname;
22         private ComponentMapper mapper;
23         
24         private String id;
25         private List<String> classes;
26         private Map<String, Object> attrs;
27         
28         private ObjectModel parent;
29         private List<ObjectModel> children;
30         
31         // アクション情報保持する
32         private Map<String, Action> actions;
33
34         /**
35          * オブジェクトモデルの初期化
36          * @param tagname
37          */
38         public ObjectModelSupport(String tagname) {
39                 this.tagname = tagname;
40                 
41                 this.id = null;
42                 this.classes = new ArrayList<String>();
43                 this.attrs = new HashMap<String, Object>();
44                 
45                 this.parent = null;
46                 this.children = new ArrayList<ObjectModel>();
47                 
48                 this.actions = new HashMap<String, Action>();
49         }
50         
51         public String tagname() {
52                 return tagname;
53         }
54         
55         public void addClass(String clazz) {
56                 classes.add(clazz);
57         }
58
59         public Map<String, Object> attrMaps() {
60                 return Collections.unmodifiableMap(attrs);
61         }
62
63         public Set<String> attrNames() {
64                 return Collections.unmodifiableSet(attrs.keySet());
65         }
66
67         public void append(ObjectModel model) {
68                 model.parent(this);
69                 children.add(model);
70         }
71
72         public void attr(String name, Object value) {
73                 if (mapper.isRealized()){
74                         mapper.setAttr(name, value);
75                 }else {
76                         attrs.put(name, value);
77                 }
78         }
79         
80         public boolean hasAttr(String name) {
81                 return attrs.containsKey(name);
82         }
83
84         public Object attr(String name) {
85                 return (mapper.isRealized() ? mapper.getAttr(name):attrs.get(name));
86         }
87         
88         public Object attrFromModel(String name) {
89                 return attrs.get(name);
90         }
91
92         public List<ObjectModel> children() {
93                 return Collections.unmodifiableList(children);
94         }
95
96         public void empty() {
97                 children.clear();
98         }
99
100         public boolean hasClass(String clazz) {
101                 return classes.contains(clazz);
102         }
103
104         public boolean id(String id) {
105                 //TODO 所属ツリー上で同一のIDが指定されていないか確認しなければならない
106                 this.id = id;
107                 return true;
108         }
109
110         public boolean id(String id, boolean force) {
111                 //TODO 所属ツリー上で同一のIDが指定されていないか確認しなければならない
112                 this.id = id;
113                 return true;
114         }
115
116         public String id() {
117                 return id;
118         }
119
120         public ObjectModel parent() {
121                 return parent;
122         }
123
124         @Override
125         public void parent(ObjectModel parent) {
126                 this.parent = parent;
127         }
128         
129         public ObjectModel rootModel() {
130                 ObjectModel m = this;
131                 while( m.parent() != null ) {
132                         m = m.parent();
133                 }
134                 return m;
135         }
136
137         public void remove() {
138                 parent.remove(this);
139         }
140
141         public Object removeAttr(String name) {
142                 return attrs.remove(name);
143         }
144
145         public void removeClass(String clazz) {
146                 classes.remove(clazz);
147         }
148
149         public void remove(ObjectModel model) {
150                 children.remove(model);
151         }
152
153         @Override
154         public void setComponentMapper(ComponentMapper mapper) {
155                 this.mapper = mapper;
156                 mapper.setModel(this);
157         }
158
159         @Override
160         public Object getComponent() {
161                 return ( mapper.isRealized() ? mapper.getComponent() : null );
162         }
163
164         @Override
165         public void realize() {
166                 mapper.realize();
167         }
168
169         @Override
170         public void setAction(Action action, String event){
171                 actions.put(event, action);
172         }
173
174         @Override
175         public Action getAction(EventType event) {
176                 return actions.get(event);
177         }
178         
179         @Override
180         public Map<String, Action> getActions() {
181                 return actions;
182         }
183         
184         @Override
185         public void attrWeakly(String name, String value){
186                 if( !hasAttr(name) && ( mapper != null ))  {
187                         attr(name, mapper.typeConvert(name, value));
188                 }
189         }
190         
191         @Override
192         public List<String> classes(){ return classes; }
193
194         @Override
195         public void close() { }
196
197         @Override
198         public void open() { }
199
200
201         @Override
202         public void setRealize(boolean realize){
203                 mapper.setRealize(realize);
204         }
205
206 }