1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.util.ArrayList ; 59 import java.util.Collections ; 60 import java.util.HashMap ; 61 import java.util.List ; 62 import java.util.Map ; 63 64 import org.apache.commons.lang.Validate; 65 import org.objectstyle.cayenne.map.ObjEntity; 66 67 73 public class ObjEntityView { 74 75 private DataView owner; 76 private ObjEntity objEntity; 77 private List fields = new ArrayList (); 78 private Map nameFieldMap = new HashMap (); 79 private List readOnlyFields = Collections.unmodifiableList(fields); 80 private String name; 81 82 public ObjEntityView() { 83 } 84 85 public String getName() { 86 return name; 87 } 88 89 public void setName(String name) { 90 Validate.notNull(name); 91 this.name = name; 92 } 93 94 public List getFields() { 95 return readOnlyFields; 96 } 97 98 public List getVisibleFields() { 99 int size = getFieldCount(); 100 List dst = new ArrayList (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 getEditableFields() { 110 int size = getFieldCount(); 111 List dst = new ArrayList (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 fieldName) { 134 return (ObjEntityViewField) nameFieldMap.get(fieldName); 135 } 136 137 public ObjEntityViewField getFieldForObjAttribute(String 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 } 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 |