1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui.wizard;
20
21 import java.awt.BorderLayout;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.Font;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27
28 import javax.swing.AbstractAction;
29 import javax.swing.Action;
30 import javax.swing.BorderFactory;
31 import javax.swing.BoxLayout;
32 import javax.swing.ButtonGroup;
33 import javax.swing.DefaultListModel;
34 import javax.swing.JButton;
35 import javax.swing.JLabel;
36 import javax.swing.JList;
37 import javax.swing.JPanel;
38 import javax.swing.JToggleButton;
39 import javax.swing.SwingConstants;
40 import javax.swing.border.EtchedBorder;
41 import javax.swing.event.ListSelectionEvent;
42 import javax.swing.event.ListSelectionListener;
43
44 import net.sf.magicproject.database.DatabaseFactory;
45 import net.sf.magicproject.database.Proxy;
46 import net.sf.magicproject.tools.Configuration;
47 import net.sf.magicproject.ui.UIHelper;
48 import net.sf.magicproject.ui.i18n.LanguageManager;
49 import net.sf.magicproject.ui.layout.FlowLayoutVertical;
50
51 import com.l2fprod.common.swing.JButtonBar;
52 import com.l2fprod.common.swing.plaf.blue.BlueishButtonBarUI;
53
54 /***
55 * The settings forms is built of a button bar and many tabs associated to.
56 *
57 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
58 * @since 0.90
59 */
60 public class Settings extends YesNo {
61
62 /***
63 * Creates a new instance of Settings <br>
64 */
65 public Settings() {
66 super(LanguageManager.getString("wiz_settings.title"), LanguageManager
67 .getString("wiz_settings.description"), "wiz_settings.gif",
68 LanguageManager.getString(LABEL_OK), LanguageManager
69 .getString(LABEL_CANCEL), 550, 500);
70 }
71
72 /***
73 * Create a list containing the givent proxy list. This list is added to the
74 * given container. The built list is also associated to 2 buttons (up/down)
75 * added to the same container.
76 *
77 * @param listTitle
78 * the title of this list. Must be an existing key.
79 * @param proxies
80 * the proxy list to fill in the list.
81 * @param container
82 * the container of list and buttons.
83 * @return the list model of the built list.
84 */
85 private DefaultListModel createListPanel(String listTitle, Proxy[] proxies,
86 JPanel container) {
87 final DefaultListModel listModel = new DefaultListModel();
88 for (Proxy proxy : proxies) {
89 listModel.addElement(proxy);
90 }
91 final DataBaseList list = new DataBaseList(listModel);
92 final JPanel panel = new JPanel(new BorderLayout());
93 panel.add(new JLabel(LanguageManager.getString(listTitle)),
94 BorderLayout.NORTH);
95 panel.add(list);
96 final JPanel buttonPanel = new JPanel(new FlowLayoutVertical());
97 buttonPanel.add(list.upButton);
98 buttonPanel.add(list.downButton);
99 buttonPanel.add(new JPanel());
100 panel.add(BorderLayout.EAST, buttonPanel);
101 panel.setPreferredSize(new Dimension(100, 100));
102 container.add(panel);
103 return (DefaultListModel) list.getModel();
104 }
105
106 /***
107 *
108 */
109 private class DataBaseList extends JList implements ListSelectionListener,
110 ActionListener {
111 /***
112 *
113 * Create a new instance of this class.
114 * @param listModel
115 */
116 protected DataBaseList(DefaultListModel listModel) {
117 super(listModel);
118 setBorder(new EtchedBorder());
119 upButton = new JButton(LanguageManager
120 .getString("wiz_settings.database.button.up"), UIHelper
121 .getIcon("up.gif"));
122 upButton.setMnemonic(upButton.getText().charAt(0));
123 upButton.setToolTipText("wiz_settings.database.button.up.tooltip");
124 upButton.setHorizontalTextPosition(SwingConstants.RIGHT);
125 upButton.setHorizontalAlignment(SwingConstants.LEFT);
126 upButton.addActionListener(this);
127 downButton = new JButton(LanguageManager
128 .getString("wiz_settings.database.button.down"), UIHelper
129 .getIcon("down.gif"));
130 downButton.setMnemonic(downButton.getText().charAt(0));
131 downButton.setToolTipText("wiz_settings.database.button.down.tooltip");
132 downButton.setHorizontalTextPosition(SwingConstants.RIGHT);
133 downButton.setHorizontalAlignment(SwingConstants.LEFT);
134 downButton.addActionListener(this);
135 setVisibleRowCount(10);
136 addListSelectionListener(this);
137 setSelectedIndex(0);
138 }
139
140 public void actionPerformed(ActionEvent event) {
141 final int index = getSelectedIndex();
142 final Object element = getModel().getElementAt(index);
143 ((DefaultListModel) getModel()).removeElementAt(index);
144 int nextIndex = index;
145 if (event.getSource() == upButton) {
146 nextIndex--;
147 } else if (event.getSource() == downButton) {
148 nextIndex++;
149 }
150 ((DefaultListModel) getModel()).add(nextIndex, element);
151 setSelectedIndex(nextIndex);
152 revalidate();
153 }
154
155 public void valueChanged(ListSelectionEvent e) {
156 if (!e.getValueIsAdjusting()) {
157
158 if (getSelectedIndex() == -1) {
159
160 upButton.setEnabled(false);
161 downButton.setEnabled(false);
162 } else {
163 upButton.setEnabled(getSelectedIndex() != 0);
164 downButton.setEnabled(getSelectedIndex() != getModel().getSize() - 1);
165 }
166 }
167 }
168
169 /***
170 * The button giving an higher priority to a database.
171 */
172 protected JButton upButton;
173
174 /***
175 * The button giving a lower priority to a database.
176 */
177 protected JButton downButton;
178 }
179
180 @Override
181 public void setVisible(boolean visible) {
182 settingsPanel = new JPanel(new BorderLayout());
183 final JPanel dataBasePanel = makeComponent(LanguageManager
184 .getString("wiz_settings.database.title"));
185
186 final JPanel subpanel = new JPanel();
187 subpanel.setBorder(new EtchedBorder());
188 subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.X_AXIS));
189 pictureProxies = createListPanel("wiz_settings.database.proxy.picture",
190 DatabaseFactory.pictureProxies, subpanel);
191 databaseProxies = createListPanel("wiz_settings.database.proxy.data",
192 DatabaseFactory.dataProxies, subpanel);
193 dataBasePanel.add(subpanel);
194 final JButtonBar toolbar = new JButtonBar(SwingConstants.VERTICAL);
195 toolbar.setUI(new BlueishButtonBarUI());
196 final ButtonGroup group = new ButtonGroup();
197 settingsPanel.add(BorderLayout.WEST, toolbar);
198 addButton(LanguageManager.getString("wiz_settings.database.button"),
199 "wiz_database.gif", dataBasePanel, toolbar, group);
200 addButton("TODO", "cards.gif", makeComponent("Todo"), toolbar, group);
201 gameParamPanel.add(BorderLayout.CENTER, settingsPanel);
202 super.setVisible(visible);
203 }
204
205 /***
206 * Make a tiled component displayable with a button bar in our wizard form.
207 *
208 * @param title
209 * the component title displayed on the top of this tab.
210 * @return the built component.
211 */
212 private JPanel makeComponent(String title) {
213 final JPanel panel = new JPanel(new BorderLayout());
214 final JLabel top = new JLabel(title);
215 top.setFont(top.getFont().deriveFont(Font.BOLD));
216 top.setOpaque(true);
217 top.setBackground(panel.getBackground().brighter());
218 panel.add(BorderLayout.NORTH, top);
219 panel.setPreferredSize(new Dimension(400, 300));
220 panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
221 return panel;
222 }
223
224 /***
225 * Show a configuration component. The current component will be hidden.
226 *
227 * @param component
228 * the component to show.
229 */
230 protected void show(Component component) {
231 if (currentComponent != null) {
232 settingsPanel.remove(currentComponent);
233 }
234 currentComponent = component;
235 settingsPanel.add(BorderLayout.CENTER, currentComponent);
236 settingsPanel.revalidate();
237 settingsPanel.repaint();
238 }
239
240 /***
241 * Add a button to the wizard.
242 *
243 * @param title
244 * the button title
245 * @param iconUrl
246 * the icon url to display in the button
247 * @param component
248 * the associated component to this new button
249 * @param bar
250 * the button bar containing the other buttons
251 * @param group
252 * the group this new button would be added to.
253 */
254 private void addButton(String title, String iconUrl,
255 final Component component, JButtonBar bar, ButtonGroup group) {
256 Action action = new AbstractAction(title, UIHelper.getIcon(iconUrl)) {
257 public void actionPerformed(ActionEvent e) {
258 show(component);
259 }
260 };
261
262 JToggleButton button = new JToggleButton(action);
263 button.setPreferredSize(new Dimension(64, 90));
264 bar.add(button);
265
266 group.add(button);
267
268 if (group.getSelection() == null) {
269 button.setSelected(true);
270 show(component);
271 }
272 }
273
274 @Override
275 public void actionPerformed(ActionEvent event) {
276 if (event.getSource() == okBtn) {
277
278 StringBuilder order = new StringBuilder();
279 for (int i = 0; i < databaseProxies.getSize(); i++) {
280 DatabaseFactory.dataProxies[i] = (Proxy) databaseProxies.get(i);
281 order.append(DatabaseFactory.dataProxies[i].getXmlName());
282 order.append("|");
283 }
284 Configuration.setProperty("database.dataOrder", order);
285
286
287 order = new StringBuilder();
288 for (int i = 0; i < pictureProxies.getSize(); i++) {
289 DatabaseFactory.pictureProxies[i] = (Proxy) pictureProxies.get(i);
290 order.append(DatabaseFactory.pictureProxies[i].getXmlName());
291 order.append("|");
292 }
293 Configuration.setProperty("database.pictureOrder", order);
294
295 }
296 super.actionPerformed(event);
297 }
298
299 /***
300 * The panel containing the tabs to display. The current component displayed
301 * is <code>currentComponent</code>.
302 */
303 private JPanel settingsPanel;
304
305 /***
306 * The current panel displayed in the wizard. Used to add/reove a tab from
307 * <code>settingsPanel</code>.
308 */
309 private Component currentComponent;
310
311 private DefaultListModel databaseProxies;
312
313 private DefaultListModel pictureProxies;
314 }