1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.unit; 21 22 import java.awt.Color ; 23 import java.awt.event.ItemEvent ; 24 import java.awt.event.ItemListener ; 25 import javax.swing.event.ChangeListener ; 26 import javax.swing.event.DocumentEvent ; 27 import javax.swing.event.DocumentListener ; 28 import org.netbeans.api.db.explorer.ConnectionManager; 29 import org.netbeans.api.db.explorer.DatabaseConnection; 30 import org.netbeans.api.db.explorer.support.DatabaseExplorerUIs; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.netbeans.api.project.libraries.Library; 34 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence; 35 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 36 import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException; 37 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject; 38 import org.netbeans.modules.j2ee.persistence.provider.Provider; 39 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 40 import org.netbeans.modules.j2ee.persistence.util.PersistenceProviderComboboxHelper; 41 import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibrarySupport; 42 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanel.TableGeneration; 43 import org.openide.util.NbBundle; 44 45 49 public class PersistenceUnitWizardPanelJdbc extends PersistenceUnitWizardPanel{ 50 51 private Project project; 52 53 public PersistenceUnitWizardPanelJdbc(Project project, ChangeListener changeListener, boolean editName) { 54 this(project, changeListener, editName, TableGeneration.CREATE); 55 } 56 57 public PersistenceUnitWizardPanelJdbc(Project project, ChangeListener changeListener, 58 boolean editName, TableGeneration tg) { 59 initComponents(); 60 setTableGeneration(tg); 61 this.project = project; 62 63 PersistenceProviderComboboxHelper comboHelper = new PersistenceProviderComboboxHelper(project); 64 comboHelper.connect(libraryCombo); 65 66 unitNameTextField.setText(ProjectUtils.getInformation(project).getName() + "PU"); unitNameTextField.setVisible(editName); 69 unitNameLabel.setVisible(editName); 70 71 DatabaseExplorerUIs.connect(jdbcCombo, ConnectionManager.getDefault()); 72 jdbcCombo.addItemListener(new ItemListener () { 73 public void itemStateChanged(ItemEvent e) { 74 checkValidity(); 75 } 76 }); 77 78 unitNameTextField.getDocument().addDocumentListener(new ValidationListener()); 79 errorMessage.setForeground(Color.RED); 80 } 81 82 83 86 private void setTableGeneration(TableGeneration tg){ 87 if (TableGeneration.CREATE.equals(tg)){ 88 ddlCreate.setSelected(true); 89 } else if (TableGeneration.DROP_CREATE.equals(tg)){ 90 ddlDropCreate.setSelected(true); 91 } else { 92 ddlUnkown.setSelected(true); 93 } 94 } 95 96 100 private void checkValidity(){ 101 if (isValidPanel()) { 102 firePropertyChange(IS_VALID, false, true); 103 } else { 104 firePropertyChange(IS_VALID, true, false); 105 } 106 } 107 108 109 110 public String getPersistenceUnitName() { 111 return unitNameTextField.getText(); 112 } 113 114 public Library getPersistenceLibrary() { 115 return PersistenceLibrarySupport.getLibrary((Provider) libraryCombo.getSelectedItem()); 116 } 117 118 public Provider getSelectedProvider(){ 119 return (Provider) libraryCombo.getSelectedItem(); 120 } 121 122 public DatabaseConnection getPersistenceConnection() { 123 return (DatabaseConnection) jdbcCombo.getSelectedItem(); 124 } 125 126 public void setPreselectedDB(String db) { 127 boolean hasItem = false; 128 for (int i = 0; i < jdbcCombo.getItemCount(); i++) { 129 if (jdbcCombo.getItemAt(i) instanceof DatabaseConnection) { 130 DatabaseConnection conn = (DatabaseConnection) jdbcCombo.getItemAt(i); 131 if (conn.getDisplayName().equals(db) || conn.toString().equals(db)) { 132 hasItem = true; 133 break; 134 } 135 } 136 } 137 jdbcCombo.setSelectedItem(ConnectionManager.getDefault().getConnection(db)); 138 jdbcCombo.setEnabled(!hasItem); 139 } 140 141 public String getTableGeneration() { 142 if (ddlCreate.isSelected()) { 143 return Provider.TABLE_GENERATION_CREATE; 144 } else if (ddlDropCreate.isSelected()) { 145 return Provider.TABLE_GENERATION_DROPCREATE; 146 } else { 147 return Provider.TABLE_GENERATTION_UNKOWN; 148 } 149 } 150 151 public boolean isValidPanel() { 152 if (!(libraryCombo.getSelectedItem() instanceof Provider)) { 153 return false; 154 } 155 if (!(jdbcCombo.getSelectedItem() instanceof DatabaseConnection)) { 156 return false; 157 } 158 try{ 159 if (!isNameValid()){ 160 return false; 161 } 162 } catch (InvalidPersistenceXmlException ipx){ 163 setErrorMessage(NbBundle.getMessage(PersistenceUnitWizardDescriptor.class,"ERR_InvalidPersistenceXml", ipx.getPath())); return false; 165 } 166 return true; 167 } 168 169 173 private boolean isNameValid() throws InvalidPersistenceXmlException{ 174 return isEmptyOrNull(getPersistenceUnitName()) ? false : isNameUnique(); 175 } 176 177 180 public boolean isNameUnique() throws InvalidPersistenceXmlException{ 181 if (!ProviderUtil.persistenceExists(project)){ 182 return true; 183 } 184 Persistence persistence = null; 185 PUDataObject pud = ProviderUtil.getPUDataObject(project); 186 persistence = pud.getPersistence(); 187 188 PersistenceUnit[] punits = persistence.getPersistenceUnit(); 189 for (int i = 0; i < punits.length; i++) { 190 if (punits[i].getName().equals(getPersistenceUnitName())){ 191 return false; 192 } 193 } 194 return true; 195 } 196 197 200 private boolean isEmptyOrNull(String str){ 201 return str == null || "".equals(str.trim()); 202 } 203 204 public void setErrorMessage(String msg) { 205 errorMessage.setText(msg); 206 } 207 208 213 private void initComponents() { 215 buttonGroup1 = new javax.swing.ButtonGroup (); 216 tableCreationButtonGroup = new javax.swing.ButtonGroup (); 217 unitNameLabel = new javax.swing.JLabel (); 218 unitNameTextField = new javax.swing.JTextField (); 219 jLabel1 = new javax.swing.JLabel (); 220 jLabel2 = new javax.swing.JLabel (); 221 ddlCreate = new javax.swing.JRadioButton (); 222 ddlDropCreate = new javax.swing.JRadioButton (); 223 ddlUnkown = new javax.swing.JRadioButton (); 224 libraryLabel = new javax.swing.JLabel (); 225 libraryCombo = new javax.swing.JComboBox (); 226 jdbcCombo = new javax.swing.JComboBox (); 227 jdbcLabel = new javax.swing.JLabel (); 228 errorMessage = new javax.swing.JLabel (); 229 230 setName(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_Step1")); 231 unitNameLabel.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("MN_UnitName").charAt(0)); 232 unitNameLabel.setLabelFor(unitNameTextField); 233 unitNameLabel.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_UnitName")); 234 unitNameLabel.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("ACSD_UnitName")); 235 236 unitNameTextField.setColumns(40); 237 unitNameTextField.setText("em"); 238 239 jLabel1.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_SpecifyPersistenceProvider")); 240 241 jLabel2.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_TableGeneration")); 242 jLabel2.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("ACSD_TableGeneration")); 243 244 tableCreationButtonGroup.add(ddlCreate); 245 ddlCreate.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("CHB_Create_mnem").charAt(0)); 246 ddlCreate.setSelected(true); 247 ddlCreate.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_Create")); 248 ddlCreate.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 249 ddlCreate.setMargin(new java.awt.Insets (0, 0, 0, 0)); 250 ddlCreate.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("ACSD_Create")); 251 252 tableCreationButtonGroup.add(ddlDropCreate); 253 ddlDropCreate.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("CHB_DropCreate_mnem").charAt(0)); 254 ddlDropCreate.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_DropCreate")); 255 ddlDropCreate.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 256 ddlDropCreate.setMargin(new java.awt.Insets (0, 0, 0, 0)); 257 ddlDropCreate.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("ACSD_DropCreate")); 258 259 tableCreationButtonGroup.add(ddlUnkown); 260 ddlUnkown.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("CHB_None_mnem").charAt(0)); 261 ddlUnkown.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_None")); 262 ddlUnkown.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 263 ddlUnkown.setMargin(new java.awt.Insets (0, 0, 0, 0)); 264 ddlUnkown.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("ACSD_None")); 265 266 libraryLabel.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("MN_Library").charAt(0)); 267 libraryLabel.setLabelFor(libraryCombo); 268 libraryLabel.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_Library")); 269 libraryLabel.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("ACSD_Library")); 270 271 libraryCombo.addActionListener(new java.awt.event.ActionListener () { 272 public void actionPerformed(java.awt.event.ActionEvent evt) { 273 libraryComboActionPerformed(evt); 274 } 275 }); 276 277 jdbcCombo.setRenderer(new JdbcListCellRenderer()); 278 279 jdbcLabel.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("MN_JdbcConnection").charAt(0)); 280 jdbcLabel.setLabelFor(jdbcCombo); 281 jdbcLabel.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_JdbcConnection")); 282 jdbcLabel.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("ACSD_JdbcConnection")); 283 284 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 285 this.setLayout(layout); 286 layout.setHorizontalGroup( 287 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 288 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 289 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 290 .add(errorMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE) 291 .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() 292 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 293 .add(unitNameLabel) 294 .add(libraryLabel) 295 .add(jdbcLabel)) 296 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 297 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 298 .add(libraryCombo, 0, 352, Short.MAX_VALUE) 299 .add(jdbcCombo, 0, 352, Short.MAX_VALUE) 300 .add(unitNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))) 301 .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1) 302 .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() 303 .add(jLabel2) 304 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 305 .add(ddlCreate) 306 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 307 .add(ddlDropCreate) 308 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 309 .add(ddlUnkown))) 310 .addContainerGap()) 311 ); 312 layout.setVerticalGroup( 313 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 314 .add(layout.createSequentialGroup() 315 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 316 .add(unitNameLabel) 317 .add(unitNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 318 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 319 .add(jLabel1) 320 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 321 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 322 .add(libraryLabel) 323 .add(libraryCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 324 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 325 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 326 .add(jdbcLabel) 327 .add(jdbcCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 328 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 329 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 330 .add(ddlCreate) 331 .add(ddlDropCreate) 332 .add(ddlUnkown) 333 .add(jLabel2)) 334 .add(20, 20, 20) 335 .add(errorMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE) 336 .addContainerGap()) 337 ); 338 } 340 private void libraryComboActionPerformed(java.awt.event.ActionEvent evt) { checkValidity(); 342 } 344 345 private javax.swing.ButtonGroup buttonGroup1; 347 private javax.swing.JRadioButton ddlCreate; 348 private javax.swing.JRadioButton ddlDropCreate; 349 private javax.swing.JRadioButton ddlUnkown; 350 private javax.swing.JLabel errorMessage; 351 private javax.swing.JLabel jLabel1; 352 private javax.swing.JLabel jLabel2; 353 private javax.swing.JComboBox jdbcCombo; 354 private javax.swing.JLabel jdbcLabel; 355 private javax.swing.JComboBox libraryCombo; 356 private javax.swing.JLabel libraryLabel; 357 private javax.swing.ButtonGroup tableCreationButtonGroup; 358 private javax.swing.JLabel unitNameLabel; 359 private javax.swing.JTextField unitNameTextField; 360 362 366 private class ValidationListener implements DocumentListener { 367 public void insertUpdate(DocumentEvent e) { 368 checkValidity(); 369 } 370 public void removeUpdate(DocumentEvent e) { 371 checkValidity(); 372 } 373 public void changedUpdate(DocumentEvent e) { 374 checkValidity(); 375 } 376 } 377 } 378 | Popular Tags |