1 19 20 package org.netbeans.modules.tasklist.core.export; 21 22 import java.io.File ; 23 import javax.swing.JFileChooser ; 24 import javax.swing.event.DocumentEvent ; 25 import javax.swing.event.DocumentListener ; 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 34 public class SaveFilePanel extends javax.swing.JPanel { 35 private JFileChooser fc = new JFileChooser (); 36 private SimpleWizardPanel panel; 37 38 39 public SaveFilePanel() { 40 initComponents(); 41 fc.setDialogType(JFileChooser.SAVE_DIALOG); 42 jTextFieldFile.getDocument().addDocumentListener( 43 new DocumentListener () { 44 public void insertUpdate(DocumentEvent e) { 45 check(); 46 } 47 public void removeUpdate(DocumentEvent e) { 48 check(); 49 } 50 public void changedUpdate(DocumentEvent e) { 51 check(); 52 } 53 } 54 ); 55 jCheckBoxOpen.setVisible(false); 56 } 57 58 63 public void setOpenFileCheckBoxVisible(boolean b) { 64 jCheckBoxOpen.setVisible(b); 65 } 66 67 72 public boolean getOpenExportedFile() { 73 return jCheckBoxOpen.isSelected(); 74 } 75 76 82 public void setWizardPanel(SimpleWizardPanel panel) { 83 this.panel = panel; 84 } 85 86 91 private void initComponents() { 93 java.awt.GridBagConstraints gridBagConstraints; 94 95 jLabel1 = new javax.swing.JLabel (); 96 jTextFieldFile = new javax.swing.JTextField (); 97 jButtonChoose = new javax.swing.JButton (); 98 jCheckBoxOpen = new javax.swing.JCheckBox (); 99 100 setLayout(new java.awt.GridBagLayout ()); 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 (); 105 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 12); 106 add(jLabel1, gridBagConstraints); 107 108 gridBagConstraints = new java.awt.GridBagConstraints (); 109 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 110 gridBagConstraints.weightx = 1.0; 111 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 12); 112 add(jTextFieldFile, gridBagConstraints); 113 114 org.openide.awt.Mnemonics.setLocalizedText(jButtonChoose, "..."); 115 jButtonChoose.addActionListener(new java.awt.event.ActionListener () { 116 public void actionPerformed(java.awt.event.ActionEvent evt) { 117 jButtonChooseActionPerformed(evt); 118 } 119 }); 120 121 gridBagConstraints = new java.awt.GridBagConstraints (); 122 gridBagConstraints.insets = new java.awt.Insets (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 (0, 0, 0, 0)); 128 gridBagConstraints = new java.awt.GridBagConstraints (); 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 } 137 private void jButtonChooseActionPerformed(java.awt.event.ActionEvent evt) { fc.setSelectedFile(new File (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"), NbBundle.getMessage(SaveFilePanel.class, 147 "Warning"), NotifyDescriptor.OK_CANCEL_OPTION); 149 if (DialogDisplayer.getDefault().notify(nd) != 150 NotifyDescriptor.OK_OPTION) 151 return; 152 } 153 jTextFieldFile.setText(fc.getSelectedFile().getAbsolutePath()); 154 } 156 157 private javax.swing.JButton jButtonChoose; 159 private javax.swing.JCheckBox jCheckBoxOpen; 160 private javax.swing.JLabel jLabel1; 161 private javax.swing.JTextField jTextFieldFile; 162 164 169 public JFileChooser getFileChooser() { 170 return fc; 171 } 172 173 178 public void setFile(File f) { 179 jTextFieldFile.setText(f.getAbsolutePath()); 180 fc.setSelectedFile(f); 181 check(); 182 } 183 184 189 public File getFile() { 190 return new File (jTextFieldFile.getText()); 191 } 192 193 196 private void check() { 197 String t = jTextFieldFile.getText().trim(); 198 if (t.length() == 0) { 199 panel.setErrorMessage(NbBundle.getMessage( 200 SaveFilePanel.class, "EmptyFileName")); return; 202 } 203 204 File f = new File (t); 205 if (f.isDirectory()) { 206 panel.setErrorMessage(NbBundle.getMessage( 207 SaveFilePanel.class, "NotAFile")); return; 209 } 210 211 File dir = f.getParentFile(); 212 if (dir == null || !dir.isDirectory()) { 213 panel.setErrorMessage(NbBundle.getMessage( 215 SaveFilePanel.class, "NoParent")); return; 217 } 218 219 if (!f.isAbsolute()) { 220 panel.setErrorMessage(NbBundle.getMessage( 221 SaveFilePanel.class, "NotAnAbsolutePath")); return; 223 } 224 225 try { 226 System.getSecurityManager().checkWrite(f.getAbsolutePath()); 227 panel.setErrorMessage(null); 228 } catch (SecurityException e) { 229 panel.setErrorMessage(e.getLocalizedMessage()); 230 } 231 } 232 233 238 public void setOpenFileCheckBox(boolean open) { 239 jCheckBoxOpen.setSelected(open); 240 } 241 } 242 | Popular Tags |