1 package javax.swing.beaninfo;
2
3 import javax.swing;
4 import java.awt.event.KeyAdapter;
5 import java.awt.event.KeyEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.ActionEvent;
8 import java.net.URL;
9 import java.net.MalformedURLException;
10
11 /**
12 * Created by IntelliJ IDEA.
13 * User: UHLIG
14 * Date: 12 mai 2003
15 * Time: 15:16:20
16 * To change this template use Options | File Templates.
17 */
18 public class SwingURLEditor extends SwingEditorSupport {
19
20 private JTextField textfield;
21 private JButton urlChooserButton;
22
23 protected JFileChooser chooser = new JFileChooser();
24
25 public SwingURLEditor() {
26 // setup filechooser
27 chooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
28 chooser.setMultiSelectionEnabled( false );
29
30 // textfield
31 textfield = new JTextField();
32 textfield.addActionListener( new ActionListener() {
33 public void actionPerformed(ActionEvent e) {
34 try {
35 setValue( new URL(textfield.getText()) );
36 } catch (MalformedURLException e1) {
37 }
38 }
39 });
40
41 //button
42 UIDefaults table = UIManager.getDefaults();
43 table.put("beaninfo.OpenIcon", LookAndFeel.makeIcon(getClass(), "icons/open16.gif"));
44 Icon openIcon = UIManager.getIcon("beaninfo.OpenIcon");
45 urlChooserButton = new JButton(openIcon);
46 urlChooserButton.addActionListener( new ActionListener() {
47 public void actionPerformed(ActionEvent e) {
48 int returnVal = chooser.showOpenDialog( panel.getParent() );
49 if(returnVal == JFileChooser.APPROVE_OPTION) {
50 setTheValue();
51 }
52 }
53 });
54
55 panel = new JPanel();
56 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
57 panel.add(textfield);
58 panel.add( urlChooserButton );
59 }
60
61 protected void setTheValue() {
62 try {
63 setValue( chooser.getSelectedFile().toURL() );
64 } catch (MalformedURLException e1) {
65 }
66 }
67
68 public void setValue(Object value) {
69 super.setValue(value);
70 if (value != null) {
71 textfield.setText(value.toString());
72 } else {
73 textfield.setText("");
74 }
75 }
76 }