KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.dataview;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Collections JavaDoc;
60 import java.util.HashMap JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import org.apache.commons.lang.Validate;
65 import org.objectstyle.cayenne.map.ObjEntity;
66
67 /**
68  * A view rooted in an ObjEntity.
69  *
70  * @since 1.1
71  * @author Andriy Shapochka
72  */

73 public class ObjEntityView {
74
75     private DataView owner;
76     private ObjEntity objEntity;
77     private List JavaDoc fields = new ArrayList JavaDoc();
78     private Map JavaDoc nameFieldMap = new HashMap JavaDoc();
79     private List JavaDoc readOnlyFields = Collections.unmodifiableList(fields);
80     private String JavaDoc name;
81
82     public ObjEntityView() {
83     }
84
85     public String JavaDoc getName() {
86         return name;
87     }
88
89     public void setName(String JavaDoc name) {
90         Validate.notNull(name);
91         this.name = name;
92     }
93
94     public List JavaDoc getFields() {
95         return readOnlyFields;
96     }
97
98     public List JavaDoc getVisibleFields() {
99         int size = getFieldCount();
100         List JavaDoc dst = new ArrayList JavaDoc(size);
101         for (int i = 0; i < size; i++) {
102             ObjEntityViewField field = getField(i);
103             if (field.isVisible())
104                 dst.add(field);
105         }
106         return dst;
107     }
108
109     public List JavaDoc getEditableFields() {
110         int size = getFieldCount();
111         List JavaDoc dst = new ArrayList JavaDoc(size);
112         for (int i = 0; i < size; i++) {
113             ObjEntityViewField field = getField(i);
114             if (field.isVisible() && field.isEditable())
115                 dst.add(field);
116         }
117         return dst;
118     }
119
120     public ObjEntity getObjEntity() {
121         return objEntity;
122     }
123
124     public void setObjEntity(ObjEntity objEntity) {
125         Validate.notNull(objEntity);
126         this.objEntity = objEntity;
127     }
128
129     public ObjEntityViewField getField(int index) {
130         return (ObjEntityViewField) fields.get(index);
131     }
132
133     public ObjEntityViewField getField(String JavaDoc fieldName) {
134         return (ObjEntityViewField) nameFieldMap.get(fieldName);
135     }
136
137     public ObjEntityViewField getFieldForObjAttribute(String JavaDoc objAttributeName) {
138         for (int i = 0; i < fields.size(); i++) {
139             ObjEntityViewField field = getField(i);
140             if (objAttributeName.equals(field.getObjAttribute().getName()))
141                 return field;
142         }
143         return null;
144     }
145
146     public int getFieldCount() {
147         return fields.size();
148     }
149
150     public boolean hasFields() {
151         return !fields.isEmpty();
152     }
153
154     public void clearFields() {
155         for (int i = 0; i < getFieldCount(); i++) {
156             ObjEntityViewField field = getField(i);
157             field.setOwner(null);
158             field.setIndex(-1);
159         }
160         fields.clear();
161         nameFieldMap.clear();
162     }
163
164     public boolean removeField(ObjEntityViewField field) {
165         if (field.getOwner() != this)
166             return false;
167         field.setOwner(null);
168         field.setIndex(-1);
169         nameFieldMap.remove(field.getName());
170         return fields.remove(field);
171     }
172
173     public int insertField(ObjEntityViewField field) {
174         if (field.getOwner() == this)
175             return field.getIndex();
176
177         Validate.notNull(field.getName());
178         Validate.isTrue(!nameFieldMap.containsKey(field.getName()));
179         field.setOwner(this);
180         nameFieldMap.put(field.getName(), field);
181
182         int prefIndex = field.getPreferredIndex();
183         int fieldCount = getFieldCount();
184
185         if (prefIndex < 0) {
186             int newIndex = fieldCount;
187             fields.add(field);
188             field.setIndex(newIndex);
189             return newIndex;
190         }
191
192         int index = 0;
193         int curPrefIndex = -1;
194         while (index < fieldCount
195                 && ((curPrefIndex = getField(index++).getPreferredIndex()) <= prefIndex)
196                 && curPrefIndex >= 0) {
197             // skip forward
198
}
199
200         fields.add(index, field);
201         field.setIndex(index);
202         fieldCount++;
203
204         for (int i = (index + 1); i < fieldCount; i++) {
205             getField(i).setIndex(i);
206         }
207
208         return index;
209     }
210
211     public DataView getOwner() {
212         return owner;
213     }
214
215     void setOwner(DataView owner) {
216         this.owner = owner;
217     }
218 }
Popular Tags