001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.wings.*;
016: import org.wings.border.SEmptyBorder;
017: import org.wings.util.SStringBuilder;
018:
019: import java.awt.*;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ItemEvent;
022: import java.awt.event.ItemListener;
023: import java.io.BufferedReader;
024: import java.io.File;
025: import java.io.FileReader;
026: import java.io.IOException;
027:
028: /**
029: * @author <a href="mailto:engels@mercatis.de">Holger Engels</a>
030: */
031: public class FileChooserExample extends WingSetPane {
032: static final Color WARN_COLOR = new Color(255, 255, 127);
033:
034: /**
035: * the file chooser that gets the files.
036: */
037: SFileChooser chooser;
038:
039: /**
040: * three cards for different content to be previewd: images, text and
041: * unknown.
042: */
043: SCardLayout contentSwitcher;
044:
045: /**
046: * label that shows image content.
047: */
048: SLabel iconLabel;
049:
050: /**
051: * text area to show text content.
052: */
053: STextArea textArea;
054:
055: /**
056: * form, that contains the text-area
057: */
058: SPanel textForm;
059:
060: /**
061: * label for unknown content.
062: */
063: SLabel unknownLabel;
064:
065: /**
066: * remember the previous file to remove it.
067: */
068: File previousFile;
069: private FileChooserControls controls;
070:
071: protected SComponent createControls() {
072: controls = new FileChooserControls();
073: return controls;
074: }
075:
076: public SComponent createExample() {
077: SPanel panel = new SPanel(new SBorderLayout());
078: panel.add(createUpload(), SBorderLayout.WEST);
079: panel.add(createPreview(), SBorderLayout.CENTER);
080: panel.setVerticalAlignment(SConstants.TOP_ALIGN);
081:
082: controls.addControllable(chooser);
083: return panel;
084: }
085:
086: protected String getText(File f) {
087: try {
088: SStringBuilder buffer = new SStringBuilder();
089: BufferedReader reader = new BufferedReader(
090: new FileReader(f));
091:
092: String line = reader.readLine();
093: while (line != null) {
094: buffer.append(line).append("\n");
095: line = reader.readLine();
096: }
097:
098: return buffer.toString();
099: } catch (Exception ex) {
100: return "got exception " + ex.getMessage();
101: }
102: }
103:
104: protected void adaptPreview() {
105: if (previousFile != null) {
106: previousFile.delete();
107: previousFile = null;
108: }
109: try {
110: if (chooser.getFileType().startsWith("text/")) {
111: textArea.setText(getText(chooser.getSelectedFile()));
112: contentSwitcher.show(textForm);
113: } else if (chooser.getFileType().startsWith("image/")) {
114: iconLabel
115: .setIcon(new SFileIcon(chooser
116: .getSelectedFile(), null, chooser
117: .getFileType()));
118: contentSwitcher.show(iconLabel);
119: } else {
120: contentSwitcher.show(unknownLabel);
121: }
122: previousFile = chooser.getSelectedFile();
123: } catch (Exception ex) {
124: contentSwitcher.show(unknownLabel);
125: }
126: }
127:
128: protected SComponent createPreview() {
129: SPanel p = new SPanel(new SFlowDownLayout());
130: p.setVerticalAlignment(TOP);
131:
132: contentSwitcher = new SCardLayout();
133:
134: SPanel contentPane = new SPanel(contentSwitcher);
135:
136: iconLabel = new SLabel();
137:
138: textForm = new SPanel();
139:
140: textArea = new STextArea();
141: textArea.setColumns(50);
142: textArea.setRows(20);
143: textArea.setEditable(false);
144:
145: unknownLabel = new SLabel("Unknown Content");
146: unknownLabel.setBackground(WARN_COLOR);
147:
148: contentPane.add(iconLabel, "ICON");
149:
150: textForm.add(textArea);
151: contentPane.add(textForm, "TEXT");
152:
153: contentPane.add(unknownLabel, "UNKNOWN");
154:
155: contentSwitcher.show(unknownLabel);
156:
157: p.setBorder(new SEmptyBorder(20, 80, 0, 0));
158: p.add(contentPane);
159:
160: return p;
161: }
162:
163: protected SComponent createUpload() {
164: SPanel form = new SPanel(new SFlowDownLayout());
165: form.setBorder(new SEmptyBorder(20, 20, 0, 0));
166:
167: chooser = new SFileChooser();
168: chooser.setColumns(25);
169: form.add(chooser);
170:
171: SButton submit = new SButton("Upload");
172: submit.setVerticalAlignment(SConstants.RIGHT_ALIGN);
173: form.add(new SSpacer(0, 10));
174: form.add(submit);
175:
176: SPanel p = new SPanel(new SGridLayout(0, 2, 10, 5));
177: p.add(new SLabel("Message:"));
178: final SLabel message = new SLabel("");
179: p.add(message);
180:
181: p.add(new SLabel("Filename:"));
182: final SLabel filename = new SLabel("");
183: p.add(filename);
184:
185: p.add(new SLabel("Fileid:"));
186: final SLabel fileid = new SLabel("");
187: p.add(fileid);
188:
189: p.add(new SLabel("Filetype:"));
190: final SLabel filetype = new SLabel("");
191: p.add(filetype);
192:
193: p.add(new SLabel("Size:"));
194: final SLabel size = new SLabel("");
195: p.add(size);
196:
197: form.add(new SSpacer(0, 30));
198: form.add(p);
199:
200: submit.addActionListener(new java.awt.event.ActionListener() {
201: public void actionPerformed(ActionEvent e) {
202: try {
203: if (chooser.getSelectedFile() != null) {
204: message.setText("OK");
205: message.setBackground(null);
206: filename.setText(chooser.getFileName());
207: fileid.setText(chooser.getFileId());
208: filetype.setText(chooser.getFileType());
209: size.setText(""
210: + chooser.getSelectedFile().length());
211: adaptPreview();
212: } else {
213: message.setText("No file chosen");
214: message.setBackground(WARN_COLOR);
215: }
216: } catch (IOException ex) {
217: message.setText(ex.getMessage());
218: message.setBackground(WARN_COLOR);
219: filename.setText("");
220: fileid.setText("");
221: filetype.setText("");
222: size.setText("");
223: contentSwitcher.show(unknownLabel);
224: }
225: chooser.reset();
226: }
227: });
228:
229: form.setVerticalAlignment(TOP);
230:
231: return form;
232: }
233:
234: class FileChooserControls extends ComponentControls {
235: public FileChooserControls() {
236: //showAsFormComponentCheckBox.setVisible(false);
237: globalControls.setVisible(false);
238:
239: Object[] values = { new Integer(1), new Integer(2),
240: new Integer(4), new Integer(8), new Integer(16),
241: new Integer(32), new Integer(64), new Integer(128),
242: new Integer(256) };
243:
244: final SComboBox comboBox = new SComboBox(values);
245: comboBox.addItemListener(new ItemListener() {
246: public void itemStateChanged(ItemEvent e) {
247: getSession().setMaxContentLength(
248: ((Integer) comboBox.getSelectedItem())
249: .intValue());
250: }
251: });
252: comboBox.setSelectedItem(new Integer(getSession()
253: .getMaxContentLength()));
254:
255: addControl(new SLabel(" Maximum Content Length [kB]"));
256: addControl(comboBox);
257: }
258: }
259: }
|