KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > awt > FilenameTextField


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.awt;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.Image JavaDoc;
27 import java.awt.Panel JavaDoc;
28 import java.awt.TextField JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.io.File JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import com.sshtools.ui.FileFilter;
36 import com.sshtools.ui.FileSelect;
37 import com.sshtools.ui.awt.options.Option;
38 import com.sshtools.ui.awt.options.OptionDialog;
39
40 /**
41  * @author brett
42  */

43 public class FilenameTextField extends Panel JavaDoc {
44
45     private TextField JavaDoc textField;
46     private ImageButton chooserButton;
47     private BorderPanel borderPanel;
48     private Vector JavaDoc listeners;
49     private FileSelect chooser;
50
51     public FilenameTextField(int columns) {
52         this("", columns); //$NON-NLS-1$
53
}
54
55     public FilenameTextField(String JavaDoc text) {
56         this(text, (text != null) ? text.length() : 0);
57     }
58
59     public FilenameTextField(String JavaDoc text, int columns) {
60         this(text, columns, null);
61     }
62
63     public FilenameTextField(String JavaDoc text, int columns, Image JavaDoc chooserButtonImage) {
64         this(text, columns, chooserButtonImage, Messages.getString("FilenameTextField.selectAFile")); //$NON-NLS-1$
65
}
66
67     public FilenameTextField(String JavaDoc text, int columns, Image JavaDoc chooserButtonImage, String JavaDoc chooserToolTipText) {
68         super(new BorderLayout JavaDoc(4, 1));
69         chooserButton = new ImageButton(chooserButtonImage, text, Messages.getString("FilenameTextField.browse")); //$NON-NLS-1$
70
chooserButton.setToolTipText(chooserToolTipText);
71         chooserButton.setHorizontalAlignment(ImageButton.CENTER_ALIGNMENT);
72         chooserButton.addActionListener(new ActionListener JavaDoc() {
73             public void actionPerformed(ActionEvent JavaDoc evt) {
74                 showChooser();
75             }
76         });
77         textField = new TextField JavaDoc(text, columns) {
78             public Dimension JavaDoc getPreferredSize() {
79                 return new Dimension JavaDoc(super.getPreferredSize().width, chooserButton.getPreferredSize().height);
80             }
81
82             public Dimension JavaDoc getMinimumSize() {
83                 return new Dimension JavaDoc(super.getMinimumSize().width, chooserButton.getMinimumSize().height);
84             }
85         };
86         textField.setFont(new Font JavaDoc("Arial", Font.PLAIN, 12)); //$NON-NLS-1$
87

88         textField.addActionListener(new ActionListener JavaDoc() {
89             public void actionPerformed(ActionEvent JavaDoc e) {
90                 fireActionEvent(e);
91             }
92         });
93         chooserButton.setHoverButton(true);
94         add(textField, BorderLayout.CENTER);
95         add(chooserButton, BorderLayout.EAST);
96     }
97     
98     public void setEnabled(boolean enabled) {
99         textField.setEnabled(enabled);
100         chooserButton.setEnabled(enabled);
101         super.setEnabled(enabled);
102     }
103     
104     public void addFileFilter(FileFilter filter) {
105         createChooser();
106         chooser.addFileFilter(filter);
107     }
108     
109     private void fireActionEvent(ActionEvent JavaDoc evt) {
110         for (Enumeration JavaDoc en = listeners.elements(); en.hasMoreElements();) {
111             ActionListener JavaDoc l = (ActionListener JavaDoc)en.nextElement();
112             l.actionPerformed(evt);
113         }
114     }
115     
116     private void createChooser() {
117         if(chooser == null) {
118             File JavaDoc cwd = getText() == null ? null : new File JavaDoc(getText());
119             if(cwd == null || !cwd.exists()) {
120                 cwd = new File JavaDoc(System.getProperty("user.home")); //$NON-NLS-1$
121
}
122             chooser = new FileSelect(FileSelect.FILES_AND_DIRECTORIES,
123                             cwd, true, true);
124         }
125     }
126     
127     private void showChooser() {
128         createChooser();
129         File JavaDoc cwd = new File JavaDoc(getText());
130         if(!cwd.exists()) {
131             cwd = new File JavaDoc(System.getProperty("user.home")); //$NON-NLS-1$
132
}
133         chooser.setWorkingDirectory(cwd);
134         Option option = chooser.showDialog(this, Messages.getString("FilenameTextField.selectAFile")); //$NON-NLS-1$
135
if(option == OptionDialog.CHOICE_OK) {
136             setText(chooser.getSelectedFile().getAbsolutePath());
137         }
138         textField.requestFocus();
139     }
140     
141     public void setText(String JavaDoc text) {
142         textField.setText(text);
143     }
144
145     /**
146      * @param l
147      */

148     public synchronized void addActionListener(ActionListener JavaDoc l) {
149         if (listeners == null) {
150             listeners = new Vector JavaDoc();
151         }
152         listeners.addElement(l);
153     }
154
155     /**
156      * @return
157      */

158     public String JavaDoc getText() {
159         return textField.getText();
160     }
161
162     /**
163      * @param textFieldBackgroundColor
164      */

165     public void setTextFieldBackground(Color JavaDoc textFieldBackgroundColor) {
166         textField.setBackground(textFieldBackgroundColor);
167     }
168
169     /**
170      * @param filter
171      */

172     public void setSelectedFileFilter(FileFilter filter) {
173         createChooser();
174         chooser.setSelectedFileFilter(filter);
175     }
176
177 }
Popular Tags