1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.EnumSet ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.TreeMap ; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.ChangeListener ; 34 import org.netbeans.api.project.SourceGroup; 35 import org.netbeans.modules.j2ee.persistence.dd.JavaPersistenceQLKeywords; 36 import org.netbeans.modules.j2ee.persistence.entitygenerator.EntityMember; 37 import org.openide.filesystems.FileObject; 38 import org.openide.util.NbBundle; 39 import org.openide.util.Utilities; 40 41 45 public final class SelectedTables { 46 47 enum Problem { NO_JAVA_IDENTIFIER, JPA_QL_IDENTIFIER, ALREADY_EXISTS }; 49 50 private final PersistenceGenerator persistenceGen; 51 private final Map <Table, String > table2ClassName = new HashMap <Table, String >(); 52 private final Map <Table, Set <Problem>> table2Problems = new TreeMap <Table, Set <Problem>>(); 53 54 private final ChangeListener tableClosureListener = new TableClosureListener(); 55 private final ChangeSupport changeSupport = new ChangeSupport(this); 56 57 private TableClosure tableClosure; 58 private SourceGroup location; 59 private String packageName; 60 private FileObject targetFolder; 61 62 private Set <Table> validatedTables = Collections.emptySet(); 63 64 public SelectedTables(PersistenceGenerator persistenceGen, TableClosure tableClosure, SourceGroup location, String packageName) throws IOException { 65 assert persistenceGen != null; 66 67 this.persistenceGen = persistenceGen; 68 setTableClosureAndTargetFolder(tableClosure, location, packageName); 69 } 70 71 76 public void setTableClosureAndTargetFolder(TableClosure tableClosure, SourceGroup location, String packageName) throws IOException { 77 assert tableClosure != null; 78 79 boolean tableClosureChanged = changeTableClosure(tableClosure); 80 boolean targetFolderChanged = changeTargetFolder(location, packageName); 81 82 if (tableClosureChanged || targetFolderChanged) { 83 revalidateTables(); 84 } 85 } 86 87 90 public void setTargetFolder(SourceGroup location, String packageName) throws IOException { 91 if (changeTargetFolder(location, packageName)) { 92 revalidateTables(); 93 } 94 } 95 96 101 private boolean changeTableClosure(TableClosure tableClosure) { 102 if (!tableClosure.equals(this.tableClosure)) { 103 if (this.tableClosure != null) { 104 this.tableClosure.removeChangeListener(tableClosureListener); 105 } 106 this.tableClosure = tableClosure; 107 table2ClassName.clear(); 108 this.tableClosure.addChangeListener(tableClosureListener); 109 return true; 110 } 111 return false; 112 } 113 114 119 private boolean changeTargetFolder(SourceGroup location, String packageName) throws IOException { 120 if (!Utilities.compareObjects(location, this.location) || !Utilities.compareObjects(packageName, this.packageName)) { 121 this.location = location; 122 this.packageName = packageName; 123 124 if (location != null && packageName != null) { 125 targetFolder = SourceGroupSupport.getFolderForPackage(location, packageName, false); 126 } else { 127 targetFolder = null; 128 } 129 return true; 130 } 131 return false; 132 } 133 134 139 public void addChangeListener(ChangeListener listener) { 140 changeSupport.addChangeListener(listener); 141 } 142 143 146 public void removeChangeListener(ChangeListener listener) { 147 changeSupport.removeChangeListener(listener); 148 } 149 150 private void revalidateTables() { 151 validatedTables = Collections.emptySet(); 152 table2Problems.clear(); 153 validateTables(); 154 } 155 156 private void validateTables() { 157 Set <Table> addedTables = new HashSet <Table>(tableClosure.getSelectedTables()); 158 addedTables.removeAll(validatedTables); 159 160 Set <Table> removedTables = new HashSet <Table>(validatedTables); 161 removedTables.removeAll(tableClosure.getSelectedTables()); 162 163 for (Table table : removedTables) { 164 table2Problems.remove(table); 165 } 166 for (Table table : addedTables) { 167 putProblems(table, validateClassName(getClassName(table))); 168 } 169 validatedTables = new HashSet <Table>(tableClosure.getSelectedTables()); 170 171 changeSupport.fireChange(); 172 } 173 174 178 public List <Table> getTables() { 179 List <Table> result = new ArrayList <Table>(tableClosure.getSelectedTables()); 180 Collections.sort(result); 181 return result; 182 } 183 184 187 public String getClassName(Table table) { 188 assert table != null; 189 190 String className = table2ClassName.get(table); 191 if (className == null) { 192 className = EntityMember.makeClassName(table.getName()); 193 className = persistenceGen.generateEntityName(className); 194 } 195 return className; 196 } 197 198 201 public void setClassName(Table table, String className) { 202 assert table != null; 203 assert className != null; 204 205 table2ClassName.put(table, className); 206 putProblems(table, validateClassName(className)); 207 208 changeSupport.fireChange(); 209 } 210 211 private void putProblems(Table table, Set <Problem> problems) { 212 if (problems.isEmpty()) { 213 table2Problems.remove(table); 214 } else { 215 table2Problems.put(table, problems); 216 } 217 } 218 219 private Set <Problem> validateClassName(String className) { 220 Set <Problem> problems = EnumSet.noneOf(Problem.class); 221 if (!Utilities.isJavaIdentifier(className)) { 222 problems.add(Problem.NO_JAVA_IDENTIFIER); 223 } 224 if (JavaPersistenceQLKeywords.isKeyword(className)) { 225 problems.add(Problem.JPA_QL_IDENTIFIER); 226 } 227 if (targetFolder != null && targetFolder.getFileObject(className, "java") != null) { problems.add(Problem.ALREADY_EXISTS); 229 } 230 return problems; 231 } 232 233 public void ensureUniqueClassNames() { 234 Set <Table> tables = tableClosure.getSelectedTables(); 236 if (targetFolder != null) { 237 for (Table t : tables) { 238 String className = getClassName(t); 239 boolean existingFile = (targetFolder.getFileObject(className, "java") != null); 241 if (existingFile){ 242 setClassName(t, className+'1'); 243 } 244 } 245 } 246 } 247 248 254 public String getFirstProblemDisplayName() { 255 Map.Entry <Table, Set <Problem>> firstProblemEntry = getFirstProblemEntry(); 256 if (firstProblemEntry == null) { 257 return null; 258 } 259 return getProblemDisplayNameForTable(firstProblemEntry.getKey(), 260 firstProblemEntry.getValue()); 261 } 262 263 private Map.Entry <Table, Set <Problem>> getFirstProblemEntry() { 264 Set <Map.Entry <Table, Set <Problem>>> problemEntries = table2Problems.entrySet(); 265 if (problemEntries.isEmpty()) { 266 return null; 267 } 268 return problemEntries.iterator().next(); 269 } 270 271 private String getProblemDisplayNameForTable(Table table, Set <Problem> problems) { 272 Problem problem = problems.iterator().next(); 273 return getProblemDisplayName(problem, getClassName(table)); 274 } 275 276 String getProblemDisplayNameForTable(Table table) { 277 Set <Problem> problems = table2Problems.get(table); 278 return ((problems != null) ? 279 getProblemDisplayNameForTable(table, problems) : null); 280 } 281 282 boolean hasProblem(Table table) { 283 return table2Problems.containsKey(table); 284 } 285 286 289 Problem getFirstProblem() { 290 Map.Entry <Table, Set <Problem>> firstProblemEntry = getFirstProblemEntry(); 291 if (firstProblemEntry == null) { 292 return null; 293 } 294 Set <Problem> problems = firstProblemEntry.getValue(); 295 return problems.iterator().next(); 296 } 297 298 private static String getProblemDisplayName(Problem problem, String className) { 299 switch (problem) { 300 case NO_JAVA_IDENTIFIER: 301 return NbBundle.getMessage(SelectedTables.class, "ERR_NoJavaIdentifier", className); 302 303 case JPA_QL_IDENTIFIER: 304 return NbBundle.getMessage(SelectedTables.class, "ERR_ReservedQLKeyword", className); 305 306 case ALREADY_EXISTS: 307 return NbBundle.getMessage(SelectedTables.class, "ERR_AlreadyExists", className); 308 309 default: 310 assert false : problem + " should be handled in getProblemDisplayName()"; } 312 return null; 313 } 314 315 private final class TableClosureListener implements ChangeListener { 316 317 public void stateChanged(ChangeEvent event) { 318 validateTables(); 319 } 320 } 321 } 322 | Popular Tags |