KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > derby > ui > CreateDatabasePanel


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.derby.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.io.File JavaDoc;
24 import javax.swing.UIManager JavaDoc;
25 import javax.swing.event.DocumentListener JavaDoc;
26 import org.netbeans.modules.derby.api.DerbyDatabases;
27 import org.openide.DialogDescriptor;
28 import org.openide.util.NbBundle;
29 /**
30  *
31  * @author Andrei Badea
32  */

33 public class CreateDatabasePanel extends javax.swing.JPanel JavaDoc {
34
35     private File JavaDoc derbySystemHome;
36     private DialogDescriptor descriptor;
37     private Color JavaDoc nbErrorForeground;
38     private Color JavaDoc nbWarningForeground;
39
40     private DocumentListener JavaDoc docListener = new DocumentListener JavaDoc() {
41         
42         public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
43             validateDatabaseName();
44         }
45
46         public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
47             validateDatabaseName();
48         }
49
50         public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
51             validateDatabaseName();
52         }
53     };
54     
55     public CreateDatabasePanel(String JavaDoc derbySystemHome) {
56         // copied from WizardDescriptor
57
nbErrorForeground = UIManager.getColor("nb.errorForeground"); //NOI18N
58
if (nbErrorForeground == null) {
59             //nbErrorForeground = new Color(89, 79, 191); // RGB suggested by Bruce in #28466
60
nbErrorForeground = new Color JavaDoc(255, 0, 0); // RGB suggested by jdinga in #65358
61
}
62         nbWarningForeground = UIManager.getColor("nb.warningForeground"); //NOI18N
63
if (nbWarningForeground == null) {
64             nbWarningForeground = new Color JavaDoc(51, 51, 51); // Label.foreground
65
}
66
67         this.derbySystemHome = new File JavaDoc(derbySystemHome);
68         
69         initComponents();
70         databaseNameTextField.getDocument().addDocumentListener(docListener);
71         userTextField.getDocument().addDocumentListener(docListener);
72         passwordTextField.getDocument().addDocumentListener(docListener);
73         updateLocation();
74     }
75     
76     public void setDialogDescriptor(DialogDescriptor descriptor) {
77         this.descriptor = descriptor;
78         validateDatabaseName();
79     }
80
81     public String JavaDoc getDatabaseName() {
82         return databaseNameTextField.getText().trim();
83     }
84     
85     public String JavaDoc getUser() {
86         String JavaDoc user = userTextField.getText().trim();
87         return user.length() > 0 ? user : null;
88     }
89     
90     public String JavaDoc getPassword() {
91         String JavaDoc password = passwordTextField.getText().trim();
92         return password.length() > 0 ? password : null;
93     }
94     
95     private void validateDatabaseName() {
96         if (descriptor == null) {
97             return;
98         }
99         
100         String JavaDoc error = null;
101         String JavaDoc warning = null;
102         
103         String JavaDoc databaseName = getDatabaseName();
104         int illegalChar = DerbyDatabases.getFirstIllegalCharacter(databaseName);
105         // workaround for issue 69265
106
int unsupportedChar = getFirstUnsupportedCharacter(databaseName);
107         
108         if (databaseName.length() <= 0) { // NOI18N
109
error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DatabaseNameEmpty");
110         } else if (illegalChar >= 0) {
111             error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DatabaseNameIllegalChar", new Character JavaDoc((char)illegalChar));
112         } else if (databaseName.length() > 0 && new File JavaDoc(derbySystemHome, databaseName).exists()) { // NOI18N
113
error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DatabaseDirectoryExists", databaseName);
114         } else if (unsupportedChar >= 0) {
115             warning = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DatabaseNameUnsupportedChar", new Character JavaDoc((char)unsupportedChar));
116         } else if (getUser() == null || getPassword() == null) {
117             warning = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_UserNamePasswordRecommended");
118         }
119         
120         if (error != null) {
121             messageLabel.setForeground(nbErrorForeground);
122             messageLabel.setText(error);
123             messageLabel.setToolTipText(error);
124             descriptor.setValid(false);
125         } else if (warning != null) {
126             messageLabel.setForeground(nbWarningForeground);
127             messageLabel.setText(warning);
128             messageLabel.setToolTipText(warning);
129             descriptor.setValid(true);
130         } else {
131             messageLabel.setText(" "); // NOI18N
132
descriptor.setValid(true);
133         }
134     }
135     
136     private void updateLocation() {
137         databaseLocationTextField.setText(derbySystemHome.getAbsolutePath());
138     }
139     
140     private int getFirstUnsupportedCharacter(String JavaDoc databaseName) {
141         for (int i = 0; i < databaseName.length(); i++) {
142             char ch = databaseName.charAt(i);
143             if (ch < '\u0020' || ch > '\u00ff') {
144                 return (int)ch;
145             }
146         }
147         return -1;
148     }
149     
150     /** This method is called from within the constructor to
151      * initialize the form.
152      * WARNING: Do NOT modify this code. The content of this method is
153      * always regenerated by the Form Editor.
154      */

155     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
156
private void initComponents() {
157         databaseNameLabel = new javax.swing.JLabel JavaDoc();
158         databaseNameTextField = new javax.swing.JTextField JavaDoc();
159         infoScrollPane = new javax.swing.JScrollPane JavaDoc();
160         infoTextArea = new javax.swing.JTextArea JavaDoc();
161         databaseLocationLabel = new javax.swing.JLabel JavaDoc();
162         databaseLocationTextField = new javax.swing.JTextField JavaDoc();
163         userLabel = new javax.swing.JLabel JavaDoc();
164         userTextField = new javax.swing.JTextField JavaDoc();
165         passwordLabel = new javax.swing.JLabel JavaDoc();
166         passwordTextField = new javax.swing.JTextField JavaDoc();
167         messageLabel = new javax.swing.JLabel JavaDoc();
168
169         databaseNameLabel.setLabelFor(databaseNameTextField);
170         org.openide.awt.Mnemonics.setLocalizedText(databaseNameLabel, org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "LBL_DatabaseName"));
171
172         databaseNameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "ACSD_CreateDatabasePanel_databaseNameTextField"));
173
174         infoScrollPane.setBorder(null);
175         infoScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
176         infoScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
177         infoTextArea.setColumns(20);
178         infoTextArea.setEditable(false);
179         infoTextArea.setLineWrap(true);
180         infoTextArea.setRows(5);
181         infoTextArea.setText(org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "LBL_DatabaseLocationInfo"));
182         infoTextArea.setWrapStyleWord(true);
183         infoTextArea.setFocusable(false);
184         infoTextArea.setOpaque(false);
185         infoScrollPane.setViewportView(infoTextArea);
186
187         databaseLocationLabel.setLabelFor(databaseLocationTextField);
188         org.openide.awt.Mnemonics.setLocalizedText(databaseLocationLabel, org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "LBL_DatabaseLocation"));
189
190         databaseLocationTextField.setEditable(false);
191         databaseLocationTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "ACSD_CreateDatabasePanel_databaseLocationTextField"));
192
193         userLabel.setLabelFor(userTextField);
194         org.openide.awt.Mnemonics.setLocalizedText(userLabel, org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "LBL_UserName"));
195
196         userTextField.setColumns(15);
197         userTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "ACSD_CreateDatabasePanel_userTextField"));
198
199         passwordLabel.setLabelFor(passwordTextField);
200         org.openide.awt.Mnemonics.setLocalizedText(passwordLabel, org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "LBL_Password"));
201
202         passwordTextField.setColumns(15);
203         passwordTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(CreateDatabasePanel.class, "ACSD_CreateDatabasePanel_passwordTextField"));
204
205         org.openide.awt.Mnemonics.setLocalizedText(messageLabel, " ");
206
207         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
208         this.setLayout(layout);
209         layout.setHorizontalGroup(
210             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
211             .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
212                 .addContainerGap()
213                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
214                     .add(infoScrollPane, 0, 440, Short.MAX_VALUE)
215                     .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
216                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
217                             .add(databaseNameLabel)
218                             .add(userLabel)
219                             .add(passwordLabel)
220                             .add(databaseLocationLabel))
221                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
222                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
223                             .add(org.jdesktop.layout.GroupLayout.LEADING, passwordTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
224                             .add(org.jdesktop.layout.GroupLayout.LEADING, userTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
225                             .add(org.jdesktop.layout.GroupLayout.LEADING, databaseNameTextField, 0, 310, Short.MAX_VALUE)
226                             .add(databaseLocationTextField, 0, 310, Short.MAX_VALUE)))
227                     .add(org.jdesktop.layout.GroupLayout.TRAILING, messageLabel, 0, 440, Short.MAX_VALUE))
228                 .addContainerGap())
229         );
230         layout.setVerticalGroup(
231             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
232             .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
233                 .addContainerGap()
234                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
235                     .add(databaseNameLabel)
236                     .add(databaseNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
237                 .add(11, 11, 11)
238                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
239                     .add(userLabel)
240                     .add(userTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
241                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
242                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
243                     .add(passwordLabel)
244                     .add(passwordTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
245                 .add(11, 11, 11)
246                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
247                     .add(databaseLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
248                     .add(databaseLocationLabel))
249                 .add(11, 11, 11)
250                 .add(infoScrollPane, 0, 66, Short.MAX_VALUE)
251                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
252                 .add(messageLabel)
253                 .addContainerGap())
254         );
255     }// </editor-fold>//GEN-END:initComponents
256

257     
258     // Variables declaration - do not modify//GEN-BEGIN:variables
259
public javax.swing.JLabel JavaDoc databaseLocationLabel;
260     public javax.swing.JTextField JavaDoc databaseLocationTextField;
261     public javax.swing.JLabel JavaDoc databaseNameLabel;
262     public javax.swing.JTextField JavaDoc databaseNameTextField;
263     public javax.swing.JScrollPane JavaDoc infoScrollPane;
264     public javax.swing.JTextArea JavaDoc infoTextArea;
265     public javax.swing.JLabel JavaDoc messageLabel;
266     public javax.swing.JLabel JavaDoc passwordLabel;
267     public javax.swing.JTextField JavaDoc passwordTextField;
268     public javax.swing.JLabel JavaDoc userLabel;
269     public javax.swing.JTextField JavaDoc userTextField;
270     // End of variables declaration//GEN-END:variables
271

272 }
273
Popular Tags