KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > FileChooserDemo


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * -Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)FileChooserDemo.java 1.13 03/01/23
38  */

39 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.text.*;
44 import swingwtx.swing.border.*;
45 import swingwtx.swing.colorchooser.*;
46 import swingwtx.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import swingwt.awt.*;
50 import swingwt.awt.event.*;
51 import java.beans.*;
52 import java.util.*;
53 import java.io.*;
54 import java.net.*;
55
56 /**
57  * JFileChooserDemo
58  *
59  * @version 1.1 07/16/99
60  * @author Jeff Dinkins
61  */

62 public class FileChooserDemo extends DemoModule {
63     JLabel theImage;
64     Icon jpgIcon;
65     Icon gifIcon;
66
67     /**
68      * main method allows us to run as a standalone demo.
69      */

70     public static void main(String JavaDoc[] args) {
71     FileChooserDemo demo = new FileChooserDemo(null);
72     demo.mainImpl();
73     }
74
75     /**
76      * FileChooserDemo Constructor
77      */

78     public FileChooserDemo(SwingSet2 swingset) {
79     // Set the title for this demo, and an icon used to represent this
80
// demo inside the SwingSet2 app.
81
super(swingset, "FileChooserDemo", "toolbar/JFileChooser.gif");
82     createFileChooserDemo();
83     }
84
85     public void createFileChooserDemo() {
86     theImage = new JLabel("");
87     jpgIcon = createImageIcon("filechooser/jpgIcon.jpg", "jpg image");
88     gifIcon = createImageIcon("filechooser/gifIcon.gif", "gif image");
89
90     JPanel demoPanel = getDemoPanel();
91     demoPanel.setLayout(new BoxLayout(demoPanel, BoxLayout.Y_AXIS));
92
93     JPanel innerPanel = new JPanel();
94     innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
95
96     demoPanel.add(Box.createRigidArea(VGAP20));
97     demoPanel.add(innerPanel);
98     demoPanel.add(Box.createRigidArea(VGAP20));
99
100     innerPanel.add(Box.createRigidArea(HGAP20));
101
102     // Create a panel to hold buttons
103
JPanel buttonPanel = new JPanel() {
104         public Dimension getMaximumSize() {
105         return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
106         }
107     };
108     buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
109
110     buttonPanel.add(Box.createRigidArea(VGAP15));
111     buttonPanel.add(createPlainFileChooserButton());
112     buttonPanel.add(Box.createRigidArea(VGAP15));
113     buttonPanel.add(createPreviewFileChooserButton());
114     buttonPanel.add(Box.createRigidArea(VGAP15));
115     buttonPanel.add(createCustomFileChooserButton());
116     buttonPanel.add(Box.createVerticalGlue());
117
118     // Create a panel to hold the image
119
JPanel imagePanel = new JPanel();
120     imagePanel.setLayout(new BorderLayout());
121     imagePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
122     JScrollPane scroller = new JScrollPane(theImage);
123         scroller.getVerticalScrollBar().setUnitIncrement(10);
124         scroller.getHorizontalScrollBar().setUnitIncrement(10);
125     imagePanel.add(scroller, BorderLayout.CENTER);
126
127     // add buttons and image panels to inner panel
128
innerPanel.add(buttonPanel);
129     innerPanel.add(Box.createRigidArea(HGAP30));
130     innerPanel.add(imagePanel);
131     innerPanel.add(Box.createRigidArea(HGAP20));
132     }
133
134     public JFileChooser createFileChooser() {
135     // create a filechooser
136
JFileChooser fc = new JFileChooser();
137     
138     // set the current directory to be the images directory
139
File swingFile = new File("resources/images/About.jpg");
140     if(swingFile.exists()) {
141         fc.setCurrentDirectory(swingFile);
142         fc.setSelectedFile(swingFile);
143     }
144     
145     return fc;
146     }
147
148
149     public JButton createPlainFileChooserButton() {
150     Action a = new AbstractAction(getString("FileChooserDemo.plainbutton")) {
151         public void actionPerformed(ActionEvent e) {
152         JFileChooser fc = createFileChooser();
153
154         // show the filechooser
155
int result = fc.showOpenDialog(getDemoPanel());
156                     // if we selected an image, load the image
157
if(result == JFileChooser.APPROVE_OPTION) {
158                         loadImage(fc.getSelectedFile().getPath());
159                     }
160         
161
162         }
163     };
164     return createButton(a);
165     }
166
167     public JButton createPreviewFileChooserButton() {
168     Action a = new AbstractAction(getString("FileChooserDemo.previewbutton")) {
169         public void actionPerformed(ActionEvent e) {
170         JFileChooser fc = createFileChooser();
171
172         // Add filefilter & fileview
173
ExampleFileFilter filter = new ExampleFileFilter(
174             new String JavaDoc[] {"jpg", "gif"}, getString("FileChooserDemo.filterdescription")
175         );
176         ExampleFileView fileView = new ExampleFileView();
177         fileView.putIcon("jpg", jpgIcon);
178         fileView.putIcon("gif", gifIcon);
179         //fc.setFileView(fileView);
180
fc.addChoosableFileFilter(filter);
181         fc.setFileFilter(filter);
182         
183         // add preview accessory
184
fc.setAccessory(new FilePreviewer(fc));
185
186         // show the filechooser
187
int result = fc.showOpenDialog(getDemoPanel());
188                     // if we selected an image, load the image
189
if(result == JFileChooser.APPROVE_OPTION) {
190                         loadImage(fc.getSelectedFile().getPath());
191                     }
192         
193
194         }
195     };
196     return createButton(a);
197     }
198
199     JDialog dialog;
200     JFileChooser fc;
201
202     public JButton createCustomFileChooserButton() {
203     Action a = new AbstractAction(getString("FileChooserDemo.custombutton")) {
204         public void actionPerformed(ActionEvent e) {
205         fc = createFileChooser();
206
207         // Add filefilter & fileview
208
ExampleFileFilter filter = new ExampleFileFilter(
209             new String JavaDoc[] {"jpg", "gif"}, getString("FileChooserDemo.filterdescription")
210         );
211         ExampleFileView fileView = new ExampleFileView();
212         fileView.putIcon("jpg", jpgIcon);
213         fileView.putIcon("gif", gifIcon);
214         //fc.setFileView(fileView);
215
fc.addChoosableFileFilter(filter);
216
217         // add preview accessory
218
fc.setAccessory(new FilePreviewer(fc));
219
220         // remove the approve/cancel buttons
221
fc.setControlButtonsAreShown(false);
222
223         // make custom controls
224
//wokka
225
JPanel custom = new JPanel();
226         custom.setLayout(new BorderLayout());
227         JLabel description = new JLabel(getString("FileChooserDemo.description"));
228         description.setAlignmentX(JLabel.CENTER_ALIGNMENT);
229         custom.add(description, BorderLayout.NORTH);
230         custom.add(fc, BorderLayout.CENTER);
231
232         Action okAction = createOKAction();
233         fc.addActionListener(okAction);
234
235         JPanel buttons = new JPanel();
236         buttons.setLayout(new FlowLayout());
237         buttons.add(Box.createRigidArea(HGAP10));
238         buttons.add(createImageButton(createFindAction()));
239         buttons.add(Box.createRigidArea(HGAP10));
240         buttons.add(createButton(createAboutAction()));
241         buttons.add(Box.createRigidArea(HGAP10));
242         buttons.add(createButton(okAction));
243         buttons.add(Box.createRigidArea(HGAP10));
244         buttons.add(createButton(createCancelAction()));
245         buttons.add(Box.createRigidArea(HGAP10));
246         buttons.add(createImageButton(createHelpAction()));
247         buttons.add(Box.createRigidArea(HGAP10));
248
249         custom.add(buttons, BorderLayout.SOUTH);
250         
251         Frame parent = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, getDemoPanel());
252         dialog = new JDialog(parent, getString("FileChooserDemo.dialogtitle"), true);
253                 dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
254         dialog.getContentPane().add(custom, BorderLayout.CENTER);
255         dialog.setSize(640, 480);
256         dialog.setLocationRelativeTo(getDemoPanel());
257         dialog.show();
258         }
259     };
260     return createButton(a);
261     }
262
263     public Action createAboutAction() {
264     return new AbstractAction(getString("FileChooserDemo.about")) {
265         public void actionPerformed(ActionEvent e) {
266         File file = fc.getSelectedFile();
267         String JavaDoc text;
268         if(file == null) {
269             text = getString("FileChooserDemo.nofileselected");
270         } else {
271             text = "<html>" + getString("FileChooserDemo.thefile");
272             text += "<br><font color=green>" + file.getName() + "</font><br>";
273             text += getString("FileChooserDemo.isprobably") + "</html>";
274         }
275         JOptionPane.showMessageDialog(getDemoPanel(), text);
276         }
277     };
278     }
279
280     public Action createOKAction() {
281     return new AbstractAction(getString("FileChooserDemo.ok")) {
282         public void actionPerformed(ActionEvent e) {
283         dialog.dispose();
284         if (!e.getActionCommand().equals(JFileChooser.CANCEL_SELECTION)
285             && fc.getSelectedFile() != null) {
286
287             loadImage(fc.getSelectedFile().getPath());
288         }
289         }
290     };
291     }
292
293     public Action createCancelAction() {
294     return new AbstractAction(getString("FileChooserDemo.cancel")) {
295         public void actionPerformed(ActionEvent e) {
296         dialog.dispose();
297         }
298     };
299     }
300
301     public Action createFindAction() {
302     Icon icon = createImageIcon("filechooser/find.gif", getString("FileChooserDemo.find"));
303     return new AbstractAction("", icon) {
304         public void actionPerformed(ActionEvent e) {
305                 String JavaDoc result = JOptionPane.showInputDialog(getDemoPanel(), getString("FileChooserDemo.findquestion"));
306         if (result != null) {
307             JOptionPane.showMessageDialog(getDemoPanel(), getString("FileChooserDemo.findresponse"));
308         }
309         }
310     };
311     }
312
313     public Action createHelpAction() {
314     Icon icon = createImageIcon("filechooser/help.gif", getString("FileChooserDemo.help"));
315     return new AbstractAction("", icon) {
316         public void actionPerformed(ActionEvent e) {
317         JOptionPane.showMessageDialog(getDemoPanel(), getString("FileChooserDemo.helptext"));
318         }
319     };
320     }
321
322
323     public void loadImage(String JavaDoc filename) {
324     theImage.setIcon(new ImageIcon(filename));
325     }
326
327     public JButton createButton(Action a) {
328     JButton b = new JButton(a) {
329         public Dimension getMaximumSize() {
330         int width = Short.MAX_VALUE;
331         int height = super.getMaximumSize().height;
332         return new Dimension(width, height);
333         }
334     };
335     return b;
336     }
337
338     public JButton createImageButton(Action a) {
339     JButton b = new JButton(a);
340     b.setMargin(new Insets(0,0,0,0));
341     return b;
342     }
343 }
344
345 class FilePreviewer extends JLabel implements PropertyChangeListener {
346     
347     public FilePreviewer(JFileChooser fc) {
348     setPreferredSize(new Dimension(100, 50));
349     fc.addPropertyChangeListener(this);
350     setBorder(new BevelBorder(BevelBorder.LOWERED));
351     }
352     
353     public void loadImage(File f) {
354         if (f == null) {
355             setIcon(null);
356         } else {
357         ImageIcon tmpIcon = new ImageIcon(f.getPath());
358         if(tmpIcon.getIconWidth() > 90) {
359         setIcon(new ImageIcon(
360             tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT)));
361         } else {
362         setIcon(tmpIcon);
363         }
364     }
365     }
366     
367     public void propertyChange(PropertyChangeEvent e) {
368     String JavaDoc prop = e.getPropertyName();
369     if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
370         if(isShowing()) {
371                 loadImage((File) e.getNewValue());
372         repaint();
373         }
374     }
375     }
376 }
377
378
Popular Tags