Commit | Line | Data |
---|---|---|
d350b96b NR |
1 | package be.nikiroo.utils.ui; |
2 | ||
3 | import java.awt.BorderLayout; | |
d18e136e | 4 | import java.awt.Color; |
d350b96b NR |
5 | import java.awt.event.ActionEvent; |
6 | import java.awt.event.ActionListener; | |
7 | import java.io.IOException; | |
9cc3f77b | 8 | import java.util.ArrayList; |
d350b96b NR |
9 | import java.util.List; |
10 | ||
11 | import javax.swing.BoxLayout; | |
12 | import javax.swing.JButton; | |
49f79f31 NR |
13 | import javax.swing.JComponent; |
14 | import javax.swing.JLabel; | |
d350b96b | 15 | import javax.swing.JPanel; |
49f79f31 | 16 | import javax.swing.JScrollPane; |
d18e136e | 17 | import javax.swing.JTextArea; |
d350b96b | 18 | import javax.swing.border.EmptyBorder; |
d18e136e | 19 | import javax.swing.border.TitledBorder; |
d350b96b | 20 | |
d18e136e | 21 | import be.nikiroo.utils.StringUtils; |
d350b96b | 22 | import be.nikiroo.utils.resources.Bundle; |
9e834013 | 23 | import be.nikiroo.utils.resources.MetaInfo; |
d350b96b NR |
24 | |
25 | /** | |
26 | * A configuration panel for a {@link Bundle}. | |
27 | * <p> | |
28 | * All the items in the given {@link Bundle} will be displayed in editable | |
29 | * controls, with options to Save, Reset and/or Reset to the application default | |
30 | * values. | |
31 | * | |
32 | * @author niki | |
db31c358 | 33 | * |
d350b96b NR |
34 | * @param <E> |
35 | * the type of {@link Bundle} to edit | |
36 | */ | |
37 | public class ConfigEditor<E extends Enum<E>> extends JPanel { | |
38 | private static final long serialVersionUID = 1L; | |
9e834013 | 39 | private List<MetaInfo<E>> items; |
d350b96b NR |
40 | |
41 | /** | |
42 | * Create a new {@link ConfigEditor} for this {@link Bundle}. | |
43 | * | |
44 | * @param type | |
45 | * a class instance of the item type to work on | |
46 | * @param bundle | |
47 | * the {@link Bundle} to sort through | |
49f79f31 NR |
48 | * @param title |
49 | * the title to display before the options | |
d350b96b | 50 | */ |
49f79f31 NR |
51 | public ConfigEditor(Class<E> type, final Bundle<E> bundle, String title) { |
52 | this.setLayout(new BorderLayout()); | |
53 | JPanel main = new JPanel(); | |
54 | ||
55 | JScrollPane scroll = new JScrollPane(main); | |
56 | scroll.getVerticalScrollBar().setUnitIncrement(16); | |
57 | this.add(scroll, BorderLayout.CENTER); | |
58 | ||
59 | main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS)); | |
d18e136e | 60 | main.setBorder(new EmptyBorder(5, 5, 5, 5)); |
9cc3f77b | 61 | |
49f79f31 | 62 | main.add(new JLabel(title)); |
d350b96b | 63 | |
9cc3f77b NR |
64 | items = new ArrayList<MetaInfo<E>>(); |
65 | List<MetaInfo<E>> groupedItems = MetaInfo.getItems(type, bundle); | |
66 | for (MetaInfo<E> item : groupedItems) { | |
67 | // will init this.items | |
d18e136e | 68 | addItem(main, item, 0); |
d350b96b NR |
69 | } |
70 | ||
49f79f31 | 71 | main.add(createButton("Reset", new ActionListener() { |
cd0c27d2 | 72 | @Override |
d350b96b | 73 | public void actionPerformed(ActionEvent e) { |
9e834013 | 74 | for (MetaInfo<E> item : items) { |
d350b96b NR |
75 | item.reload(); |
76 | } | |
77 | } | |
49f79f31 | 78 | })); |
d350b96b | 79 | |
49f79f31 | 80 | main.add(createButton("Default", new ActionListener() { |
cd0c27d2 | 81 | @Override |
d350b96b NR |
82 | public void actionPerformed(ActionEvent e) { |
83 | Object snap = bundle.takeSnapshot(); | |
84 | bundle.reload(true); | |
9e834013 | 85 | for (MetaInfo<E> item : items) { |
d350b96b NR |
86 | item.reload(); |
87 | } | |
88 | bundle.reload(false); | |
89 | bundle.restoreSnapshot(snap); | |
90 | } | |
49f79f31 | 91 | })); |
d350b96b | 92 | |
49f79f31 | 93 | main.add(createButton("Save", new ActionListener() { |
cd0c27d2 | 94 | @Override |
d350b96b | 95 | public void actionPerformed(ActionEvent e) { |
9e834013 | 96 | for (MetaInfo<E> item : items) { |
d5026c09 | 97 | item.save(true); |
d350b96b NR |
98 | } |
99 | ||
100 | try { | |
101 | bundle.updateFile(); | |
102 | } catch (IOException e1) { | |
103 | e1.printStackTrace(); | |
104 | } | |
105 | } | |
49f79f31 | 106 | })); |
d350b96b NR |
107 | } |
108 | ||
d18e136e | 109 | private void addItem(JPanel main, MetaInfo<E> item, int nhgap) { |
76b51de9 | 110 | if (item.isGroup()) { |
d18e136e NR |
111 | JPanel bpane = new JPanel(new BorderLayout()); |
112 | bpane.setBorder(new TitledBorder(item.getName())); | |
113 | JPanel pane = new JPanel(); | |
114 | pane.setBorder(new EmptyBorder(5, 5, 5, 5)); | |
115 | pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); | |
116 | ||
117 | String info = item.getDescription(); | |
118 | info = StringUtils.justifyTexts(info, 100); | |
119 | if (!info.isEmpty()) { | |
fde375c1 | 120 | info = info + "\n"; |
d18e136e NR |
121 | JTextArea text = new JTextArea(info); |
122 | text.setWrapStyleWord(true); | |
123 | text.setOpaque(false); | |
124 | text.setForeground(new Color(100, 100, 180)); | |
125 | text.setEditable(false); | |
126 | pane.add(text); | |
127 | } | |
128 | ||
76b51de9 | 129 | for (MetaInfo<E> subitem : item) { |
d18e136e | 130 | addItem(pane, subitem, nhgap + 11); |
76b51de9 | 131 | } |
d18e136e NR |
132 | bpane.add(pane, BorderLayout.CENTER); |
133 | main.add(bpane); | |
76b51de9 | 134 | } else { |
9cc3f77b | 135 | items.add(item); |
d18e136e | 136 | main.add(new ConfigItem<E>(item, nhgap)); |
76b51de9 NR |
137 | } |
138 | } | |
139 | ||
d350b96b NR |
140 | /** |
141 | * Add an action button for this action. | |
142 | * | |
143 | * @param title | |
144 | * the action title | |
145 | * @param listener | |
146 | * the action | |
147 | */ | |
49f79f31 | 148 | private JComponent createButton(String title, ActionListener listener) { |
d350b96b NR |
149 | JButton button = new JButton(title); |
150 | button.addActionListener(listener); | |
151 | ||
152 | JPanel panel = new JPanel(); | |
153 | panel.setLayout(new BorderLayout()); | |
154 | panel.setBorder(new EmptyBorder(2, 10, 2, 10)); | |
155 | panel.add(button, BorderLayout.CENTER); | |
156 | ||
49f79f31 | 157 | return panel; |
d350b96b NR |
158 | } |
159 | } |