001    /*
002     * Copyright 2005,2009 Ivan SZKIBA
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.ini4j.demo;
017    
018    import bsh.util.JConsole;
019    
020    import org.ini4j.demo.DemoModel.Mode;
021    
022    import java.awt.Color;
023    import java.awt.Container;
024    import java.awt.Dimension;
025    import java.awt.event.ActionEvent;
026    import java.awt.event.ActionListener;
027    
028    import java.io.IOException;
029    
030    import javax.swing.Box;
031    import javax.swing.BoxLayout;
032    import javax.swing.ButtonGroup;
033    import javax.swing.JButton;
034    import javax.swing.JLabel;
035    import javax.swing.JPanel;
036    import javax.swing.JRadioButton;
037    import javax.swing.JScrollPane;
038    import javax.swing.JTabbedPane;
039    import javax.swing.JTextArea;
040    
041    public class Demo
042    {
043        private enum Command
044        {
045            MODE_INI,
046            MODE_REG,
047            MODE_OPTIONS,
048            LOAD_TEST_DATA,
049            PARSE_DATA,
050            CLEAR_DATA
051        }
052    
053        private JConsole _console;
054        private final Container _container;
055        private JTextArea _dataTextArea;
056        private JTextArea _helpTextArea;
057        private DemoModel _model;
058        private JTextArea _tipTextArea;
059        private ActionListener _actionListener = new ActionListener()
060        {
061            public void actionPerformed(ActionEvent event)
062            {
063                Command cmd = Command.valueOf(event.getActionCommand());
064    
065                switch (cmd)
066                {
067    
068                    case MODE_INI:
069                        doMode(Mode.INI);
070                        break;
071    
072                    case MODE_REG:
073                        doMode(Mode.REG);
074                        break;
075    
076                    case MODE_OPTIONS:
077                        doMode(Mode.OPTIONS);
078                        break;
079    
080                    case LOAD_TEST_DATA:
081                        doLoad();
082                        break;
083    
084                    case PARSE_DATA:
085                        doParse();
086                        break;
087    
088                    case CLEAR_DATA:
089                        doClear();
090                        break;
091                }
092            }
093        };
094    
095        public Demo(Container container)
096        {
097            _container = container;
098        }
099    
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    }