2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.j2ee.persistence.wizard.unit;
44
import java.awt.Color;
45
import java.awt.event.ItemEvent;
46
import java.awt.event.ItemListener;
47
import javax.swing.event.ChangeListener;
48
import javax.swing.event.DocumentEvent;
49
import javax.swing.event.DocumentListener;
50
import org.netbeans.api.db.explorer.ConnectionManager;
51
import org.netbeans.api.db.explorer.DatabaseConnection;
52
import org.netbeans.api.db.explorer.support.DatabaseExplorerUIs;
53
import org.netbeans.api.project.Project;
54
import org.netbeans.api.project.libraries.Library;
55
import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException;
56
import org.netbeans.modules.j2ee.persistence.provider.Provider;
57
import org.netbeans.modules.j2ee.persistence.util.PersistenceProviderComboboxHelper;
58
import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibrarySupport;
59
import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanel.TableGeneration;
60
import org.openide.util.NbBundle;
64
* @author Martin Adamek
66
public class PersistenceUnitWizardPanelJdbc extends PersistenceUnitWizardPanel{
68
public PersistenceUnitWizardPanelJdbc(Project project, ChangeListener changeListener, boolean editName) {
69
this(project, changeListener, editName, TableGeneration.CREATE);
72
public PersistenceUnitWizardPanelJdbc(Project project, ChangeListener changeListener,
73
boolean editName, TableGeneration tg) {
77
setTableGeneration(tg);
79
PersistenceProviderComboboxHelper comboHelper = new PersistenceProviderComboboxHelper(project);
80
comboHelper.connect(libraryCombo);
82
unitNameTextField.setText(getCandidateName());
83
unitNameTextField.selectAll();
84
// unit name editing is not available when adding first PU
85
unitNameTextField.setVisible(editName);
86
unitNameLabel.setVisible(editName);
88
DatabaseExplorerUIs.connect(jdbcCombo, ConnectionManager.getDefault());
89
jdbcCombo.addItemListener(new ItemListener() {
90
public void itemStateChanged(ItemEvent e) {
95
unitNameTextField.getDocument().addDocumentListener(new ValidationListener());
96
errorMessage.setForeground(Color.RED);
101
* Pre-selects appropriate table generation strategy radio button.
103
private void setTableGeneration(TableGeneration tg){
104
if (TableGeneration.CREATE.equals(tg)){
105
ddlCreate.setSelected(true);
106
} else if (TableGeneration.DROP_CREATE.equals(tg)){
107
ddlDropCreate.setSelected(true);
109
ddlUnkown.setSelected(true);
114
* Checks whether this panel is in valid state (see <code>#isValidPanel()</code>)
115
* and fires appropriate property changes.
117
private void checkValidity(){
118
if (isValidPanel()) {
119
firePropertyChange(IS_VALID, false, true);
121
firePropertyChange(IS_VALID, true, false);
127
public String getPersistenceUnitName() {
128
return unitNameTextField.getText();
131
public Library getPersistenceLibrary() {
132
return PersistenceLibrarySupport.getLibrary((Provider) libraryCombo.getSelectedItem());
135
public Provider getSelectedProvider(){
136
return (Provider) libraryCombo.getSelectedItem();
139
public DatabaseConnection getPersistenceConnection() {
140
return (DatabaseConnection) jdbcCombo.getSelectedItem();
143
public void setPreselectedDB(String db) {
144
boolean hasItem = false;
145
for (int i = 0; i < jdbcCombo.getItemCount(); i++) {
146
if (jdbcCombo.getItemAt(i) instanceof DatabaseConnection) {
147
DatabaseConnection conn = (DatabaseConnection) jdbcCombo.getItemAt(i);
148
if (conn.getDisplayName().equals(db) || conn.toString().equals(db)) {
154
jdbcCombo.setSelectedItem(ConnectionManager.getDefault().getConnection(db));
155
jdbcCombo.setEnabled(!hasItem);
158
public String getTableGeneration() {
159
if (ddlCreate.isSelected()) {
160
return Provider.TABLE_GENERATION_CREATE;
161
} else if (ddlDropCreate.isSelected()) {
162
return Provider.TABLE_GENERATION_DROPCREATE;
164
return Provider.TABLE_GENERATTION_UNKOWN;
168
public boolean isValidPanel() {
169
if (!(libraryCombo.getSelectedItem() instanceof Provider)) {
172
if (!(jdbcCombo.getSelectedItem() instanceof DatabaseConnection)) {
179
} catch (InvalidPersistenceXmlException ipx){
180
setErrorMessage(NbBundle.getMessage(PersistenceUnitWizardDescriptor.class,"ERR_InvalidPersistenceXml", ipx.getPath())); //NO18N
187
* Checks whether name of the persistence unit is valid, i.e. it's not
188
* empty and it's unique.
190
private boolean isNameValid() throws InvalidPersistenceXmlException{
191
return isEmptyOrNull(getPersistenceUnitName()) ? false : isNameUnique();
195
* @return true if given str is null, empty or contains only spaces.
197
private boolean isEmptyOrNull(String str){
198
return str == null || "".equals(str.trim());
201
public void setErrorMessage(String msg) {
202
errorMessage.setText(msg);
205
/** This method is called from within the constructor to
206
* initialize the form.
207
* WARNING: Do NOT modify this code. The content of this method is
208
* always regenerated by the Form Editor.
210
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
211
private void initComponents() {
213
buttonGroup1 = new javax.swing.ButtonGroup();
214
tableCreationButtonGroup = new javax.swing.ButtonGroup();
215
unitNameLabel = new javax.swing.JLabel();
216
unitNameTextField = new javax.swing.JTextField();
217
jLabel1 = new javax.swing.JLabel();
218
jLabel2 = new javax.swing.JLabel();
219
ddlCreate = new javax.swing.JRadioButton();
220
ddlDropCreate = new javax.swing.JRadioButton();
221
ddlUnkown = new javax.swing.JRadioButton();
222
libraryLabel = new javax.swing.JLabel();
223
libraryCombo = new javax.swing.JComboBox();
224
jdbcCombo = new javax.swing.JComboBox();
225
jdbcLabel = new javax.swing.JLabel();
226
errorMessage = new javax.swing.JLabel();
228
setName(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_Step1")); // NOI18N
230
unitNameLabel.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("MN_UnitName").charAt(0));
231
unitNameLabel.setLabelFor(unitNameTextField);
232
unitNameLabel.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_UnitName")); // NOI18N
234
unitNameTextField.setColumns(40);
235
unitNameTextField.setText("em");
237
jLabel1.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_SpecifyPersistenceProvider")); // NOI18N
239
jLabel2.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_TableGeneration")); // NOI18N
241
tableCreationButtonGroup.add(ddlCreate);
242
ddlCreate.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("CHB_Create_mnem").charAt(0));
243
ddlCreate.setSelected(true);
244
ddlCreate.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_Create")); // NOI18N
245
ddlCreate.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
247
tableCreationButtonGroup.add(ddlDropCreate);
248
ddlDropCreate.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("CHB_DropCreate_mnem").charAt(0));
249
ddlDropCreate.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_DropCreate")); // NOI18N
250
ddlDropCreate.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
252
tableCreationButtonGroup.add(ddlUnkown);
253
ddlUnkown.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("CHB_None_mnem").charAt(0));
254
ddlUnkown.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_None")); // NOI18N
255
ddlUnkown.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
257
libraryLabel.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("MN_Library").charAt(0));
258
libraryLabel.setLabelFor(libraryCombo);
259
libraryLabel.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_Library")); // NOI18N
261
libraryCombo.addActionListener(new java.awt.event.ActionListener() {
262
public void actionPerformed(java.awt.event.ActionEvent evt) {
263
libraryComboActionPerformed(evt);
267
jdbcCombo.setRenderer(new JdbcListCellRenderer());
269
jdbcLabel.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle").getString("MN_JdbcConnection").charAt(0));
270
jdbcLabel.setLabelFor(jdbcCombo);
271
jdbcLabel.setText(org.openide.util.NbBundle.getMessage(PersistenceUnitWizardPanelJdbc.class, "LBL_JdbcConnection")); // NOI18N
273
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
274
this.setLayout(layout);
275
layout.setHorizontalGroup(
276
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
277
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
278
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
279
.add(errorMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
280
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
281
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
285
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
286
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
287
.add(libraryCombo, 0, 372, Short.MAX_VALUE)
288
.add(jdbcCombo, 0, 372, Short.MAX_VALUE)
289
.add(unitNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)))
290
.add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1)
291
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
293
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
295
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
297
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
301
layout.setVerticalGroup(
302
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
303
.add(layout.createSequentialGroup()
304
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
306
.add(unitNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
307
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
309
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
310
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
312
.add(libraryCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
313
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
314
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
316
.add(jdbcCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
317
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
318
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
324
.add(errorMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
328
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/unit/Bundle"); // NOI18N
329
unitNameLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_UnitName")); // NOI18N
330
jLabel2.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TableGeneration")); // NOI18N
331
ddlCreate.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Create")); // NOI18N
332
ddlDropCreate.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_DropCreate")); // NOI18N
333
ddlUnkown.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_None")); // NOI18N
334
libraryLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Library")); // NOI18N
335
jdbcLabel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_JdbcConnection")); // NOI18N
336
}// </editor-fold>//GEN-END:initComponents
338
private void libraryComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_libraryComboActionPerformed
340
}//GEN-LAST:event_libraryComboActionPerformed
343
// Variables declaration - do not modify//GEN-BEGIN:variables
344
private javax.swing.ButtonGroup buttonGroup1;
345
private javax.swing.JRadioButton ddlCreate;
346
private javax.swing.JRadioButton ddlDropCreate;
347
private javax.swing.JRadioButton ddlUnkown;
348
private javax.swing.JLabel errorMessage;
349
private javax.swing.JLabel jLabel1;
350
private javax.swing.JLabel jLabel2;
351
private javax.swing.JComboBox jdbcCombo;
352
private javax.swing.JLabel jdbcLabel;
353
private javax.swing.JComboBox libraryCombo;
354
private javax.swing.JLabel libraryLabel;
355
private javax.swing.ButtonGroup tableCreationButtonGroup;
356
private javax.swing.JLabel unitNameLabel;
357
private javax.swing.JTextField unitNameTextField;
358
// End of variables declaration//GEN-END:variables
361
* Document listener that invokes <code>checkValidity</code> when
364
private class ValidationListener implements DocumentListener {
365
public void insertUpdate(DocumentEvent e) {
368
public void removeUpdate(DocumentEvent e) {
371
public void changedUpdate(DocumentEvent e) {