1 | |
package org.jtheque.core.managers.view.impl.components.panel; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.Managers; |
20 | |
import org.jtheque.core.managers.language.ILanguageManager; |
21 | |
import org.jtheque.core.managers.language.Internationalizable; |
22 | |
import org.jtheque.core.managers.view.able.IViewManager; |
23 | |
import org.jtheque.core.utils.ui.PanelBuilder; |
24 | |
import org.jtheque.utils.io.SimpleFilter; |
25 | |
|
26 | |
import javax.swing.BorderFactory; |
27 | |
import javax.swing.JButton; |
28 | |
import javax.swing.JComponent; |
29 | |
import javax.swing.JLabel; |
30 | |
import javax.swing.JPanel; |
31 | |
import javax.swing.JTextField; |
32 | |
import java.awt.BorderLayout; |
33 | |
import java.awt.Container; |
34 | |
import java.awt.Dimension; |
35 | |
import java.awt.GridBagConstraints; |
36 | |
import java.awt.Insets; |
37 | |
import java.awt.event.ActionEvent; |
38 | |
import java.awt.event.ActionListener; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public final class FileChooserPanel extends JPanel implements Internationalizable { |
46 | |
private JTextField fieldFilePath; |
47 | |
private JLabel label; |
48 | |
private JButton button; |
49 | |
private SimpleFilter filter; |
50 | |
|
51 | |
private boolean directoriesOnly; |
52 | |
|
53 | |
private String key; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public FileChooserPanel() { |
59 | 0 | super(); |
60 | |
|
61 | 0 | build(); |
62 | 0 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private void build() { |
68 | 0 | PanelBuilder builder = new PanelBuilder(this); |
69 | |
|
70 | 0 | builder.setDefaultInsets(new Insets(0, 0, 0, 0)); |
71 | |
|
72 | 0 | label = builder.add(new JLabel(), |
73 | |
builder.gbcSet(0, 0, GridBagConstraints.NONE, GridBagConstraints.ABOVE_BASELINE_LEADING, 1, 0)); |
74 | |
|
75 | 0 | JComponent panel = new JPanel(new BorderLayout()); |
76 | 0 | panel.setBorder(BorderFactory.createEtchedBorder(1)); |
77 | |
|
78 | 0 | fieldFilePath = new JTextField(15); |
79 | 0 | Insets insets = fieldFilePath.getMargin(); |
80 | 0 | fieldFilePath.setBorder(BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right)); |
81 | 0 | panel.add(fieldFilePath, BorderLayout.CENTER); |
82 | |
|
83 | 0 | addBrowseButton(panel); |
84 | |
|
85 | 0 | builder.setDefaultInsets(new Insets(0, 5, 0, 5)); |
86 | |
|
87 | 0 | builder.add(panel, builder.gbcSet(1, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.ABOVE_BASELINE_LEADING, 0, 0, 1.0, 1.0)); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private void addBrowseButton(Container panel) { |
96 | 0 | button = new JButton("..."); |
97 | |
|
98 | 0 | button.setMargin(new Insets(0, 0, 0, 0)); |
99 | 0 | button.setDefaultCapable(false); |
100 | 0 | button.setFocusPainted(false); |
101 | 0 | button.setFocusable(false); |
102 | 0 | button.addActionListener(new BrowseAction()); |
103 | |
|
104 | 0 | panel.add(button, BorderLayout.EAST); |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
void setText(String text) { |
113 | 0 | label.setText(text); |
114 | 0 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public void setTextKey(String key) { |
122 | 0 | this.key = key; |
123 | |
|
124 | 0 | setText(Managers.getManager(ILanguageManager.class).getMessage(key)); |
125 | 0 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public String getFilePath() { |
133 | 0 | return fieldFilePath.getText(); |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void setFilePath(String path) { |
142 | 0 | fieldFilePath.setText(path); |
143 | 0 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public void setFileFilter(SimpleFilter filter) { |
151 | 0 | this.filter = filter; |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public void setDirectoriesOnly() { |
158 | 0 | directoriesOnly = true; |
159 | 0 | } |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public void setFilesOnly() { |
165 | 0 | directoriesOnly = false; |
166 | 0 | } |
167 | |
|
168 | |
@Override |
169 | |
public void setEnabled(boolean enable) { |
170 | 0 | super.setEnabled(enable); |
171 | |
|
172 | 0 | fieldFilePath.setEnabled(enable); |
173 | 0 | button.setEnabled(enable); |
174 | 0 | } |
175 | |
|
176 | |
@Override |
177 | |
public void refreshText() { |
178 | 0 | if (key != null) { |
179 | 0 | setText(Managers.getManager(ILanguageManager.class).getMessage(key)); |
180 | |
} |
181 | 0 | } |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
private static final class BrowseButton extends JButton { |
189 | |
|
190 | |
|
191 | |
|
192 | |
private BrowseButton() { |
193 | 0 | super("..."); |
194 | 0 | } |
195 | |
|
196 | |
@Override |
197 | |
public Dimension getPreferredSize() { |
198 | 0 | Dimension size = super.getPreferredSize(); |
199 | 0 | size.height = 0; |
200 | 0 | return size; |
201 | |
} |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | 0 | private final class BrowseAction implements ActionListener { |
210 | |
@Override |
211 | |
public void actionPerformed(ActionEvent e) { |
212 | 0 | String file = directoriesOnly ? Managers.getManager(IViewManager.class).chooseDirectory() : Managers.getManager(IViewManager.class).chooseFile(filter); |
213 | |
|
214 | 0 | if (file != null) { |
215 | 0 | fieldFilePath.setText(file); |
216 | |
} |
217 | 0 | } |
218 | |
} |
219 | |
} |