1 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import javax.swing.*; 24 import java.beans.*; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.net.URL ; 28 import java.net.MalformedURLException ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 32 import org.jdesktop.jdic.desktop.*; 33 34 35 41 public class FileChooser extends JPanel { 42 JPanel fileAndUrlPanel = new JPanel(); 43 JFileChooser jFileChooser = new JFileChooser(); 44 JPanel urlPanel = new JPanel(); 45 46 JPanel jVerbPanel = new JPanel(); 47 JPanel jFileButtonPanel = new JPanel(); 48 JPanel jUrlButtonPanel = new JPanel(); 49 JButton jMailButton = new JButton(); 50 JButton jPrintButton = new JButton(); 51 JButton jEditButton = new JButton(); 52 JButton jOpenButton = new JButton(); 53 JButton jBrowseButton = new JButton(); 54 55 JLabel jUrlLabel = new JLabel("URL: "); 57 JTextField jUrlTextField = new JTextField(); 58 59 File selectedFile; 60 61 public FileChooser() { 62 try { 63 jbInit(); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 69 public static void main(String [] args) { 70 try { 71 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 72 } catch (Exception e) {} 73 74 JFrame frame = new JFrame("JDIC API Demo - FileChooser"); 75 76 Container contentPane = frame.getContentPane(); 77 78 contentPane.setLayout(new GridLayout(1, 1)); 79 contentPane.add(new FileChooser()); 80 81 frame.addWindowListener(new WindowAdapter() { 82 public void windowClosing(WindowEvent e) { 83 System.exit(0); 84 } 85 }); 86 87 frame.pack(); 88 frame.setVisible(true); 89 } 90 91 private void jbInit() throws Exception { 92 jFileChooser.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0)); 93 jFileChooser.setControlButtonsAreShown(false); 94 jFileChooser.addPropertyChangeListener(new FileChooser_jFileChooser_propertyChangeAdapter(this)); 95 jUrlTextField.addActionListener(new FileChooser_jUrlTextField_actionAdapter(this)); 96 jMailButton.setEnabled(true); 97 jMailButton.setText("Mail to ..."); 98 jMailButton.addActionListener(new FileChooser_jCreateMailButton_actionAdapter(this)); 99 jPrintButton.setEnabled(false); 100 jPrintButton.setText("Print"); 101 jPrintButton.addActionListener(new FileChooser_jPrintButton_actionAdapter(this)); 102 jEditButton.setEnabled(false); 103 jEditButton.setText("Edit"); 104 jEditButton.addActionListener(new FileChooser_jEditButton_actionAdapter(this)); 105 jOpenButton.setEnabled(false); 106 jOpenButton.setText("Open"); 107 jOpenButton.addActionListener(new FileChooser_jOpenButton_actionAdapter(this)); 108 jBrowseButton.setText("Browse"); 109 jBrowseButton.setToolTipText("Browse the given URL with system default browser"); 110 jBrowseButton.addActionListener(new FileChooser_jBrowseButton_actionAdapter(this)); 111 112 jFileButtonPanel.setLayout(new GridLayout(8, 1, 10, 10)); 113 jFileButtonPanel.setBorder(BorderFactory.createEmptyBorder(42, 10, 0, 10)); 116 jFileButtonPanel.add(jOpenButton, null); 117 jFileButtonPanel.add(jEditButton, null); 118 jFileButtonPanel.add(jPrintButton, null); 119 jFileButtonPanel.add(jMailButton, null); 121 jUrlButtonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 122 jUrlButtonPanel.setLayout(new GridLayout()); 123 jUrlButtonPanel.add(jBrowseButton, null); 124 jVerbPanel.setLayout(new BorderLayout()); 125 jVerbPanel.add(jFileButtonPanel, BorderLayout.CENTER); 126 jVerbPanel.add(jUrlButtonPanel, BorderLayout.SOUTH); 127 128 urlPanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 11, 0)); 129 urlPanel.setLayout(new BorderLayout()); 130 urlPanel.add(jUrlLabel, BorderLayout.WEST); 131 urlPanel.add(jUrlTextField, BorderLayout.CENTER); 132 133 fileAndUrlPanel.setLayout(new BorderLayout()); 134 fileAndUrlPanel.add(jFileChooser, BorderLayout.CENTER); 135 fileAndUrlPanel.add(urlPanel, BorderLayout.SOUTH); 136 137 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 139 140 this.setPreferredSize(new Dimension(screenSize.width * 6 / 10, 141 screenSize.height * 5 / 10)); 142 this.setLayout(new BorderLayout()); 143 this.add(fileAndUrlPanel, BorderLayout.CENTER); 144 this.add(jVerbPanel, BorderLayout.EAST); 146 } 147 148 void jOpenButton_actionPerformed(ActionEvent e) { 149 try { 150 Desktop.open(selectedFile); 151 } catch (DesktopException de) { 152 JOptionPane.showMessageDialog(this, "Exception: " + de.toString(), 153 "Error message", JOptionPane.ERROR_MESSAGE); 154 } 155 } 156 157 void jBrowseButton_actionPerformed(ActionEvent e) { 158 String inputUrl = jUrlTextField.getText(); 159 if (inputUrl == null) { 160 return; 161 } 162 163 try { 164 Desktop.browse(new URL (inputUrl)); 165 } catch (MalformedURLException de) { 166 JOptionPane.showMessageDialog(this, "Invalid URL: " + inputUrl, 167 "Exception", JOptionPane.ERROR_MESSAGE); 168 } catch (DesktopException de) { 169 JOptionPane.showMessageDialog(this, de.toString(), "Exception", 170 JOptionPane.ERROR_MESSAGE); 171 } 172 } 173 174 void jEditButton_actionPerformed(ActionEvent e) { 175 try { 176 Desktop.edit(selectedFile); 177 } catch (DesktopException de) { 178 JOptionPane.showMessageDialog(this, de.toString(), "Exception", 179 JOptionPane.ERROR_MESSAGE); 180 } 181 } 182 183 void jPrintButton_actionPerformed(ActionEvent e) { 184 try { 185 Desktop.print(selectedFile); 186 } catch (DesktopException de) { 187 JOptionPane.showMessageDialog(this, de.toString(), "Exception", 188 JOptionPane.ERROR_MESSAGE); 189 } 190 } 191 192 void jCreateMailButton_actionPerformed(ActionEvent e) { 193 if (selectedFile == null) { 194 try { 196 Desktop.mail(); 197 } catch (DesktopException de) { 198 JOptionPane.showMessageDialog(this, de.toString(), "Exception", 199 JOptionPane.ERROR_MESSAGE); 200 } 201 } else { 202 205 Message msg = new Message(); 206 207 List attachList = new ArrayList (); 208 209 attachList.add(selectedFile.toString()); 210 try { 212 msg.setAttachments(attachList); 213 } catch (IOException ioe) { 214 JOptionPane.showMessageDialog(this, ioe.toString(), "Exception", 215 JOptionPane.ERROR_MESSAGE); 216 } 217 218 try { 219 Desktop.mail(msg); 220 } catch (DesktopException de) { 221 JOptionPane.showMessageDialog(this, de.toString(), "Exception", 222 JOptionPane.ERROR_MESSAGE); 223 } 224 } 225 } 226 227 void jFileChooser_propertyChange(PropertyChangeEvent e) { 228 if(JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(e.getPropertyName())){ 229 jOpenButton.setEnabled(false); 230 jEditButton.setEnabled(false); 231 jPrintButton.setEnabled(false); 232 }else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) { 233 selectedFile = jFileChooser.getSelectedFile(); 235 if (selectedFile != null) { 236 jOpenButton.setEnabled(true); 238 if (Desktop.isEditable(selectedFile)) { 240 jEditButton.setEnabled(true); 241 } else { 242 jEditButton.setEnabled(false); 243 } 244 if (Desktop.isPrintable(selectedFile)) { 245 jPrintButton.setEnabled(true); 246 } else { 247 jPrintButton.setEnabled(false); 248 } 249 250 } else { 251 jOpenButton.setEnabled(false); 254 jEditButton.setEnabled(false); 255 jPrintButton.setEnabled(false); 256 } 257 } 258 } 259 260 void jUrlTextField_actionPerformed(ActionEvent e) { 261 String inputUrl = jUrlTextField.getText(); 262 if (inputUrl == null) { 263 return; 264 } 265 266 try { 267 Desktop.browse(new URL (inputUrl)); 268 } catch (MalformedURLException de) { 269 JOptionPane.showMessageDialog(this, "Invalid URL: " + inputUrl, 270 "Exception", JOptionPane.ERROR_MESSAGE); 271 } catch (DesktopException de) { 272 JOptionPane.showMessageDialog(this, de.toString(), "Exception", 273 JOptionPane.ERROR_MESSAGE); 274 } 275 } 276 } 277 278 279 class FileChooser_jOpenButton_actionAdapter implements java.awt.event.ActionListener { 280 FileChooser adaptee; 281 282 FileChooser_jOpenButton_actionAdapter(FileChooser adaptee) { 283 this.adaptee = adaptee; 284 } 285 286 public void actionPerformed(ActionEvent e) { 287 adaptee.jOpenButton_actionPerformed(e); 288 } 289 } 290 291 292 class FileChooser_jBrowseButton_actionAdapter implements java.awt.event.ActionListener { 293 FileChooser adaptee; 294 295 FileChooser_jBrowseButton_actionAdapter(FileChooser adaptee) { 296 this.adaptee = adaptee; 297 } 298 299 public void actionPerformed(ActionEvent e) { 300 adaptee.jBrowseButton_actionPerformed(e); 301 } 302 } 303 304 305 class FileChooser_jEditButton_actionAdapter implements java.awt.event.ActionListener { 306 FileChooser adaptee; 307 308 FileChooser_jEditButton_actionAdapter(FileChooser adaptee) { 309 this.adaptee = adaptee; 310 } 311 312 public void actionPerformed(ActionEvent e) { 313 adaptee.jEditButton_actionPerformed(e); 314 } 315 } 316 317 318 class FileChooser_jPrintButton_actionAdapter implements java.awt.event.ActionListener { 319 FileChooser adaptee; 320 321 FileChooser_jPrintButton_actionAdapter(FileChooser adaptee) { 322 this.adaptee = adaptee; 323 } 324 325 public void actionPerformed(ActionEvent e) { 326 adaptee.jPrintButton_actionPerformed(e); 327 } 328 } 329 330 331 class FileChooser_jCreateMailButton_actionAdapter implements java.awt.event.ActionListener { 332 FileChooser adaptee; 333 334 FileChooser_jCreateMailButton_actionAdapter(FileChooser adaptee) { 335 this.adaptee = adaptee; 336 } 337 338 public void actionPerformed(ActionEvent e) { 339 adaptee.jCreateMailButton_actionPerformed(e); 340 } 341 } 342 343 344 class FileChooser_jFileChooser_propertyChangeAdapter implements java.beans.PropertyChangeListener { 345 FileChooser adaptee; 346 347 FileChooser_jFileChooser_propertyChangeAdapter(FileChooser adaptee) { 348 this.adaptee = adaptee; 349 } 350 351 public void propertyChange(PropertyChangeEvent e) { 352 adaptee.jFileChooser_propertyChange(e); 353 } 354 } 355 356 class FileChooser_jUrlTextField_actionAdapter implements java.awt.event.ActionListener { 357 FileChooser adaptee; 358 359 FileChooser_jUrlTextField_actionAdapter(FileChooser adaptee) { 360 this.adaptee = adaptee; 361 } 362 363 public void actionPerformed(ActionEvent e) { 364 adaptee.jUrlTextField_actionPerformed(e); 365 } 366 } 367 | Popular Tags |