~ubuntu-branches/ubuntu/gutsy/jflex/gutsy

« back to all changes in this revision

Viewing changes to src/JFlex/gui/MainFrame.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2002-02-16 13:38:21 UTC
  • Revision ID: james.westby@ubuntu.com-20020216133821-5wsdprpt9xl7ondr
Tags: upstream-1.3.5
ImportĀ upstreamĀ versionĀ 1.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
2
 * JFlex 1.3.5                                                             *
 
3
 * Copyright (C) 1998-2001  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;
 
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.3.5, $Revision: 1.16 $, $Date: 2001/10/08 10:08:14 $
 
35
 */
 
36
final public class MainFrame extends Frame implements Handles {
 
37
 
 
38
  private boolean choosing;
 
39
 
 
40
  private String fileName = "";
 
41
  private String dirName = "";
 
42
  
 
43
  private Button quit; 
 
44
  private Button generate;
 
45
  private Button stop;
 
46
  private Button specChoose; 
 
47
  private Button dirChoose;
 
48
 
 
49
  private TextField spec;
 
50
  private TextField dir;
 
51
 
 
52
  private TextArea messages;
 
53
 
 
54
  private GeneratorThread thread;
 
55
 
 
56
  
 
57
  public MainFrame() {
 
58
    super("JFlex "+Main.version);
 
59
    buildContent();
 
60
    
 
61
    addWindowListener( new WindowAdapter() {
 
62
      public void windowClosing(WindowEvent e) {
 
63
        quit();
 
64
      }
 
65
    });
 
66
    
 
67
    pack();
 
68
    show();
 
69
  }
 
70
 
 
71
 
 
72
  private void buildContent() {
 
73
    setBackground(SystemColor.control);
 
74
 
 
75
    generate   = new Button("Generate");
 
76
    quit       = new Button("Quit");
 
77
    stop       = new Button("Stop");
 
78
    dirChoose  = new Button("Choose");
 
79
    dir        = new TextField(10);
 
80
    specChoose = new Button("Choose");
 
81
    spec       = new TextField(10);            
 
82
    messages   = new TextArea(10,80);
 
83
 
 
84
    messages.setEditable(false);
 
85
    Font font = messages.getFont();
 
86
    if (font != null)
 
87
      messages.setFont(new Font("Monospaced", font.getStyle(), font.getSize()));
 
88
    else
 
89
      messages.setFont(new Font("Monospaced", Font.PLAIN, 12));
 
90
 
 
91
    generate.addActionListener( new ActionListener() {
 
92
      public void actionPerformed(ActionEvent e) {
 
93
        generate();
 
94
      }
 
95
    } );
 
96
 
 
97
    quit.addActionListener( new ActionListener() {
 
98
      public void actionPerformed(ActionEvent e) {
 
99
        quit();
 
100
      }
 
101
    } );
 
102
    
 
103
    stop.addActionListener( new ActionListener() {
 
104
      public void actionPerformed(ActionEvent e) {
 
105
        stop();
 
106
      }
 
107
    } );        
 
108
 
 
109
    specChoose.addActionListener( new ActionListener() {
 
110
      public void actionPerformed(ActionEvent e) {
 
111
        specChoose();
 
112
      }
 
113
    } );
 
114
 
 
115
    dirChoose.addActionListener( new ActionListener() {
 
116
      public void actionPerformed(ActionEvent e) {
 
117
        dirChoose();
 
118
      }
 
119
    } );
 
120
 
 
121
    spec.addActionListener( new ActionListener() {
 
122
      public void actionPerformed(ActionEvent e) {
 
123
        fileName = spec.getText();
 
124
        generate();
 
125
      }
 
126
    } );
 
127
    
 
128
    spec.addTextListener( new TextListener() {
 
129
      public void textValueChanged(TextEvent e) {
 
130
        fileName = spec.getText();
 
131
      }
 
132
    } );
 
133
    
 
134
    dir.addActionListener( new ActionListener() {
 
135
      public void actionPerformed(ActionEvent e) {
 
136
        dirName = dir.getText();
 
137
        generate();
 
138
      }
 
139
    } );
 
140
    
 
141
    dir.addTextListener( new TextListener() {
 
142
      public void textValueChanged(TextEvent e) {
 
143
        dirName = dir.getText();
 
144
      }
 
145
    } );
 
146
 
 
147
    GridPanel north = new GridPanel(5,4,10,10);
 
148
    north.setInsets( new Insets(10,5,5,10) );
 
149
 
 
150
    north.add( 4,0, quit);
 
151
    north.add( 4,1, generate);
 
152
    north.add( 4,2, stop);
 
153
 
 
154
    north.add( 0,0, BOTTOM, new Label("Lexical specification:"));
 
155
    north.add( 0,1, 2,1, spec);
 
156
    north.add( 2,1, specChoose);
 
157
 
 
158
    north.add( 0,2, BOTTOM, new Label("Output directory:"));
 
159
    north.add( 0,3, 2,1, dir);
 
160
    north.add( 2,3, dirChoose);
 
161
 
 
162
    Panel center = new Panel(new BorderLayout());   
 
163
    center.add("North", new Label("Messages:"));
 
164
    center.add("Center", messages);
 
165
 
 
166
    add("North", north);
 
167
    add("Center", center);
 
168
 
 
169
    setEnabledAll(false);
 
170
  }
 
171
 
 
172
  public Dimension getPreferredSize() {
 
173
    Dimension d = super.getPreferredSize();
 
174
    d.width = messages.getPreferredSize().width;
 
175
    return d;
 
176
  }
 
177
  
 
178
  private void setEnabledAll(boolean generating) {
 
179
    stop.setEnabled( generating );
 
180
    quit.setEnabled( !generating );
 
181
    generate.setEnabled( !generating );
 
182
    dirChoose.setEnabled( !generating );
 
183
    dir.setEnabled( !generating );
 
184
    specChoose.setEnabled( !generating );
 
185
    spec.setEnabled( !generating );
 
186
  }
 
187
 
 
188
  private void generate() {
 
189
    // workaround for a weird AWT bug
 
190
    if (choosing) return;
 
191
   
 
192
    setEnabledAll(true);
 
193
 
 
194
    thread = new GeneratorThread(this, fileName, messages, dirName);
 
195
    thread.start();
 
196
  } 
 
197
 
 
198
  public void generationFinished(boolean success) {
 
199
    setEnabledAll(false);
 
200
    
 
201
    if (success) 
 
202
      messages.append(Out.NL+"Generation finished successfully."+Out.NL);
 
203
    else
 
204
      messages.append(Out.NL+"Generation aborted."+Out.NL);
 
205
  }
 
206
 
 
207
  private void stop() {
 
208
    if (thread != null) {
 
209
      thread.stop();
 
210
      thread = null;
 
211
      generationFinished(false);
 
212
    }
 
213
  }
 
214
 
 
215
  private void quit() {
 
216
    setVisible(false);
 
217
    System.exit(0);
 
218
  }
 
219
  
 
220
  private void dirChoose() {
 
221
    choosing = true;
 
222
    
 
223
    FileDialog d = new FileDialog(this, "Choose directory", FileDialog.LOAD);
 
224
    
 
225
    d.show();
 
226
    
 
227
    if (d.getDirectory() != null) {
 
228
      dir.setText( (new File(d.getDirectory())).getAbsolutePath() );
 
229
    }
 
230
    
 
231
    choosing = false;    
 
232
  }
 
233
 
 
234
  private void specChoose() {
 
235
    choosing = true;
 
236
    
 
237
    FileDialog d = new FileDialog(this, "Choose file", FileDialog.LOAD);
 
238
    
 
239
    d.setFile("*.flex");
 
240
    d.show();
 
241
    
 
242
    if (d.getFile() != null) {
 
243
      fileName = d.getDirectory()+d.getFile();
 
244
      dir.setText(d.getDirectory());
 
245
      spec.setText(fileName);
 
246
    }
 
247
    
 
248
    choosing = false;    
 
249
  }
 
250
    
 
251
}