1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.awt.Component ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.Rectangle ; 26 import java.io.File ; 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.List ; 31 import java.util.Set ; 32 import javax.swing.JPanel ; 33 import javax.swing.SwingUtilities ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 36 import javax.swing.event.ListSelectionEvent ; 37 import javax.swing.event.ListSelectionListener ; 38 import org.netbeans.api.db.explorer.ConnectionManager; 39 import org.netbeans.api.db.explorer.DatabaseConnection; 40 import org.netbeans.api.db.explorer.JDBCDriver; 41 import org.netbeans.api.db.explorer.JDBCDriverManager; 42 import org.netbeans.api.db.explorer.support.DatabaseExplorerUIs; 43 import org.netbeans.api.project.Project; 44 import org.netbeans.api.project.SourceGroup; 45 import org.netbeans.modules.dbschema.SchemaElement; 46 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 47 import org.netbeans.modules.j2ee.persistence.spi.datasource.JPADataSource; 48 import org.netbeans.modules.j2ee.persistence.spi.datasource.JPADataSourcePopulator; 49 import org.netbeans.modules.j2ee.persistence.spi.datasource.JPADataSourceProvider; 50 import org.netbeans.modules.j2ee.persistence.util.SourceLevelChecker; 51 import org.netbeans.modules.j2ee.persistence.wizard.Util; 52 import org.netbeans.spi.project.ui.templates.support.Templates; 53 import org.openide.DialogDisplayer; 54 import org.openide.NotifyDescriptor; 55 import org.openide.WizardDescriptor; 56 import org.openide.filesystems.FileObject; 57 import org.openide.filesystems.FileUtil; 58 import org.openide.util.HelpCtx; 59 import org.openide.util.NbBundle; 60 import org.openide.util.RequestProcessor; 61 62 66 public class DatabaseTablesPanel extends javax.swing.JPanel { 67 68 private DBSchemaManager dbschemaManager = new DBSchemaManager(); 69 70 private PersistenceGenerator persistenceGen; 71 72 private SchemaElement sourceSchemaElement; 73 private DatabaseConnection dbconn; 74 private FileObject dbschemaFile; 75 private String datasourceName; 76 private TableClosure tableClosure; 77 78 private ChangeSupport changeSupport = new ChangeSupport(this); 79 80 private boolean sourceSchemaUpdateEnabled; 81 82 private Project project; 83 84 public DatabaseTablesPanel() { 85 initComponents(); 86 87 ListSelectionListener selectionListener = new ListSelectionListener () { 88 public void valueChanged(ListSelectionEvent e) { 89 updateButtons(); 90 } 91 }; 92 availableTablesList.getSelectionModel().addListSelectionListener(selectionListener); 93 selectedTablesList.getSelectionModel().addListSelectionListener(selectionListener); 94 } 95 96 public void addChangeListener(ChangeListener listener) { 97 changeSupport.addChangeListener(listener); 98 } 99 100 public void initialize(Project project, DBSchemaFileList dbschemaFileList, PersistenceGenerator persistenceGen, TableSource tableSource, FileObject targetFolder) { 101 this.persistenceGen = persistenceGen; 102 this.project = project; 103 104 boolean enabled = ProviderUtil.isValidServerInstanceOrNone(project); 105 106 if (enabled) { 107 boolean withDatasources = Util.isSupportedJavaEEVersion(project) || Util.isEjb21Module(project); 108 if (withDatasources) { 109 initializeWithDatasources(); 110 } else { 111 initializeWithDbConnections(); 112 } 113 114 DBSchemaUISupport.connect(dbschemaComboBox, dbschemaFileList); 115 boolean hasDBSchemas = (dbschemaComboBox.getItemCount() > 0 && dbschemaComboBox.getItemAt(0) instanceof FileObject); 116 if (!hasDBSchemas) { 117 dbschemaRadioButton.setEnabled(hasDBSchemas); 118 dbschemaComboBox.setEnabled(hasDBSchemas); 119 } 120 121 selectDefaultTableSource(tableSource, withDatasources, project, targetFolder); 122 } else { 123 datasourceRadioButton.setEnabled(false); 124 datasourceComboBox.setEnabled(false); 125 dbschemaRadioButton.setEnabled(false); 126 dbschemaComboBox.setEnabled(false); 127 } 128 129 sourceSchemaUpdateEnabled = true; 134 SwingUtilities.invokeLater(new Runnable () { 135 public void run() { 136 updateSourceSchema(); 137 } 138 }); 139 } 140 141 private void initializeWithDatasources() { 142 org.openide.awt.Mnemonics.setLocalizedText(datasourceRadioButton, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_Datasource")); 143 JPADataSourcePopulator dsPopulator = project.getLookup().lookup(JPADataSourcePopulator.class); 144 dsPopulator.connect(datasourceComboBox); 145 } 146 147 private void initializeWithDbConnections() { 148 org.openide.awt.Mnemonics.setLocalizedText(datasourceRadioButton, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_JDBCConnection")); 149 DatabaseExplorerUIs.connect(datasourceComboBox, ConnectionManager.getDefault()); 150 } 151 152 155 private void selectDefaultTableSource(TableSource tableSource, boolean withDatasources, Project project, FileObject targetFolder) { 156 if (tableSource == null) { 157 int dbschemaCount = dbschemaComboBox.getItemCount(); 161 if (targetFolder != null) { 162 SourceGroup[] sourceGroups = SourceGroupSupport.getJavaSourceGroups(project); 163 SourceGroup targetSourceGroup = SourceGroupSupport.getFolderSourceGroup(sourceGroups, targetFolder); 164 if (targetSourceGroup != null) { 165 for (int i=0; i<dbschemaCount; i++){ 166 Object nextSchema = dbschemaComboBox.getItemAt(i); 167 if (nextSchema instanceof FileObject) { 168 FileObject parent = ((FileObject)nextSchema).getParent(); 169 if (parent.equals(targetFolder)){ 170 dbschemaComboBox.setSelectedIndex(i); 171 dbschemaRadioButton.setSelected(true); 172 return; 173 } 174 } 175 } 176 } 177 } 178 if (dbschemaCount > 0 && dbschemaComboBox.getItemAt(0) instanceof FileObject) { 179 dbschemaComboBox.setSelectedIndex(0); 180 dbschemaRadioButton.setSelected(true); 181 return; 182 } 183 } else { 184 String tableSourceName = tableSource.getName(); 186 switch (tableSource.getType()) { 187 case DATA_SOURCE: 188 if (withDatasources) { 192 if (selectDatasource(tableSourceName)) { 193 return; 194 } 195 } 196 break; 197 198 case CONNECTION: 199 if (!withDatasources) { 202 if (selectDbConnection(tableSourceName)) { 203 return; 204 } 205 } 206 break; 207 208 case SCHEMA_FILE: 209 if (selectDBSchemaFile(tableSourceName)) { 211 return; 212 } 213 break; 214 } 215 } 216 217 datasourceRadioButton.setSelected(true); 221 } 222 223 233 private static List <DatabaseConnection> findDatabaseConnections(JPADataSource datasource) { 234 if (datasource == null) { 236 throw new NullPointerException ("The datasource parameter cannot be null."); } 238 String databaseUrl = datasource.getUrl(); 239 String user = datasource.getUsername(); 240 if (databaseUrl == null || user == null) { 241 return Collections.emptyList(); 242 } 243 List <DatabaseConnection> result = new ArrayList <DatabaseConnection>(); 244 for (DatabaseConnection dbconn : ConnectionManager.getDefault().getConnections()) { 245 if (databaseUrl.equals(dbconn.getDatabaseURL()) && user.equals(dbconn.getUser())) { 246 result.add(dbconn); 247 } 248 } 249 if (result.size() > 0) { 250 return Collections.unmodifiableList(result); 251 } else { 252 return Collections.emptyList(); 253 } 254 } 255 256 259 private boolean selectDatasource(String jndiName) { 260 JPADataSourceProvider dsProvider = project.getLookup().lookup(JPADataSourceProvider.class); 261 if (dsProvider == null){ 262 return false; 263 } 264 JPADataSource datasource = null; 265 for (JPADataSource each : dsProvider.getDataSources()){ 266 if (jndiName.equals(each.getJndiName())){ 267 datasource = each; 268 } 269 } 270 271 List <DatabaseConnection> dbconns = findDatabaseConnections(datasource); 272 if (dbconns.size() == 0) { 273 return false; 274 } 275 DatabaseConnection dbconn = dbconns.get(0); 276 if (dbconn.getJDBCConnection() == null) { 277 return false; 278 } 279 datasourceComboBox.setSelectedItem(datasource); 280 if (!datasource.equals(datasourceComboBox.getSelectedItem())) { 281 return false; 282 } 283 datasourceRadioButton.setSelected(true); 284 return true; 285 } 286 287 290 private boolean selectDbConnection(String name) { 291 DatabaseConnection dbconn = ConnectionManager.getDefault().getConnection(name); 292 if (dbconn == null || dbconn.getJDBCConnection() == null) { 293 return false; 294 } 295 datasourceComboBox.setSelectedItem(dbconn); 296 if (!dbconn.equals(datasourceComboBox.getSelectedItem())) { 297 return false; 298 } 299 datasourceRadioButton.setSelected(true); 300 return true; 301 } 302 303 306 private boolean selectDBSchemaFile(String name) { 307 FileObject dbschemaFile = FileUtil.toFileObject(new File (name)); 308 if (dbschemaFile == null) { 309 return false; 310 } 311 dbschemaComboBox.setSelectedItem(dbschemaFile); 312 if (!dbschemaFile.equals(dbschemaComboBox.getSelectedItem())) { 313 return false; 314 } 315 dbschemaRadioButton.setSelected(true); 316 return true; 317 } 318 319 public SchemaElement getSourceSchemaElement() { 320 return sourceSchemaElement; 321 } 322 323 public DatabaseConnection getDatabaseConnection() { 324 return dbconn; 325 } 326 327 public FileObject getDBSchemaFile() { 328 return dbschemaFile; 329 } 330 331 public String getDatasourceName() { 332 return datasourceName; 333 } 334 335 public TableClosure getTableClosure() { 336 return tableClosure; 337 } 338 339 private void updateSourceSchema() { 340 if (!sourceSchemaUpdateEnabled) { 341 return; 342 } 343 344 sourceSchemaElement = null; 345 datasourceName = null; 346 dbconn = null; 347 dbschemaFile = null; 348 349 if (datasourceRadioButton.isSelected()) { 350 Object item = datasourceComboBox.getSelectedItem(); 351 if (item instanceof JPADataSource) { 352 JPADataSource ds = (JPADataSource)item; 353 List <DatabaseConnection> dbconns = findDatabaseConnections(ds); 354 if (dbconns.size() > 0) { 355 dbconn = dbconns.get(0); 356 } else { 357 String drvClass = ds.getDriverClassName(); 358 if (drvClass == null) { 359 notify("ERR_NoDriverClassName"); 360 } else { 361 JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers(drvClass); 362 if (drivers.length == 0) { 363 notify("ERR_NoDriverError"); 364 } else { 365 JDBCDriver driver = JDBCDriverManager.getDefault().getDrivers(drvClass)[0]; 366 dbconn = ConnectionManager.getDefault().showAddConnectionDialogFromEventThread( 367 driver, ds.getUrl(), ds.getUsername(), ds.getPassword()); 368 } 369 } 370 } 371 if (dbconn != null) { 372 try { 373 sourceSchemaElement = dbschemaManager.getSchemaElement(dbconn); 374 datasourceName = ds.getJndiName(); 375 } catch (SQLException e) { 376 notify("ERR_DatabaseError"); 377 } 378 } 379 } else if (item instanceof DatabaseConnection) { 380 dbconn = (DatabaseConnection)item; 381 try { 382 sourceSchemaElement = dbschemaManager.getSchemaElement(dbconn); 383 } catch (SQLException e) { 384 notify("ERR_DatabaseError"); 385 } 386 } 387 } else if (dbschemaRadioButton.isSelected()) { 388 Object item = dbschemaComboBox.getSelectedItem(); 389 if (item instanceof FileObject) { 390 dbschemaFile = (FileObject)item; 391 sourceSchemaElement = dbschemaManager.getSchemaElement(dbschemaFile); 392 } 393 } 394 395 TableProvider tableProvider = null; 396 397 if (sourceSchemaElement != null) { 398 tableProvider = new DBSchemaTableProvider(sourceSchemaElement, persistenceGen); 399 } else { 400 tableProvider = new EmptyTableProvider(); 401 } 402 403 tableClosure = new TableClosure(tableProvider); 404 tableClosure.setClosureEnabled(tableClosureCheckBox.isSelected()); 405 406 TableUISupport.connectAvailable(availableTablesList, tableClosure); 407 TableUISupport.connectSelected(selectedTablesList, tableClosure); 408 409 updateButtons(); 410 411 changeSupport.fireChange(); 412 } 413 414 private static void notify(String msgName) { 415 NotifyDescriptor nd = new NotifyDescriptor.Message( 416 NbBundle.getMessage(DatabaseTablesPanel.class, msgName), 417 NotifyDescriptor.ERROR_MESSAGE); 418 DialogDisplayer.getDefault().notify(nd); 419 } 420 421 private void updateSourceSchemaComboBoxes() { 422 datasourceComboBox.setEnabled(datasourceRadioButton.isSelected()); 423 dbschemaComboBox.setEnabled(dbschemaRadioButton.isSelected()); 424 } 425 426 private void updateButtons() { 427 Set <Table> addTables = TableUISupport.getSelectedTables(availableTablesList); 428 addButton.setEnabled(tableClosure.canAddAllTables(addTables)); 429 430 addAllButton.setEnabled(tableClosure.canAddSomeTables(tableClosure.getAvailableTables())); 431 432 Set <Table> tables = TableUISupport.getSelectedTables(selectedTablesList); 433 removeButton.setEnabled(tableClosure.canRemoveAllTables(tables)); 434 435 removeAllButton.setEnabled(tableClosure.getSelectedTables().size() > 0); 436 tableError.setText(""); 437 for (Table t : addTables) { 438 if (t.isDisabled()) { 439 if (t.getDisabledReason() instanceof Table.ExistingDisabledReason) { 440 String existingClass = ((Table.ExistingDisabledReason) t.getDisabledReason()).getFQClassName(); 441 tableError.setText(NbBundle.getMessage(DatabaseTablesPanel.class, "MSG_Already_Mapped", new Object [] {t.getName(), existingClass})); 442 break; 443 } else if (t.getDisabledReason() instanceof Table.NoPrimaryKeyDisabledReason) { 444 tableError.setText(NbBundle.getMessage(DatabaseTablesPanel.class, "MSG_No_Primary_Key", new Object [] {t.getName()})); 445 break; 446 } 447 } 448 } 449 } 450 451 456 private void initComponents() { 458 java.awt.GridBagConstraints gridBagConstraints; 459 460 schemaSource = new javax.swing.ButtonGroup (); 461 datasourceRadioButton = new javax.swing.JRadioButton (); 462 datasourceComboBox = new javax.swing.JComboBox (); 463 dbschemaRadioButton = new javax.swing.JRadioButton (); 464 dbschemaComboBox = new javax.swing.JComboBox (); 465 tablesPanel = new TablesPanel(); 466 availableTablesLabel = new javax.swing.JLabel (); 467 availableTablesScrollPane = new javax.swing.JScrollPane (); 468 availableTablesList = new javax.swing.JList (); 469 selectedTablesLabel = new javax.swing.JLabel (); 470 selectedTablesScrollPane = new javax.swing.JScrollPane (); 471 selectedTablesList = new javax.swing.JList (); 472 buttonPanel = new javax.swing.JPanel (); 473 addButton = new javax.swing.JButton (); 474 removeButton = new javax.swing.JButton (); 475 addAllButton = new javax.swing.JButton (); 476 removeAllButton = new javax.swing.JButton (); 477 tableClosureCheckBox = new javax.swing.JCheckBox (); 478 jScrollPane3 = new javax.swing.JScrollPane (); 479 tableError = new javax.swing.JTextPane (); 480 481 setName(org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_DatabaseTables")); 482 schemaSource.add(datasourceRadioButton); 483 org.openide.awt.Mnemonics.setLocalizedText(datasourceRadioButton, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_Datasource")); 484 datasourceRadioButton.addItemListener(new java.awt.event.ItemListener () { 485 public void itemStateChanged(java.awt.event.ItemEvent evt) { 486 datasourceRadioButtonItemStateChanged(evt); 487 } 488 }); 489 490 datasourceComboBox.setEnabled(false); 491 datasourceComboBox.addActionListener(new java.awt.event.ActionListener () { 492 public void actionPerformed(java.awt.event.ActionEvent evt) { 493 datasourceComboBoxActionPerformed(evt); 494 } 495 }); 496 497 schemaSource.add(dbschemaRadioButton); 498 org.openide.awt.Mnemonics.setLocalizedText(dbschemaRadioButton, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_DbSchema")); 499 dbschemaRadioButton.addItemListener(new java.awt.event.ItemListener () { 500 public void itemStateChanged(java.awt.event.ItemEvent evt) { 501 dbschemaRadioButtonItemStateChanged(evt); 502 } 503 }); 504 505 dbschemaComboBox.setEnabled(false); 506 dbschemaComboBox.setNextFocusableComponent(availableTablesList); 507 dbschemaComboBox.addActionListener(new java.awt.event.ActionListener () { 508 public void actionPerformed(java.awt.event.ActionEvent evt) { 509 dbschemaComboBoxActionPerformed(evt); 510 } 511 }); 512 513 tablesPanel.setLayout(new java.awt.GridBagLayout ()); 514 515 availableTablesLabel.setLabelFor(availableTablesList); 516 org.openide.awt.Mnemonics.setLocalizedText(availableTablesLabel, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_AvailableTables")); 517 gridBagConstraints = new java.awt.GridBagConstraints (); 518 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 519 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 520 tablesPanel.add(availableTablesLabel, gridBagConstraints); 521 522 availableTablesList.setNextFocusableComponent(addButton); 523 availableTablesScrollPane.setViewportView(availableTablesList); 524 availableTablesList.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "ACSN_AvailableTables")); 525 availableTablesList.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "ACSD_AvailableTables")); 526 527 gridBagConstraints = new java.awt.GridBagConstraints (); 528 gridBagConstraints.gridx = 0; 529 gridBagConstraints.gridy = 1; 530 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 531 gridBagConstraints.weightx = 1.0; 532 gridBagConstraints.weighty = 1.0; 533 tablesPanel.add(availableTablesScrollPane, gridBagConstraints); 534 535 selectedTablesLabel.setLabelFor(selectedTablesList); 536 org.openide.awt.Mnemonics.setLocalizedText(selectedTablesLabel, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_SelectedTables")); 537 gridBagConstraints = new java.awt.GridBagConstraints (); 538 gridBagConstraints.gridx = 2; 539 gridBagConstraints.gridy = 0; 540 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 541 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 542 tablesPanel.add(selectedTablesLabel, gridBagConstraints); 543 544 selectedTablesScrollPane.setViewportView(selectedTablesList); 545 selectedTablesList.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "ACSN_SelectedTables")); 546 selectedTablesList.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "ACSD_SelectedTables")); 547 548 gridBagConstraints = new java.awt.GridBagConstraints (); 549 gridBagConstraints.gridx = 2; 550 gridBagConstraints.gridy = 1; 551 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 552 gridBagConstraints.weightx = 1.0; 553 gridBagConstraints.weighty = 1.0; 554 tablesPanel.add(selectedTablesScrollPane, gridBagConstraints); 555 556 buttonPanel.setLayout(new java.awt.GridBagLayout ()); 557 558 org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_Add")); 559 addButton.addActionListener(new java.awt.event.ActionListener () { 560 public void actionPerformed(java.awt.event.ActionEvent evt) { 561 addButtonActionPerformed(evt); 562 } 563 }); 564 565 gridBagConstraints = new java.awt.GridBagConstraints (); 566 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 567 buttonPanel.add(addButton, gridBagConstraints); 568 569 org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_Remove")); 570 removeButton.addActionListener(new java.awt.event.ActionListener () { 571 public void actionPerformed(java.awt.event.ActionEvent evt) { 572 removeButtonActionPerformed(evt); 573 } 574 }); 575 576 gridBagConstraints = new java.awt.GridBagConstraints (); 577 gridBagConstraints.gridx = 0; 578 gridBagConstraints.gridy = 1; 579 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 580 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 581 buttonPanel.add(removeButton, gridBagConstraints); 582 583 org.openide.awt.Mnemonics.setLocalizedText(addAllButton, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_AddAll")); 584 addAllButton.addActionListener(new java.awt.event.ActionListener () { 585 public void actionPerformed(java.awt.event.ActionEvent evt) { 586 addAllButtonActionPerformed(evt); 587 } 588 }); 589 590 gridBagConstraints = new java.awt.GridBagConstraints (); 591 gridBagConstraints.gridx = 0; 592 gridBagConstraints.gridy = 2; 593 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 594 gridBagConstraints.insets = new java.awt.Insets (17, 0, 0, 0); 595 buttonPanel.add(addAllButton, gridBagConstraints); 596 597 org.openide.awt.Mnemonics.setLocalizedText(removeAllButton, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_RemoveAll")); 598 removeAllButton.addActionListener(new java.awt.event.ActionListener () { 599 public void actionPerformed(java.awt.event.ActionEvent evt) { 600 removeAllButtonActionPerformed(evt); 601 } 602 }); 603 604 gridBagConstraints = new java.awt.GridBagConstraints (); 605 gridBagConstraints.gridx = 0; 606 gridBagConstraints.gridy = 3; 607 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 608 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 609 buttonPanel.add(removeAllButton, gridBagConstraints); 610 611 gridBagConstraints = new java.awt.GridBagConstraints (); 612 gridBagConstraints.gridx = 1; 613 gridBagConstraints.gridy = 0; 614 gridBagConstraints.gridheight = 2; 615 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 616 gridBagConstraints.insets = new java.awt.Insets (0, 11, 0, 11); 617 tablesPanel.add(buttonPanel, gridBagConstraints); 618 619 tableClosureCheckBox.setSelected(true); 620 org.openide.awt.Mnemonics.setLocalizedText(tableClosureCheckBox, org.openide.util.NbBundle.getMessage(DatabaseTablesPanel.class, "LBL_IncludeRelatedTables")); 621 tableClosureCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 622 tableClosureCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 623 tableClosureCheckBox.addItemListener(new java.awt.event.ItemListener () { 624 public void itemStateChanged(java.awt.event.ItemEvent evt) { 625 tableClosureCheckBoxItemStateChanged(evt); 626 } 627 }); 628 629 gridBagConstraints = new java.awt.GridBagConstraints (); 630 gridBagConstraints.gridx = 2; 631 gridBagConstraints.gridy = 2; 632 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 633 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 634 tablesPanel.add(tableClosureCheckBox, gridBagConstraints); 635 636 jScrollPane3.setBorder(null); 637 tableError.setEditable(false); 638 tableError.setOpaque(false); 639 jScrollPane3.setViewportView(tableError); 640 641 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 642 this.setLayout(layout); 643 layout.setHorizontalGroup( 644 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 645 .add(layout.createSequentialGroup() 646 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 647 .add(datasourceRadioButton) 648 .add(dbschemaRadioButton)) 649 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 650 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 651 .add(dbschemaComboBox, 0, 364, Short.MAX_VALUE) 652 .add(datasourceComboBox, 0, 364, Short.MAX_VALUE))) 653 .add(org.jdesktop.layout.GroupLayout.TRAILING, tablesPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE) 654 .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE) 655 ); 656 layout.setVerticalGroup( 657 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 658 .add(layout.createSequentialGroup() 659 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 660 .add(datasourceRadioButton) 661 .add(datasourceComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 662 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 663 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 664 .add(dbschemaRadioButton) 665 .add(dbschemaComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 666 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 667 .add(tablesPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 235, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 668 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 669 .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 670 ); 671 } 673 private void tableClosureCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) { tableClosure.setClosureEnabled(tableClosureCheckBox.isSelected()); 675 } 677 private void removeAllButtonActionPerformed(java.awt.event.ActionEvent evt) { tableClosure.removeAllTables(); 679 selectedTablesList.clearSelection(); 680 updateButtons(); 681 682 changeSupport.fireChange(); 683 } 685 private void addAllButtonActionPerformed(java.awt.event.ActionEvent evt) { tableClosure.addAllTables(); 687 availableTablesList.clearSelection(); 688 updateButtons(); 689 690 changeSupport.fireChange(); 691 } 693 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { Set <Table> tables = TableUISupport.getSelectedTables(selectedTablesList); 695 tableClosure.removeTables(tables); 696 selectedTablesList.clearSelection(); 697 updateButtons(); 698 699 changeSupport.fireChange(); 700 } 702 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { Set <Table> tables = TableUISupport.getSelectedTables(availableTablesList); 704 tableClosure.addTables(tables); 705 availableTablesList.clearSelection(); 706 updateButtons(); 707 708 changeSupport.fireChange(); 709 } 711 private void dbschemaComboBoxActionPerformed(java.awt.event.ActionEvent evt) { updateSourceSchema(); 713 } 715 private void dbschemaRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) { updateSourceSchemaComboBoxes(); 717 updateSourceSchema(); 718 } 720 private void datasourceComboBoxActionPerformed(java.awt.event.ActionEvent evt) { updateSourceSchema(); 722 } 724 private void datasourceRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) { updateSourceSchemaComboBoxes(); 726 updateSourceSchema(); 727 } 729 730 private javax.swing.JButton addAllButton; 732 private javax.swing.JButton addButton; 733 private javax.swing.JLabel availableTablesLabel; 734 private javax.swing.JList availableTablesList; 735 private javax.swing.JScrollPane availableTablesScrollPane; 736 private javax.swing.JPanel buttonPanel; 737 private javax.swing.JComboBox datasourceComboBox; 738 private javax.swing.JRadioButton datasourceRadioButton; 739 private javax.swing.JComboBox dbschemaComboBox; 740 private javax.swing.JRadioButton dbschemaRadioButton; 741 private javax.swing.JScrollPane jScrollPane3; 742 private javax.swing.JButton removeAllButton; 743 private javax.swing.JButton removeButton; 744 private javax.swing.ButtonGroup schemaSource; 745 private javax.swing.JLabel selectedTablesLabel; 746 private javax.swing.JList selectedTablesList; 747 private javax.swing.JScrollPane selectedTablesScrollPane; 748 private javax.swing.JCheckBox tableClosureCheckBox; 749 private javax.swing.JTextPane tableError; 750 private javax.swing.JPanel tablesPanel; 751 753 private final class TablesPanel extends JPanel { 754 755 public void doLayout() { 756 super.doLayout(); 757 758 Rectangle availableBounds = availableTablesScrollPane.getBounds(); 759 Rectangle selectedBounds = selectedTablesScrollPane.getBounds(); 760 761 if (Math.abs(availableBounds.width - selectedBounds.width) > 1) { 762 GridBagConstraints buttonPanelConstraints = ((GridBagLayout )getLayout()).getConstraints(buttonPanel); 763 int totalWidth = getWidth() - buttonPanel.getWidth() - buttonPanelConstraints.insets.left - buttonPanelConstraints.insets.right; 764 int equalWidth = totalWidth / 2; 765 int xOffset = equalWidth - availableBounds.width; 766 767 availableBounds.width = equalWidth; 768 availableTablesScrollPane.setBounds(availableBounds); 769 770 Rectangle buttonBounds = buttonPanel.getBounds(); 771 buttonBounds.x += xOffset; 772 buttonPanel.setBounds(buttonBounds); 773 774 Rectangle labelBounds = selectedTablesLabel.getBounds(); 775 labelBounds.x += xOffset; 776 selectedTablesLabel.setBounds(labelBounds); 777 778 selectedBounds.x += xOffset; 779 selectedBounds.width = totalWidth - equalWidth; 780 selectedTablesScrollPane.setBounds(selectedBounds); 781 782 Rectangle tableClosureBounds = tableClosureCheckBox.getBounds(); 783 tableClosureBounds.x += xOffset; 784 tableClosureBounds.width = totalWidth - equalWidth; 785 tableClosureCheckBox.setBounds(tableClosureBounds); 786 } 787 } 788 } 789 790 public static final class WizardPanel implements WizardDescriptor.Panel<WizardDescriptor>, ChangeListener { 791 792 private DatabaseTablesPanel component; 793 private boolean componentInitialized; 794 795 private WizardDescriptor wizardDescriptor; 796 private Project project; 797 private boolean cmp; 798 799 private ChangeSupport changeSupport = new ChangeSupport(this); 800 801 boolean waitingForScan; 802 803 public Component getComponent() { 804 return getTypedComponent(); 805 } 806 807 private DatabaseTablesPanel getTypedComponent() { 808 if (component == null) { 809 component = new DatabaseTablesPanel(); 810 component.addChangeListener(this); 811 } 812 return component; 813 } 814 815 public HelpCtx getHelp() { 816 if (cmp) { 817 return new HelpCtx("org.netbeans.modules.j2ee.ejbcore.ejb.wizard.cmp." + DatabaseTablesPanel.class.getSimpleName()); } else { 819 return new HelpCtx(DatabaseTablesPanel.class); 820 } 821 } 822 823 public void addChangeListener(ChangeListener listener) { 824 changeSupport.addChangeListener(listener); 825 } 826 827 public void removeChangeListener(ChangeListener listener) { 828 changeSupport.removeChangeListener(listener); 829 } 830 831 public void readSettings(WizardDescriptor settings) { 832 wizardDescriptor = settings; 833 if (!componentInitialized) { 834 componentInitialized = true; 835 836 project = Templates.getProject(wizardDescriptor); 837 cmp = RelatedCMPWizard.isCMP(wizardDescriptor); 838 RelatedCMPHelper helper = RelatedCMPWizard.getHelper(wizardDescriptor); 839 DBSchemaFileList dbschemaFileList = helper.getDBSchemaFileList(); 840 PersistenceGenerator persistenceGen = helper.getPersistenceGenerator(); 841 TableSource tableSource = helper.getTableSource(); 842 FileObject targetFolder = Templates.getTargetFolder(wizardDescriptor); 843 844 getTypedComponent().initialize(project, dbschemaFileList, persistenceGen, tableSource, targetFolder); 845 } 846 } 847 848 public boolean isValid() { 849 if (!ProviderUtil.isValidServerInstanceOrNone(project)) { 850 setErrorMessage(NbBundle.getMessage(DatabaseTablesPanel.class, "ERR_MissingServer")); 851 return false; 852 } 853 854 if (false){ 857 if (!waitingForScan) { 858 waitingForScan = true; 859 RequestProcessor.Task task = RequestProcessor.getDefault().create(new Runnable () { 860 public void run() { 861 waitingForScan = false; 864 changeSupport.fireChange(); 865 } 866 }); 867 setErrorMessage(NbBundle.getMessage(DatabaseTablesPanel.class, "scanning-in-progress")); 868 task.schedule(0); 869 } 870 return false; 871 } 872 873 if (!cmp && SourceLevelChecker.isSourceLevel14orLower(project)) { 874 setErrorMessage(NbBundle.getMessage(DatabaseTablesPanel.class, "ERR_NeedProperSourceLevel")); 875 return false; 876 } 877 878 if (getTypedComponent().getSourceSchemaElement() == null) { 879 setErrorMessage(NbBundle.getMessage(DatabaseTablesPanel.class, "ERR_SelectTableSource")); 880 return false; 881 } 882 883 if (getTypedComponent().getTableClosure().getSelectedTables().size() <= 0) { 884 setErrorMessage(NbBundle.getMessage(DatabaseTablesPanel.class, "ERR_SelectTables")); 885 return false; 886 } 887 888 setErrorMessage(" "); return true; 890 } 891 892 public void storeSettings(WizardDescriptor settings) { 893 WizardDescriptor wiz = settings; 894 Object buttonPressed = wiz.getValue(); 895 if (buttonPressed.equals(WizardDescriptor.NEXT_OPTION) || 896 buttonPressed.equals(WizardDescriptor.FINISH_OPTION)) { 897 RelatedCMPHelper helper = RelatedCMPWizard.getHelper(wizardDescriptor); 898 899 SchemaElement sourceSchemaElement = getTypedComponent().getSourceSchemaElement(); 900 DatabaseConnection dbconn = getTypedComponent().getDatabaseConnection(); 901 FileObject dbschemaFile = getTypedComponent().getDBSchemaFile(); 902 String datasourceName = getTypedComponent().getDatasourceName(); 903 904 if (dbschemaFile != null) { 905 helper.setTableSource(sourceSchemaElement, dbschemaFile); 906 } else { 907 helper.setTableSource(sourceSchemaElement, dbconn, datasourceName); 908 } 909 helper.setTableClosure(getTypedComponent().getTableClosure()); 910 } 911 } 912 913 public void stateChanged(ChangeEvent event) { 914 changeSupport.fireChange(event); 915 } 916 917 private void setErrorMessage(String errorMessage) { 918 wizardDescriptor.putProperty("WizardPanel_errorMessage", errorMessage); } 920 } 921 } 922 | Popular Tags |