KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > FileChooserExample


1 /*
2  * $Id: FileChooserExample.java,v 1.4 2005/01/04 15:26:46 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.*;
17 import org.wings.border.SEmptyBorder;
18
19 import java.awt.*;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.awt.event.ItemEvent JavaDoc;
23 import java.awt.event.ItemListener JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:engels@mercatis.de">Holger Engels</a>
31  * @version $Revision: 1.4 $
32  */

33 public class FileChooserExample
34         extends WingSetPane
35 {
36     final static Color WARN_COLOR = new Color(255, 255, 127);
37
38     /**
39      * the file chooser that gets the files.
40      */

41     SFileChooser chooser;
42
43     /**
44      * three cards for different content to be previewd: images, text and
45      * unknown.
46      */

47     SCardLayout contentSwitcher;
48
49     /**
50      * label that shows image content.
51      */

52     SLabel iconLabel;
53
54     /**
55      * text area to show text content.
56      */

57     STextArea textArea;
58
59     /**
60      * form, that contains the text-area
61      */

62     SPanel textForm;
63
64     /**
65      * label for unknown content.
66      */

67     SLabel unknownLabel;
68
69     /**
70      * remember the previous file to remove it.
71      */

72     File JavaDoc previousFile;
73     private FileChooserControls controls;
74
75     public SComponent createExample() {
76         controls = new FileChooserControls();
77
78         SForm p = new SForm(new SBorderLayout());
79         p.setEncodingType("multipart/form-data");
80
81         p.add(controls, SBorderLayout.NORTH);
82         p.add(createUpload(), SBorderLayout.WEST);
83         p.add(createPreview(), SBorderLayout.CENTER);
84
85         controls.addSizable(chooser);
86         return p;
87     }
88
89     protected String JavaDoc getText(File JavaDoc f) {
90         try {
91             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
92             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
93
94             String JavaDoc line = reader.readLine();
95             while (line != null) {
96                 buffer.append(line).append("\n");
97                 line = reader.readLine();
98             }
99
100             return buffer.toString();
101         } catch (Exception JavaDoc ex) {
102             return "got exception " + ex.getMessage();
103         }
104     }
105
106     protected void adaptPreview() {
107         if (previousFile != null) {
108             previousFile.delete();
109             previousFile = null;
110         }
111         try {
112             if (chooser.getFileType().startsWith("text/")) {
113                 textArea.setText(getText(chooser.getFile()));
114                 contentSwitcher.show(textForm);
115             } else if (chooser.getFileType().startsWith("image/")) {
116                 iconLabel.setIcon(new SFileIcon(chooser.getFile(), null,
117                         chooser.getFileType()));
118                 contentSwitcher.show(iconLabel);
119             } else {
120                 contentSwitcher.show(unknownLabel);
121             }
122             previousFile = chooser.getFile();
123         } catch (Exception JavaDoc ex) {
124             contentSwitcher.show(unknownLabel);
125         }
126     }
127
128
129     protected SComponent createPreview() {
130         SPanel p = new SPanel(new SFlowDownLayout());
131         p.setVerticalAlignment(TOP);
132
133         SLabel previewLabel = new SLabel("Preview");
134         previewLabel.setBorder(new SEmptyBorder(0, 20, 0, 0));
135         p.add(previewLabel);
136
137         contentSwitcher = new SCardLayout();
138
139         SPanel contentPane = new SPanel(contentSwitcher);
140
141         iconLabel = new SLabel();
142
143         textForm = new SPanel();
144
145         textArea = new STextArea();
146         textArea.setColumns(50);
147         textArea.setRows(20);
148         textArea.setEditable(false);
149
150         unknownLabel = new SLabel("Unknown Content");
151         unknownLabel.setBackground(WARN_COLOR);
152
153         contentPane.add(iconLabel, "ICON");
154
155         textForm.add(textArea);
156         contentPane.add(textForm, "TEXT");
157
158         contentPane.add(unknownLabel, "UNKNOWN");
159
160         contentSwitcher.show(unknownLabel);
161
162         contentPane.setBorder(new SEmptyBorder(10, 20, 0, 0));
163         p.add(contentPane);
164         return p;
165     }
166
167     protected SComponent createUpload() {
168         SPanel form = new SPanel(new SFlowDownLayout());
169         form.setBorder(new SEmptyBorder(10, 5, 0, 0));
170
171         chooser = new SFileChooser();
172         form.add(chooser);
173
174         SButton submit = new SButton("upload");
175         submit.setVerticalAlignment(RIGHT);
176         form.add(submit);
177
178         SPanel p = new SPanel(new SGridLayout(2));
179         p.setBorder(new SEmptyBorder(10, 5, 0, 0));
180         p.add(new SLabel("message:"));
181         final SLabel message = new SLabel("");
182         p.add(message);
183
184         p.add(new SLabel("filename:"));
185         final SLabel filename = new SLabel("");
186         p.add(filename);
187
188         p.add(new SLabel("fileid:"));
189         final SLabel fileid = new SLabel("");
190         p.add(fileid);
191
192         p.add(new SLabel("filetype:"));
193         final SLabel filetype = new SLabel("");
194         p.add(filetype);
195
196         p.add(new SLabel("size:"));
197         final SLabel size = new SLabel("");
198         p.add(size);
199
200         form.add(p);
201
202
203         submit.addActionListener(new ActionListener JavaDoc() {
204             public void actionPerformed(ActionEvent JavaDoc e) {
205                 try {
206                     if (chooser.getFile() != null) {
207                         message.setText("OK");
208                         message.setBackground(null);
209                         filename.setText(chooser.getFileName());
210                         fileid.setText(chooser.getFileId());
211                         filetype.setText(chooser.getFileType());
212                         size.setText("" + chooser.getFile().length());
213                         adaptPreview();
214                     } else {
215                         message.setText("No file chosen");
216                         message.setBackground(WARN_COLOR);
217                     }
218                 } catch (IOException JavaDoc ex) {
219                     message.setText(ex.getMessage());
220                     message.setBackground(WARN_COLOR);
221                     filename.setText("");
222                     fileid.setText("");
223                     filetype.setText("");
224                     size.setText("");
225                     contentSwitcher.show(unknownLabel);
226                 }
227                 chooser.reset();
228             }
229         });
230
231         form.setVerticalAlignment(TOP);
232
233         return form;
234     }
235
236     class FileChooserControls extends ComponentControls {
237         public FileChooserControls() {
238             Object JavaDoc[] values = {new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(4),
239                                new Integer JavaDoc(8), new Integer JavaDoc(16), new Integer JavaDoc(32),
240                                new Integer JavaDoc(64)};
241
242             final SComboBox comboBox = new SComboBox(values);
243             comboBox.addItemListener(new ItemListener JavaDoc() {
244                 public void itemStateChanged(ItemEvent JavaDoc e) {
245                     getSession().setMaxContentLength(((Integer JavaDoc) comboBox.getSelectedItem()).intValue());
246                 }
247             });
248             comboBox.setSelectedItem(new Integer JavaDoc(getSession().getMaxContentLength()));
249
250             add(new SLabel("Maximum Content Length [kB]: "));
251             add(comboBox);
252         }
253     }
254 }
255
Popular Tags