fix ConfigItems, especially regarding items
[nikiroo-utils.git] / src / be / nikiroo / utils / ui / ConfigItemBrowse.java
CommitLineData
d5026c09
NR
1package be.nikiroo.utils.ui;
2
3import java.awt.BorderLayout;
4import java.awt.event.ActionEvent;
5import java.awt.event.ActionListener;
6import java.awt.event.KeyAdapter;
7import java.awt.event.KeyEvent;
8import java.io.File;
9import java.util.HashMap;
10import java.util.Map;
11
12import javax.swing.JButton;
13import javax.swing.JComponent;
14import javax.swing.JFileChooser;
15import javax.swing.JPanel;
16import javax.swing.JTextField;
17
18import be.nikiroo.utils.resources.MetaInfo;
19
8cea9dc5 20class ConfigItemBrowse<E extends Enum<E>> extends ConfigItem<E> {
d5026c09
NR
21 private static final long serialVersionUID = 1L;
22
23 private boolean dir;
24 private Map<JComponent, JTextField> fields = new HashMap<JComponent, JTextField>();
25
26 /**
27 * Create a new {@link ConfigItemBrowse} for the given {@link MetaInfo}.
28 *
29 * @param info
30 * the {@link MetaInfo}
31 * @param dir
32 * TRUE for directory browsing, FALSE for file browsing
33 */
34 public ConfigItemBrowse(MetaInfo<E> info, boolean dir) {
35 super(info, false);
36 this.dir = dir;
37 }
38
39 @Override
40 protected Object getFromField(int item) {
41 JTextField field = fields.get(getField(item));
42 if (field != null) {
43 return new File(field.getText());
44 }
45
46 return null;
47 }
48
49 @Override
50 protected Object getFromInfo(int item) {
51 String path = info.getString(item, false);
52 if (path != null && !path.isEmpty()) {
53 return new File(path);
54 }
55
56 return null;
57 }
58
59 @Override
60 protected void setToField(Object value, int item) {
61 JTextField field = fields.get(getField(item));
62 if (field != null) {
63 field.setText(value == null ? "" : ((File) value).getPath());
64 }
65 }
66
67 @Override
68 protected void setToInfo(Object value, int item) {
69 info.setString(((File) value).getPath(), item);
70 }
71
72 @Override
a0376372 73 protected JComponent createEmptyField(final int item) {
d5026c09
NR
74 final JPanel pane = new JPanel(new BorderLayout());
75 final JTextField field = new JTextField();
76 field.addKeyListener(new KeyAdapter() {
77 @Override
78 public void keyTyped(KeyEvent e) {
79 File file = null;
80 if (!field.getText().isEmpty()) {
81 file = new File(field.getText());
82 }
83
a0376372 84 if (hasValueChanged(file, item)) {
d5026c09
NR
85 setDirtyItem(item);
86 }
87 }
88 });
89
90 final JButton browseButton = new JButton("...");
91 browseButton.addActionListener(new ActionListener() {
92 @Override
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();
100 if (file != null) {
101 setToField(file, item);
a0376372 102 if (hasValueChanged(file, item)) {
d5026c09
NR
103 setDirtyItem(item);
104 }
105 }
106 }
107 }
108 });
109
110 pane.add(browseButton, BorderLayout.WEST);
111 pane.add(field, BorderLayout.CENTER);
112
113 fields.put(pane, field);
114 return pane;
115 }
116}