1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.component;
21
22 import java.awt.Insets;
23 import java.awt.event.ActionEvent;
24 import java.io.File;
25
26 import javax.swing.AbstractAction;
27 import javax.swing.Action;
28 import javax.swing.Box;
29 import javax.swing.BoxLayout;
30 import javax.swing.JButton;
31 import javax.swing.JFileChooser;
32 import javax.swing.JTextField;
33
34 import org.xnap.XNap;
35 import org.xnap.gui.util.IconHelper;
36 import org.xnap.util.FileHelper;
37 import org.xnap.util.SystemHelper;
38
39 /***
40 * This class provides a {@link JTextField} that displays a directory
41 * and button that can launch a {@link JFileChooser} dialog or
42 * a {@link DirectoryChooser} dialog to select a directory.
43 */
44 public class DirectoryPanel extends Box
45 {
46
47
48
49 private XNapTextField jtfFilename;
50
51
52
53 public DirectoryPanel(String text, int size)
54 {
55 super(BoxLayout.X_AXIS);
56
57 jtfFilename = new XNapTextField(text, size);
58 jtfFilename.setCompletionModel(new FileCompletionModel(true));
59 jtfFilename.setPreferences("directoryPanel");
60 add(jtfFilename);
61
62 add(Box.createHorizontalStrut(1));
63
64
65
66
67
68 Action action;
69 if (SystemHelper.isMacOSX) {
70 action = new SwingDirectoryChooserAction();
71 }
72 else {
73 action = new DirectoryChooserAction();
74 }
75 JButton jbChooser = new XNapButton(action);
76 jbChooser.setMargin(new Insets(1, 1, 1, 1));
77 add(jbChooser);
78 }
79
80
81
82 /***
83 * Sub classes can overwrite this.
84 */
85 protected void directorySelected(String dir)
86 {
87 }
88
89 public String getDirectory()
90 {
91 return FileHelper.directory(jtfFilename.getText());
92 }
93
94 public void setDirectory(String dir)
95 {
96 jtfFilename.setText(dir);
97 }
98
99 public JTextField getTextField()
100 {
101 return jtfFilename;
102 }
103
104
105
106 private class DirectoryChooserAction extends AbstractAction
107 {
108 private DirectoryChooser chooser;
109
110 public DirectoryChooserAction()
111 {
112 chooser = new DirectoryChooser();
113
114 putValue(IconHelper.XNAP_ICON, "fileopen.png");
115 putValue(Action.SHORT_DESCRIPTION, XNap.tr("Choose a directory"));
116 }
117
118 public void actionPerformed(ActionEvent event)
119 {
120 chooser.setSelectedDirectory(new File(getDirectory()));
121
122 if (chooser.showChooseDialog(DirectoryPanel.this)
123 == DirectoryChooser.APPROVE_OPTION) {
124 setDirectory(chooser.getSelectedDirectory().getAbsolutePath());
125 directorySelected(getDirectory());
126 }
127 }
128 }
129
130 private class SwingDirectoryChooserAction extends AbstractAction {
131
132 private JFileChooser chooser;
133
134 public SwingDirectoryChooserAction()
135 {
136 chooser = new JFileChooser();
137 chooser.setApproveButtonText(XNap.tr("OK"));
138 chooser.setDialogTitle(XNap.tr("Choose Directory"));
139 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
140
141 putValue(IconHelper.XNAP_ICON, "fileopen.png");
142 putValue(Action.SHORT_DESCRIPTION, XNap.tr("Choose a directory"));
143 }
144
145 public void actionPerformed(ActionEvent event)
146 {
147 chooser.setSelectedFile(new File(getDirectory()));
148
149 if (chooser.showOpenDialog(DirectoryPanel.this)
150 == JFileChooser.APPROVE_OPTION) {
151 setDirectory(chooser.getSelectedFile().getAbsolutePath());
152 directorySelected(getDirectory());
153 }
154 }
155 }
156
157 }