author | prr |
Thu, 18 Dec 2014 10:45:45 -0800 | |
changeset 29908 | 83e2c403fefd |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
7 |
* |
|
8 |
* - Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
14 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 16 |
* contributors may be used to endorse or promote products derived |
17 |
* from this software without specific prior written permission. |
|
18 |
* |
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
31 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
32 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8978
diff
changeset
|
40 |
|
2 | 41 |
|
42 |
import java.applet.Applet; |
|
43 |
import java.awt.*; |
|
44 |
import java.awt.event.*; |
|
45 |
import java.io.*; |
|
46 |
import java.net.*; |
|
47 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
48 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
49 |
@SuppressWarnings("serial") |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
50 |
public class SpreadSheet extends Applet implements MouseListener, KeyListener { |
2 | 51 |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
52 |
String title; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
53 |
Font titleFont; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
54 |
Color cellColor; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
55 |
Color inputColor; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
56 |
int cellWidth = 100; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
57 |
int cellHeight = 15; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
58 |
int titleHeight = 15; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
59 |
int rowLabelWidth = 15; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
60 |
Font inputFont; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
61 |
boolean isStopped = false; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
62 |
boolean fullUpdate = true; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
63 |
int rows; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
64 |
int columns; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
65 |
int currentKey = -1; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
66 |
int selectedRow = -1; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
67 |
int selectedColumn = -1; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
68 |
SpreadSheetInput inputArea; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
69 |
Cell cells[][]; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
70 |
Cell current = null; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
71 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
72 |
@Override |
2 | 73 |
public synchronized void init() { |
74 |
String rs; |
|
75 |
||
76 |
cellColor = Color.white; |
|
77 |
inputColor = new Color(100, 100, 225); |
|
78 |
inputFont = new Font("Monospaced", Font.PLAIN, 10); |
|
79 |
titleFont = new Font("Monospaced", Font.BOLD, 12); |
|
80 |
title = getParameter("title"); |
|
81 |
if (title == null) { |
|
82 |
title = "Spreadsheet"; |
|
83 |
} |
|
84 |
rs = getParameter("rows"); |
|
85 |
if (rs == null) { |
|
86 |
rows = 9; |
|
87 |
} else { |
|
88 |
rows = Integer.parseInt(rs); |
|
89 |
} |
|
90 |
rs = getParameter("columns"); |
|
91 |
if (rs == null) { |
|
92 |
columns = 5; |
|
93 |
} else { |
|
94 |
columns = Integer.parseInt(rs); |
|
95 |
} |
|
96 |
cells = new Cell[rows][columns]; |
|
97 |
char l[] = new char[1]; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
98 |
for (int i = 0; i < rows; i++) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
99 |
for (int j = 0; j < columns; j++) { |
2 | 100 |
|
101 |
cells[i][j] = new Cell(this, |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
102 |
Color.lightGray, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
103 |
Color.black, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
104 |
cellColor, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
105 |
cellWidth - 2, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
106 |
cellHeight - 2); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
107 |
l[0] = (char) ((int) 'a' + j); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
108 |
rs = getParameter("" + new String(l) + (i + 1)); |
2 | 109 |
if (rs != null) { |
110 |
cells[i][j].setUnparsedValue(rs); |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
Dimension d = getSize(); |
|
116 |
inputArea = new SpreadSheetInput(null, this, d.width - 2, cellHeight - 1, |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
117 |
inputColor, Color.white); |
2 | 118 |
resize(columns * cellWidth + rowLabelWidth, |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
119 |
(rows + 3) * cellHeight + titleHeight); |
2 | 120 |
addMouseListener(this); |
121 |
addKeyListener(this); |
|
122 |
} |
|
123 |
||
124 |
public void setCurrentValue(float val) { |
|
125 |
if (selectedRow == -1 || selectedColumn == -1) { |
|
126 |
return; |
|
127 |
} |
|
128 |
cells[selectedRow][selectedColumn].setValue(val); |
|
129 |
repaint(); |
|
130 |
} |
|
131 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
132 |
@Override |
2 | 133 |
public void stop() { |
134 |
isStopped = true; |
|
135 |
} |
|
136 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
137 |
@Override |
2 | 138 |
public void start() { |
139 |
isStopped = false; |
|
140 |
} |
|
141 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
142 |
@Override |
2 | 143 |
public void destroy() { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
144 |
for (int i = 0; i < rows; i++) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
145 |
for (int j = 0; j < columns; j++) { |
2 | 146 |
if (cells[i][j].type == Cell.URL) { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
147 |
cells[i][j].updaterThread.run = false; |
2 | 148 |
} |
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
public void setCurrentValue(int type, String val) { |
|
154 |
if (selectedRow == -1 || selectedColumn == -1) { |
|
155 |
return; |
|
156 |
} |
|
157 |
cells[selectedRow][selectedColumn].setValue(type, val); |
|
158 |
repaint(); |
|
159 |
} |
|
160 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
161 |
@Override |
2 | 162 |
public void update(Graphics g) { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
163 |
if (!fullUpdate) { |
2 | 164 |
int cx, cy; |
165 |
||
166 |
g.setFont(titleFont); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
167 |
for (int i = 0; i < rows; i++) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
168 |
for (int j = 0; j < columns; j++) { |
2 | 169 |
if (cells[i][j].needRedisplay) { |
170 |
cx = (j * cellWidth) + 2 + rowLabelWidth; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
171 |
cy = ((i + 1) * cellHeight) + 2 + titleHeight; |
2 | 172 |
cells[i][j].paint(g, cx, cy); |
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
} else { |
|
177 |
paint(g); |
|
178 |
fullUpdate = false; |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
public void recalculate() { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
183 |
int i, j; |
2 | 184 |
|
185 |
//System.out.println("SpreadSheet.recalculate"); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
186 |
for (i = 0; i < rows; i++) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
187 |
for (j = 0; j < columns; j++) { |
2 | 188 |
if (cells[i][j] != null && cells[i][j].type == Cell.FORMULA) { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
189 |
cells[i][j].setRawValue(evaluateFormula( |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
190 |
cells[i][j].parseRoot)); |
2 | 191 |
cells[i][j].needRedisplay = true; |
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
repaint(); |
|
196 |
} |
|
197 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
198 |
float evaluateFormula(Node n) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
199 |
float val = 0.0f; |
2 | 200 |
|
201 |
//System.out.println("evaluateFormula:"); |
|
202 |
//n.print(3); |
|
203 |
if (n == null) { |
|
204 |
//System.out.println("Null node"); |
|
205 |
return val; |
|
206 |
} |
|
207 |
switch (n.type) { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
208 |
case Node.OP: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
209 |
val = evaluateFormula(n.left); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
210 |
switch (n.op) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
211 |
case '+': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
212 |
val += evaluateFormula(n.right); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
213 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
214 |
case '*': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
215 |
val *= evaluateFormula(n.right); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
216 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
217 |
case '-': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
218 |
val -= evaluateFormula(n.right); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
219 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
220 |
case '/': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
221 |
val /= evaluateFormula(n.right); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
222 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
223 |
} |
2 | 224 |
break; |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
225 |
case Node.VALUE: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
226 |
//System.out.println("=>" + n.value); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
227 |
return n.value; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
228 |
case Node.CELL: |
2 | 229 |
if (cells[n.row][n.column] == null) { |
230 |
//System.out.println("NULL at 193"); |
|
231 |
} else { |
|
232 |
//System.out.println("=>" + cells[n.row][n.column].value); |
|
233 |
return cells[n.row][n.column].value; |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
//System.out.println("=>" + val); |
|
238 |
return val; |
|
239 |
} |
|
240 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
241 |
@Override |
2 | 242 |
public synchronized void paint(Graphics g) { |
243 |
int i, j; |
|
244 |
int cx, cy; |
|
245 |
char l[] = new char[1]; |
|
246 |
||
247 |
||
248 |
Dimension d = getSize(); |
|
249 |
||
250 |
g.setFont(titleFont); |
|
251 |
i = g.getFontMetrics().stringWidth(title); |
|
252 |
g.drawString((title == null) ? "Spreadsheet" : title, |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
253 |
(d.width - i) / 2, 12); |
2 | 254 |
g.setColor(inputColor); |
255 |
g.fillRect(0, cellHeight, d.width, cellHeight); |
|
256 |
g.setFont(titleFont); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
257 |
for (i = 0; i < rows + 1; i++) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
258 |
cy = (i + 2) * cellHeight; |
2 | 259 |
g.setColor(getBackground()); |
260 |
g.draw3DRect(0, cy, d.width, 2, true); |
|
261 |
if (i < rows) { |
|
262 |
g.setColor(Color.red); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
263 |
g.drawString("" + (i + 1), 2, cy + 12); |
2 | 264 |
} |
265 |
} |
|
266 |
||
267 |
g.setColor(Color.red); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
268 |
cy = (rows + 3) * cellHeight + (cellHeight / 2); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
269 |
for (i = 0; i < columns; i++) { |
2 | 270 |
cx = i * cellWidth; |
271 |
g.setColor(getBackground()); |
|
272 |
g.draw3DRect(cx + rowLabelWidth, |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
273 |
2 * cellHeight, 1, d.height, true); |
2 | 274 |
if (i < columns) { |
275 |
g.setColor(Color.red); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
276 |
l[0] = (char) ((int) 'A' + i); |
2 | 277 |
g.drawString(new String(l), |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
278 |
cx + rowLabelWidth + (cellWidth / 2), |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
279 |
cy); |
2 | 280 |
} |
281 |
} |
|
282 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
283 |
for (i = 0; i < rows; i++) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
284 |
for (j = 0; j < columns; j++) { |
2 | 285 |
cx = (j * cellWidth) + 2 + rowLabelWidth; |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
286 |
cy = ((i + 1) * cellHeight) + 2 + titleHeight; |
2 | 287 |
if (cells[i][j] != null) { |
288 |
cells[i][j].paint(g, cx, cy); |
|
289 |
} |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
g.setColor(getBackground()); |
|
294 |
g.draw3DRect(0, titleHeight, |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
295 |
d.width, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
296 |
d.height - titleHeight, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
297 |
false); |
2 | 298 |
inputArea.paint(g, 1, titleHeight + 1); |
299 |
} |
|
300 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
301 |
//1.1 event handling |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
302 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
303 |
public void mouseClicked(MouseEvent e) { |
2 | 304 |
} |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
305 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
306 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
307 |
public void mousePressed(MouseEvent e) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
308 |
int x = e.getX(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
309 |
int y = e.getY(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
310 |
Cell cell; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
311 |
if (y < (titleHeight + cellHeight)) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
312 |
selectedRow = -1; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
313 |
if (y <= titleHeight && current != null) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
314 |
current.deselect(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
315 |
current = null; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
316 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
317 |
e.consume(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
318 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
319 |
if (x < rowLabelWidth) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
320 |
selectedRow = -1; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
321 |
if (current != null) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
322 |
current.deselect(); |
2 | 323 |
current = null; |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
324 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
325 |
e.consume(); |
2 | 326 |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
327 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
328 |
selectedRow = ((y - cellHeight - titleHeight) / cellHeight); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
329 |
selectedColumn = (x - rowLabelWidth) / cellWidth; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
330 |
if (selectedRow > rows |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
331 |
|| selectedColumn >= columns) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
332 |
selectedRow = -1; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
333 |
if (current != null) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
334 |
current.deselect(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
335 |
current = null; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
336 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
337 |
} else { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
338 |
if (selectedRow >= rows) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
339 |
selectedRow = -1; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
340 |
if (current != null) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
341 |
current.deselect(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
342 |
current = null; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
343 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
344 |
e.consume(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
345 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
346 |
if (selectedRow != -1) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
347 |
cell = cells[selectedRow][selectedColumn]; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
348 |
inputArea.setText(cell.getPrintString()); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
349 |
if (current != null) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
350 |
current.deselect(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
351 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
352 |
current = cell; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
353 |
current.select(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
354 |
requestFocus(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
355 |
fullUpdate = true; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
356 |
repaint(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
357 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
358 |
e.consume(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
359 |
} |
2 | 360 |
} |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
361 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
362 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
363 |
public void mouseReleased(MouseEvent e) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
364 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
365 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
366 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
367 |
public void mouseEntered(MouseEvent e) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
368 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
369 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
370 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
371 |
public void mouseExited(MouseEvent e) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
372 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
373 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
374 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
375 |
public void keyPressed(KeyEvent e) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
376 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
377 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
378 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
379 |
public void keyTyped(KeyEvent e) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
380 |
fullUpdate = true; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
381 |
inputArea.processKey(e); |
2 | 382 |
e.consume(); |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
383 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
384 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
385 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
386 |
public void keyReleased(KeyEvent e) { |
2 | 387 |
} |
388 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
389 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
390 |
public String getAppletInfo() { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
391 |
return "Title: SpreadSheet \nAuthor: Sami Shaio \nA simple spread sheet."; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
392 |
} |
2 | 393 |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
394 |
@Override |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
395 |
public String[][] getParameterInfo() { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
396 |
String[][] info = { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
397 |
{ "title", "string", |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
398 |
"The title of the spread sheet. Default is 'Spreadsheet'" }, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
399 |
{ "rows", "int", "The number of rows. Default is 9." }, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
400 |
{ "columns", "int", "The number of columns. Default is 5." } |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
401 |
}; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
402 |
return info; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
403 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
404 |
} |
2 | 405 |
|
406 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
407 |
class CellUpdater extends Thread { |
2 | 408 |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
409 |
Cell target; |
2 | 410 |
InputStream dataStream = null; |
411 |
StreamTokenizer tokenStream; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
412 |
public volatile boolean run = true; |
2 | 413 |
|
414 |
public CellUpdater(Cell c) { |
|
415 |
super("cell updater"); |
|
416 |
target = c; |
|
417 |
} |
|
418 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
419 |
@Override |
2 | 420 |
public void run() { |
421 |
try { |
|
422 |
dataStream = new URL(target.app.getDocumentBase(), |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
423 |
target.getValueString()).openStream(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
424 |
tokenStream = new StreamTokenizer(new BufferedReader( |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
425 |
new InputStreamReader(dataStream))); |
2 | 426 |
tokenStream.eolIsSignificant(false); |
427 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
428 |
while (run) { |
2 | 429 |
switch (tokenStream.nextToken()) { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
430 |
case StreamTokenizer.TT_EOF: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
431 |
dataStream.close(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
432 |
return; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
433 |
default: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
434 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
435 |
case StreamTokenizer.TT_NUMBER: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
436 |
target.setTransientValue((float) tokenStream.nval); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
437 |
if (!target.app.isStopped && !target.paused) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
438 |
target.app.repaint(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
439 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
440 |
break; |
2 | 441 |
} |
442 |
try { |
|
443 |
Thread.sleep(2000); |
|
444 |
} catch (InterruptedException e) { |
|
445 |
break; |
|
446 |
} |
|
447 |
} |
|
448 |
} catch (IOException e) { |
|
449 |
return; |
|
450 |
} |
|
451 |
} |
|
452 |
} |
|
453 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
454 |
|
2 | 455 |
class Cell { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
456 |
|
2 | 457 |
public static final int VALUE = 0; |
458 |
public static final int LABEL = 1; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
459 |
public static final int URL = 2; |
2 | 460 |
public static final int FORMULA = 3; |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
461 |
Node parseRoot; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
462 |
boolean needRedisplay; |
2 | 463 |
boolean selected = false; |
464 |
boolean transientValue = false; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
465 |
public int type = Cell.VALUE; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
466 |
String valueString = ""; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
467 |
String printString = "v"; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
468 |
float value; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
469 |
Color bgColor; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
470 |
Color fgColor; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
471 |
Color highlightColor; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
472 |
int width; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
473 |
int height; |
2 | 474 |
SpreadSheet app; |
475 |
CellUpdater updaterThread; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
476 |
boolean paused = false; |
2 | 477 |
|
478 |
public Cell(SpreadSheet app, |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
479 |
Color bgColor, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
480 |
Color fgColor, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
481 |
Color highlightColor, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
482 |
int width, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
483 |
int height) { |
2 | 484 |
this.app = app; |
485 |
this.bgColor = bgColor; |
|
486 |
this.fgColor = fgColor; |
|
487 |
this.highlightColor = highlightColor; |
|
488 |
this.width = width; |
|
489 |
this.height = height; |
|
490 |
needRedisplay = true; |
|
491 |
} |
|
492 |
||
493 |
public void setRawValue(float f) { |
|
494 |
valueString = Float.toString(f); |
|
495 |
value = f; |
|
496 |
} |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
497 |
|
2 | 498 |
public void setValue(float f) { |
499 |
setRawValue(f); |
|
500 |
printString = "v" + valueString; |
|
501 |
type = Cell.VALUE; |
|
502 |
paused = false; |
|
503 |
app.recalculate(); |
|
504 |
needRedisplay = true; |
|
505 |
} |
|
506 |
||
507 |
public void setTransientValue(float f) { |
|
508 |
transientValue = true; |
|
509 |
value = f; |
|
510 |
needRedisplay = true; |
|
511 |
app.recalculate(); |
|
512 |
} |
|
513 |
||
514 |
public void setUnparsedValue(String s) { |
|
515 |
switch (s.charAt(0)) { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
516 |
case 'v': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
517 |
setValue(Cell.VALUE, s.substring(1)); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
518 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
519 |
case 'f': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
520 |
setValue(Cell.FORMULA, s.substring(1)); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
521 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
522 |
case 'l': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
523 |
setValue(Cell.LABEL, s.substring(1)); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
524 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
525 |
case 'u': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
526 |
setValue(Cell.URL, s.substring(1)); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
527 |
break; |
2 | 528 |
} |
529 |
} |
|
530 |
||
531 |
/** |
|
532 |
* Parse a spreadsheet formula. The syntax is defined as: |
|
533 |
* |
|
534 |
* formula -> value |
|
535 |
* formula -> value op value |
|
536 |
* value -> '(' formula ')' |
|
537 |
* value -> cell |
|
538 |
* value -> <number> |
|
539 |
* op -> '+' | '*' | '/' | '-' |
|
540 |
* cell -> <letter><number> |
|
541 |
*/ |
|
542 |
public String parseFormula(String formula, Node node) { |
|
543 |
String subformula; |
|
544 |
String restFormula; |
|
545 |
Node left; |
|
546 |
Node right; |
|
547 |
char op; |
|
548 |
||
549 |
if (formula == null) { |
|
550 |
return null; |
|
551 |
} |
|
552 |
subformula = parseValue(formula, node); |
|
553 |
//System.out.println("subformula = " + subformula); |
|
554 |
if (subformula == null || subformula.length() == 0) { |
|
555 |
//System.out.println("Parse succeeded"); |
|
556 |
return null; |
|
557 |
} |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
558 |
if (subformula.equals(formula)) { |
2 | 559 |
//System.out.println("Parse failed"); |
560 |
return formula; |
|
561 |
} |
|
562 |
||
563 |
// parse an operator and then another value |
|
564 |
switch (op = subformula.charAt(0)) { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
565 |
case 0: |
2 | 566 |
//System.out.println("Parse succeeded"); |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
567 |
return null; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
568 |
case ')': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
569 |
//System.out.println("Returning subformula=" + subformula); |
2 | 570 |
return subformula; |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
571 |
case '+': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
572 |
case '*': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
573 |
case '-': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
574 |
case '/': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
575 |
restFormula = subformula.substring(1); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
576 |
subformula = parseValue(restFormula, right = new Node()); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
577 |
//System.out.println("subformula(2) = " + subformula); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
578 |
if (subformula == null ? restFormula != null : !subformula. |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
579 |
equals(restFormula)) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
580 |
//System.out.println("Parse succeeded"); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
581 |
left = new Node(node); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
582 |
node.left = left; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
583 |
node.right = right; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
584 |
node.op = op; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
585 |
node.type = Node.OP; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
586 |
//node.print(3); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
587 |
return subformula; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
588 |
} else { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
589 |
//System.out.println("Parse failed"); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
590 |
return formula; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
591 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
592 |
default: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
593 |
//System.out.println("Parse failed (bad operator): " + subformula); |
2 | 594 |
return formula; |
595 |
} |
|
596 |
} |
|
597 |
||
598 |
public String parseValue(String formula, Node node) { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
599 |
char c = formula.charAt(0); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
600 |
String subformula; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
601 |
String restFormula; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
602 |
float _value; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
603 |
int row; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
604 |
int column; |
2 | 605 |
|
606 |
//System.out.println("parseValue: " + formula); |
|
607 |
restFormula = formula; |
|
608 |
if (c == '(') { |
|
609 |
//System.out.println("parseValue(" + formula + ")"); |
|
610 |
restFormula = formula.substring(1); |
|
611 |
subformula = parseFormula(restFormula, node); |
|
612 |
//System.out.println("rest=(" + subformula + ")"); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
613 |
if (subformula == null |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
614 |
|| subformula.length() == restFormula.length()) { |
2 | 615 |
//System.out.println("Failed"); |
616 |
return formula; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
617 |
} else if (!(subformula.charAt(0) == ')')) { |
2 | 618 |
//System.out.println("Failed (missing parentheses)"); |
619 |
return formula; |
|
620 |
} |
|
621 |
restFormula = subformula; |
|
622 |
} else if (c >= '0' && c <= '9') { |
|
623 |
int i; |
|
624 |
||
625 |
//System.out.println("formula=" + formula); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
626 |
for (i = 0; i < formula.length(); i++) { |
2 | 627 |
c = formula.charAt(i); |
628 |
if ((c < '0' || c > '9') && c != '.') { |
|
629 |
break; |
|
630 |
} |
|
631 |
} |
|
632 |
try { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
633 |
_value = Float.valueOf(formula.substring(0, i)).floatValue(); |
2 | 634 |
} catch (NumberFormatException e) { |
635 |
//System.out.println("Failed (number format error)"); |
|
636 |
return formula; |
|
637 |
} |
|
638 |
node.type = Node.VALUE; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
639 |
node.value = _value; |
2 | 640 |
//node.print(3); |
641 |
restFormula = formula.substring(i); |
|
642 |
//System.out.println("value= " + value + " i=" + i + |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
643 |
// " rest = " + restFormula); |
2 | 644 |
return restFormula; |
645 |
} else if (c >= 'A' && c <= 'Z') { |
|
646 |
int i; |
|
647 |
||
648 |
column = c - 'A'; |
|
649 |
restFormula = formula.substring(1); |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
650 |
for (i = 0; i < restFormula.length(); i++) { |
2 | 651 |
c = restFormula.charAt(i); |
652 |
if (c < '0' || c > '9') { |
|
653 |
break; |
|
654 |
} |
|
655 |
} |
|
656 |
row = Float.valueOf(restFormula.substring(0, i)).intValue(); |
|
657 |
//System.out.println("row = " + row + " column = " + column); |
|
658 |
node.row = row - 1; |
|
659 |
node.column = column; |
|
660 |
node.type = Node.CELL; |
|
661 |
//node.print(3); |
|
662 |
if (i == restFormula.length()) { |
|
663 |
restFormula = null; |
|
664 |
} else { |
|
665 |
restFormula = restFormula.substring(i); |
|
666 |
if (restFormula.charAt(0) == 0) { |
|
667 |
return null; |
|
668 |
} |
|
669 |
} |
|
670 |
} |
|
671 |
||
672 |
return restFormula; |
|
673 |
} |
|
674 |
||
675 |
public void setValue(int type, String s) { |
|
676 |
paused = false; |
|
677 |
if (this.type == Cell.URL) { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
678 |
updaterThread.run = false; |
2 | 679 |
updaterThread = null; |
680 |
} |
|
681 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
682 |
valueString = s; |
2 | 683 |
this.type = type; |
684 |
needRedisplay = true; |
|
685 |
switch (type) { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
686 |
case Cell.VALUE: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
687 |
setValue(Float.valueOf(s).floatValue()); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
688 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
689 |
case Cell.LABEL: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
690 |
printString = "l" + valueString; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
691 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
692 |
case Cell.URL: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
693 |
printString = "u" + valueString; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
694 |
updaterThread = new CellUpdater(this); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
695 |
updaterThread.start(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
696 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
697 |
case Cell.FORMULA: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
698 |
parseFormula(valueString, parseRoot = new Node()); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
699 |
printString = "f" + valueString; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
700 |
break; |
2 | 701 |
} |
702 |
app.recalculate(); |
|
703 |
} |
|
704 |
||
705 |
public String getValueString() { |
|
706 |
return valueString; |
|
707 |
} |
|
708 |
||
709 |
public String getPrintString() { |
|
710 |
return printString; |
|
711 |
} |
|
712 |
||
713 |
public void select() { |
|
714 |
selected = true; |
|
715 |
paused = true; |
|
716 |
} |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
717 |
|
2 | 718 |
public void deselect() { |
719 |
selected = false; |
|
720 |
paused = false; |
|
721 |
needRedisplay = true; |
|
722 |
app.repaint(); |
|
723 |
} |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
724 |
|
2 | 725 |
public void paint(Graphics g, int x, int y) { |
726 |
if (selected) { |
|
727 |
g.setColor(highlightColor); |
|
728 |
} else { |
|
729 |
g.setColor(bgColor); |
|
730 |
} |
|
731 |
g.fillRect(x, y, width - 1, height); |
|
732 |
if (valueString != null) { |
|
733 |
switch (type) { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
734 |
case Cell.VALUE: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
735 |
case Cell.LABEL: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
736 |
g.setColor(fgColor); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
737 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
738 |
case Cell.FORMULA: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
739 |
g.setColor(Color.red); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
740 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
741 |
case Cell.URL: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
742 |
g.setColor(Color.blue); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
743 |
break; |
2 | 744 |
} |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
745 |
if (transientValue) { |
2 | 746 |
g.drawString("" + value, x, y + (height / 2) + 5); |
747 |
} else { |
|
748 |
if (valueString.length() > 14) { |
|
749 |
g.drawString(valueString.substring(0, 14), |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
750 |
x, y + (height / 2) + 5); |
2 | 751 |
} else { |
752 |
g.drawString(valueString, x, y + (height / 2) + 5); |
|
753 |
} |
|
754 |
} |
|
755 |
} |
|
756 |
needRedisplay = false; |
|
757 |
} |
|
758 |
} |
|
759 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
760 |
|
2 | 761 |
class Node { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
762 |
|
2 | 763 |
public static final int OP = 0; |
764 |
public static final int VALUE = 1; |
|
765 |
public static final int CELL = 2; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
766 |
int type; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
767 |
Node left; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
768 |
Node right; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
769 |
int row; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
770 |
int column; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
771 |
float value; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
772 |
char op; |
2 | 773 |
|
774 |
public Node() { |
|
775 |
left = null; |
|
776 |
right = null; |
|
777 |
value = 0; |
|
778 |
row = -1; |
|
779 |
column = -1; |
|
780 |
op = 0; |
|
781 |
type = Node.VALUE; |
|
782 |
} |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
783 |
|
2 | 784 |
public Node(Node n) { |
785 |
left = n.left; |
|
786 |
right = n.right; |
|
787 |
value = n.value; |
|
788 |
row = n.row; |
|
789 |
column = n.column; |
|
790 |
op = n.op; |
|
791 |
type = n.type; |
|
792 |
} |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
793 |
|
2 | 794 |
public void indent(int ind) { |
795 |
for (int i = 0; i < ind; i++) { |
|
796 |
System.out.print(" "); |
|
797 |
} |
|
798 |
} |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
799 |
|
2 | 800 |
public void print(int indentLevel) { |
801 |
char l[] = new char[1]; |
|
802 |
indent(indentLevel); |
|
803 |
System.out.println("NODE type=" + type); |
|
804 |
indent(indentLevel); |
|
805 |
switch (type) { |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
806 |
case Node.VALUE: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
807 |
System.out.println(" value=" + value); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
808 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
809 |
case Node.CELL: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
810 |
l[0] = (char) ((int) 'A' + column); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
811 |
System.out.println(" cell=" + new String(l) + (row + 1)); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
812 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
813 |
case Node.OP: |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
814 |
System.out.println(" op=" + op); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
815 |
left.print(indentLevel + 3); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
816 |
right.print(indentLevel + 3); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
817 |
break; |
2 | 818 |
} |
819 |
} |
|
820 |
} |
|
821 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
822 |
|
2 | 823 |
class InputField { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
824 |
|
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
825 |
int maxchars = 50; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
826 |
int cursorPos = 0; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
827 |
Applet app; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
828 |
String sval; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
829 |
char buffer[]; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
830 |
int nChars; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
831 |
int width; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
832 |
int height; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
833 |
Color bgColor; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
834 |
Color fgColor; |
2 | 835 |
|
836 |
public InputField(String initValue, Applet app, int width, int height, |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
837 |
Color bgColor, Color fgColor) { |
2 | 838 |
this.width = width; |
839 |
this.height = height; |
|
840 |
this.bgColor = bgColor; |
|
841 |
this.fgColor = fgColor; |
|
842 |
this.app = app; |
|
843 |
buffer = new char[maxchars]; |
|
844 |
nChars = 0; |
|
845 |
if (initValue != null) { |
|
846 |
initValue.getChars(0, initValue.length(), this.buffer, 0); |
|
847 |
nChars = initValue.length(); |
|
848 |
} |
|
849 |
sval = initValue; |
|
850 |
} |
|
851 |
||
852 |
public void setText(String val) { |
|
853 |
int i; |
|
854 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
855 |
for (i = 0; i < maxchars; i++) { |
2 | 856 |
buffer[i] = 0; |
857 |
} |
|
858 |
if (val == null) { |
|
859 |
sval = ""; |
|
860 |
} else { |
|
861 |
sval = val; |
|
862 |
} |
|
863 |
nChars = sval.length(); |
|
864 |
sval.getChars(0, sval.length(), buffer, 0); |
|
865 |
} |
|
866 |
||
867 |
public String getValue() { |
|
868 |
return sval; |
|
869 |
} |
|
870 |
||
871 |
public void paint(Graphics g, int x, int y) { |
|
872 |
g.setColor(bgColor); |
|
873 |
g.fillRect(x, y, width, height); |
|
874 |
if (sval != null) { |
|
875 |
g.setColor(fgColor); |
|
876 |
g.drawString(sval, x, y + (height / 2) + 3); |
|
877 |
} |
|
878 |
} |
|
879 |
||
880 |
public void processKey(KeyEvent e) { |
|
881 |
char ch = e.getKeyChar(); |
|
882 |
switch (ch) { |
|
883 |
case '\b': // delete |
|
884 |
if (nChars > 0) { |
|
885 |
nChars--; |
|
886 |
sval = new String(buffer, 0, nChars); |
|
887 |
} |
|
888 |
break; |
|
889 |
case '\n': // return |
|
890 |
selected(); |
|
891 |
break; |
|
892 |
default: |
|
893 |
if (nChars < maxchars && ch >= '0') { |
|
894 |
buffer[nChars++] = ch; |
|
895 |
sval = new String(buffer, 0, nChars); |
|
896 |
} |
|
897 |
} |
|
898 |
app.repaint(); |
|
899 |
} |
|
900 |
||
901 |
public void keyReleased(KeyEvent e) { |
|
902 |
} |
|
903 |
||
904 |
public void selected() { |
|
905 |
} |
|
906 |
} |
|
907 |
||
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
908 |
|
2 | 909 |
class SpreadSheetInput |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
910 |
extends InputField { |
2 | 911 |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
912 |
public SpreadSheetInput(String initValue, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
913 |
SpreadSheet app, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
914 |
int width, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
915 |
int height, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
916 |
Color bgColor, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
917 |
Color fgColor) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
918 |
super(initValue, app, width, height, bgColor, fgColor); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
919 |
} |
2 | 920 |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
921 |
@Override |
2 | 922 |
public void selected() { |
923 |
float f; |
|
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
924 |
sval = ("".equals(sval)) ? "v" : sval; |
2 | 925 |
switch (sval.charAt(0)) { |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
926 |
case 'v': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
927 |
String s = sval.substring(1); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
928 |
try { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
929 |
int i; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
930 |
for (i = 0; i < s.length(); i++) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
931 |
char c = s.charAt(i); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
932 |
if (c < '0' || c > '9') { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
933 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
934 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
935 |
} |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
936 |
s = s.substring(0, i); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
937 |
f = Float.valueOf(s).floatValue(); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
938 |
((SpreadSheet) app).setCurrentValue(f); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
939 |
} catch (NumberFormatException e) { |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
940 |
System.out.println("Not a float: '" + s + "'"); |
2 | 941 |
} |
8978
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
942 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
943 |
case 'l': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
944 |
((SpreadSheet) app).setCurrentValue(Cell.LABEL, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
945 |
sval.substring(1)); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
946 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
947 |
case 'u': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
948 |
((SpreadSheet) app).setCurrentValue(Cell.URL, sval.substring(1)); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
949 |
break; |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
950 |
case 'f': |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
951 |
((SpreadSheet) app).setCurrentValue(Cell.FORMULA, |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
952 |
sval.substring(1)); |
046c16ff00cb
7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
953 |
break; |
2 | 954 |
} |
955 |
} |
|
956 |
} |