view src/kryshen/tema/demo/DemoFrame.java @ 2:6c41a0b43e58

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents 548a93c24e55
children e9d13c7ffeb1
line source
1 /*
2 * Copyright 2006-2008 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 *
20 * $Id: DemoFrame.java,v 1.13 2008/02/16 17:38:03 mikhail Exp $
21 */
23 package kryshen.tema.demo;
25 import java.awt.Button;
26 import java.awt.FlowLayout;
27 import java.awt.Font;
28 import java.awt.Frame;
29 import java.awt.GridBagConstraints;
30 import java.awt.GridBagLayout;
31 import java.awt.Insets;
32 import java.awt.Label;
33 import java.awt.Panel;
34 import java.awt.TextArea;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.WindowAdapter;
38 import java.awt.event.WindowEvent;
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.io.InputStreamReader;
42 import java.io.Reader;
43 import java.io.StringReader;
44 import java.io.StringWriter;
45 import java.net.URL;
46 import kryshen.tema.TemplateParser;
47 import kryshen.tema.io.TemplateReader;
49 /**
50 * Tema demonstation console.
51 */
52 public class DemoFrame extends Frame {
53 private static final Font TEXT_FONT =
54 new Font("monospaced", Font.PLAIN, 12);
56 private TextArea input;
57 private TextArea output;
58 private TextArea error;
60 private String sample;
62 public DemoFrame() {
63 super("Tema demo console");
65 GridBagLayout gbl = new GridBagLayout();
67 GridBagConstraints c = new GridBagConstraints();
69 setLayout(gbl);
71 input = new TextArea(28, 50);
72 input.setEditable(true);
73 input.setFont(TEXT_FONT);
75 output = new TextArea(28, 50);
76 output.setEditable(false);
77 output.setFont(TEXT_FONT);
79 error = new TextArea(5, 90);
80 error.setEditable(false);
81 error.setFont(TEXT_FONT);
83 Label l;
85 c.insets = new Insets(3, 3, 3, 3);
86 c.fill = GridBagConstraints.BOTH;
87 c.gridwidth = GridBagConstraints.RELATIVE;
89 l = new Label("Enter your code and press \"Process\".");
91 gbl.setConstraints(l, c);
92 add(l);
94 Panel buttons = new Panel();
95 buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
97 Button process = new Button("Process");
98 Button clear = new Button("Clear");
99 Button reset = new Button("Reset");
100 Button close = new Button("Close");
102 buttons.add(process);
103 buttons.add(clear);
104 buttons.add(reset);
105 buttons.add(close);
107 c.gridwidth = GridBagConstraints.REMAINDER;
109 gbl.setConstraints(buttons, c);
110 add(buttons);
112 c.fill = GridBagConstraints.BOTH;
113 c.anchor = GridBagConstraints.CENTER;
114 c.gridwidth = GridBagConstraints.RELATIVE;
116 l = new Label("Input:");
118 gbl.setConstraints(l, c);
119 add(l);
121 c.gridwidth = GridBagConstraints.REMAINDER;
123 l = new Label("Output:");
125 gbl.setConstraints(l, c);
126 add(l);
128 c.gridwidth = GridBagConstraints.RELATIVE;
129 c.weightx = 1.0;
130 c.weighty = 1.0;
132 gbl.setConstraints(input, c);
133 add(input);
135 c.gridwidth = GridBagConstraints.REMAINDER;
137 gbl.setConstraints(output, c);
138 add(output);
140 c.weightx = 0.0;
141 c.weighty = 0.0;
143 l = new Label("Errors:");
145 gbl.setConstraints(l, c);
146 add(l);
148 c.weightx = 1.0;
149 c.weighty = 0.25;
151 gbl.setConstraints(error, c);
152 add(error);
154 process.addActionListener(new ActionListener() {
155 public void actionPerformed(ActionEvent e) {
156 process();
157 }
158 });
160 clear.addActionListener(new ActionListener() {
161 public void actionPerformed(ActionEvent e) {
162 error.setText("");
163 output.setText("");
164 input.setText("");
165 input.requestFocus();
166 }
167 });
169 reset.addActionListener(new ActionListener() {
170 public void actionPerformed(ActionEvent e) {
171 error.setText("");
172 output.setText("");
173 input.setText(sample);
174 }
175 });
177 close.addActionListener(new ActionListener() {
178 public void actionPerformed(ActionEvent e) {
179 dispose();
180 }
181 });
183 addWindowListener(new WindowAdapter() {
184 public void windowClosing(WindowEvent e) {
185 dispose();
186 }
187 });
189 URL source = getClass().getResource("demo.template");
190 StringBuffer buffer = new StringBuffer();
192 try {
193 Reader in = new BufferedReader
194 (new InputStreamReader(source.openStream(), "UTF-8"));
196 for (int ch = in.read(); ch >= 0; ch = in.read()) {
197 buffer.append((char)ch);
198 }
200 in.close();
201 } catch (IOException e) {
202 e.printStackTrace();
203 }
205 sample = buffer.toString();
207 input.setText(sample);
208 }
210 private void process() {
211 String s = input.getText();
213 TemplateReader tr = new TemplateReader(new StringReader(s), "input");
214 TemplateParser tp = new TemplateParser();
215 StringWriter sw = new StringWriter();
217 error.setText("");
219 try {
220 tp.parse(tr, sw);
221 } catch (Exception e) {
222 error.setText(e.toString());
223 e.printStackTrace();
224 }
226 output.setText(sw.toString());
227 }
228 }