1 14 package org.wings; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.plaf.FileChooserCG; 19 20 import javax.servlet.http.HttpUtils ; 21 import java.io.File ; 22 import java.io.FilterOutputStream ; 23 import java.io.IOException ; 24 import java.util.Hashtable ; 25 26 81 public class SFileChooser 82 extends SComponent 83 implements LowLevelEventListener { 84 private final transient static Log log = LogFactory.getLog(SFileChooser.class); 85 86 89 protected int columns = 16; 90 91 protected String fileNameFilter = null; 92 93 protected Class filter = null; 94 protected String fileDir = null; 95 protected String fileName = null; 96 protected String fileId = null; 97 protected String fileType = null; 98 99 103 protected TempFile currentFile = null; 104 105 109 protected IOException exception = null; 110 111 114 public SFileChooser() { 115 } 116 117 120 protected final SForm getParentForm() { 121 SComponent parent = getParent(); 122 123 while (parent != null && !(parent instanceof SForm)) { 124 parent = parent.getParent(); 125 } 126 127 return (SForm) parent; 128 } 129 130 135 protected final void notifyParentForm() { 136 SForm form = getParentForm(); 137 138 if (form != null) { 139 SForm.addArmedComponent(form); 140 } 141 } 142 143 148 public void setColumns(int c) { 149 int oldColumns = columns; 150 columns = c; 151 if (columns != oldColumns) 152 reload(); 153 } 154 155 160 public int getColumns() { 161 return columns; 162 } 163 164 181 public void setFileNameFilter(String mimeFilter) { 182 fileNameFilter = mimeFilter; 183 } 184 185 192 public String getFileNameFilter() { 193 return fileNameFilter; 194 } 195 196 205 public String getFileName() throws IOException { 206 if (exception != null) 207 throw exception; 208 209 return fileName; 210 } 211 212 223 public String getFileDir() throws IOException { 224 if (exception != null) 225 throw exception; 226 227 return fileDir; 228 } 229 230 242 public String getFileId() throws IOException { 243 if (exception != null) 244 throw exception; 245 246 return fileId; 247 } 248 249 257 public String getFileType() throws IOException { 258 if (exception != null) 259 throw exception; 260 261 return fileType; 262 } 263 264 272 public File getSelectedFile() throws IOException { 273 return getFile(); 274 } 275 276 285 public void reset() { 286 currentFile = null; 287 fileId = null; 288 fileDir = null; 289 fileType = null; 290 fileName = null; 291 exception = null; 292 } 293 294 315 public File getFile() throws IOException { 316 if (exception != null) 317 throw exception; 318 319 return currentFile; 320 } 321 322 330 public void setUploadFilter(Class filter) { 331 if (!FilterOutputStream .class.isAssignableFrom(filter)) 332 throw new IllegalArgumentException (filter.getName() + " is not a FilterOutputStream!"); 333 334 UploadFilterManager.registerFilter(getLowLevelEventId(), filter); 335 this.filter = filter; 336 } 337 338 341 public Class getUploadFilter() { 342 return filter; 343 } 344 345 346 public FilterOutputStream getUploadFilterInstance() { 347 return UploadFilterManager.getFilterInstance(getLowLevelEventId()); 348 } 349 350 public void setCG(FileChooserCG cg) { 351 super.setCG(cg); 352 } 353 354 public void processLowLevelEvent(String action, String [] values) { 356 processKeyEvents(values); 357 358 exception = null; 359 360 String value = values[0]; 361 362 if ("exception".equals(value)) { 363 exception = new IOException (values[1]); 364 365 notifyParentForm(); 366 } else { 367 try { 368 Hashtable params = HttpUtils.parseQueryString(value); 369 String [] arr; 370 arr = (String []) params.get("dir"); 371 this.fileDir = (arr != null) ? arr[0] : null; 372 arr = (String []) params.get("name"); 373 this.fileName = (arr != null) ? arr[0] : null; 374 arr = (String []) params.get("id"); 375 this.fileId = (arr != null) ? arr[0] : null; 376 arr = (String []) params.get("type"); 377 this.fileType = (arr != null) ? arr[0] : null; 378 if (fileDir != null && fileId != null) { 379 currentFile = new TempFile(fileDir, fileId); 380 } 381 } catch (Exception e) { 382 log.fatal(null, e); 383 } 384 } 385 } 386 387 public void fireIntermediateEvents() { 388 } 389 390 391 private boolean epochCheckEnabled = true; 392 393 394 public boolean isEpochCheckEnabled() { 395 return epochCheckEnabled; 396 } 397 398 399 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 400 this.epochCheckEnabled = epochCheckEnabled; 401 } 402 403 408 private static class TempFile extends File { 409 private boolean isTemp; 410 411 public TempFile(String parent, String child) { 412 super(parent, child); 413 deleteOnExit(); 414 isTemp = true; 415 } 416 417 421 public boolean renameTo(File newfile) { 422 boolean success; 423 success = super.renameTo(newfile); 424 isTemp &= !success; return success; 426 } 427 428 431 private void cleanup() { 432 if (isTemp) { 433 delete(); 434 } 435 } 436 437 440 protected void finalize() throws Throwable { 441 super.finalize(); 442 if (isTemp) log.debug("garbage collect file " + getName()); 443 cleanup(); 444 } 445 } 446 } 447 448 449 | Popular Tags |