1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.ini4j.demo;
17
18 import bsh.util.JConsole;
19
20 import org.ini4j.demo.DemoModel.Mode;
21
22 import java.awt.Color;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27
28 import java.io.IOException;
29
30 import javax.swing.Box;
31 import javax.swing.BoxLayout;
32 import javax.swing.ButtonGroup;
33 import javax.swing.JButton;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36 import javax.swing.JRadioButton;
37 import javax.swing.JScrollPane;
38 import javax.swing.JTabbedPane;
39 import javax.swing.JTextArea;
40
41 public class Demo
42 {
43 private enum Command
44 {
45 MODE_INI,
46 MODE_REG,
47 MODE_OPTIONS,
48 LOAD_TEST_DATA,
49 PARSE_DATA,
50 CLEAR_DATA
51 }
52
53 private JConsole _console;
54 private final Container _container;
55 private JTextArea _dataTextArea;
56 private JTextArea _helpTextArea;
57 private DemoModel _model;
58 private JTextArea _tipTextArea;
59 private ActionListener _actionListener = new ActionListener()
60 {
61 public void actionPerformed(ActionEvent event)
62 {
63 Command cmd = Command.valueOf(event.getActionCommand());
64
65 switch (cmd)
66 {
67
68 case MODE_INI:
69 doMode(Mode.INI);
70 break;
71
72 case MODE_REG:
73 doMode(Mode.REG);
74 break;
75
76 case MODE_OPTIONS:
77 doMode(Mode.OPTIONS);
78 break;
79
80 case LOAD_TEST_DATA:
81 doLoad();
82 break;
83
84 case PARSE_DATA:
85 doParse();
86 break;
87
88 case CLEAR_DATA:
89 doClear();
90 break;
91 }
92 }
93 };
94
95 public Demo(Container container)
96 {
97 _container = container;
98 }
99
100 public void init()
101 {
102 _container.setBackground(Color.WHITE);
103 _container.setLayout(new BoxLayout(_container, BoxLayout.PAGE_AXIS));
104 initInputPane();
105 initButtonsPane();
106 initOutputPane();
107
108
109 new Thread(_model).start();
110 doMode(Mode.INI);
111 }
112
113 private void addButton(JPanel panel, String label, Command command)
114 {
115 JButton button = new JButton();
116
117 button.setText(label);
118 button.setActionCommand(command.name());
119 button.addActionListener(_actionListener);
120 panel.add(button);
121 }
122
123 private void addModeButton(ButtonGroup group, JPanel panel, Mode mode)
124 {
125 String label = mode.name().charAt(0) + mode.name().toLowerCase().substring(1);
126 JRadioButton button = new JRadioButton(label);
127
128 button.setActionCommand("MODE_" + mode.name());
129 button.setSelected(mode == Mode.INI);
130 panel.add(button);
131 button.addActionListener(_actionListener);
132 group.add(button);
133 }
134
135 private void doClear()
136 {
137 try
138 {
139 _dataTextArea.setText("");
140 _model.clear();
141 }
142 catch (Exception x)
143 {
144 exceptionThrown(x);
145 }
146 }
147
148 private void doLoad()
149 {
150 try
151 {
152 _dataTextArea.setText(_model.load());
153 _console.println("Test data loaded");
154 }
155 catch (Exception x)
156 {
157 exceptionThrown(x);
158 }
159 }
160
161 private void doMode(Mode mode)
162 {
163 _model.setMode(mode);
164 try
165 {
166 _tipTextArea.setText(_model.tip());
167 }
168 catch (Exception x)
169 {
170 exceptionThrown(x);
171 }
172 }
173
174 private void doParse()
175 {
176 try
177 {
178 _model.parse(_dataTextArea.getText());
179 _console.println("Parse ready");
180 }
181 catch (Exception x)
182 {
183 exceptionThrown(x);
184 }
185 }
186
187 private void exceptionThrown(Exception exception)
188 {
189 _console.error(exception);
190 _console.error("\n");
191 exception.printStackTrace();
192 }
193
194 private void initButtonsPane()
195 {
196 JPanel buttons = new JPanel();
197
198 buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
199 buttons.setBackground(Color.WHITE);
200 buttons.add(new JLabel("Mode: "));
201 ButtonGroup group = new ButtonGroup();
202
203 addModeButton(group, buttons, Mode.INI);
204 addModeButton(group, buttons, Mode.REG);
205 addModeButton(group, buttons, Mode.OPTIONS);
206 buttons.add(Box.createHorizontalGlue());
207 addButton(buttons, " C L E A R ", Command.CLEAR_DATA);
208 addButton(buttons, " L O A D ", Command.LOAD_TEST_DATA);
209 addButton(buttons, " P A R S E ", Command.PARSE_DATA);
210 _container.add(buttons);
211 }
212
213 private void initInputPane()
214 {
215 JTabbedPane inputPane = new JTabbedPane(JTabbedPane.TOP);
216
217 inputPane.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
218 inputPane.setBackground(Color.WHITE);
219 _dataTextArea = new JTextArea();
220 JScrollPane sp = new JScrollPane(_dataTextArea);
221
222 inputPane.addTab("data", sp);
223 _tipTextArea = new JTextArea();
224 _tipTextArea.setEditable(false);
225 sp = new JScrollPane(_tipTextArea);
226 inputPane.addTab("tip", sp);
227 _helpTextArea = new JTextArea();
228 _helpTextArea.setEditable(false);
229 sp = new JScrollPane(_helpTextArea);
230 inputPane.addTab("help", sp);
231
232 _container.add(inputPane);
233 }
234
235 private void initOutputPane()
236 {
237 JTabbedPane output = new JTabbedPane(JTabbedPane.BOTTOM);
238 JConsole console = new JConsole();
239
240 console.setBackground(Color.WHITE);
241 _model = new DemoModel(console);
242 _console = new JConsole();
243
244 output.addTab("Console", _console);
245 output.setBackground(Color.WHITE);
246 output.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
247 output.addTab("Interpreter", console);
248 try
249 {
250
251
252 _helpTextArea.setText(_model.help());
253 }
254 catch (IOException x)
255 {
256 exceptionThrown(x);
257 }
258
259
260 _container.add(output);
261 }
262 }