KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > export > SaveFilePanel


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.tasklist.core.export;
21
22 import java.io.File JavaDoc;
23 import javax.swing.JFileChooser JavaDoc;
24 import javax.swing.event.DocumentEvent JavaDoc;
25 import javax.swing.event.DocumentListener JavaDoc;
26 import org.netbeans.modules.tasklist.core.util.SimpleWizardPanel;
27 import org.openide.DialogDisplayer;
28 import org.openide.NotifyDescriptor;
29 import org.openide.util.NbBundle;
30
31 /**
32  * Panel with a file chooser
33  */

34 public class SaveFilePanel extends javax.swing.JPanel JavaDoc {
35     private JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
36     private SimpleWizardPanel panel;
37
38     /** Creates new form ChooseFilePanel */
39     public SaveFilePanel() {
40         initComponents();
41         fc.setDialogType(JFileChooser.SAVE_DIALOG);
42         jTextFieldFile.getDocument().addDocumentListener(
43             new DocumentListener JavaDoc() {
44                 public void insertUpdate(DocumentEvent JavaDoc e) {
45                     check();
46                 }
47                 public void removeUpdate(DocumentEvent JavaDoc e) {
48                     check();
49                 }
50                 public void changedUpdate(DocumentEvent JavaDoc e) {
51                     check();
52                 }
53             }
54         );
55         jCheckBoxOpen.setVisible(false);
56     }
57     
58     /**
59      * Sets the visibility of "Open File" checkbox.
60      *
61      * @param b true = visible
62      */

63     public void setOpenFileCheckBoxVisible(boolean b) {
64         jCheckBoxOpen.setVisible(b);
65     }
66     
67     /**
68      * Returns the state of the "open exported file" checkbox.
69      *
70      * @return true = the checkbox is checked.
71      */

72     public boolean getOpenExportedFile() {
73         return jCheckBoxOpen.isSelected();
74     }
75     
76     /**
77      * Associates a wizard panel with this Swing panel.
78      * This method should be called to initialize this panel.
79      *
80      * @param panel associated panel.
81      */

82     public void setWizardPanel(SimpleWizardPanel panel) {
83         this.panel = panel;
84     }
85     
86     /** This method is called from within the constructor to
87      * initialize the form.
88      * WARNING: Do NOT modify this code. The content of this method is
89      * always regenerated by the Form Editor.
90      */

91     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
92
private void initComponents() {
93         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
94
95         jLabel1 = new javax.swing.JLabel JavaDoc();
96         jTextFieldFile = new javax.swing.JTextField JavaDoc();
97         jButtonChoose = new javax.swing.JButton JavaDoc();
98         jCheckBoxOpen = new javax.swing.JCheckBox JavaDoc();
99
100         setLayout(new java.awt.GridBagLayout JavaDoc());
101
102         setName(org.openide.util.NbBundle.getBundle(SaveFilePanel.class).getString("ChooseFile"));
103         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(SaveFilePanel.class).getString("File"));
104         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
105         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 12);
106         add(jLabel1, gridBagConstraints);
107
108         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
109         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
110         gridBagConstraints.weightx = 1.0;
111         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 12);
112         add(jTextFieldFile, gridBagConstraints);
113
114         org.openide.awt.Mnemonics.setLocalizedText(jButtonChoose, "...");
115         jButtonChoose.addActionListener(new java.awt.event.ActionListener JavaDoc() {
116             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
117                 jButtonChooseActionPerformed(evt);
118             }
119         });
120
121         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
122         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
123         add(jButtonChoose, gridBagConstraints);
124
125         jCheckBoxOpen.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/tasklist/core/export/Bundle").getString("OpenExportedFile"));
126         jCheckBoxOpen.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
127         jCheckBoxOpen.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
128         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
129         gridBagConstraints.gridx = 0;
130         gridBagConstraints.gridy = 1;
131         gridBagConstraints.gridwidth = 3;
132         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
133         add(jCheckBoxOpen, gridBagConstraints);
134
135     }// </editor-fold>//GEN-END:initComponents
136

137     private void jButtonChooseActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonChooseActionPerformed
138
fc.setSelectedFile(new File JavaDoc(jTextFieldFile.getText()));
139         if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
140             return;
141         
142         if (fc.getSelectedFile().exists()) {
143             NotifyDescriptor nd = new NotifyDescriptor.Confirmation(
144                 NbBundle.getMessage(SaveFilePanel.class,
145                     "FileExistsOverwrite"), // NOI18N
146
NbBundle.getMessage(SaveFilePanel.class,
147                     "Warning"), // NOI18N
148
NotifyDescriptor.OK_CANCEL_OPTION);
149             if (DialogDisplayer.getDefault().notify(nd) !=
150             NotifyDescriptor.OK_OPTION)
151                 return;
152         }
153         jTextFieldFile.setText(fc.getSelectedFile().getAbsolutePath());
154     }//GEN-LAST:event_jButtonChooseActionPerformed
155

156     
157     // Variables declaration - do not modify//GEN-BEGIN:variables
158
private javax.swing.JButton JavaDoc jButtonChoose;
159     private javax.swing.JCheckBox JavaDoc jCheckBoxOpen;
160     private javax.swing.JLabel JavaDoc jLabel1;
161     private javax.swing.JTextField JavaDoc jTextFieldFile;
162     // End of variables declaration//GEN-END:variables
163

164     /**
165      * Returns the associated file chooser
166      *
167      * @return file chooser
168      */

169     public JFileChooser JavaDoc getFileChooser() {
170         return fc;
171     }
172
173     /**
174      * Sets choosed file
175      *
176      * @param f choosed file
177      */

178     public void setFile(File JavaDoc f) {
179         jTextFieldFile.setText(f.getAbsolutePath());
180         fc.setSelectedFile(f);
181         check();
182     }
183     
184     /**
185      * Returns selected file
186      *
187      * @return selected file or null
188      */

189     public File JavaDoc getFile() {
190         return new File JavaDoc(jTextFieldFile.getText());
191     }
192     
193     /**
194      * Sets the valid property
195      */

196     private void check() {
197         String JavaDoc t = jTextFieldFile.getText().trim();
198         if (t.length() == 0) {
199             panel.setErrorMessage(NbBundle.getMessage(
200                 SaveFilePanel.class, "EmptyFileName")); // NOI18N
201
return;
202         }
203             
204         File JavaDoc f = new File JavaDoc(t);
205         if (f.isDirectory()) {
206             panel.setErrorMessage(NbBundle.getMessage(
207                 SaveFilePanel.class, "NotAFile")); // NOI18N
208
return;
209         }
210         
211         File JavaDoc dir = f.getParentFile();
212         if (dir == null || !dir.isDirectory()) {
213             // Parent directory does not exist
214
panel.setErrorMessage(NbBundle.getMessage(
215                 SaveFilePanel.class, "NoParent")); // NOI18N
216
return;
217         }
218         
219         if (!f.isAbsolute()) {
220             panel.setErrorMessage(NbBundle.getMessage(
221                 SaveFilePanel.class, "NotAnAbsolutePath")); // NOI18N
222
return;
223         }
224         
225         try {
226             System.getSecurityManager().checkWrite(f.getAbsolutePath());
227             panel.setErrorMessage(null);
228         } catch (SecurityException JavaDoc e) {
229             panel.setErrorMessage(e.getLocalizedMessage());
230         }
231     }
232
233     /**
234      * Changes the state of the "open file" checkbox.
235      *
236      * @param open true = selected
237      */

238     public void setOpenFileCheckBox(boolean open) {
239         jCheckBoxOpen.setSelected(open);
240     }
241 }
242
Popular Tags