1 package fr.ove.openmath.courses;
2
3 import java.applet.Applet;
4 import java.awt.*;
5 import java.util.*;
6 import java.io.*;
7 import fr.ove.applet.*;
8 import fr.ove.applet.events.*;
9 import fr.ove.utils.Connection;
10 import fr.ove.openmath.OpenMathizable;
11 import fr.ove.openmath.compounder.OMCompounder;
12 import fr.ove.openmath.courses.*;
13 import fr.ove.openmath.jome.Jome;
14 import fr.ove.openmath.mathematica.ResponseReader;
15
16 /***
17 *
18 */
19 public class CompounderApplet extends Applet implements AppletListener, OpenMathizable {
20 /***
21 * The connection used if we first have to send the result of the composition
22 * to a CAS before rendering.
23 */
24 private Connection connection;
25
26 /***
27 * Flag to indicates if we first have to send the result of the composition
28 * to a CAS before rendering.<BR>
29 * The default behaviour is only displaying the result of the composition.
30 */
31 private boolean doCalculation = false;
32
33 /***
34 * The reader of the response sent back by the CAS.
35 */
36 private ResponseReader reader;
37
38 /***
39 * The compounder.
40 */
41 private OMCompounder compounder;
42
43 /***
44 * The list of elements of the composition and their value.
45 */
46 private CheckedRegistry registry;
47
48 /***
49 * JOME for the rendering of the formula
50 */
51 private Jome jome;
52
53 /***
54 * Initializes the instance.
55 */
56 public void init() {
57 setLayout(new BorderLayout(0,0));
58 setBackground(Color.white);
59
60 jome = new Jome();
61 jome.setShiftX(10);
62 jome.setDrawBounds(false);
63 jome.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
64 jome.setFont(new Font("Times New Roman", Font.PLAIN, 14));
65 add("Center", jome);
66
67 String description = getParameter("description");
68 if (description != null) {
69 try {
70 InputStream is = getClass().getResourceAsStream(description);
71 BufferedReader br = new BufferedReader(new InputStreamReader(is));
72 String line;
73 StringBuffer buffer = new StringBuffer();
74 while ((line = br.readLine()) != null) {
75 buffer.append(line);
76 }
77 description = buffer.toString();
78
79 System.out.println("Description :");
80 System.out.println(description);
81
82 compounder = new OMCompounder();
83 compounder.setSource(description);
84 }
85 catch (IOException ioe) {
86 System.out.println("Failed to find the description");
87 ioe.printStackTrace();
88 }
89 }
90
91
92 String elements = getParameter("listened");
93 if (elements != null) {
94 StringTokenizer tokenizer = new StringTokenizer(elements, "{, }");
95 registry = new CheckedRegistry();
96 int numArg = 1;
97 String arg;
98 while (tokenizer.hasMoreTokens()) {
99 arg = "arg" + numArg++;
100 registry.add((String) tokenizer.nextElement(), null, arg);
101 }
102
103 }
104
105 String strDoCalculation = getParameter("doCalculation");
106 if ((strDoCalculation != null) && strDoCalculation.equals("true")) {
107 doCalculation = true;
108
109 String strPortNumber = getParameter("portNumber");
110 try {
111 int portNumber = Integer.parseInt(strPortNumber);
112 connection = new Connection(getDocumentBase().getHost(), portNumber);
113 try {
114
115 System.out.println("Ouverture de la conn");
116
117 connection.open();
118 }
119 catch (IOException ioe) {
120 doCalculation = false;
121 System.out.println("Failed to open a connection");
122 ioe.printStackTrace();
123 }
124 catch (Exception e) {
125
126
127
128 doCalculation = false;
129 System.out.println("Failed to open a connection");
130 e.printStackTrace();
131 }
132 }
133 catch (NumberFormatException nfe) {
134 System.out.println("Invalid port number specification");
135 nfe.printStackTrace();
136 }
137
138 if (connection.isOpened())
139 reader = new ResponseReader();
140 }
141 }
142
143 /***
144 * Consumes (i.e. treats) the event received.
145 * @param appletEvent the event to consume.
146 */
147 public void consumeAppletEvent(AppletEvent appletEvent) {
148 ListenableApplet listenableApplet = (ListenableApplet) appletEvent.getSource();
149 String appletName = listenableApplet.getName();
150
151 Object action = appletEvent.getAction();
152 Object argument = appletEvent.getArgument();
153
154
155
156
157 if ((action == null) && (argument == null)) {
158 if (listenableApplet instanceof AppletLoadingChecked) {
159 OpenMathizable omizable = (OpenMathizable) ((AppletLoadingChecked) listenableApplet).getTheApplet();
160 registry.setOMizable(appletName, omizable);
161 compounder.addElement(registry.getArg(appletName), omizable);
162 }
163 }
164
165 if (registry.gotAll()) {
166 compounder.compound();
167 String compounded = compounder.getCompounded();
168 if (doCalculation) {
169 try {
170
171 System.out.println("Ecriture dans la conn");
172
173 connection.getOutputStream().write(compounded.getBytes());
174 String response = reader.read(connection.getInputStream());
175
176 if (response != null)
177 setOpenMath(response);
178 else
179 setOpenMath(compounded);
180 }
181 catch (IOException ioe) {
182 System.out.println("Communication problems encountered");
183 ioe.printStackTrace();
184 }
185 }
186 else
187 setOpenMath(compounded);
188 }
189
190 if (getParent() instanceof ListenableApplet) {
191 ListenableApplet parentApplet = (ListenableApplet) getParent();
192
193 AppletEvent thisAppletEvent = new AppletEvent(parentApplet);
194 thisAppletEvent.setAction(parentApplet.getName(), this);
195 parentApplet.fireAppletEvent(thisAppletEvent);
196 }
197 }
198
199 /***
200 * Sets the OpenMath object.
201 * @param obj the OpenMath object.
202 */
203 public void setOpenMath(String openMath) {
204 jome.setOpenMath(openMath);
205 }
206
207 /***
208 * Returns the OpenMath object.
209 */
210 public String getOpenMath() {
211 return jome.getOpenMath();
212 }
213 }