1 19 20 package org.netbeans.modules.derby.ui; 21 22 import java.awt.Color ; 23 import java.awt.Dialog ; 24 import java.io.File ; 25 import javax.swing.JFileChooser ; 26 import javax.swing.UIManager ; 27 import javax.swing.event.DocumentListener ; 28 import org.openide.DialogDescriptor; 29 import org.openide.DialogDisplayer; 30 import org.openide.NotifyDescriptor; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.util.Mutex; 33 import org.openide.util.NbBundle; 34 35 39 public class DerbySystemHomePanel extends javax.swing.JPanel { 40 41 private DialogDescriptor descriptor; 42 private Color nbErrorForeground; 43 44 private DocumentListener docListener = new DocumentListener () { 45 46 public void removeUpdate(javax.swing.event.DocumentEvent e) { 47 validatePanel(); 48 } 49 50 public void insertUpdate(javax.swing.event.DocumentEvent e) { 51 validatePanel(); 52 } 53 54 public void changedUpdate(javax.swing.event.DocumentEvent e) { 55 validatePanel(); 56 } 57 }; 58 59 62 public static String findDerbySystemHome() { 63 return Mutex.EVENT.writeAccess(new Mutex.Action<String >() { 65 public String run() { 66 DerbySystemHomePanel panel = new DerbySystemHomePanel(); 67 String title = NbBundle.getMessage(DerbySystemHomePanel.class, "LBL_SetDerbySystemHome"); 68 69 DialogDescriptor desc = new DialogDescriptor(panel, title); 70 panel.setDialogDescriptor(desc); 71 72 for (;;) { 73 Dialog dialog = DialogDisplayer.getDefault().createDialog(desc); 74 String acsd = NbBundle.getMessage(DerbySystemHomePanel.class, "ACSD_DerbySystemHomePanel"); 75 dialog.getAccessibleContext().setAccessibleDescription(acsd); 76 dialog.setVisible(true); 77 dialog.dispose(); 78 79 if (!desc.OK_OPTION.equals(desc.getValue())) { 80 return ""; } 82 83 File derbySystemHome = new File (panel.getDerbySystemHome()); 84 85 if (!derbySystemHome.exists()) { 86 boolean success = derbySystemHome.mkdirs(); 87 if (!success) { 88 String message = NbBundle.getMessage(DerbySystemHomePanel.class, "ERR_DerbySystemHomeCantCreate"); 89 NotifyDescriptor ndesc = new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE); 90 DialogDisplayer.getDefault().notify(ndesc); 91 continue; 92 } 93 } 94 95 return panel.getDerbySystemHome(); 96 } 97 } 98 }); 99 } 100 101 private DerbySystemHomePanel() { 102 nbErrorForeground = UIManager.getColor("nb.errorForeground"); if (nbErrorForeground == null) { 105 nbErrorForeground = new Color (255, 0, 0); } 108 109 initComponents(); 110 derbySystemHomeTextField.getDocument().addDocumentListener(docListener); 111 } 112 113 private void setDialogDescriptor(DialogDescriptor descriptor) { 114 this.descriptor = descriptor; 115 validatePanel(); 116 } 117 118 private String getDerbySystemHome() { 119 return derbySystemHomeTextField.getText().trim(); 120 } 121 122 private void setDerbySystemHome(String derbySystemHome) { 123 derbySystemHomeTextField.setText(derbySystemHome); 124 } 125 126 private void validatePanel() { 127 if (descriptor == null) { 128 return; 129 } 130 131 String error = null; 132 File derbySystemHome = new File (getDerbySystemHome()); 133 134 if (derbySystemHome.getPath().length() <= 0) { 135 error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DerbySystemHomeNotEntered"); 136 } else if (derbySystemHome.exists() && !derbySystemHome.isDirectory()) { 137 error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DerbySystemHomeNotDirectory"); 138 } else if (!derbySystemHome.isAbsolute()) { 139 error = NbBundle.getMessage(CreateDatabasePanel.class, "ERR_DerbySystemHomeNotAbsolute"); 140 } 141 142 if (error != null) { 143 messageLabel.setText(error); 144 descriptor.setValid(false); 145 } else { 146 messageLabel.setText(" "); descriptor.setValid(true); 148 } 149 } 150 151 156 private void initComponents() { 158 infoScrollPane = new javax.swing.JScrollPane (); 159 infoTextArea = new javax.swing.JTextArea (); 160 derbySystemHomeLabel = new javax.swing.JLabel (); 161 derbySystemHomeTextField = new javax.swing.JTextField (); 162 browseButton = new javax.swing.JButton (); 163 messageLabel = new javax.swing.JLabel (); 164 165 infoScrollPane.setBorder(null); 166 infoScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 167 infoScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); 168 infoTextArea.setColumns(20); 169 infoTextArea.setEditable(false); 170 infoTextArea.setLineWrap(true); 171 infoTextArea.setRows(5); 172 infoTextArea.setText(org.openide.util.NbBundle.getMessage(DerbySystemHomePanel.class, "LBL_DerbySystemHomeInfo")); 173 infoTextArea.setWrapStyleWord(true); 174 infoTextArea.setFocusable(false); 175 infoTextArea.setOpaque(false); 176 infoScrollPane.setViewportView(infoTextArea); 177 178 derbySystemHomeLabel.setLabelFor(derbySystemHomeTextField); 179 org.openide.awt.Mnemonics.setLocalizedText(derbySystemHomeLabel, org.openide.util.NbBundle.getMessage(DerbySystemHomePanel.class, "LBL_DerbySystemHome")); 180 181 derbySystemHomeTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DerbySystemHomePanel.class, "ACSD_DerbySystemHomePanel_derbySystemHomeTextField")); 182 183 org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(DerbySystemHomePanel.class, "LBL_Browse")); 184 browseButton.addActionListener(new java.awt.event.ActionListener () { 185 public void actionPerformed(java.awt.event.ActionEvent evt) { 186 browseButtonActionPerformed(evt); 187 } 188 }); 189 190 browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DerbySystemHomePanel.class, "ACSD_DerbySystemHomePanel_browseButton")); 191 192 messageLabel.setForeground(nbErrorForeground); 193 org.openide.awt.Mnemonics.setLocalizedText(messageLabel, " "); 194 195 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 196 this.setLayout(layout); 197 layout.setHorizontalGroup( 198 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 199 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 200 .addContainerGap() 201 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 202 .add(org.jdesktop.layout.GroupLayout.LEADING, infoScrollPane, 0, 505, Short.MAX_VALUE) 203 .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() 204 .add(derbySystemHomeLabel) 205 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 206 .add(derbySystemHomeTextField, 0, 280, Short.MAX_VALUE) 207 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 208 .add(browseButton)) 209 .add(org.jdesktop.layout.GroupLayout.LEADING, messageLabel)) 210 .addContainerGap()) 211 ); 212 layout.setVerticalGroup( 213 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 214 .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() 215 .addContainerGap() 216 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 217 .add(derbySystemHomeLabel) 218 .add(derbySystemHomeTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 219 .add(browseButton)) 220 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 11, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 221 .add(infoScrollPane, 0, 134, Short.MAX_VALUE) 222 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 223 .add(messageLabel) 224 .addContainerGap()) 225 ); 226 } 228 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 230 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 231 chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); 232 String derbySystemHome = getDerbySystemHome(); 233 if (derbySystemHome.length() > 0) { 234 chooser.setSelectedFile(new File (derbySystemHome)); 235 } else { 236 chooser.setCurrentDirectory(new File (System.getProperty("user.home"))); } 238 if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) { 239 return; 240 } 241 setDerbySystemHome(chooser.getSelectedFile().getAbsolutePath()); 242 } 244 245 public javax.swing.JButton browseButton; 247 public javax.swing.JLabel derbySystemHomeLabel; 248 public javax.swing.JTextField derbySystemHomeTextField; 249 public javax.swing.JScrollPane infoScrollPane; 250 public javax.swing.JTextArea infoTextArea; 251 public javax.swing.JLabel messageLabel; 252 254 } 255 | Popular Tags |