KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > gui > MainFrame


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20
21 package JFlex.gui;
22
23 import JFlex.Main;
24 import JFlex.Out;
25
26 import java.io.File JavaDoc;
27 import java.awt.*;
28 import java.awt.event.*;
29
30 /**
31  * JFlex main application frame (GUI mode only)
32  *
33  * @author Gerwin Klein
34  * @version JFlex 1.4.1, $Revision: 2.7 $, $Date: 2004/11/06 23:03:33 $
35  */

36 final public class MainFrame extends Frame implements Handles {
37
38   private volatile boolean choosing;
39
40   private String JavaDoc fileName = "";
41   private String JavaDoc dirName = "";
42   
43   private Button quit;
44   private Button options;
45   private Button generate;
46   private Button stop;
47   private Button specChoose;
48   private Button dirChoose;
49
50   private TextField spec;
51   private TextField dir;
52
53   private TextArea messages;
54
55   private GeneratorThread thread;
56
57   private OptionsDialog dialog;
58
59   
60   public MainFrame() {
61     super("JFlex "+Main.version);
62     buildContent();
63     
64     addWindowListener( new WindowAdapter() {
65       public void windowClosing(WindowEvent e) {
66         quit();
67       }
68     });
69     
70     pack();
71     show();
72   }
73
74
75   private void buildContent() {
76     setBackground(SystemColor.control);
77
78     generate = new Button("Generate");
79     quit = new Button("Quit");
80     options = new Button("Options");
81     stop = new Button("Stop");
82     dirChoose = new Button("Browse");
83     dir = new TextField(10);
84     specChoose = new Button("Browse");
85     spec = new TextField(10);
86     messages = new TextArea(10,80);
87
88     messages.setEditable(false);
89     Font font = messages.getFont();
90     if (font != null)
91       messages.setFont(new Font("Monospaced", font.getStyle(), font.getSize()));
92     else
93       messages.setFont(new Font("Monospaced", Font.PLAIN, 12));
94
95     Out.setGUIMode(messages);
96
97     generate.addActionListener( new ActionListener() {
98       public void actionPerformed(ActionEvent e) {
99         generate();
100       }
101     } );
102
103     options.addActionListener( new ActionListener() {
104       public void actionPerformed(ActionEvent e) {
105         showOptions();
106       }
107     } );
108
109     quit.addActionListener( new ActionListener() {
110       public void actionPerformed(ActionEvent e) {
111         quit();
112       }
113     } );
114     
115     stop.addActionListener( new ActionListener() {
116       public void actionPerformed(ActionEvent e) {
117         stop();
118       }
119     } );
120
121     specChoose.addActionListener( new ActionListener() {
122       public void actionPerformed(ActionEvent e) {
123         specChoose();
124       }
125     } );
126
127     dirChoose.addActionListener( new ActionListener() {
128       public void actionPerformed(ActionEvent e) {
129         dirChoose();
130       }
131     } );
132
133     spec.addActionListener( new ActionListener() {
134       public void actionPerformed(ActionEvent e) {
135         fileName = spec.getText();
136         generate();
137       }
138     } );
139     
140     spec.addTextListener( new TextListener() {
141       public void textValueChanged(TextEvent e) {
142         fileName = spec.getText();
143       }
144     } );
145     
146     dir.addActionListener( new ActionListener() {
147       public void actionPerformed(ActionEvent e) {
148         dirName = dir.getText();
149         generate();
150       }
151     } );
152     
153     dir.addTextListener( new TextListener() {
154       public void textValueChanged(TextEvent e) {
155         dirName = dir.getText();
156       }
157     } );
158
159     GridPanel north = new GridPanel(5,4,10,10);
160     north.setInsets( new Insets(10,5,5,10) );
161
162     north.add( 4,0, quit);
163     north.add( 4,1, generate);
164     north.add( 4,2, options);
165     north.add( 4,3, stop);
166
167     north.add( 0,0, BOTTOM, new Label("Lexical specification:"));
168     north.add( 0,1, 2,1, spec);
169     north.add( 2,1, specChoose);
170
171     north.add( 0,2, BOTTOM, new Label("Output directory:"));
172     north.add( 0,3, 2,1, dir);
173     north.add( 2,3, dirChoose);
174
175     Panel center = new Panel(new BorderLayout());
176     center.add("North", new Label("Messages:"));
177     center.add("Center", messages);
178
179     add("North", north);
180     add("Center", center);
181
182     setEnabledAll(false);
183   }
184
185   protected void showOptions() {
186     if (dialog == null) {
187       dialog = new OptionsDialog(this);
188     }
189     dialog.show();
190   }
191
192
193   public Dimension getPreferredSize() {
194     Dimension d = super.getPreferredSize();
195     d.width = messages.getPreferredSize().width;
196     return d;
197   }
198   
199   private void setEnabledAll(boolean generating) {
200     stop.setEnabled( generating );
201     quit.setEnabled( !generating );
202     generate.setEnabled( !generating );
203     dirChoose.setEnabled( !generating );
204     dir.setEnabled( !generating );
205     specChoose.setEnabled( !generating );
206     spec.setEnabled( !generating );
207   }
208
209   private void generate() {
210     // workaround for a weird AWT bug
211
if (choosing) return;
212    
213     setEnabledAll(true);
214
215     thread = new GeneratorThread(this, fileName, dirName);
216     thread.start();
217   }
218
219   public void generationFinished(boolean success) {
220     setEnabledAll(false);
221     
222     if (success)
223       messages.append(Out.NL+"Generation finished successfully."+Out.NL);
224     else
225       messages.append(Out.NL+"Generation aborted."+Out.NL);
226   }
227
228   private void stop() {
229     if (thread != null) {
230         /* stop ok here despite deprecation (?)
231            I don't know any good way to abort generation without changing the
232              generator code */

233       thread.stop();
234       thread = null;
235     }
236     generationFinished(false);
237   }
238  
239   private void quit() {
240     setVisible(false);
241     System.exit(0);
242   }
243   
244   private void dirChoose() {
245     choosing = true;
246     
247     FileDialog d = new FileDialog(this, "Choose directory", FileDialog.LOAD);
248     
249     d.show();
250     
251     if (d.getDirectory() != null) {
252       dir.setText( (new File JavaDoc(d.getDirectory())).getAbsolutePath() );
253     }
254     
255     choosing = false;
256   }
257
258   private void specChoose() {
259     choosing = true;
260     
261     FileDialog d = new FileDialog(this, "Choose file", FileDialog.LOAD);
262     
263     d.setFile("*.flex");
264     d.show();
265     
266     if (d.getFile() != null) {
267       fileName = d.getDirectory()+d.getFile();
268       dir.setText(d.getDirectory());
269       spec.setText(fileName);
270     }
271     
272     choosing = false;
273   }
274     
275 }
276
Popular Tags