001 package escjava.gui;
002
003 import javafe.InputEntry;
004 import junitutils.Utils;
005
006 import java.io.*;
007 import java.util.ArrayList;
008 import java.util.Iterator;
009 import java.lang.reflect.Field;
010
011 import javax.swing.JOptionPane;
012
013 public class Project {
014
015 static String magic = "-EscProjectFileV0"; // See also escjava.Options
016 static File currentProject = null;
017 static final String eol = System.getProperty("line.separator");
018
019 static public void init() {
020 init(new String[0]);
021 }
022
023 static public void init(String[] args) {
024 currentProject = null;
025 GUI.gui.clear(false);
026 ByteArrayOutputStream ba = Utils.setStreams();
027 try {
028 GUI.gui.run(args);
029 GUI.gui.escframe.init();
030 } finally {
031 Utils.restoreStreams(true);
032 }
033 String s = ba.toString();
034 if (s.length() != 0) JOptionPane.showMessageDialog(
035 GUI.gui.escframe,
036 "Problems reported in reading the project file: " + eol +
037 s);
038 }
039
040
041 static public void read(File file) {
042 BufferedReader r = null;
043 try {
044 r = new BufferedReader(new FileReader(file));
045 String s = r.readLine();
046 if (!s.equals(magic)) {
047 JOptionPane.showMessageDialog(GUI.gui.escframe,
048 "The selected file, " +
049 file.getAbsolutePath() +
050 ", does not appear to be a project file");
051 init();
052 return;
053 }
054 } catch (IOException e) {
055 JOptionPane.showMessageDialog(GUI.gui.escframe,
056 "The selected file, " +
057 file.getAbsolutePath() +
058 ", could not be successfully read: " + e);
059 init();
060 return;
061 } finally {
062 try {
063 if (r != null) r.close();
064 } catch (IOException e) {
065 JOptionPane.showMessageDialog(GUI.gui.escframe,
066 "The selected file, " +
067 file.getAbsolutePath() +
068 ", could not be successfully closed: " + e);
069 init();
070 return;
071 }
072 }
073 // FIXME need to capture messages about invalid material in the
074 // file.
075 init(new String[]{ "-f", file.getAbsolutePath() });
076 currentProject = file;
077 }
078
079 static public void write() {
080 write(currentProject);
081 }
082
083 static public void write(File file) {
084 FileWriter fw = null;
085 try {
086 fw = new FileWriter(file);
087
088 // Write a header that marks this as a project file
089 fw.write(magic);
090 fw.write(eol);
091
092 // Write out all the options (that aren't defaults)
093 String s = optionString();
094 if (s != null) {
095 fw.write(optionString());
096 fw.write(eol);
097 }
098
099 // Write out the classpath
100 if (GUI.gui.options.userPath != null) {
101 fw.write("-classpath ");
102 fw.write(GUI.gui.options.userPath);
103 fw.write(eol);
104 }
105 fw.write(eol);
106
107 // Write out the specs path
108 if (GUI.gui.options().specspath != null) {
109 fw.write("-specs ");
110 fw.write(GUI.gui.options().specspath);
111 fw.write(eol);
112 }
113 fw.write(eol);
114
115 // Write out the input entries
116 Iterator i = GUI.gui.options.inputEntries.iterator();
117 while (i.hasNext()) {
118 fw.write(((InputEntry)i.next()).savedString());
119 fw.write(eol);
120 }
121
122 currentProject = file;
123 } catch (Exception e) {
124 } finally {
125 try {
126 if (fw != null) fw.close();
127 } catch (java.io.IOException e) {
128 }
129 }
130 }
131
132 // FIXME - should really ahve this come from Options, rather
133 // than being part of the GUI code
134 static public String optionString() {
135 String[][] optionarray = EscOptions.optionsShown;
136 escjava.Options options = escjava.Main.options();
137 StringBuffer sb = new StringBuffer(200);
138
139 String s = System.getProperty("simplify");
140 if (s != null && s.trim().length() != 0)
141 sb.append(" -simplify " + System.getProperty("simplify") + eol);
142 for (int i=0; i<optionarray.length; ++i) {
143 String[] opt = optionarray[i];
144 if (opt[1]==null) continue;
145 try {
146 Field f = EscOptions.escoptions.getField(opt[1]);
147 boolean b = f.getBoolean(options);
148 if (b) { sb.append(" -"); sb.append(opt[0]); }
149 } catch (Exception e) {
150 System.out.println("FAILED TO RECOGNIZE FIELD " + opt[1]);
151 }
152 }
153 return sb.toString() + " " + GUI.gui.options().nowarnOptionString();
154 }
155
156 }