source: trunk/src/org/expeditee/simple/Context.java@ 21

Last change on this file since 21 was 21, checked in by ra33, 16 years ago
File size: 6.3 KB
Line 
1package org.expeditee.simple;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileReader;
7import java.io.FileWriter;
8
9import org.expeditee.gui.Frame;
10import org.expeditee.gui.FrameGraphics;
11import org.expeditee.gui.FrameIO;
12import org.expeditee.items.Text;
13
14public class Context {
15 private Primitives primitives_ = new Primitives();
16
17 private Pointers pointers_ = new Pointers();
18
19 public Primitives getPrimitives() {
20 return primitives_;
21 }
22
23 public Pointers getPointers() {
24 return pointers_;
25 }
26
27 public Context() {
28 }
29
30 public int size() {
31 return primitives_.size() + pointers_.size();
32 }
33
34 public void clear() {
35 primitives_.clear();
36 pointers_.clear();
37 }
38
39 public void display() {
40 if (size() == 0) {
41 FrameGraphics.ErrorMessage("No variables exist!");
42 return;
43 }
44
45 primitives_.display();
46 pointers_.display();
47 }
48
49 /**
50 *
51 * @param var1
52 * @param var2
53 * @return
54 * @throws Exception
55 * if the variables could not be converted to the same type.
56 */
57 public boolean equalValues(String var1, String var2) throws Exception {
58 if (Primitives.isPrimitive(var1) && Primitives.isPrimitive(var2)) {
59 return primitives_.equalValues(var1, var2);
60 } else if (Pointers.isPointer(var1) && Pointers.isPointer(var2)) {
61 throw new UnsupportedOperationException(
62 "Pointer comparison not yet implemented");
63 }
64 return false;
65 }
66
67 public void readFrame(String frameNameVar, String frameVar,
68 String successVar) throws Exception {
69 // Get the values to be set
70 String frameName = primitives_.getStringValue(frameNameVar);
71 Frame currentFrame = FrameIO.LoadFrame(frameName);
72
73 pointers_.setObject(frameVar, currentFrame);
74
75 if (successVar != null) {
76 Boolean success = currentFrame != null;
77 primitives_.setValue(successVar, new SBoolean(success));
78 }
79 }
80
81 public void createFrame(String framesetNameVar, String frameVar,
82 String successVar) throws Exception {
83 // Get the values to be set
84 String framesetName = primitives_.getStringValue(framesetNameVar);
85 Frame currentFrame = FrameIO.CreateFrame(framesetName, "", null);
86 pointers_.setObject(frameVar, currentFrame);
87
88 if (successVar != null) {
89 Boolean success = currentFrame != null;
90 primitives_.setValue(successVar, new SBoolean(success));
91 }
92 }
93
94 public void closeFrame(String frameVar, String successVar) throws Exception {
95 // Get the values to be set
96 Frame frame = (Frame) pointers_.getVariable(frameVar).getValue();
97 frame.change();
98 String contents = FrameIO.SaveFrame(frame, false);
99
100 if (successVar != null) {
101 Boolean success = contents != null;
102 primitives_.setValue(successVar, new SBoolean(success));
103 }
104 }
105
106 public void closeWriteFile(String fileVar) throws Exception {
107 BufferedWriter bw = (BufferedWriter) pointers_.getVariable(fileVar)
108 .getValue();
109 bw.close();
110 }
111
112 public void closeReadFile(String fileVar) throws Exception {
113 BufferedReader br = (BufferedReader) pointers_.getVariable(fileVar)
114 .getValue();
115 br.close();
116 }
117
118 public void writeFile(String fileVar, String text) throws Exception {
119 BufferedWriter bw = (BufferedWriter) pointers_.getVariable(fileVar)
120 .getValue();
121 bw.write(text);
122 }
123
124 public void openWriteFile(String fileNameVar, String fileVar)
125 throws Exception {
126 openWriteFile(fileNameVar, fileVar, null);
127 }
128
129 public void openWriteFile(String fileNameVar, String fileVar,
130 String successVar) throws Exception {
131 // Get the values to be set
132 File filePath = new File(primitives_.getStringValue(fileNameVar));
133 BufferedWriter currentFile = new BufferedWriter(
134 new FileWriter(filePath));
135
136 pointers_.setObject(fileVar, currentFile);
137
138 if (successVar != null) {
139 Boolean success = currentFile != null;
140 primitives_.setValue(successVar, new SBoolean(success));
141 }
142 }
143
144 public void readLineFile(String fileVar, String textVar) throws Exception {
145 readLineFile(fileVar, fileVar, null);
146 }
147
148 public void readLineFile(String fileVar, String textVar, String successVar)
149 throws Exception {
150
151 BufferedReader br = (BufferedReader) pointers_.getVariable(fileVar)
152 .getValue();
153
154 String text = br.readLine();
155 boolean success = text != null;
156 primitives_.setValue(textVar, text);
157
158 if (successVar != null) {
159 primitives_.setValue(successVar, new SBoolean(success));
160 }
161 }
162
163 public void readItemFile(String fileVar, String itemVar) throws Exception {
164 readItemFile(fileVar, fileVar, null);
165 }
166
167 public void readItemFile(String fileVar, String itemVar, String successVar)
168 throws Exception {
169
170 BufferedReader br = (BufferedReader) pointers_.getVariable(fileVar)
171 .getValue();
172 boolean success = true;
173 try {
174 Text item = (Text) pointers_.getVariable(itemVar).getValue();
175 String nextLine = br.readLine();
176 String text = nextLine;
177 success = (text != null);
178 while ((nextLine = br.readLine()) != null)
179 text += '\n' + nextLine;
180 item.setText(text);
181 } catch (Exception e) {
182 success = false;
183 }
184
185 if (successVar != null) {
186 primitives_.setValue(successVar, new SBoolean(success));
187 }
188 }
189
190 public void openReadFile(String fileNameVar, String fileVar)
191 throws Exception {
192 openReadFile(fileNameVar, fileVar, null);
193 }
194
195 public void openReadFile(String fileNameVar, String fileVar,
196 String successVar) throws Exception {
197 // Get the values to be set
198 File filePath = new File(primitives_.getStringValue(fileNameVar));
199 Boolean success = true;
200 BufferedReader currentFile = null;
201 try {
202 currentFile = new BufferedReader(new FileReader(filePath));
203 } catch (Exception e) {
204 success = false;
205 }
206 pointers_.setObject(fileVar, currentFile);
207
208 if (successVar != null) {
209 primitives_.setValue(successVar, new SBoolean(success));
210 }
211 }
212
213 public boolean isDefined(String varName) {
214 try {
215 if (Primitives.isPrimitive(varName)) {
216 getPrimitives().getVariable(varName);
217 } else {
218 getPointers().getVariable(varName);
219 }
220 return true;
221 } catch (Exception e) {
222 return false;
223 }
224 }
225
226 public void createFrameset(String framesetNameVar, String successVar)
227 throws IncorrectTypeException {
228 boolean success = true;
229 try {
230 // Get the values to be set
231 String framesetName = primitives_.getStringValue(framesetNameVar);
232 Frame firstFrame = FrameIO.CreateNewFrameset(framesetName);
233 success = firstFrame != null;
234 } catch (Exception e) {
235 success = false;
236 }
237 if (successVar != null) {
238 primitives_.setValue(successVar, new SBoolean(success));
239 }
240
241 }
242}
Note: See TracBrowser for help on using the repository browser.