1 56 57 package org.objectstyle.cayenne.dataview.dvmodeler; 58 59 import java.io.File ; 60 import java.util.ArrayList ; 61 import java.util.Collections ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 import java.util.Set ; 65 import java.util.TreeSet ; 66 67 import org.jdom.Element; 68 69 74 75 public class DataView extends DVObject { 76 private CayenneProject cayenneProject; 77 private List objEntityViews = new ArrayList (); 78 private File file; 79 80 private List saveErrors = new ArrayList (); 81 82 public DataView(CayenneProject cayenneProject, String name, File file){ 83 setName(name); 84 this.cayenneProject = cayenneProject; 85 this.file = file; 86 } 87 88 public DataView(CayenneProject cayenneProject, File file){ 89 setName(file.getName()); 90 this.cayenneProject = cayenneProject; 91 this.file = file; 92 } 93 94 public DataView(CayenneProject cayenneProject){ 95 this.cayenneProject = cayenneProject; 96 setName("DataView"); 97 file = new File (cayenneProject.getProjectDirectory(), "DataView.view.xml"); 98 } 99 100 public List getSaveErrors(){ 101 return Collections.unmodifiableList(saveErrors); 102 } 103 104 public CayenneProject getCayenneProject(){ 105 return cayenneProject; 106 } 107 108 public void setFile(File file){ 109 this.file = file; 110 } 111 112 public File getFile(){ 113 return file; 114 } 115 116 public List getObjEntityViews() { 117 return Collections.unmodifiableList(objEntityViews); 118 } 119 120 public ObjEntityView createObjEntityView(List allObjEntityViews){ 121 String nameRoot = "ObjEntityView"; 122 String name = nameRoot; 123 for (int i = 0; i < Integer.MAX_VALUE; i++) { 124 name = nameRoot + i; 125 boolean nameExists = false; 126 for (int j = 0; j < allObjEntityViews.size(); j++) { 127 ObjEntityView objEntityView = (ObjEntityView)allObjEntityViews.get(j); 128 nameExists = name.equalsIgnoreCase(objEntityView.getName()); 129 if (nameExists) 130 break; 131 } 132 if (!nameExists) 133 break; 134 } 135 ObjEntityView objEntityView = new ObjEntityView(this); 136 objEntityView.setName(name); 137 return objEntityView; 138 } 139 140 public void addObjEntityView(ObjEntityView objEntityView){ 141 objEntityViews.add(objEntityView); 142 } 143 144 public void removeObjEntityView(ObjEntityView objEntityView){ 145 ObjEntity entity = objEntityView.getObjEntity(); 146 if (entity != null){ 147 entity.removeObjEntityView(objEntityView); 148 } 149 objEntityViews.remove(objEntityView); 150 } 151 152 public ObjEntityView getObjEntityView(String name){ 153 Iterator itr = objEntityViews.iterator(); 154 while (itr.hasNext()){ 155 Object o = itr.next(); 156 ObjEntityView objEnt = (ObjEntityView)o; 157 if (objEnt.getName().equals(name)){ 158 return objEnt; 159 } 160 } 161 return null; 162 } 163 164 public ObjEntityView getObjEntityView(int index){ 165 return (ObjEntityView)(objEntityViews.get(index)); 166 } 167 168 public int getIndexOfObjEntityView(ObjEntityView objEntityView){ 169 return objEntityViews.indexOf(objEntityView); 170 } 171 172 173 public int getObjEntityViewCount(){ 174 return objEntityViews.size(); 175 } 176 177 public void clear(){ 178 if (objEntityViews.size() != 0){ 179 for (Iterator j = this.getObjEntityViews().iterator(); j.hasNext();){ 180 ObjEntityView view = (ObjEntityView)j.next(); 181 ObjEntity entity = view.getObjEntity(); 182 entity.removeObjEntityView(view); 183 } 184 objEntityViews.clear(); 185 } 186 } 187 188 public String toString(){ 189 return this.getName(); 190 } 191 public Element getDataViewElement(){ 192 Element dataViewElement = new Element("data-view"); 193 194 if (saveErrors.size() != 0){ 195 saveErrors.clear(); 196 } 197 198 List objEntityViewElements = new ArrayList (); 199 Set objEntityViewsSort = new TreeSet (new ObjEntityViewsComparator()); 200 objEntityViewsSort.addAll(objEntityViews); 201 202 Iterator itr = objEntityViewsSort.iterator(); 203 while (itr.hasNext()){ 204 Object o = itr.next(); 205 ObjEntityView view = (ObjEntityView)o; 206 Element objEntityViewElement = 207 view.getObjEntityViewElement(); 208 saveErrors.addAll(view.getSaveErrors()); 209 objEntityViewElements.add(objEntityViewElement); 210 } 211 dataViewElement.setContent(objEntityViewElements); 212 213 return dataViewElement; 214 } 215 } | Popular Tags |