KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FileChooser


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20  
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import java.beans.*;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 import org.jdesktop.jdic.desktop.*;
33
34
35 /**
36  * JDIC API demo main class.
37  * <p>
38  * Class <code>FileChooser</code> creates a UI interface demonstrating the
39  * usage of the public API of <code>org.jdesktop.jdic.desktop.*</code> classes.
40  */

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     // The two spaces(" ") seperate the url label and the text field.
56
JLabel jUrlLabel = new JLabel("URL: ");
57     JTextField jUrlTextField = new JTextField();
58   
59     File JavaDoc selectedFile;
60   
61     public FileChooser() {
62         try {
63             jbInit();
64         } catch (Exception JavaDoc e) {
65             e.printStackTrace();
66         }
67     }
68
69     public static void main(String JavaDoc[] args) {
70         try {
71             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
72         } catch (Exception JavaDoc 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 JavaDoc {
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         // Here, it aligns the top of jFileButtonPanel with the top of the file list
114
// box in the jFileChooser.
115
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(new JSeparator());
120
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         // JSeparator jSeparator = new JSeparator();
138
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(jSeparator, BorderLayout.CENTER);
145
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 JavaDoc inputUrl = jUrlTextField.getText();
159         if (inputUrl == null) {
160             return;
161         }
162
163         try {
164             Desktop.browse(new URL JavaDoc(inputUrl));
165         } catch (MalformedURLException JavaDoc 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             // No selected file as attachment, just launch the system mailer.
195
try {
196                 Desktop.mail();
197             } catch (DesktopException de) {
198                 JOptionPane.showMessageDialog(this, de.toString(), "Exception",
199                         JOptionPane.ERROR_MESSAGE);
200             }
201         } else {
202             // Has selected file as attachment, construct a message specifing the
203
// attachment and launch the system mailer.
204

205             Message msg = new Message();
206
207             List JavaDoc attachList = new ArrayList JavaDoc();
208
209             attachList.add(selectedFile.toString());
210             // attachList.add(selectedFile);
211
try {
212                 msg.setAttachments(attachList);
213             } catch (IOException JavaDoc 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             // The selected file should always be the same as newFile
234
selectedFile = jFileChooser.getSelectedFile();
235             if (selectedFile != null) {
236                 // A file is selected, enable button "Open".
237
jOpenButton.setEnabled(true);
238                 // To check that whether "Edit" and "Print" buttons should be enabled.
239
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                 // A directory is selected, then disable buttons
252
// "Open", "Edit" and "Print".
253
jOpenButton.setEnabled(false);
254                 jEditButton.setEnabled(false);
255                 jPrintButton.setEnabled(false);
256             }
257         }
258     }
259     
260     void jUrlTextField_actionPerformed(ActionEvent e) {
261         String JavaDoc inputUrl = jUrlTextField.getText();
262         if (inputUrl == null) {
263             return;
264         }
265
266         try {
267             Desktop.browse(new URL JavaDoc(inputUrl));
268         } catch (MalformedURLException JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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