00001 package gov.nasa.arc.ase.jpf.tools;
00002
00003 import java.awt.*;
00004 import java.io.*;
00005 import java.util.*;
00006 import java.awt.event.*;
00007 import gov.nasa.arc.ase.jpf.*;
00008
00009 public class ErrorTrail
00010 extends Frame
00011 implements WindowListener, ActionListener, KeyListener
00012 {
00013
00014
00015
00016 final private int BUFSIZE = 5000;
00017
00018
00019
00020 TextArea textArea = new TextArea();
00021
00022 private String newString = new String("New");
00023 private String openString = new String("Open");
00024 private String saveString = new String("Save");
00025 private String saveAsString = new String("Save As");
00026 private String quitString = new String("Quit");
00027
00028 private File file;
00029
00030 private String openPrompt = new String("Open text file");
00031 private String closePrompt = new String("Save file as");
00032
00033
00034
00035 private boolean neverNamed = true;
00036 private boolean dirty = false;
00037 private boolean menuBusy = false;
00038
00039 private Vector errors = new Vector();
00040 private Vector frames = new Vector();
00041 private Vector names = new Vector();
00042
00043 private int index = 0;
00044 private int current_index = 0;
00045 boolean is_errorTrail = false;
00046
00047
00048
00049 public ErrorTrail(boolean b, String file_name)
00050 {
00051 this.setTitle();
00052 is_errorTrail = b;
00053 file = new File(file_name);
00054 MenuBar mbar = new MenuBar();
00055 Menu m = new Menu("File");
00056 m.add(new MenuItem(newString));
00057 m.add(new MenuItem(openString));
00058 m.add(new MenuItem(saveString));
00059 m.add(new MenuItem(saveAsString));
00060 m.addSeparator();
00061 m.add(new MenuItem(quitString));
00062 m.addActionListener(this);
00063 mbar.add(m);
00064 setMenuBar(mbar);
00065
00066 textArea.addKeyListener(this);
00067 add("Center", textArea);
00068
00069 if(is_errorTrail){
00070 Button prev_button = new Button("Previous");
00071 Button next_button = new Button("Next");
00072 Button quit_button = new Button("Quit");
00073 Panel button_panel = new Panel();
00074 button_panel.add(prev_button);
00075 button_panel.add(quit_button);
00076 button_panel.add(next_button);
00077 add("South",button_panel);
00078 addWindowListener(this);
00079 }
00080
00081 }
00082 public boolean action(Event evt, Object arg){
00083 if(arg.equals("Quit")){
00084 System.exit(0);
00085 } else if(arg.equals("Previous")){
00086 if(index > 0){
00087 index--;
00088 current_index -= ((String)errors.elementAt(index)).length();
00089 textArea.select(current_index, current_index + ((String)errors.elementAt(index)).length());
00090 parse_error();
00091 }
00092 } else if(arg.equals("Next")){
00093 if(index < errors.size()){
00094 current_index += ((String)errors.elementAt(index)).length();
00095 index++;
00096 textArea.select(current_index, current_index + ((String)errors.elementAt(index)).length());
00097 parse_error();
00098 }
00099 }
00100 return true;
00101 }
00102
00103
00104 public void actionPerformed(ActionEvent evt)
00105 {
00106 String cmd = evt.getActionCommand();
00107
00108 if (cmd.equals(newString))
00109 {
00110 newFile();
00111 }
00112 else if (cmd.equals(openString))
00113 {
00114 openFile();
00115 }
00116 else if (cmd.equals(saveString))
00117 saveFile(false);
00118 else if (cmd.equals(saveAsString))
00119 saveFile(true);
00120 else if (cmd.equals(quitString))
00121 {
00122 System.exit(0);
00123 }
00124 }
00125 public void errorSelect(int line_num){
00126 int counter = 0;
00127 int index_num = 0;
00128 while(counter < line_num-1){
00129 index_num += ((String)errors.elementAt(counter)).length();
00130 counter++;
00131 }
00132
00133 textArea.select(index_num, index_num + ((String)errors.elementAt(counter)).length());
00134 textArea.requestFocus();
00135
00136 }
00137
00138
00139 private boolean getFile(int mode)
00140 {
00141 String prompt;
00142 String filename;
00143 String pathname;
00144
00145 if (mode == FileDialog.LOAD)
00146 prompt = openPrompt;
00147 else
00148 prompt = closePrompt;
00149
00150 FileDialog d = new FileDialog(this, prompt, mode);
00151
00152 if (mode == FileDialog.LOAD)
00153 d.setFile("*");
00154 else
00155 d.setFile(file.getName());
00156
00157 d.setDirectory(".");
00158 d.setVisible(true);
00159
00160 filename = d.getFile();
00161
00162 pathname = d.getDirectory() + filename;
00163
00164 if (filename != null)
00165 {
00166 file = new File(pathname);
00167 return(true);
00168 }
00169 else
00170 return(false);
00171 }
00172 public void keyPressed(KeyEvent e)
00173 {
00174 }
00175 public void keyReleased(KeyEvent e)
00176 {
00177 }
00178
00179
00180 public void keyTyped(KeyEvent e)
00181 {
00182 }
00183
00184
00185 public static void main(String[] args)
00186 {
00187
00188 Frame f = new ErrorTrail(true, "error.trail");
00189 f.setSize(500, 400);
00190 f.setVisible(true);
00191 }
00192
00193
00194 private void newFile()
00195 {
00196 }
00197 public void openErrorFile(){
00198 String part;
00199 StringBuffer all = new StringBuffer("");
00200
00201 try
00202 {
00203 FileReader fr = new FileReader(file);
00204 BufferedReader br=new BufferedReader(fr);
00205
00206 while ( (part=br.readLine()) != null)
00207 {
00208 errors.addElement(part+"\n");
00209 all.append(part);
00210 all.append('\n');
00211 }
00212
00213 br.close();
00214 fr.close();
00215 this.setTitle("Error Trail");
00216 textArea.setText(all.toString());
00217 textArea.select(0, ((String)errors.elementAt(0)).length());
00218 parse_error();
00219 textArea.requestFocus();
00220 }
00221 catch (IOException e)
00222 {
00223 System.out.println("Error - could not read file");
00224 }
00225 }
00226
00227
00228 private void openFile()
00229 {
00230 String part;
00231 StringBuffer all = new StringBuffer("");
00232 int lineNum = 1;
00233 String highlight ="";
00234
00235 if (getFile(FileDialog.LOAD))
00236 {
00237 try
00238 {
00239 FileReader fr = new FileReader(file);
00240 BufferedReader br=new BufferedReader(fr);
00241
00242 while ( (part=br.readLine()) != null)
00243 {
00244 if(lineNum == 50) {
00245 highlight = part;
00246 }
00247 all.append(part);
00248 all.append('\n');
00249 lineNum++;
00250 }
00251
00252 br.close();
00253 fr.close();
00254 neverNamed = false;
00255 dirty = false;
00256 this.setTitle();
00257 textArea.setText(all.toString());
00258 int index = textArea.getText().indexOf(highlight);
00259 textArea.select(index, index+ highlight.length());
00260 }
00261
00262 catch (IOException e)
00263 {
00264 }
00265 }
00266
00267 repaint();
00268 }
00269 public void openSrcFile(){
00270 String part;
00271 StringBuffer all = new StringBuffer("");
00272 try
00273 {
00274 FileReader fr = new FileReader(file);
00275 BufferedReader br=new BufferedReader(fr);
00276
00277 while ( (part=br.readLine()) != null)
00278 {
00279 errors.addElement(part+"\n");
00280 all.append(part);
00281 all.append('\n');
00282 }
00283
00284
00285 br.close();
00286 fr.close();
00287 textArea.setText(all.toString());
00288 textArea.requestFocus();
00289 }
00290 catch (IOException e)
00291 {
00292 System.out.println("Error - could not read file");
00293 }
00294 }
00295 public void parse_error(){
00296
00297 StringTokenizer command = new StringTokenizer((String)errors.elementAt(index), " ");
00298 String thread_num = command.nextToken();
00299 String file_name = command.nextToken();
00300 String line = command.nextToken();
00301 line = line.substring(0, line.length()-1);
00302 int line_num = Integer.valueOf(line).intValue();
00303 boolean found = false;
00304 int i;
00305
00306 for(i = 0; i < names.size(); ++i){
00307 if(((String)names.elementAt(i)).equals(file_name)){
00308 found = true;
00309 break;
00310 }
00311 }
00312
00313 if(found){
00314 ((ErrorTrail)frames.elementAt(i)).errorSelect(line_num);
00315 } else {
00316 frames.addElement(new ErrorTrail(false, file_name));
00317 names.addElement(file_name);
00318 ((ErrorTrail)frames.elementAt(frames.size()-1)).setSize(500, 400);
00319 ((ErrorTrail)frames.elementAt(frames.size()-1)).setVisible(true);
00320 ((ErrorTrail)frames.elementAt(frames.size()-1)).setTitle(file_name);
00321 ((ErrorTrail)frames.elementAt(frames.size()-1)).openSrcFile();
00322 ((ErrorTrail)frames.elementAt(frames.size()-1)).errorSelect(line_num);
00323 }
00324
00325 }
00326
00327
00328 private void saveFile(boolean As)
00329 {
00330 boolean ok;
00331
00332 if (neverNamed || As)
00333 {
00334 ok = getFile(FileDialog.SAVE);
00335 neverNamed = false;
00336 }
00337 else
00338 ok = true;
00339
00340 if (ok)
00341 {
00342 String all = textArea.getText();
00343 try
00344 {
00345 FileWriter fw = new FileWriter(file);
00346 BufferedWriter bw=new BufferedWriter(fw);
00347 bw.write(all,0,all.length());
00348 bw.close();
00349 fw.close();
00350 this.setTitle();
00351 }
00352
00353 catch (IOException e)
00354 {
00355 }
00356 }
00357 repaint();
00358 }
00359
00360
00361 private void setTitle()
00362 {
00363 super.setTitle("ErrorTrail");
00364 }
00365 public void windowActivated(WindowEvent event) {
00366 }
00367 public void windowClosed(WindowEvent event) {
00368 }
00369 public void windowClosing(WindowEvent event) {
00370 System.exit(0);
00371 }
00372 public void windowDeactivated(WindowEvent event) {
00373 }
00374
00375
00376 public void windowDeiconified(WindowEvent event) {
00377 }
00378 public void windowIconified(WindowEvent event) {
00379 }
00380 public void windowOpened(WindowEvent event) {
00381 if(is_errorTrail)
00382 openErrorFile();
00383 }
00384 }