1 21 package org.lobobrowser.html.renderer; 22 23 import java.awt.event.ActionEvent ; 24 25 import org.lobobrowser.html.domimpl.*; 26 27 import javax.swing.*; 28 import java.io.*; 29 30 public class InputFileControl extends BaseInputControl { 31 private final JTextField textField = new JTextField(); 32 private final JButton browseButton = new JButton(); 33 34 public InputFileControl(HTMLBaseInputElement modelNode) { 35 super(modelNode); 36 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 37 JButton browseButton = this.browseButton; 38 browseButton.setAction(new BrowseAction()); 39 browseButton.setText("Browse"); 40 java.awt.Dimension ps = this.textField.getPreferredSize(); 41 this.textField.setPreferredSize(new java.awt.Dimension (128, ps.height)); 42 this.textField.setEditable(false); 43 this.add(this.textField); 44 this.add(Box.createHorizontalStrut(4)); 45 this.add(browseButton); 46 } 47 48 public String getValue() { 49 return this.textField.getText(); 52 } 53 54 public void setDisabled(boolean disabled) { 55 this.browseButton.setEnabled(!disabled); 56 } 57 58 public void setValue(String value) { 59 } 61 62 private File fileValue; 63 64 private void setFileValue(File file) { 65 this.fileValue = file; 66 if(file == null) { 67 this.textField.setText(""); 68 } 69 else { 70 this.textField.setText(file.getAbsolutePath()); 71 } 72 } 73 74 public File getFileValue() { 75 return this.fileValue; 76 } 77 78 public void resetInput() { 79 this.setFileValue(null); 80 } 81 82 private class BrowseAction extends AbstractAction { 83 public void actionPerformed(ActionEvent e) { 84 JFileChooser chooser = new JFileChooser(); 85 if(chooser.showOpenDialog(InputFileControl.this) == JFileChooser.APPROVE_OPTION) { 86 setFileValue(chooser.getSelectedFile()); 87 } 88 else { 89 setFileValue(null); 90 } 91 } 92 } 93 } 94 | Popular Tags |