KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > unit > PersistenceUnitWizardPanelJdbc


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard.unit;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.event.ItemEvent JavaDoc;
24 import java.awt.event.ItemListener JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import javax.swing.event.DocumentEvent JavaDoc;
27 import javax.swing.event.DocumentListener JavaDoc;
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 /**
46  *
47  * @author Martin Adamek
48  */

49 public class PersistenceUnitWizardPanelJdbc extends PersistenceUnitWizardPanel{
50     
51     private Project project;
52     
53     public PersistenceUnitWizardPanelJdbc(Project project, ChangeListener JavaDoc changeListener, boolean editName) {
54         this(project, changeListener, editName, TableGeneration.CREATE);
55     }
56     
57     public PersistenceUnitWizardPanelJdbc(Project project, ChangeListener JavaDoc 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"); //NOI18N
67
// unit name editing is not available when adding first PU
68
unitNameTextField.setVisible(editName);
69         unitNameLabel.setVisible(editName);
70         
71         DatabaseExplorerUIs.connect(jdbcCombo, ConnectionManager.getDefault());
72         jdbcCombo.addItemListener(new ItemListener JavaDoc() {
73             public void itemStateChanged(ItemEvent JavaDoc e) {
74                 checkValidity();
75             }
76         });
77         
78         unitNameTextField.getDocument().addDocumentListener(new ValidationListener());
79         errorMessage.setForeground(Color.RED);
80     }
81     
82     
83     /**
84      * Pre-selects appropriate table generation strategy radio button.
85      */

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     /**
97      * Checks whether this panel is in valid state (see <code>#isValidPanel()</code>)
98      * and fires appropriate property changes.
99      */

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 JavaDoc 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 JavaDoc 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 JavaDoc 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())); //NO18N
164
return false;
165         }
166         return true;
167     }
168     
169     /**
170      * Checks whether name of the persistence unit is valid, i.e. it's not
171      * empty and it's unique.
172      */

173     private boolean isNameValid() throws InvalidPersistenceXmlException{
174         return isEmptyOrNull(getPersistenceUnitName()) ? false : isNameUnique();
175     }
176     
177     /**
178      * @see PersistenceUnitWizardPanel#isNameUnique
179      */

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     /**
198      * @return true if given str is null, empty or contains only spaces.
199      */

200     private boolean isEmptyOrNull(String JavaDoc str){
201         return str == null || "".equals(str.trim());
202     }
203     
204     public void setErrorMessage(String JavaDoc msg) {
205         errorMessage.setText(msg);
206     }
207     
208     /** This method is called from within the constructor to
209      * initialize the form.
210      * WARNING: Do NOT modify this code. The content of this method is
211      * always regenerated by the Form Editor.
212      */

213     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
214
private void initComponents() {
215         buttonGroup1 = new javax.swing.ButtonGroup JavaDoc();
216         tableCreationButtonGroup = new javax.swing.ButtonGroup JavaDoc();
217         unitNameLabel = new javax.swing.JLabel JavaDoc();
218         unitNameTextField = new javax.swing.JTextField JavaDoc();
219         jLabel1 = new javax.swing.JLabel JavaDoc();
220         jLabel2 = new javax.swing.JLabel JavaDoc();
221         ddlCreate = new javax.swing.JRadioButton JavaDoc();
222         ddlDropCreate = new javax.swing.JRadioButton JavaDoc();
223         ddlUnkown = new javax.swing.JRadioButton JavaDoc();
224         libraryLabel = new javax.swing.JLabel JavaDoc();
225         libraryCombo = new javax.swing.JComboBox JavaDoc();
226         jdbcCombo = new javax.swing.JComboBox JavaDoc();
227         jdbcLabel = new javax.swing.JLabel JavaDoc();
228         errorMessage = new javax.swing.JLabel JavaDoc();
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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc() {
272             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
339

340     private void libraryComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_libraryComboActionPerformed
341
checkValidity();
342     }//GEN-LAST:event_libraryComboActionPerformed
343

344     
345     // Variables declaration - do not modify//GEN-BEGIN:variables
346
private javax.swing.ButtonGroup JavaDoc buttonGroup1;
347     private javax.swing.JRadioButton JavaDoc ddlCreate;
348     private javax.swing.JRadioButton JavaDoc ddlDropCreate;
349     private javax.swing.JRadioButton JavaDoc ddlUnkown;
350     private javax.swing.JLabel JavaDoc errorMessage;
351     private javax.swing.JLabel JavaDoc jLabel1;
352     private javax.swing.JLabel JavaDoc jLabel2;
353     private javax.swing.JComboBox JavaDoc jdbcCombo;
354     private javax.swing.JLabel JavaDoc jdbcLabel;
355     private javax.swing.JComboBox JavaDoc libraryCombo;
356     private javax.swing.JLabel JavaDoc libraryLabel;
357     private javax.swing.ButtonGroup JavaDoc tableCreationButtonGroup;
358     private javax.swing.JLabel JavaDoc unitNameLabel;
359     private javax.swing.JTextField JavaDoc unitNameTextField;
360     // End of variables declaration//GEN-END:variables
361

362     /**
363      * Document listener that invokes <code>checkValidity</code> when
364      * changes are made.
365      */

366     private class ValidationListener implements DocumentListener JavaDoc {
367         public void insertUpdate(DocumentEvent JavaDoc e) {
368             checkValidity();
369         }
370         public void removeUpdate(DocumentEvent JavaDoc e) {
371             checkValidity();
372         }
373         public void changedUpdate(DocumentEvent JavaDoc e) {
374             checkValidity();
375         }
376     }
377 }
378
Popular Tags