KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Dialog JavaDoc;
24 import java.io.File JavaDoc;
25 import javax.swing.JFileChooser JavaDoc;
26 import javax.swing.UIManager JavaDoc;
27 import javax.swing.event.DocumentListener JavaDoc;
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 /**
36  *
37  * @author Andrei Badea
38  */

39 public class DerbySystemHomePanel extends javax.swing.JPanel JavaDoc {
40
41     private DialogDescriptor descriptor;
42     private Color JavaDoc nbErrorForeground;
43     
44     private DocumentListener JavaDoc docListener = new DocumentListener JavaDoc() {
45         
46         public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
47             validatePanel();
48         }
49
50         public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
51             validatePanel();
52         }
53
54         public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
55             validatePanel();
56         }
57     };
58     
59     /**
60      * Asks the user for the value of the Derby system home. Never returns null.
61      */

62     public static String JavaDoc findDerbySystemHome() {
63         // since this could be called from StartAction, which is asynchronous
64
return Mutex.EVENT.writeAccess(new Mutex.Action<String JavaDoc>() {
65             public String JavaDoc run() {
66                 DerbySystemHomePanel panel = new DerbySystemHomePanel();
67                 String JavaDoc title = NbBundle.getMessage(DerbySystemHomePanel.class, "LBL_SetDerbySystemHome");
68
69                 DialogDescriptor desc = new DialogDescriptor(panel, title);
70                 panel.setDialogDescriptor(desc);
71                 
72                 for (;;) {
73                     Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(desc);
74                     String JavaDoc 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 ""; // NOI18N
81
}
82                     
83                     File JavaDoc derbySystemHome = new File JavaDoc(panel.getDerbySystemHome());
84                     
85                     if (!derbySystemHome.exists()) {
86                         boolean success = derbySystemHome.mkdirs();
87                         if (!success) {
88                             String JavaDoc 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         // copied from WizardDescriptor
103
nbErrorForeground = UIManager.getColor("nb.errorForeground"); //NOI18N
104
if (nbErrorForeground == null) {
105             //nbErrorForeground = new Color(89, 79, 191); // RGB suggested by Bruce in #28466
106
nbErrorForeground = new Color JavaDoc(255, 0, 0); // RGB suggested by jdinga in #65358
107
}
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 JavaDoc getDerbySystemHome() {
119         return derbySystemHomeTextField.getText().trim();
120     }
121     
122     private void setDerbySystemHome(String JavaDoc derbySystemHome) {
123         derbySystemHomeTextField.setText(derbySystemHome);
124     }
125     
126     private void validatePanel() {
127         if (descriptor == null) {
128             return;
129         }
130         
131         String JavaDoc error = null;
132         File JavaDoc derbySystemHome = new File JavaDoc(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(" "); // NOI18N
147
descriptor.setValid(true);
148         }
149     }
150     
151     /** This method is called from within the constructor to
152      * initialize the form.
153      * WARNING: Do NOT modify this code. The content of this method is
154      * always regenerated by the Form Editor.
155      */

156     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
157
private void initComponents() {
158         infoScrollPane = new javax.swing.JScrollPane JavaDoc();
159         infoTextArea = new javax.swing.JTextArea JavaDoc();
160         derbySystemHomeLabel = new javax.swing.JLabel JavaDoc();
161         derbySystemHomeTextField = new javax.swing.JTextField JavaDoc();
162         browseButton = new javax.swing.JButton JavaDoc();
163         messageLabel = new javax.swing.JLabel JavaDoc();
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 JavaDoc() {
185             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
227

228     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
229
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
230         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
231         chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
232         String JavaDoc derbySystemHome = getDerbySystemHome();
233         if (derbySystemHome.length() > 0) {
234             chooser.setSelectedFile(new File JavaDoc(derbySystemHome));
235         } else {
236             chooser.setCurrentDirectory(new File JavaDoc(System.getProperty("user.home"))); // NOI18N
237
}
238         if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
239             return;
240         }
241         setDerbySystemHome(chooser.getSelectedFile().getAbsolutePath());
242     }//GEN-LAST:event_browseButtonActionPerformed
243

244     
245     // Variables declaration - do not modify//GEN-BEGIN:variables
246
public javax.swing.JButton JavaDoc browseButton;
247     public javax.swing.JLabel JavaDoc derbySystemHomeLabel;
248     public javax.swing.JTextField JavaDoc derbySystemHomeTextField;
249     public javax.swing.JScrollPane JavaDoc infoScrollPane;
250     public javax.swing.JTextArea JavaDoc infoTextArea;
251     public javax.swing.JLabel JavaDoc messageLabel;
252     // End of variables declaration//GEN-END:variables
253

254 }
255
Popular Tags