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