1 19 20 package org.apache.cayenne.dataview; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.commons.lang.Validate; 29 import org.apache.cayenne.map.ObjEntity; 30 31 37 public class ObjEntityView { 38 39 private DataView owner; 40 private ObjEntity objEntity; 41 private List fields = new ArrayList (); 42 private Map nameFieldMap = new HashMap (); 43 private List readOnlyFields = Collections.unmodifiableList(fields); 44 private String name; 45 46 public ObjEntityView() { 47 } 48 49 public String getName() { 50 return name; 51 } 52 53 public void setName(String name) { 54 Validate.notNull(name); 55 this.name = name; 56 } 57 58 public List getFields() { 59 return readOnlyFields; 60 } 61 62 public List getVisibleFields() { 63 int size = getFieldCount(); 64 List dst = new ArrayList (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 getEditableFields() { 74 int size = getFieldCount(); 75 List dst = new ArrayList (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 fieldName) { 98 return (ObjEntityViewField) nameFieldMap.get(fieldName); 99 } 100 101 public ObjEntityViewField getFieldForObjAttribute(String 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 } 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 |