From: Niki Roo Date: Wed, 8 Mar 2017 18:57:20 +0000 (+0100) Subject: New: ConfigEditor, UI to configure a bundle X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=d350b96b06c55189e5d0ceed9db6f237e7d8d871;p=fanfix-jexer.git New: ConfigEditor, UI to configure a bundle --- diff --git a/src/be/nikiroo/utils/ui/ConfigEditor.java b/src/be/nikiroo/utils/ui/ConfigEditor.java new file mode 100644 index 0000000..6b820bb --- /dev/null +++ b/src/be/nikiroo/utils/ui/ConfigEditor.java @@ -0,0 +1,102 @@ +package be.nikiroo.utils.ui; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.util.List; + +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; + +import be.nikiroo.utils.resources.Bundle; + +/** + * A configuration panel for a {@link Bundle}. + *

+ * All the items in the given {@link Bundle} will be displayed in editable + * controls, with options to Save, Reset and/or Reset to the application default + * values. + * + * @author niki + * + * @param + * the type of {@link Bundle} to edit + */ +public class ConfigEditor> extends JPanel { + private static final long serialVersionUID = 1L; + private List> items; + + /** + * Create a new {@link ConfigEditor} for this {@link Bundle}. + * + * @param type + * a class instance of the item type to work on + * @param bundle + * the {@link Bundle} to sort through + */ + public ConfigEditor(Class type, final Bundle bundle) { + this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); + + items = ConfigItem.getItems(type, bundle); + for (ConfigItem item : items) { + this.add(item); + } + + addButton("Reset", new ActionListener() { + public void actionPerformed(ActionEvent e) { + for (ConfigItem item : items) { + item.reload(); + } + } + }); + + addButton("Default", new ActionListener() { + public void actionPerformed(ActionEvent e) { + Object snap = bundle.takeSnapshot(); + bundle.reload(true); + for (ConfigItem item : items) { + item.reload(); + } + bundle.reload(false); + bundle.restoreSnapshot(snap); + } + }); + + addButton("Save", new ActionListener() { + public void actionPerformed(ActionEvent e) { + for (ConfigItem item : items) { + item.save(); + } + + try { + bundle.updateFile(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + }); + } + + /** + * Add an action button for this action. + * + * @param title + * the action title + * @param listener + * the action + */ + private void addButton(String title, ActionListener listener) { + JButton button = new JButton(title); + button.addActionListener(listener); + + JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout()); + panel.setBorder(new EmptyBorder(2, 10, 2, 10)); + panel.add(button, BorderLayout.CENTER); + + this.add(panel); + } +} diff --git a/src/be/nikiroo/utils/ui/ConfigItem.java b/src/be/nikiroo/utils/ui/ConfigItem.java new file mode 100644 index 0000000..3593d7c --- /dev/null +++ b/src/be/nikiroo/utils/ui/ConfigItem.java @@ -0,0 +1,86 @@ +package be.nikiroo.utils.ui; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.border.EmptyBorder; + +import be.nikiroo.utils.resources.Bundle; + +/** + * A graphical item that reflect a configuration option from the given + * {@link Bundle}. + * + * @author niki + * + * @param + * the type of {@link Bundle} to edit + */ +public class ConfigItem> extends JPanel { + private static final long serialVersionUID = 1L; + private final Bundle bundle; + private final E id; + private String value; + + private JTextField valueField; + + public ConfigItem(Class type, Bundle bundle, E id) { + this.bundle = bundle; + this.id = id; + + this.setLayout(new BorderLayout()); + this.setBorder(new EmptyBorder(2, 10, 2, 10)); + + JLabel nameLabel = new JLabel(id.toString()); + nameLabel.setPreferredSize(new Dimension(400, 0)); + this.add(nameLabel, BorderLayout.WEST); + + valueField = new JTextField(); + valueField.setText(value); + + reload(); + this.add(valueField, BorderLayout.CENTER); + } + + /** + * Reload the value from the {@link Bundle}. + */ + public void reload() { + value = bundle.getString(id); + valueField.setText(value); + } + + /** + * Save the current value to the {@link Bundle}. + */ + public void save() { + value = valueField.getText(); + bundle.setString(id, value); + } + + /** + * Create a list of {@link ConfigItem}, one for each of the item in the + * given {@link Bundle}. + * + * @param type + * a class instance of the item type to work on + * @param bundle + * the {@link Bundle} to sort through + * + * @return the list + */ + static public > List> getItems( + Class type, Bundle bundle) { + List> list = new ArrayList>(); + for (E id : type.getEnumConstants()) { + list.add(new ConfigItem(type, bundle, id)); + } + + return list; + } +}