1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.event
.ActionEvent
;
5 import java
.awt
.event
.ActionListener
;
6 import java
.awt
.event
.KeyAdapter
;
7 import java
.awt
.event
.KeyEvent
;
9 import java
.util
.HashMap
;
12 import javax
.swing
.JButton
;
13 import javax
.swing
.JComponent
;
14 import javax
.swing
.JFileChooser
;
15 import javax
.swing
.JPanel
;
16 import javax
.swing
.JTextField
;
18 import be
.nikiroo
.utils
.resources
.MetaInfo
;
20 class ConfigItemBrowse
<E
extends Enum
<E
>> extends ConfigItem
<E
> {
21 private static final long serialVersionUID
= 1L;
24 private Map
<JComponent
, JTextField
> fields
= new HashMap
<JComponent
, JTextField
>();
27 * Create a new {@link ConfigItemBrowse} for the given {@link MetaInfo}.
30 * the {@link MetaInfo}
32 * TRUE for directory browsing, FALSE for file browsing
34 public ConfigItemBrowse(MetaInfo
<E
> info
, boolean dir
) {
40 protected Object
getFromField(int item
) {
41 JTextField field
= fields
.get(getField(item
));
43 return new File(field
.getText());
50 protected Object
getFromInfo(int item
) {
51 String path
= getInfo().getString(item
, false);
52 if (path
!= null && !path
.isEmpty()) {
53 return new File(path
);
60 protected void setToField(Object value
, int item
) {
61 JTextField field
= fields
.get(getField(item
));
63 field
.setText(value
== null ?
"" : ((File
) value
).getPath());
68 protected void setToInfo(Object value
, int item
) {
69 getInfo().setString(((File
) value
).getPath(), item
);
73 protected JComponent
createEmptyField(final int item
) {
74 final JPanel pane
= new JPanel(new BorderLayout());
75 final JTextField field
= new JTextField();
76 field
.addKeyListener(new KeyAdapter() {
78 public void keyTyped(KeyEvent e
) {
80 if (!field
.getText().isEmpty()) {
81 file
= new File(field
.getText());
84 if (hasValueChanged(file
, item
)) {
90 final JButton browseButton
= new JButton("...");
91 browseButton
.addActionListener(new ActionListener() {
93 public void actionPerformed(ActionEvent e
) {
94 JFileChooser chooser
= new JFileChooser();
95 chooser
.setCurrentDirectory((File
) getFromInfo(item
));
96 chooser
.setFileSelectionMode(dir ? JFileChooser
.DIRECTORIES_ONLY
97 : JFileChooser
.FILES_ONLY
);
98 if (chooser
.showOpenDialog(ConfigItemBrowse
.this) == JFileChooser
.APPROVE_OPTION
) {
99 File file
= chooser
.getSelectedFile();
101 setToField(file
, item
);
102 if (hasValueChanged(file
, item
)) {
110 pane
.add(browseButton
, BorderLayout
.WEST
);
111 pane
.add(field
, BorderLayout
.CENTER
);
113 fields
.put(pane
, field
);