KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > renderer > InputFileControl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 package org.lobobrowser.html.renderer;
22
23 import java.awt.event.ActionEvent JavaDoc;
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 JavaDoc ps = this.textField.getPreferredSize();
41         this.textField.setPreferredSize(new java.awt.Dimension JavaDoc(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 JavaDoc getValue() {
49         // This is the way browsers behave, even
50
// though this value is not submitted.
51
return this.textField.getText();
52     }
53
54     public void setDisabled(boolean disabled) {
55         this.browseButton.setEnabled(!disabled);
56     }
57
58     public void setValue(String JavaDoc value) {
59         // nop - security
60
}
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 JavaDoc 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