view src/kryshen/tema/demo/DemoFrame.java @ 1:548a93c24e55

Tema 0.1jk - Javakonkurs edition (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 14 Dec 2006 23:22:05 +0300
parents
children 6c41a0b43e58
line source
1 /*
2 * Copyright (C) 2006 Mikhail A. Kryshen
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * $Id: DemoFrame.java,v 1.3 2006/12/14 15:59:48 mikhail Exp $
19 */
21 package kryshen.tema.demo;
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.io.*;
26 import java.net.URL;
28 import kryshen.tema.*;
30 /**
31 * TEMA demonstation console.
32 */
33 public class DemoFrame extends Frame {
34 private TextArea input;
35 private TextArea output;
36 private TextArea error;
38 public DemoFrame() {
39 super("TEMA demo console");
41 GridBagLayout gbl = new GridBagLayout();
43 GridBagConstraints c = new GridBagConstraints();
45 setLayout(gbl);
47 input = new TextArea(25, 40);
48 input.setEditable(true);
50 output = new TextArea(25, 40);
51 output.setEditable(false);
53 error = new TextArea(5, 80);
54 error.setEditable(false);
56 Label l;
58 c.insets = new Insets(3, 3, 3, 3);
59 c.fill = GridBagConstraints.BOTH;
60 c.gridwidth = GridBagConstraints.RELATIVE;
62 l = new Label("Enter your code and click \"Process\".");
64 gbl.setConstraints(l, c);
65 add(l);
67 Panel buttons = new Panel();
68 buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
70 Button process = new Button("Process");
71 Button clear = new Button("Clear");
72 Button close = new Button("Close");
74 buttons.add(process);
75 buttons.add(clear);
76 buttons.add(close);
78 c.gridwidth = GridBagConstraints.REMAINDER;
80 gbl.setConstraints(buttons, c);
81 add(buttons);
83 c.fill = GridBagConstraints.BOTH;
84 c.anchor = GridBagConstraints.CENTER;
85 c.gridwidth = GridBagConstraints.RELATIVE;
87 l = new Label("Input:");
89 gbl.setConstraints(l, c);
90 add(l);
92 c.gridwidth = GridBagConstraints.REMAINDER;
94 l = new Label("Output:");
96 gbl.setConstraints(l, c);
97 add(l);
99 c.gridwidth = GridBagConstraints.RELATIVE;
101 gbl.setConstraints(input, c);
102 add(input);
104 c.gridwidth = GridBagConstraints.REMAINDER;
106 gbl.setConstraints(output, c);
107 add(output);
109 l = new Label("Errors:");
111 gbl.setConstraints(l, c);
112 add(l);
114 gbl.setConstraints(error, c);
115 add(error);
117 process.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
119 process();
120 }
121 });
123 clear.addActionListener(new ActionListener() {
124 public void actionPerformed(ActionEvent e) {
125 error.setText("");
126 output.setText("");
127 input.setText("");
129 }
130 });
132 close.addActionListener(new ActionListener() {
133 public void actionPerformed(ActionEvent e) {
134 dispose();
135 }
136 });
138 addWindowListener(new WindowAdapter() {
139 public void windowClosing(WindowEvent e) {
140 dispose();
141 }
142 });
144 URL source = getClass().getResource("demo.template");
145 StringBuffer buffer = new StringBuffer();
147 try {
148 Reader in = new BufferedReader
149 (new InputStreamReader(source.openStream(), "UTF-8"));
151 for (int ch = in.read(); ch >= 0; ch = in.read()) {
152 buffer.append((char)ch);
153 }
155 in.close();
156 } catch (IOException e) {
157 e.printStackTrace();
158 }
160 input.setText(buffer.toString());
161 }
163 private void process() {
164 String s = input.getText();
166 TemplateReader tr = new TemplateReader
167 (new LineNumberReader(new StringReader(s)), "input");
168 TemplateParser tp = new TemplateParser();
169 StringWriter sw = new StringWriter();
171 error.setText("");
173 try {
174 tp.parse(tr, sw);
175 } catch (Exception e) {
176 error.setText(e.toString());
177 }
179 output.setText(sw.toString());
180 }
181 }