1 19 20 package org.netbeans.modules.derby.ui; 21 22 import java.awt.Color ; 23 import java.io.File ; 24 import javax.swing.UIManager ; 25 import javax.swing.event.DocumentListener ; 26 import org.netbeans.modules.derby.api.DerbyDatabases; 27 import org.openide.DialogDescriptor; 28 import org.openide.util.NbBundle; 29 33 public class CreateDatabasePanel extends javax.swing.JPanel { 34 35 private File derbySystemHome; 36 private DialogDescriptor descriptor; 37 private Color nbErrorForeground; 38 private Color nbWarningForeground; 39 40 private DocumentListener docListener = new DocumentListener () { 41 42 public void removeUpdate(javax.swing.event.DocumentEvent e) { 43 validateDatabaseName(); 44 } 45 46 public void insertUpdate(javax.swing.event.DocumentEvent e) { 47 validateDatabaseName(); 48 } 49 50 public void changedUpdate(javax.swing.event.DocumentEvent e) { 51 validateDatabaseName(); 52 } 53 }; 54 55 public CreateDatabasePanel(String derbySystemHome) { 56 nbErrorForeground = UIManager.getColor("nb.errorForeground"); if (nbErrorForeground == null) { 59 nbErrorForeground = new Color (255, 0, 0); } 62 nbWarningForeground = UIManager.getColor("nb.warningForeground"); if (nbWarningForeground == null) { 64 nbWarningForeground = new Color (51, 51, 51); } 66 67 this.derbySystemHome = new File (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 getDatabaseName() { 82 return databaseNameTextField.getText().trim(); 83 } 84 85 public String getUser() { 86 String user = userTextField.getText().trim(); 87 return user.length() > 0 ? user : null; 88 } 89 90 public String getPassword() { 91 String 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 error = null; 101 String warning = null; 102 103 String databaseName = getDatabaseName(); 104 int illegalChar = DerbyDatabases.getFirstIllegalCharacter(databaseName); 105 int unsupportedChar = getFirstUnsupportedCharacter(databaseName); 107 108 if (databaseName.length() <= 0) { error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DatabaseNameEmpty"); 110 } else if (illegalChar >= 0) { 111 error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DatabaseNameIllegalChar", new Character ((char)illegalChar)); 112 } else if (databaseName.length() > 0 && new File (derbySystemHome, databaseName).exists()) { error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DatabaseDirectoryExists", databaseName); 114 } else if (unsupportedChar >= 0) { 115 warning = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DatabaseNameUnsupportedChar", new Character ((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(" "); descriptor.setValid(true); 133 } 134 } 135 136 private void updateLocation() { 137 databaseLocationTextField.setText(derbySystemHome.getAbsolutePath()); 138 } 139 140 private int getFirstUnsupportedCharacter(String 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 155 private void initComponents() { 157 databaseNameLabel = new javax.swing.JLabel (); 158 databaseNameTextField = new javax.swing.JTextField (); 159 infoScrollPane = new javax.swing.JScrollPane (); 160 infoTextArea = new javax.swing.JTextArea (); 161 databaseLocationLabel = new javax.swing.JLabel (); 162 databaseLocationTextField = new javax.swing.JTextField (); 163 userLabel = new javax.swing.JLabel (); 164 userTextField = new javax.swing.JTextField (); 165 passwordLabel = new javax.swing.JLabel (); 166 passwordTextField = new javax.swing.JTextField (); 167 messageLabel = new javax.swing.JLabel (); 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 } 257 258 public javax.swing.JLabel databaseLocationLabel; 260 public javax.swing.JTextField databaseLocationTextField; 261 public javax.swing.JLabel databaseNameLabel; 262 public javax.swing.JTextField databaseNameTextField; 263 public javax.swing.JScrollPane infoScrollPane; 264 public javax.swing.JTextArea infoTextArea; 265 public javax.swing.JLabel messageLabel; 266 public javax.swing.JLabel passwordLabel; 267 public javax.swing.JTextField passwordTextField; 268 public javax.swing.JLabel userLabel; 269 public javax.swing.JTextField userTextField; 270 272 } 273 | Popular Tags |