view src/kryshen/tema/demo/DemoFrame.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children
line source
1 /*
2 * Copyright 2006-2009 Mikhail Kryshen
3 *
4 * This file is part of Tema.
5 *
6 * Tema is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * Tema is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the
17 * GNU Lesser General Public License along with Tema.
18 * If not, see <http://www.gnu.org/licenses/>.
19 */
21 package kryshen.tema.demo;
23 import java.awt.Button;
24 import java.awt.FlowLayout;
25 import java.awt.Font;
26 import java.awt.Frame;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.awt.Insets;
30 import java.awt.Label;
31 import java.awt.Panel;
32 import java.awt.TextArea;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.WindowAdapter;
36 import java.awt.event.WindowEvent;
37 import java.io.BufferedReader;
38 import java.io.IOException;
39 import java.io.InputStreamReader;
40 import java.io.Reader;
41 import java.io.StringReader;
42 import java.io.StringWriter;
43 import java.net.URL;
44 import kryshen.tema.TemplateParser;
45 import kryshen.tema.io.TemplateReader;
47 /**
48 * Tema demonstation console.
49 */
50 public class DemoFrame extends Frame {
51 private static final Font TEXT_FONT =
52 new Font("monospaced", Font.PLAIN, 12);
54 private TextArea input;
55 private TextArea output;
56 private TextArea error;
58 private String sample;
60 public DemoFrame() {
61 super("Tema demo console");
63 GridBagLayout gbl = new GridBagLayout();
65 GridBagConstraints c = new GridBagConstraints();
67 setLayout(gbl);
69 input = new TextArea(28, 50);
70 input.setEditable(true);
71 input.setFont(TEXT_FONT);
73 output = new TextArea(28, 50);
74 output.setEditable(false);
75 output.setFont(TEXT_FONT);
77 error = new TextArea(5, 90);
78 error.setEditable(false);
79 error.setFont(TEXT_FONT);
81 Label l;
83 c.insets = new Insets(3, 3, 3, 3);
84 c.fill = GridBagConstraints.BOTH;
85 c.gridwidth = GridBagConstraints.RELATIVE;
87 l = new Label("Enter your code and press \"Process\".");
89 gbl.setConstraints(l, c);
90 add(l);
92 Panel buttons = new Panel();
93 buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
95 Button process = new Button("Process");
96 Button clear = new Button("Clear");
97 Button reset = new Button("Reset");
98 Button close = new Button("Close");
100 buttons.add(process);
101 buttons.add(clear);
102 buttons.add(reset);
103 buttons.add(close);
105 c.gridwidth = GridBagConstraints.REMAINDER;
107 gbl.setConstraints(buttons, c);
108 add(buttons);
110 c.fill = GridBagConstraints.BOTH;
111 c.anchor = GridBagConstraints.CENTER;
112 c.gridwidth = GridBagConstraints.RELATIVE;
114 l = new Label("Input:");
116 gbl.setConstraints(l, c);
117 add(l);
119 c.gridwidth = GridBagConstraints.REMAINDER;
121 l = new Label("Output:");
123 gbl.setConstraints(l, c);
124 add(l);
126 c.gridwidth = GridBagConstraints.RELATIVE;
127 c.weightx = 1.0;
128 c.weighty = 1.0;
130 gbl.setConstraints(input, c);
131 add(input);
133 c.gridwidth = GridBagConstraints.REMAINDER;
135 gbl.setConstraints(output, c);
136 add(output);
138 c.weightx = 0.0;
139 c.weighty = 0.0;
141 l = new Label("Errors:");
143 gbl.setConstraints(l, c);
144 add(l);
146 c.weightx = 1.0;
147 c.weighty = 0.25;
149 gbl.setConstraints(error, c);
150 add(error);
152 process.addActionListener(new ActionListener() {
153 public void actionPerformed(ActionEvent e) {
154 process();
155 }
156 });
158 clear.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e) {
160 error.setText("");
161 output.setText("");
162 input.setText("");
163 input.requestFocus();
164 }
165 });
167 reset.addActionListener(new ActionListener() {
168 public void actionPerformed(ActionEvent e) {
169 error.setText("");
170 output.setText("");
171 input.setText(sample);
172 }
173 });
175 close.addActionListener(new ActionListener() {
176 public void actionPerformed(ActionEvent e) {
177 dispose();
178 }
179 });
181 addWindowListener(new WindowAdapter() {
182 public void windowClosing(WindowEvent e) {
183 dispose();
184 }
185 });
187 URL source = getClass().getResource("demo.template");
188 StringBuffer buffer = new StringBuffer();
190 try {
191 Reader in = new BufferedReader
192 (new InputStreamReader(source.openStream(), "UTF-8"));
194 for (int ch = in.read(); ch >= 0; ch = in.read()) {
195 buffer.append((char)ch);
196 }
198 in.close();
199 } catch (IOException e) {
200 e.printStackTrace();
201 }
203 sample = buffer.toString();
205 input.setText(sample);
206 }
208 private void process() {
209 String s = input.getText();
211 TemplateReader tr = new TemplateReader(new StringReader(s), "input");
212 TemplateParser tp = new TemplateParser();
213 StringWriter sw = new StringWriter();
215 error.setText("");
217 try {
218 tp.parse(tr, sw);
219 } catch (Exception e) {
220 error.setText(e.toString());
221 e.printStackTrace();
222 }
224 output.setText(sw.toString());
225 }
226 }