| 1 | package be.nikiroo.utils.ui; |
| 2 | |
| 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; |
| 8 | import java.io.File; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Map; |
| 11 | |
| 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; |
| 17 | |
| 18 | import be.nikiroo.utils.resources.MetaInfo; |
| 19 | |
| 20 | class ConfigItemBrowse<E extends Enum<E>> extends ConfigItem<E> { |
| 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 |
| 73 | protected JComponent createField(final int item) { |
| 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 | |
| 84 | if (hasValueChanged(file)) { |
| 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); |
| 102 | if (hasValueChanged(file)) { |
| 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 | } |