1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.swing.event.ChangeListener ; 29 30 34 public class TableClosure { 35 36 38 private final Set <Table> tables; 39 40 private final Set <Table> availableTables = new HashSet <Table>(); 41 private final Set <Table> wantedTables = new HashSet <Table>(); 42 private final Set <Table> selectedTables = new HashSet <Table>(); 43 private final Set <Table> referencedTables = new HashSet <Table>(); 44 45 private final Set <Table> unmodifAvailableTables = Collections.unmodifiableSet(availableTables); 47 private final Set <Table> unmodifWantedTables = Collections.unmodifiableSet(wantedTables); 48 private final Set <Table> unmodifSelectedTables = Collections.unmodifiableSet(selectedTables); 49 private final Set <Table> unmodifReferencedTables = Collections.unmodifiableSet(referencedTables); 50 51 private boolean closureEnabled = true; 52 53 private final ChangeSupport changeSupport = new ChangeSupport(this); 54 55 public TableClosure(TableProvider tableProvider) { 56 tables = tableProvider.getTables(); 57 availableTables.addAll(tables); 58 } 59 60 public void addChangeListener(ChangeListener listener) { 61 changeSupport.addChangeListener(listener); 62 } 63 64 public void removeChangeListener(ChangeListener listener) { 65 changeSupport.removeChangeListener(listener); 66 } 67 68 public Set <Table> getAvailableTables() { 69 return unmodifAvailableTables; 70 } 71 72 public Set <Table> getSelectedTables() { 73 return unmodifSelectedTables; 74 } 75 76 public Set <Table> getReferencedTables() { 77 return unmodifReferencedTables; 78 } 79 80 83 Set <Table> getWantedTables() { 84 return unmodifWantedTables; 85 } 86 87 public void addTables(Set <Table> tables) { 88 if (!canAddAllTables(tables)) { 89 return; 90 } 91 if (closureEnabled) { 92 if (wantedTables.addAll(tables)) { 93 Set <Table> refTables = removeDisabledTables(getReferencedTablesTransitively(tables)); 94 Set <Table> addedTables = new HashSet <Table>(tables); 95 addedTables.addAll(refTables); 96 97 selectedTables.addAll(addedTables); 98 referencedTables.addAll(refTables); 99 availableTables.removeAll(addedTables); 100 101 Set <Table> joinTables = removeDisabledTables(getJoinTablesTransitively(addedTables)); 102 joinTables.removeAll(selectedTables); 106 selectedTables.addAll(joinTables); 107 108 referencedTables.addAll(joinTables); 109 availableTables.removeAll(joinTables); 110 111 changeSupport.fireChange(); 112 } 113 } else { 114 wantedTables.addAll(tables); 115 selectedTables.addAll(tables); 116 availableTables.removeAll(tables); 117 118 changeSupport.fireChange(); 119 } 120 } 121 122 public void removeTables(Set <Table> tables) { 123 if (!canRemoveAllTables(tables)) { 124 return; 125 } 126 127 if (closureEnabled) { 128 if (wantedTables.removeAll(tables)) { 129 redoClosure(); 130 131 changeSupport.fireChange(); 132 } 133 } else { 134 wantedTables.removeAll(tables); 135 selectedTables.removeAll(tables); 136 availableTables.addAll(tables); 137 138 changeSupport.fireChange(); 139 } 140 } 141 142 public void addAllTables() { 143 wantedTables.clear(); 144 for (Table table : tables) { 145 if (!table.isDisabled()) { 146 wantedTables.add(table); 147 } 148 } 149 150 if (closureEnabled) { 151 redoClosure(); 152 153 changeSupport.fireChange(); 154 } else { 155 selectedTables.addAll(wantedTables); 156 availableTables.addAll(tables); 157 availableTables.removeAll(wantedTables); 158 159 changeSupport.fireChange(); 160 } 161 } 162 163 public void removeAllTables() { 164 wantedTables.clear(); 165 selectedTables.clear(); 166 referencedTables.clear(); 167 availableTables.addAll(tables); 168 169 changeSupport.fireChange(); 170 } 171 172 public boolean getClosureEnabled() { 173 return closureEnabled; 174 } 175 176 public void setClosureEnabled(boolean closureEnabled) { 177 if (this.closureEnabled == closureEnabled) { 178 return; 179 } 180 181 this.closureEnabled = closureEnabled; 182 183 if (closureEnabled) { 184 redoClosure(); 185 } else { 186 selectedTables.clear(); 187 selectedTables.addAll(wantedTables); 188 189 referencedTables.clear(); 190 191 availableTables.addAll(tables); 192 availableTables.removeAll(wantedTables); 193 } 194 195 changeSupport.fireChange(); 196 } 197 198 public boolean canAddAllTables(Set <Table> tables) { 199 if (tables.size() <= 0) { 200 return false; 201 } 202 for (Table table : tables) { 204 if (table.isDisabled()) { 205 return false; 206 } 207 } 208 return true; 209 } 210 211 public boolean canAddSomeTables(Set <Table> tables) { 212 if (tables.size() <= 0) { 213 return false; 214 } 215 for (Table table : tables) { 216 if (!table.isDisabled()) { 217 return true; 218 } 219 } 220 return false; 221 } 222 223 public boolean canRemoveAllTables(Set <Table> tables) { 224 if (tables.size() <= 0) { 225 return false; 226 } 227 228 231 if (!closureEnabled) { 232 return true; 233 } 234 for (Table table : tables) { 238 if (referencedTables.contains(table)) { 239 return false; 240 } 241 } 242 return true; 243 } 244 245 private void redoClosure() { 246 referencedTables.clear(); 247 referencedTables.addAll(removeDisabledTables(getReferencedTablesTransitively(wantedTables))); 248 249 selectedTables.clear(); 250 selectedTables.addAll(wantedTables); 251 selectedTables.addAll(referencedTables); 252 253 Set <Table> joinTables = removeDisabledTables(getJoinTablesTransitively(selectedTables)); 254 joinTables.removeAll(selectedTables); 256 257 selectedTables.addAll(joinTables); 258 referencedTables.addAll(joinTables); 259 260 availableTables.clear(); 261 availableTables.addAll(tables); 262 availableTables.removeAll(selectedTables); 263 } 264 265 270 private Set <Table> getReferencedTablesTransitively(Set <Table> tables) { 271 Queue<Table> tableQueue = new Queue<Table>(tables); 272 Set <Table> referencedTables = new HashSet <Table>(); 273 274 while (!tableQueue.isEmpty()) { 275 Table table = tableQueue.poll(); 276 277 for (Table referencedTable : table.getReferencedTables()) { 278 if (!referencedTable.equals(table)) { 279 referencedTables.add(referencedTable); 280 } 281 tableQueue.offer(referencedTable); 282 } 283 } 284 285 return referencedTables; 286 } 287 288 private Set <Table> getJoinTablesTransitively(Set <Table> tables) { 289 Queue<Table> tableQueue = new Queue<Table>(tables); 290 Set <Table> joinTables = new HashSet <Table>(); 291 292 while (!tableQueue.isEmpty()) { 293 Table table = tableQueue.poll(); 294 295 for (Table joinTable : table.getJoinTables()) { 296 if (areReferencedTablesSelected(joinTable, joinTables)) { 297 joinTables.add(joinTable); 298 tableQueue.offer(joinTable); 299 } 300 } 301 } 302 303 return joinTables; 304 } 305 306 312 private boolean areReferencedTablesSelected(Table table, Set <Table> additionalTables) { 313 for (Table referencedTable : table.getReferencedTables()) { 314 if (!selectedTables.contains(referencedTable) && !additionalTables.contains(referencedTable)) { 315 return false; 316 } 317 } 318 return true; 319 } 320 321 private static Set <Table> removeDisabledTables(Set <Table> tables) { 322 for (Iterator <Table> i = tables.iterator(); i.hasNext();) { 323 if (i.next().isDisabled()) { 324 i.remove(); 325 } 326 } 327 return tables; 328 } 329 330 336 static final class Queue<T> { 337 338 341 private final List <T> queue; 342 343 347 private final Set <T> contents; 348 349 352 private int currentIndex; 353 354 357 public Queue(Set <T> initialContents) { 358 assert !initialContents.contains(null); 359 360 queue = new ArrayList (initialContents); 361 contents = new HashSet (initialContents); 362 } 363 364 367 public void offer(T element) { 368 assert element != null; 369 370 if (!contents.contains(element)) { 371 contents.add(element); 372 queue.add(element); 373 } 374 } 375 376 379 public boolean isEmpty() { 380 return currentIndex >= queue.size(); 381 } 382 383 387 public T poll() { 388 T result = null; 389 if (!isEmpty()) { 390 result = queue.get(currentIndex); 391 currentIndex++; 392 } 393 return result; 394 } 395 } 396 } 397 | Popular Tags |