1 56 package org.objectstyle.cayenne.modeler.dialog.db; 57 58 import java.awt.Component ; 59 import java.util.ArrayList ; 60 import java.util.Collection ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 import java.util.Map ; 65 66 import org.objectstyle.cayenne.map.DataMap; 67 import org.objectstyle.cayenne.map.DbAttribute; 68 import org.objectstyle.cayenne.map.DbEntity; 69 import org.objectstyle.cayenne.map.DbRelationship; 70 import org.objectstyle.cayenne.map.DerivedDbEntity; 71 import org.objectstyle.cayenne.map.Entity; 72 import org.objectstyle.cayenne.modeler.ProjectController; 73 import org.objectstyle.cayenne.modeler.dialog.validator.ValidationDisplayHandler; 74 import org.objectstyle.cayenne.modeler.util.CayenneController; 75 import org.objectstyle.cayenne.project.validator.ValidationInfo; 76 import org.objectstyle.cayenne.project.validator.Validator; 77 import org.objectstyle.cayenne.swing.ObjectBinding; 78 import org.objectstyle.cayenne.swing.TableBindingBuilder; 79 80 83 public class TableSelector extends CayenneController { 84 85 protected TableSelectorView view; 86 protected ObjectBinding tableBinding; 87 88 protected DbEntity table; 89 protected List tables; 90 protected Map excludedTables; 91 protected Map validationMessages; 92 93 public TableSelector(ProjectController parent) { 94 super(parent); 95 this.view = new TableSelectorView(); 96 this.excludedTables = new HashMap (); 97 this.validationMessages = new HashMap (); 98 initController(); 99 } 100 101 103 public Component getView() { 104 return view; 105 } 106 107 110 public void setTable(DbEntity table) { 111 this.table = table; 112 } 113 114 117 public Collection getExcludedTables() { 118 return excludedTables.values(); 119 } 120 121 public List getTables() { 122 return tables; 123 } 124 125 public boolean isIncluded() { 126 if (table == null) { 127 return false; 128 } 129 130 return !excludedTables.containsKey(table.getName()); 131 } 132 133 public void setIncluded(boolean b) { 134 if (table == null) { 135 return; 136 } 137 138 if (b) { 139 excludedTables.remove(table.getName()); 140 } 141 else { 142 excludedTables.put(table.getName(), table); 143 } 144 } 145 146 public Object getProblem() { 147 return (table != null) ? validationMessages.get(table.getName()) : null; 148 } 149 150 152 protected void initController() { 153 TableBindingBuilder tableBuilder = new TableBindingBuilder(getApplication() 154 .getBindingFactory(), this); 155 156 tableBuilder.addColumn("Table", "#item.name", String .class, false); 157 tableBuilder.addColumn( 158 "Generate", 159 "setTable(#item), included", 160 Boolean .class, 161 true); 162 tableBuilder.addColumn( 163 "Problems", 164 "setTable(#item), problem", 165 String .class, 166 false); 167 168 this.tableBinding = tableBuilder.bindToTable(view.getTables(), "tables"); 169 } 170 171 175 public void updateTables(DataMap dataMap) { 176 this.tables = new ArrayList (dataMap.getDbEntities()); 177 178 excludedTables.clear(); 179 validationMessages.clear(); 180 181 184 187 Validator validator = ((ProjectController) getParent()) 188 .getProject() 189 .getValidator(); 190 int validationCode = validator.validate(); 191 if (validationCode >= ValidationDisplayHandler.WARNING) { 192 193 Iterator it = validator.validationResults().iterator(); 194 while (it.hasNext()) { 195 ValidationInfo nextProblem = (ValidationInfo) it.next(); 196 Entity failedEntity = null; 197 198 if (nextProblem.getValidatedObject() instanceof DbAttribute) { 199 DbAttribute failedAttribute = (DbAttribute) nextProblem 200 .getValidatedObject(); 201 failedEntity = failedAttribute.getEntity(); 202 } 203 else if (nextProblem.getValidatedObject() instanceof DbRelationship) { 204 DbRelationship failedRelationship = (DbRelationship) nextProblem 205 .getValidatedObject(); 206 failedEntity = failedRelationship.getSourceEntity(); 207 } 208 else if (nextProblem.getValidatedObject() instanceof DbEntity) { 209 failedEntity = (Entity) nextProblem.getValidatedObject(); 210 } 211 212 if (failedEntity == null) { 213 continue; 214 } 215 216 excludedTables.put(failedEntity.getName(), failedEntity); 217 validationMessages.put(failedEntity.getName(), nextProblem.getMessage()); 218 } 219 } 220 221 Iterator tablesIt = tables.iterator(); 223 while (tablesIt.hasNext()) { 224 DbEntity table = (DbEntity) tablesIt.next(); 225 if (table instanceof DerivedDbEntity) { 226 excludedTables.put(table.getName(), table); 227 validationMessages.put(table.getName(), "derived entity"); 228 } 229 } 230 231 tableBinding.updateView(); 232 } 233 } | Popular Tags |