1 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 ; 21 import java.awt.event.ActionListener ; 22 import java.awt.event.ItemEvent ; 23 import java.awt.event.ItemListener ; 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.FileReader ; 27 import java.io.IOException ; 28 29 33 public class FileChooserExample 34 extends WingSetPane 35 { 36 final static Color WARN_COLOR = new Color(255, 255, 127); 37 38 41 SFileChooser chooser; 42 43 47 SCardLayout contentSwitcher; 48 49 52 SLabel iconLabel; 53 54 57 STextArea textArea; 58 59 62 SPanel textForm; 63 64 67 SLabel unknownLabel; 68 69 72 File 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 getText(File f) { 90 try { 91 StringBuffer buffer = new StringBuffer (); 92 BufferedReader reader = new BufferedReader (new FileReader (f)); 93 94 String 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 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 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 () { 204 public void actionPerformed(ActionEvent 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 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 [] values = {new Integer (1), new Integer (2), new Integer (4), 239 new Integer (8), new Integer (16), new Integer (32), 240 new Integer (64)}; 241 242 final SComboBox comboBox = new SComboBox(values); 243 comboBox.addItemListener(new ItemListener () { 244 public void itemStateChanged(ItemEvent e) { 245 getSession().setMaxContentLength(((Integer ) comboBox.getSelectedItem()).intValue()); 246 } 247 }); 248 comboBox.setSelectedItem(new Integer (getSession().getMaxContentLength())); 249 250 add(new SLabel("Maximum Content Length [kB]: ")); 251 add(comboBox); 252 } 253 } 254 } 255 | Popular Tags |