1 | package org.expeditee.simple;
|
---|
2 |
|
---|
3 | import java.io.BufferedWriter;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileWriter;
|
---|
6 |
|
---|
7 | import org.expeditee.gui.Frame;
|
---|
8 | import org.expeditee.gui.FrameGraphics;
|
---|
9 | import org.expeditee.gui.FrameIO;
|
---|
10 |
|
---|
11 | public class Context {
|
---|
12 | private Primitives primitives_ = new Primitives();
|
---|
13 |
|
---|
14 | private Pointers pointers_ = new Pointers();
|
---|
15 |
|
---|
16 | public Primitives getPrimitives() {
|
---|
17 | return primitives_;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public Pointers getPointers() {
|
---|
21 | return pointers_;
|
---|
22 | }
|
---|
23 |
|
---|
24 | public Context() {
|
---|
25 | }
|
---|
26 |
|
---|
27 | public int size() {
|
---|
28 | return primitives_.size() + pointers_.size();
|
---|
29 | }
|
---|
30 |
|
---|
31 | public void clear() {
|
---|
32 | primitives_.clear();
|
---|
33 | pointers_.clear();
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void display() {
|
---|
37 | if (size() == 0) {
|
---|
38 | FrameGraphics.ErrorMessage("No variables exist!");
|
---|
39 | return;
|
---|
40 | }
|
---|
41 |
|
---|
42 | primitives_.display();
|
---|
43 | pointers_.display();
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | *
|
---|
48 | * @param var1
|
---|
49 | * @param var2
|
---|
50 | * @return
|
---|
51 | * @throws Exception
|
---|
52 | * if the variables could not be converted to the same type.
|
---|
53 | */
|
---|
54 | public boolean equalValues(String var1, String var2) throws Exception {
|
---|
55 | if (Primitives.isPrimitive(var1) && Primitives.isPrimitive(var2)) {
|
---|
56 | return primitives_.equalValues(var1, var2);
|
---|
57 | } else if (Pointers.isPointer(var1) && Pointers.isPointer(var2)) {
|
---|
58 | throw new UnsupportedOperationException(
|
---|
59 | "Pointer comparison not yet implemented");
|
---|
60 | }
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void readFrame(String frameNameVar, String frameVar,
|
---|
65 | String successVar) throws Exception {
|
---|
66 | // Get the values to be set
|
---|
67 | String frameName = primitives_.getStringValue(frameNameVar);
|
---|
68 | Frame currentFrame = FrameIO.LoadFrame(frameName);
|
---|
69 |
|
---|
70 | pointers_.setObject(frameVar, currentFrame);
|
---|
71 |
|
---|
72 | if (successVar != null) {
|
---|
73 | Boolean success = currentFrame != null;
|
---|
74 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void createFrame(String framesetNameVar, String frameVar,
|
---|
79 | String successVar) throws Exception {
|
---|
80 | // Get the values to be set
|
---|
81 | String framesetName = primitives_.getStringValue(framesetNameVar);
|
---|
82 | Frame currentFrame = FrameIO.CreateFrame(framesetName, "", null);
|
---|
83 | pointers_.setObject(frameVar, currentFrame);
|
---|
84 |
|
---|
85 | if (successVar != null) {
|
---|
86 | Boolean success = currentFrame != null;
|
---|
87 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void closeFrame(String frameVar, String successVar) throws Exception {
|
---|
92 | // Get the values to be set
|
---|
93 | Frame frame = (Frame) pointers_.getVariable(frameVar).getValue();
|
---|
94 | String contents = FrameIO.SaveFrame(frame, false);
|
---|
95 |
|
---|
96 | if (successVar != null) {
|
---|
97 | Boolean success = contents != null;
|
---|
98 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void closeWriteFile(String fileVar) throws Exception {
|
---|
103 | BufferedWriter bw = (BufferedWriter) pointers_.getVariable(fileVar)
|
---|
104 | .getValue();
|
---|
105 | bw.close();
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void writeFile(String fileVar, String text) throws Exception {
|
---|
109 | BufferedWriter bw = (BufferedWriter) pointers_.getVariable(fileVar)
|
---|
110 | .getValue();
|
---|
111 | bw.write(text);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void openWriteFile(String fileNameVar, String fileVar)
|
---|
115 | throws Exception {
|
---|
116 | openWriteFile(fileNameVar, fileVar, null);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void openWriteFile(String fileNameVar, String fileVar,
|
---|
120 | String successVar) throws Exception {
|
---|
121 | // Get the values to be set
|
---|
122 | File filePath = new File(primitives_.getStringValue(fileNameVar));
|
---|
123 | BufferedWriter currentFile = new BufferedWriter(
|
---|
124 | new FileWriter(filePath));
|
---|
125 |
|
---|
126 | pointers_.setObject(fileVar, currentFile);
|
---|
127 |
|
---|
128 | if (successVar != null) {
|
---|
129 | Boolean success = currentFile != null;
|
---|
130 | primitives_.setValue(successVar, new SBoolean(success));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public boolean isDefined(String varName) {
|
---|
135 | try {
|
---|
136 | if (Primitives.isPrimitive(varName)) {
|
---|
137 | getPrimitives().getVariable(varName);
|
---|
138 | } else {
|
---|
139 | getPointers().getVariable(varName);
|
---|
140 | }
|
---|
141 | return true;
|
---|
142 | } catch (Exception e) {
|
---|
143 | return false;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | } |
---|