View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.model.core.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.collections.BidiMap;
26  import org.apache.commons.collections.bidimap.DualHashBidiMap;
27  import org.seasar.tuigwaa.model.common.TgwElementVisitor;
28  import org.seasar.tuigwaa.model.core.TgwElement;
29  
30  public abstract class TgwElementImpl implements TgwElement {
31  
32  	private String name; // immutable
33  
34  	private String displayName;
35  
36  	private String description;
37  
38  	private TgwElement parent;
39  
40  	private List childList = Collections.synchronizedList(new ArrayList());
41  
42  	private Map childMap = Collections.synchronizedMap(new HashMap());
43  
44  	private BidiMap name2displayMap = new DualHashBidiMap();
45  
46  	public void setParent(TgwElement parent) {
47  		this.parent = parent;
48  	}
49  
50  	public TgwElement getParent() {
51  		return parent;
52  	}
53  
54  	public final String getName() {
55  		return name;
56  	}
57  
58  	public final void setName(String name) {
59  		this.name = name;
60  	}
61  
62  	public final String getDisplayName() {
63  		return displayName;
64  	}
65  
66  	public final void setDisplayName(String displayName) {
67  		this.displayName = displayName;
68  		if (getParent() != null) {
69  			getParent().updateChild(this);
70  		}
71  	}
72  
73  	public final String getDescription() {
74  		return description;
75  	}
76  
77  	public final void setDescription(String description) {
78  		this.description = description;
79  	}
80  
81  	public final void addChild(TgwElement elem) {
82  		if (elem == null) {
83  			return;
84  		}
85  		childList.add(elem);
86  
87  		String name = elem.getName();
88  		String displayName = elem.getDisplayName();
89  
90  		childMap.put(elem.getName(), elem);
91  		name2displayMap.put(name, displayName);
92  
93  		elem.setParent(this);
94  	}
95  
96  	public final void clearChildren() {
97  		childList.clear();
98  		childMap.clear();
99  		name2displayMap.clear();
100 	}
101 
102 	public final TgwElement getChild(String name) {
103 		return (TgwElement) childMap.get(name);
104 	}
105 
106 	public final TgwElement getChildByDisplayName(String displayName) {
107 		String name = (String) name2displayMap.inverseBidiMap()
108 				.get(displayName);
109 		return getChild(name);
110 	}
111 
112 	public final TgwElement removeChild(String name) {
113 		name2displayMap.remove(name);
114 		Object removedObj = childMap.remove(name);
115 		if (removedObj != null) {
116 			childList.remove(removedObj);
117 			return (TgwElement) removedObj;
118 		}
119 		return null;
120 	}
121 
122 	public final void updateChild(TgwElement childElem) {
123 		String childName = childElem.getName();
124 		String childDisplayName = childElem.getDisplayName();
125 		name2displayMap.put(childName, childDisplayName);
126 	}
127 
128 	public final Iterator getChildNameIterator() {
129 		return Collections.unmodifiableCollection(childMap.keySet()).iterator();
130 	}
131 
132 	public final List getChildList() {
133 		return Collections.unmodifiableList(childList);
134 	}
135 
136 	public abstract Object accept(TgwElementVisitor visitor, Object data);
137 
138 	public final void swapChild(int i, int j) {
139 		Collections.swap(childList, i, j);
140 	}
141 
142 }