OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / swing / FileChooserDemo.java
1 /* FileChooserDemo.java -- An example showing file choosers in Swing.
2    Copyright (C) 2005, 2006,  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 */
21
22 package gnu.classpath.examples.swing;
23
24 import java.awt.BorderLayout;
25 import java.awt.GridLayout;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.io.File;
29
30 import javax.swing.BorderFactory;
31 import javax.swing.DefaultListModel;
32 import javax.swing.JButton;
33 import javax.swing.JComponent;
34 import javax.swing.JFileChooser;
35 import javax.swing.JFrame;
36 import javax.swing.JLabel;
37 import javax.swing.JList;
38 import javax.swing.JPanel;
39 import javax.swing.JScrollPane;
40 import javax.swing.SwingUtilities;
41 import javax.swing.filechooser.FileFilter;
42
43 /**
44  * A simple demo showing the {@link JFileChooser} component used in different
45  * ways.
46  */
47 public class FileChooserDemo
48   extends JPanel
49   implements ActionListener 
50 {
51   /**
52    * A file filter for Java source files.
53    */
54   static class JavaFileFilter extends FileFilter
55   {
56     public String getDescription() 
57     {
58       return "Java Source Files (.java)";
59     }
60     public boolean accept(File f)
61     {
62       if (f != null) 
63       {
64         return f.getName().endsWith(".java") || f.isDirectory();
65       }
66       else 
67         return false;
68     }
69   }
70
71   /** A label to display the selected file. */
72   JLabel selectedFileLabel;
73     
74   /** 
75    * A list showing the selected files (where multi selections are 
76    * allowed). 
77    */
78   JList selectedFilesList;
79     
80   /** A label to display the return code for the JFileChooser. */
81   JLabel returnCodeLabel;
82     
83   /**
84    * Creates a new demo instance. 
85    */
86   public FileChooserDemo() 
87   {
88     super();
89     createContent();
90   }
91   
92   /**
93    * When the demo is run independently, the frame is displayed, so we should
94    * initialise the content panel (including the demo content and a close 
95    * button).  But when the demo is run as part of the Swing activity board,
96    * only the demo content panel is used, the frame itself is never displayed,
97    * so we can avoid this step.
98    */
99   void initFrameContent() 
100   {
101     JPanel closePanel = new JPanel();
102     JButton closeButton = new JButton("Close");
103     closeButton.setActionCommand("CLOSE");
104     closeButton.addActionListener(this);
105     closePanel.add(closeButton);
106     add(closePanel, BorderLayout.SOUTH);
107   }
108       
109   /**
110    * Returns a panel with the demo content.  The panel
111    * uses a BorderLayout(), and the BorderLayout.SOUTH area
112    * is empty, to allow callers to add controls to the
113    * bottom of the panel if they want to (a close button is
114    * added if this demo is being run as a standalone demo).
115    */
116   private void createContent()
117   {
118     setLayout(new BorderLayout());
119         
120     // create a panel of buttons to select the different styles of file 
121     // chooser...
122     JPanel buttonPanel = new JPanel(new GridLayout(5, 1));
123     JButton openButton = new JButton("Open...");
124     openButton.setActionCommand("OPEN");
125     openButton.addActionListener(this);
126     buttonPanel.add(openButton);
127     JButton saveButton = new JButton("Save...");
128     saveButton.setActionCommand("SAVE");
129     saveButton.addActionListener(this);
130     buttonPanel.add(saveButton);
131     JButton queryButton = new JButton("Select Directory...");
132     queryButton.setActionCommand("SELECT_DIRECTORY");
133     queryButton.addActionListener(this);
134     buttonPanel.add(queryButton);
135     JButton openJavaButton = new JButton("Open Java file...");
136     openJavaButton.setActionCommand("OPEN_JAVA");
137     openJavaButton.addActionListener(this);
138     buttonPanel.add(openJavaButton);
139     JButton openMultiButton = new JButton("Open multiple files...");
140     openMultiButton.setActionCommand("OPEN_MULTI");
141     openMultiButton.addActionListener(this);
142     buttonPanel.add(openMultiButton);
143     add(buttonPanel, BorderLayout.WEST);
144         
145     // create a panel to display the selected file(s) and the return code
146     JPanel displayPanel = new JPanel(new BorderLayout());
147         
148     selectedFileLabel = new JLabel("-");
149     selectedFileLabel.setBorder(BorderFactory.createTitledBorder("Selected File/Directory: "));
150     displayPanel.add(selectedFileLabel, BorderLayout.NORTH);
151         
152     selectedFilesList = new JList();
153     JScrollPane sp = new JScrollPane(selectedFilesList);
154     sp.setBorder(BorderFactory.createTitledBorder("Selected Files: "));
155     displayPanel.add(sp);
156         
157     returnCodeLabel = new JLabel("0");
158     returnCodeLabel.setBorder(BorderFactory.createTitledBorder("Return Code:"));
159     displayPanel.add(returnCodeLabel, BorderLayout.SOUTH);
160         
161     add(displayPanel);
162   }
163     
164   /**
165    * When the user clicks on a button, launch the appropriate file chooser
166    * and report the results.
167    * 
168    * @param e  the event.
169    */
170   public void actionPerformed(ActionEvent e)
171   {
172     int option = 0;
173     File selectedFile = null;
174     File[] selectedFiles = new File[0];
175     
176     if (e.getActionCommand().equals("CLOSE"))
177     {
178       System.exit(0);
179     }
180     else if (e.getActionCommand().equals("OPEN"))
181       {
182         JFileChooser chooser = new JFileChooser();
183         option = chooser.showOpenDialog(this);
184         selectedFile = chooser.getSelectedFile();
185         selectedFiles = chooser.getSelectedFiles();
186       }
187     else if (e.getActionCommand().equals("OPEN_MULTI"))
188       {
189         JFileChooser chooser = new JFileChooser();
190         chooser.setMultiSelectionEnabled(true);
191         option = chooser.showOpenDialog(this);
192         selectedFile = chooser.getSelectedFile();
193         selectedFiles = chooser.getSelectedFiles();
194       }
195     else if (e.getActionCommand().equals("OPEN_JAVA"))
196       {
197         JFileChooser chooser = new JFileChooser();
198         chooser.setAcceptAllFileFilterUsed(false);
199         chooser.setFileFilter(new JavaFileFilter());
200         option = chooser.showOpenDialog(this);
201         selectedFile = chooser.getSelectedFile();
202         selectedFiles = chooser.getSelectedFiles();
203       }
204     else if (e.getActionCommand().equals("SAVE"))
205       {
206         JFileChooser chooser = new JFileChooser();
207         option = chooser.showSaveDialog(this);
208         selectedFile = chooser.getSelectedFile();
209         selectedFiles = chooser.getSelectedFiles();
210       }
211     else if (e.getActionCommand().equals("SELECT_DIRECTORY"))
212       {
213         JFileChooser chooser = new JFileChooser();
214         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
215         option = chooser.showDialog(this, "Select");
216         selectedFile = chooser.getSelectedFile();
217         selectedFiles = chooser.getSelectedFiles();
218       }
219      
220     // display the selection and return code
221     if (selectedFile != null)
222       selectedFileLabel.setText(selectedFile.toString());
223     else
224       selectedFileLabel.setText("null");
225     DefaultListModel listModel = new DefaultListModel();
226     for (int i = 0; i < selectedFiles.length; i++)
227       listModel.addElement(selectedFiles[i]);
228     selectedFilesList.setModel(listModel);
229     returnCodeLabel.setText(Integer.toString(option));
230   }
231     
232   public static void main(String[] args) 
233   {
234     SwingUtilities.invokeLater
235     (new Runnable()
236      {
237        public void run()
238        {
239          FileChooserDemo app = new FileChooserDemo();
240          app.initFrameContent();
241          JFrame frame = new JFrame("FileChooser Demo");
242          frame.getContentPane().add(app);
243          frame.pack();
244          frame.setVisible(true);
245        }
246      });
247   }
248
249   /**
250    * Returns a DemoFactory that creates a FileChooserDemo.
251    *
252    * @return a DemoFactory that creates a FileChooserDemo
253    */
254   public static DemoFactory createDemoFactory()
255   {
256     return new DemoFactory()
257     {
258       public JComponent createDemo()
259       {
260         return new FileChooserDemo();
261       }
262     };
263   }
264 }