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

Last change on this file since 121 was 121, checked in by bjn8, 16 years ago

Added invalidation for graphics... biiiig commit. LOts of effeciency improvements - now can animate

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