01: package snow.utils.gui;
02:
03: import java.awt.event.*;
04: import java.net.URL;
05: import javax.swing.*;
06: import java.awt.FlowLayout;
07:
08: /** Supported:
09: *
10: * file:// uses a FileField
11: * http://
12: * ftp:// with credential / anonymous login
13: *
14: * TODO.
15: */
16: public final class URLField extends JPanel {
17: private final JComboBox type = new JComboBox(new String[] { "file",
18: "ftp", "http" });
19: /*
20: private final JPanel filePanel = new JPanel(new FlowLayout(FlowLayout.LEFT,2,0));
21: private final JPanel ftpPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,2,0));
22: private final JPanel httpPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,2,0));
23: */
24: private final FileField fileField;
25: JTextField notFileField = new JTextField(10);
26:
27: public URLField(URL url) {
28: this (url, "Please choose a file", false,
29: JFileChooser.FILES_AND_DIRECTORIES);
30: }
31:
32: public URLField(URL url, String chooserTitle, boolean saveMode,
33: int fileChooserChooserMode) {
34: super (new FlowLayout(FlowLayout.LEFT, 2, 2));
35:
36: fileField = new FileField(url.getPath(), saveMode,
37: chooserTitle, fileChooserChooserMode);
38: //fileField.setPreferredSize()
39:
40: add(type);
41:
42: System.out.println("" + url.getProtocol()); // file
43: System.out.println("" + url.getAuthority()); // c:
44: System.out.println("" + url.getPath()); // /hello.txt
45:
46: type.addActionListener(new ActionListener() {
47: public void actionPerformed(ActionEvent ae) {
48: updateVisibilities();
49: }
50: });
51:
52: add(fileField);
53: add(notFileField);
54:
55: updateVisibilities(); // initial
56: }
57:
58: void updateVisibilities() {
59: fileField.setVisible(false);
60: notFileField.setVisible(false);
61: //httpPanel.setVisible(false);
62:
63: fileField.setVisible(type.getSelectedIndex() == 0);
64: notFileField.setVisible(type.getSelectedIndex() != 0);
65:
66: }
67:
68: public URL getURL() throws Exception {
69: if (type.getSelectedIndex() == 0) {
70: //return new URL("file://", "c",
71: }
72: return new URL("");
73: }
74:
75: public static void main(String[] args) throws Exception {
76: GUIUtils.displayInFrame("Test", new URLField(new URL(
77: "file://c:/hello.txt")), true);
78: }
79:
80: }
|