KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > ObjEntityView


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dataview;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.commons.lang.Validate;
29 import org.apache.cayenne.map.ObjEntity;
30
31 /**
32  * A view rooted in an ObjEntity.
33  *
34  * @since 1.1
35  * @author Andriy Shapochka
36  */

37 public class ObjEntityView {
38
39     private DataView owner;
40     private ObjEntity objEntity;
41     private List JavaDoc fields = new ArrayList JavaDoc();
42     private Map JavaDoc nameFieldMap = new HashMap JavaDoc();
43     private List JavaDoc readOnlyFields = Collections.unmodifiableList(fields);
44     private String JavaDoc name;
45
46     public ObjEntityView() {
47     }
48
49     public String JavaDoc getName() {
50         return name;
51     }
52
53     public void setName(String JavaDoc name) {
54         Validate.notNull(name);
55         this.name = name;
56     }
57
58     public List JavaDoc getFields() {
59         return readOnlyFields;
60     }
61
62     public List JavaDoc getVisibleFields() {
63         int size = getFieldCount();
64         List JavaDoc dst = new ArrayList JavaDoc(size);
65         for (int i = 0; i < size; i++) {
66             ObjEntityViewField field = getField(i);
67             if (field.isVisible())
68                 dst.add(field);
69         }
70         return dst;
71     }
72
73     public List JavaDoc getEditableFields() {
74         int size = getFieldCount();
75         List JavaDoc dst = new ArrayList JavaDoc(size);
76         for (int i = 0; i < size; i++) {
77             ObjEntityViewField field = getField(i);
78             if (field.isVisible() && field.isEditable())
79                 dst.add(field);
80         }
81         return dst;
82     }
83
84     public ObjEntity getObjEntity() {
85         return objEntity;
86     }
87
88     public void setObjEntity(ObjEntity objEntity) {
89         Validate.notNull(objEntity);
90         this.objEntity = objEntity;
91     }
92
93     public ObjEntityViewField getField(int index) {
94         return (ObjEntityViewField) fields.get(index);
95     }
96
97     public ObjEntityViewField getField(String JavaDoc fieldName) {
98         return (ObjEntityViewField) nameFieldMap.get(fieldName);
99     }
100
101     public ObjEntityViewField getFieldForObjAttribute(String JavaDoc objAttributeName) {
102         for (int i = 0; i < fields.size(); i++) {
103             ObjEntityViewField field = getField(i);
104             if (objAttributeName.equals(field.getObjAttribute().getName()))
105                 return field;
106         }
107         return null;
108     }
109
110     public int getFieldCount() {
111         return fields.size();
112     }
113
114     public boolean hasFields() {
115         return !fields.isEmpty();
116     }
117
118     public void clearFields() {
119         for (int i = 0; i < getFieldCount(); i++) {
120             ObjEntityViewField field = getField(i);
121             field.setOwner(null);
122             field.setIndex(-1);
123         }
124         fields.clear();
125         nameFieldMap.clear();
126     }
127
128     public boolean removeField(ObjEntityViewField field) {
129         if (field.getOwner() != this)
130             return false;
131         field.setOwner(null);
132         field.setIndex(-1);
133         nameFieldMap.remove(field.getName());
134         return fields.remove(field);
135     }
136
137     public int insertField(ObjEntityViewField field) {
138         if (field.getOwner() == this)
139             return field.getIndex();
140
141         Validate.notNull(field.getName());
142         Validate.isTrue(!nameFieldMap.containsKey(field.getName()));
143         field.setOwner(this);
144         nameFieldMap.put(field.getName(), field);
145
146         int prefIndex = field.getPreferredIndex();
147         int fieldCount = getFieldCount();
148
149         if (prefIndex < 0) {
150             int newIndex = fieldCount;
151             fields.add(field);
152             field.setIndex(newIndex);
153             return newIndex;
154         }
155
156         int index = 0;
157         int curPrefIndex = -1;
158         while (index < fieldCount
159                 && ((curPrefIndex = getField(index++).getPreferredIndex()) <= prefIndex)
160                 && curPrefIndex >= 0) {
161             // skip forward
162
}
163
164         fields.add(index, field);
165         field.setIndex(index);
166         fieldCount++;
167
168         for (int i = (index + 1); i < fieldCount; i++) {
169             getField(i).setIndex(i);
170         }
171
172         return index;
173     }
174
175     public DataView getOwner() {
176         return owner;
177     }
178
179     void setOwner(DataView owner) {
180         this.owner = owner;
181     }
182 }
183
Popular Tags