1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.xnap.gui.component;
22
23 import java.awt.Insets;
24 import java.awt.event.ActionEvent;
25 import java.io.File;
26
27 import javax.swing.AbstractAction;
28 import javax.swing.Action;
29 import javax.swing.Box;
30 import javax.swing.BoxLayout;
31 import javax.swing.JButton;
32 import javax.swing.JFileChooser;
33 import javax.swing.JPanel;
34 import javax.swing.JTextField;
35
36 import org.xnap.XNap;
37 import org.xnap.gui.util.IconHelper;
38
39 /***
40 * Provides a panel with a <code>JTextField</code> and a button for file
41 * selection.
42 */
43 public class FilePanel extends JPanel {
44
45
46
47 private XNapTextField jtfFilename;
48
49
50
51 public FilePanel(String text, int size)
52 {
53 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
54
55 jtfFilename = new XNapTextField(text, size);
56 jtfFilename.setCompletionModel(new FileCompletionModel());
57 jtfFilename.setPreferences("filePanel");
58 add(jtfFilename);
59
60 add(Box.createHorizontalStrut(1));
61
62 JButton jbChooser = new XNapButton(new FileChooserAction());
63 jbChooser.setMargin(new Insets(1, 1, 1, 1));
64 add(jbChooser);
65 }
66
67 /***
68 * Sub classes can overwrite this.
69 */
70 protected void filenameSelected(String dir)
71 {
72 }
73
74 public String getFilename()
75 {
76 return jtfFilename.getText();
77 }
78
79 public void setFilename(String dir)
80 {
81 jtfFilename.setText(dir);
82 }
83
84 public XNapTextField getTextField()
85 {
86 return jtfFilename;
87 }
88
89
90
91 private class FileChooserAction extends AbstractAction {
92
93 JFileChooser chooser;
94
95 public FileChooserAction()
96 {
97 chooser = new JFileChooser();
98 chooser.setApproveButtonText(XNap.tr("OK"));
99 chooser.setDialogTitle(XNap.tr("Choose File"));
100
101 putValue(IconHelper.XNAP_ICON, "fileopen.png");
102 putValue(Action.SHORT_DESCRIPTION, XNap.tr("Choose a file") );
103 }
104
105 public void actionPerformed(ActionEvent event)
106 {
107 chooser.setSelectedFile(new File(getFilename()));
108
109 if (chooser.showSaveDialog(FilePanel.this)
110 == JFileChooser.APPROVE_OPTION) {
111 String filename = chooser.getSelectedFile().getAbsolutePath();
112 setFilename(filename);
113 filenameSelected(filename);
114 }
115 }
116 }
117
118 }