1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.io.IOException ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import javax.swing.ComboBoxModel ; 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 30 import javax.swing.event.DocumentEvent ; 31 import javax.swing.event.DocumentListener ; 32 import javax.swing.text.Document ; 33 import javax.swing.text.JTextComponent ; 34 import org.netbeans.api.java.classpath.ClassPath; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.api.project.ProjectUtils; 37 import org.netbeans.api.project.SourceGroup; 38 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 39 import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException; 40 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 41 import org.netbeans.modules.j2ee.persistence.wizard.Util; 42 import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibrarySupport; 43 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanel.TableGeneration; 44 import org.netbeans.spi.java.project.support.ui.PackageView; 45 import org.netbeans.spi.project.ui.templates.support.Templates; 46 import org.openide.ErrorManager; 47 import org.openide.WizardDescriptor; 48 import org.openide.awt.Mnemonics; 49 import org.openide.filesystems.FileObject; 50 import org.openide.util.HelpCtx; 51 import org.openide.util.NbBundle; 52 53 57 public class EntityClassesPanel extends javax.swing.JPanel { 58 59 private final static Logger LOGGER = Logger.getLogger(EntityClassesPanel.class.getName()); 60 61 private JTextComponent packageComboBoxEditor; 62 63 private PersistenceGenerator persistenceGen; 64 private Project project; 65 private boolean cmp; 66 private String tableSourceName; 68 private SelectedTables selectedTables; 69 70 private ChangeSupport changeSupport = new ChangeSupport(this); 71 72 private PersistenceUnit persistenceUnit; 73 74 public EntityClassesPanel() { 75 initComponents(); 76 77 classNamesTable.getParent().setBackground(classNamesTable.getBackground()); 78 classNamesTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); 80 packageComboBoxEditor = ((JTextComponent )packageComboBox.getEditor().getEditorComponent()); 81 Document packageComboBoxDocument = packageComboBoxEditor.getDocument(); 82 packageComboBoxDocument.addDocumentListener(new DocumentListener () { 83 84 public void removeUpdate(DocumentEvent e) { 85 packageChanged(); 86 } 87 88 public void insertUpdate(DocumentEvent e) { 89 packageChanged(); 90 } 91 92 public void changedUpdate(DocumentEvent e) { 93 packageChanged(); 94 } 95 }); 96 } 97 98 public void addChangeListener(ChangeListener listener) { 99 changeSupport.addChangeListener(listener); 100 } 101 102 public void initialize(PersistenceGenerator persistenceGen, Project project, boolean cmp, FileObject targetFolder) { 103 this.persistenceGen = persistenceGen; 104 this.project = project; 105 this.cmp = cmp; 106 107 projectTextField.setText(ProjectUtils.getInformation(project).getDisplayName()); 108 109 SourceGroup[] sourceGroups = SourceGroupSupport.getJavaSourceGroups(project); 110 SourceGroupUISupport.connect(locationComboBox, sourceGroups); 111 112 packageComboBox.setRenderer(PackageView.listRenderer()); 113 114 updatePackageComboBox(); 115 116 if (targetFolder != null) { 117 SourceGroup targetSourceGroup = SourceGroupSupport.getFolderSourceGroup(sourceGroups, targetFolder); 119 if (targetSourceGroup != null) { 120 locationComboBox.setSelectedItem(targetSourceGroup); 121 String targetPackage = SourceGroupSupport.getPackageForFolder(targetSourceGroup, targetFolder); 122 if (targetPackage != null) { 123 packageComboBoxEditor.setText(targetPackage); 124 } 125 } 126 } 127 128 if (!cmp) { 129 Mnemonics.setLocalizedText(generateFinderMethodsCheckBox, 131 NbBundle.getMessage(EntityClassesPanel.class, "TXT_GenerateNamedQueryAnnotations")); 132 cmpFieldsInInterfaceCheckBox.setVisible(false); 134 } 135 136 if (cmp) { 137 classNamesLabel.setVisible(false); 138 classNamesScrollPane.setVisible(false); 139 spacerPanel.setVisible(false); 140 141 Mnemonics.setLocalizedText(specifyNamesLabel, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "LBL_SpecifyBeansLocation")); 142 } 143 144 updatePersistenceUnitButton(); 145 } 146 147 public void update(TableClosure tableClosure, String tableSourceName) { 148 try { 149 if (selectedTables == null) { 150 selectedTables = new SelectedTables(persistenceGen, tableClosure, getLocationValue(), getPackageName()); 151 selectedTables.addChangeListener(new ChangeListener () { 152 public void stateChanged(ChangeEvent event) { 153 changeSupport.fireChange(); 154 } 155 }); 156 } else { 157 selectedTables.setTableClosureAndTargetFolder(tableClosure, getLocationValue(), getPackageName()); 158 } 159 selectedTables.ensureUniqueClassNames(); 160 } catch (IOException e) { 161 ErrorManager.getDefault().notify(e); 162 } 163 164 TableUISupport.connectClassNames(classNamesTable, selectedTables); 165 this.tableSourceName = tableSourceName; 166 } 167 168 public SelectedTables getSelectedTables() { 169 return selectedTables; 170 } 171 172 public SourceGroup getLocationValue() { 173 return (SourceGroup)locationComboBox.getSelectedItem(); 174 } 175 176 public String getPackageName() { 177 return packageComboBoxEditor.getText(); 178 } 179 180 public boolean getCmpFieldsInInterface() { 181 return cmpFieldsInInterfaceCheckBox.isSelected(); 182 } 183 184 public boolean getGenerateFinderMethods() { 185 return generateFinderMethodsCheckBox.isSelected(); 186 } 187 188 public PersistenceUnit getPersistenceUnit() { 189 return persistenceUnit; 190 } 191 192 private void locationChanged() { 193 updatePackageComboBox(); 194 updateSelectedTables(); 195 changeSupport.fireChange(); 196 } 197 198 private void packageChanged() { 199 updateSelectedTables(); 200 changeSupport.fireChange(); 201 } 202 203 private void updatePackageComboBox() { 204 SourceGroup sourceGroup = (SourceGroup)locationComboBox.getSelectedItem(); 205 if (sourceGroup != null) { 206 ComboBoxModel model = PackageView.createListView(sourceGroup); 207 if (model.getSelectedItem()!= null && model.getSelectedItem().toString().startsWith("META-INF") 208 && model.getSize() > 1) { model.setSelectedItem(model.getElementAt(1)); 210 } 211 packageComboBox.setModel(model); 212 } 213 } 214 215 private void updatePersistenceUnitButton() { 216 String warning = " "; 217 try{ 218 219 boolean showWarning = !cmp 220 && !ProviderUtil.persistenceExists(project) 221 && getPersistenceUnit() == null; 222 223 createPUButton.setVisible(showWarning); 224 225 if (showWarning) { 226 warning = NbBundle.getMessage(EntityClassesPanel.class, "ERR_NoPersistenceUnit"); 227 } 228 229 } catch (InvalidPersistenceXmlException ipx){ 230 createPUButton.setVisible(false); 231 warning = NbBundle.getMessage(EntityClassesPanel.class, "ERR_InvalidPersistenceUnit", ipx.getPath()); 232 } 233 234 createPUWarningLabel.setText(warning); 235 createPUWarningLabel.setToolTipText(warning); 236 } 237 238 private void updateSelectedTables() { 239 if (selectedTables != null) { 240 try { 241 selectedTables.setTargetFolder(getLocationValue(), getPackageName()); 242 } catch (IOException e) { 243 ErrorManager.getDefault().notify(e); 244 } 245 } 246 } 247 248 public void doLayout() { 249 Dimension size = createPUWarningLabel.getPreferredSize(); 251 size.width = getWidth(); 252 createPUWarningLabel.setPreferredSize(size); 253 254 super.doLayout(); 255 } 256 257 262 private void initComponents() { 264 specifyNamesLabel = new javax.swing.JLabel (); 265 classNamesLabel = new javax.swing.JLabel (); 266 classNamesScrollPane = new javax.swing.JScrollPane (); 267 classNamesTable = new javax.swing.JTable (); 268 projectLabel = new javax.swing.JLabel (); 269 projectTextField = new javax.swing.JTextField (); 270 locationLabel = new javax.swing.JLabel (); 271 locationComboBox = new javax.swing.JComboBox (); 272 packageLabel = new javax.swing.JLabel (); 273 packageComboBox = new javax.swing.JComboBox (); 274 createPUButton = new javax.swing.JButton (); 275 generateFinderMethodsCheckBox = new javax.swing.JCheckBox (); 276 cmpFieldsInInterfaceCheckBox = new javax.swing.JCheckBox (); 277 spacerPanel = new javax.swing.JPanel (); 278 createPUWarningLabel = new javax.swing.JLabel (); 279 280 setName(org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "LBL_EntityClasses")); 281 org.openide.awt.Mnemonics.setLocalizedText(specifyNamesLabel, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "LBL_SpecifyEntityClassNames")); 282 283 org.openide.awt.Mnemonics.setLocalizedText(classNamesLabel, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "LBL_ClassNames")); 284 285 classNamesScrollPane.setViewportView(classNamesTable); 286 287 org.openide.awt.Mnemonics.setLocalizedText(projectLabel, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "LBL_Project")); 288 289 projectTextField.setEditable(false); 290 291 org.openide.awt.Mnemonics.setLocalizedText(locationLabel, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "LBL_SrcLocation")); 292 293 locationComboBox.addActionListener(new java.awt.event.ActionListener () { 294 public void actionPerformed(java.awt.event.ActionEvent evt) { 295 locationComboBoxActionPerformed(evt); 296 } 297 }); 298 299 org.openide.awt.Mnemonics.setLocalizedText(packageLabel, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "LBL_Package")); 300 301 packageComboBox.setEditable(true); 302 303 org.openide.awt.Mnemonics.setLocalizedText(createPUButton, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "LBL_CreatePersistenceUnit")); 304 createPUButton.addActionListener(new java.awt.event.ActionListener () { 305 public void actionPerformed(java.awt.event.ActionEvent evt) { 306 createPUButtonActionPerformed(evt); 307 } 308 }); 309 310 generateFinderMethodsCheckBox.setSelected(true); 311 org.openide.awt.Mnemonics.setLocalizedText(generateFinderMethodsCheckBox, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "TXT_GenerateFinderMethods")); 312 generateFinderMethodsCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)); 313 generateFinderMethodsCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 314 315 cmpFieldsInInterfaceCheckBox.setSelected(true); 316 org.openide.awt.Mnemonics.setLocalizedText(cmpFieldsInInterfaceCheckBox, org.openide.util.NbBundle.getMessage(EntityClassesPanel.class, "TXT_AddFieldsToInterface")); 317 cmpFieldsInInterfaceCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)); 318 cmpFieldsInInterfaceCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 319 320 org.jdesktop.layout.GroupLayout spacerPanelLayout = new org.jdesktop.layout.GroupLayout(spacerPanel); 321 spacerPanel.setLayout(spacerPanelLayout); 322 spacerPanelLayout.setHorizontalGroup( 323 spacerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 324 .add(0, 388, Short.MAX_VALUE) 325 ); 326 spacerPanelLayout.setVerticalGroup( 327 spacerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 328 .add(0, 9, Short.MAX_VALUE) 329 ); 330 331 org.openide.awt.Mnemonics.setLocalizedText(createPUWarningLabel, " "); 332 333 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 334 this.setLayout(layout); 335 layout.setHorizontalGroup( 336 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 337 .add(specifyNamesLabel) 338 .add(layout.createSequentialGroup() 339 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 340 .add(classNamesLabel) 341 .add(projectLabel) 342 .add(locationLabel) 343 .add(packageLabel)) 344 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 345 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 346 .add(spacerPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 347 .add(packageComboBox, 0, 388, Short.MAX_VALUE) 348 .add(locationComboBox, 0, 388, Short.MAX_VALUE) 349 .add(org.jdesktop.layout.GroupLayout.TRAILING, projectTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE) 350 .add(classNamesScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))) 351 .add(cmpFieldsInInterfaceCheckBox) 352 .add(generateFinderMethodsCheckBox) 353 .add(createPUButton) 354 .add(createPUWarningLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE) 355 ); 356 layout.setVerticalGroup( 357 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 358 .add(layout.createSequentialGroup() 359 .add(specifyNamesLabel) 360 .add(11, 11, 11) 361 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 362 .add(classNamesLabel) 363 .add(classNamesScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)) 364 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 365 .add(spacerPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 366 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 367 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 368 .add(projectTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 369 .add(projectLabel)) 370 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 371 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 372 .add(locationComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 373 .add(locationLabel)) 374 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 375 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 376 .add(packageComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 377 .add(packageLabel)) 378 .add(21, 21, 21) 379 .add(generateFinderMethodsCheckBox) 380 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 381 .add(cmpFieldsInInterfaceCheckBox) 382 .add(21, 21, 21) 383 .add(createPUWarningLabel) 384 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 385 .add(createPUButton)) 386 ); 387 } 389 private void locationComboBoxActionPerformed(java.awt.event.ActionEvent evt) { locationChanged(); 391 } 393 private void createPUButtonActionPerformed(java.awt.event.ActionEvent evt) { persistenceUnit = Util.buildPersistenceUnitUsingWizard(project, tableSourceName, TableGeneration.NONE); 395 if (persistenceUnit != null){ 396 updatePersistenceUnitButton(); 397 changeSupport.fireChange(); 398 } 399 } 401 402 private javax.swing.JLabel classNamesLabel; 404 private javax.swing.JScrollPane classNamesScrollPane; 405 private javax.swing.JTable classNamesTable; 406 private javax.swing.JCheckBox cmpFieldsInInterfaceCheckBox; 407 private javax.swing.JButton createPUButton; 408 private javax.swing.JLabel createPUWarningLabel; 409 private javax.swing.JCheckBox generateFinderMethodsCheckBox; 410 private javax.swing.JComboBox locationComboBox; 411 private javax.swing.JLabel locationLabel; 412 private javax.swing.JComboBox packageComboBox; 413 private javax.swing.JLabel packageLabel; 414 private javax.swing.JLabel projectLabel; 415 private javax.swing.JTextField projectTextField; 416 private javax.swing.JPanel spacerPanel; 417 private javax.swing.JLabel specifyNamesLabel; 418 420 public static final class WizardPanel implements WizardDescriptor.Panel, ChangeListener { 421 422 private EntityClassesPanel component; 423 private boolean componentInitialized; 424 425 private WizardDescriptor wizardDescriptor; 426 private Project project; 427 private boolean cmp; 428 429 private ChangeSupport changeSupport = new ChangeSupport(this); 430 431 public Component getComponent() { 432 return getTypedComponent(); 433 } 434 435 private EntityClassesPanel getTypedComponent() { 436 if (component == null) { 437 component = new EntityClassesPanel(); 438 component.addChangeListener(this); 439 } 440 return component; 441 } 442 443 public void removeChangeListener(ChangeListener listener) { 444 changeSupport.removeChangeListener(listener); 445 } 446 447 public void addChangeListener(ChangeListener listener) { 448 changeSupport.addChangeListener(listener); 449 } 450 451 public HelpCtx getHelp() { 452 if (cmp) { 453 return new HelpCtx("org.netbeans.modules.j2ee.ejbcore.ejb.wizard.cmp." + EntityClassesPanel.class.getSimpleName()); } else { 455 return new HelpCtx(EntityClassesPanel.class); 456 } 457 } 458 459 public void readSettings(Object settings) { 460 wizardDescriptor = (WizardDescriptor)settings; 461 RelatedCMPHelper helper = RelatedCMPWizard.getHelper(wizardDescriptor); 462 463 if (!componentInitialized) { 464 componentInitialized = true; 465 466 PersistenceGenerator persistenceGen = helper.getPersistenceGenerator(); 467 project = Templates.getProject(wizardDescriptor); 468 cmp = RelatedCMPWizard.isCMP(wizardDescriptor); 469 FileObject targetFolder = Templates.getTargetFolder(wizardDescriptor); 470 471 getTypedComponent().initialize(persistenceGen, project, cmp, targetFolder); 472 } 473 474 TableSource tableSource = helper.getTableSource(); 475 String tableSourceName = null; 476 if (tableSource != null) { 477 TableSource.Type tableSourceType = tableSource.getType(); 482 if (tableSourceType == TableSource.Type.DATA_SOURCE || tableSourceType == TableSource.Type.CONNECTION) { 483 tableSourceName = tableSource.getName(); 484 } 485 } 486 487 getTypedComponent().update(helper.getTableClosure(), tableSourceName); 488 } 489 490 public boolean isValid() { 491 SourceGroup sourceGroup = getTypedComponent().getLocationValue(); 492 if (sourceGroup == null) { 493 setErrorMessage(NbBundle.getMessage(EntityClassesPanel.class, "ERR_JavaTargetChooser_SelectSourceGroup")); 494 return false; 495 } 496 497 String packageName = getTypedComponent().getPackageName(); 498 if (packageName.trim().equals("")) { setErrorMessage(NbBundle.getMessage(EntityClassesPanel.class, "ERR_JavaTargetChooser_CantUseDefaultPackage")); 500 return false; 501 } 502 503 if (!SourceGroupSupport.isValidPackageName(packageName)) { 504 setErrorMessage(NbBundle.getMessage(EntityClassesPanel.class,"ERR_JavaTargetChooser_InvalidPackage")); return false; 506 } 507 508 if (!SourceGroupSupport.isFolderWritable(sourceGroup, packageName)) { 509 setErrorMessage(NbBundle.getMessage(EntityClassesPanel.class, "ERR_JavaTargetChooser_UnwritablePackage")); return false; 511 } 512 513 ClassPath classPath = null; 517 try { 518 FileObject packageFO = SourceGroupSupport.getFolderForPackage(sourceGroup, packageName, false); 519 if (packageFO == null) { 520 packageFO = sourceGroup.getRootFolder(); 521 } 522 classPath = ClassPath.getClassPath(packageFO, ClassPath.COMPILE); 523 } catch (IOException e) { 524 LOGGER.log(Level.WARNING, null, e); 525 } 526 if (classPath != null) { 527 if (classPath.findResource("javax/persistence/EntityManager.class") == null && PersistenceLibrarySupport.getProvidersFromLibraries().size() == 0) { 529 setErrorMessage(NbBundle.getMessage(EntityClassesPanel.class, "ERR_NoJavaPersistenceAPI")); return false; 531 } 532 } else { 533 LOGGER.warning("Cannot get a classpath for package " + packageName + " in " + sourceGroup); } 535 536 SelectedTables selectedTables = getTypedComponent().getSelectedTables(); 537 if (selectedTables != null) { 541 String problem = selectedTables.getFirstProblemDisplayName(); 542 if (problem != null) { 543 setErrorMessage(problem); 544 return false; 545 } 546 } 547 548 setErrorMessage(" "); return true; 550 } 551 552 public void storeSettings(Object settings) { 553 Object buttonPressed = ((WizardDescriptor)settings).getValue(); 554 if (buttonPressed.equals(WizardDescriptor.NEXT_OPTION) || 555 buttonPressed.equals(WizardDescriptor.FINISH_OPTION)) { 556 557 RelatedCMPHelper helper = RelatedCMPWizard.getHelper(wizardDescriptor); 558 559 helper.setSelectedTables(getTypedComponent().getSelectedTables()); 560 helper.setLocation(getTypedComponent().getLocationValue()); 561 helper.setPackageName(getTypedComponent().getPackageName()); 562 helper.setCmpFieldsInInterface(getTypedComponent().getCmpFieldsInInterface()); 563 helper.setGenerateFinderMethods(getTypedComponent().getGenerateFinderMethods()); 564 helper.setPersistenceUnit(getTypedComponent().getPersistenceUnit()); 565 } 566 } 567 568 public void stateChanged(ChangeEvent event) { 569 changeSupport.fireChange(event); 570 } 571 572 private void setErrorMessage(String errorMessage) { 573 wizardDescriptor.putProperty("WizardPanel_errorMessage", errorMessage); } 575 } 576 } 577 | Popular Tags |