2016-03-04 03:56:28 +00:00
|
|
|
package com.bytezone.diskbrowser.visicalc;
|
|
|
|
|
2016-03-09 10:38:53 +00:00
|
|
|
class Cell implements Comparable<Cell>, Value
|
2016-03-04 03:56:28 +00:00
|
|
|
{
|
2016-03-09 10:38:53 +00:00
|
|
|
// private static final Pattern cellContents =
|
|
|
|
// Pattern.compile ("([-+/*]?)(([A-Z]{1,2}[0-9]{1,3})|([0-9.]+)|(@[^-+/*]+))");
|
2016-03-04 03:56:28 +00:00
|
|
|
|
|
|
|
final Address address;
|
2016-03-06 08:05:32 +00:00
|
|
|
private final Sheet parent;
|
2016-03-04 03:56:28 +00:00
|
|
|
|
|
|
|
private String label;
|
2016-03-09 10:38:53 +00:00
|
|
|
// private double value;
|
|
|
|
// private String formulaText;
|
2016-03-07 12:16:11 +00:00
|
|
|
|
2016-03-05 21:53:29 +00:00
|
|
|
private char format = ' ';
|
2016-03-04 03:56:28 +00:00
|
|
|
private char repeatingChar;
|
|
|
|
private String repeat = "";
|
2016-03-09 10:38:53 +00:00
|
|
|
// private boolean valid;
|
|
|
|
|
|
|
|
private String expressionText;
|
|
|
|
private Expression expression;
|
2016-03-04 03:56:28 +00:00
|
|
|
|
2016-03-06 08:05:32 +00:00
|
|
|
public Cell (Sheet parent, Address address)
|
2016-03-04 03:56:28 +00:00
|
|
|
{
|
|
|
|
this.parent = parent;
|
|
|
|
this.address = address;
|
|
|
|
}
|
|
|
|
|
2016-03-08 09:39:35 +00:00
|
|
|
void format (String format)
|
|
|
|
{
|
|
|
|
// /FG - general
|
|
|
|
// /FD - default
|
|
|
|
// /FI - integer
|
|
|
|
// /F$ - dollars and cents
|
|
|
|
// /FL - left justified
|
|
|
|
// /FR - right justified
|
|
|
|
// /F* - graph
|
|
|
|
if (format.startsWith ("/F"))
|
|
|
|
this.format = format.charAt (2);
|
|
|
|
else if (format.startsWith ("/-"))
|
|
|
|
{
|
|
|
|
repeatingChar = format.charAt (2);
|
|
|
|
for (int i = 0; i < 20; i++)
|
|
|
|
repeat += repeatingChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
System.out.printf ("Unexpected format [%s]%n", format);
|
|
|
|
}
|
|
|
|
|
2016-03-04 05:27:14 +00:00
|
|
|
void doCommand (String command)
|
2016-03-04 03:56:28 +00:00
|
|
|
{
|
2016-03-04 05:27:14 +00:00
|
|
|
switch (command.charAt (0))
|
2016-03-04 03:56:28 +00:00
|
|
|
{
|
2016-03-04 05:27:14 +00:00
|
|
|
case '"':
|
|
|
|
label = command.substring (1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-03-09 10:38:53 +00:00
|
|
|
expressionText = command;
|
2016-03-04 03:56:28 +00:00
|
|
|
}
|
2016-03-09 10:38:53 +00:00
|
|
|
|
|
|
|
// FUTURE.VC
|
|
|
|
if (address.sortValue == 67)
|
|
|
|
expressionText = "50";
|
|
|
|
if (address.sortValue == 131)
|
|
|
|
expressionText = ".04";
|
|
|
|
if (address.sortValue == 195)
|
|
|
|
expressionText = "12";
|
|
|
|
if (address.sortValue == 259)
|
|
|
|
expressionText = "5";
|
2016-03-04 03:56:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 05:27:14 +00:00
|
|
|
boolean hasValue ()
|
2016-03-04 03:56:28 +00:00
|
|
|
{
|
2016-03-09 10:38:53 +00:00
|
|
|
return expressionText != null;
|
2016-03-04 03:56:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 21:53:29 +00:00
|
|
|
char getFormat ()
|
|
|
|
{
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2016-03-09 10:38:53 +00:00
|
|
|
String getText ()
|
2016-03-04 03:56:28 +00:00
|
|
|
{
|
2016-03-09 10:38:53 +00:00
|
|
|
if (label != null)
|
|
|
|
return label;
|
|
|
|
if (repeatingChar > 0)
|
|
|
|
return repeat;
|
|
|
|
return "bollocks";
|
|
|
|
}
|
2016-03-05 02:25:15 +00:00
|
|
|
|
2016-03-09 10:38:53 +00:00
|
|
|
@Override
|
|
|
|
public double getValue ()
|
|
|
|
{
|
|
|
|
if (expression == null)
|
|
|
|
expression = new Expression (parent, expressionText);
|
|
|
|
return expression.getValue ();
|
|
|
|
// if (valid || formulaText == null)
|
|
|
|
// return value;
|
|
|
|
//
|
|
|
|
// double result = 0.0;
|
|
|
|
// double interim = 0.0;
|
|
|
|
//
|
|
|
|
// if (formulaText.startsWith ("@LOOKUP("))
|
|
|
|
// {
|
|
|
|
// Lookup lookup = new Lookup (parent, formulaText);
|
|
|
|
// return lookup.getValue ();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// System.out.printf ("Matching:[%s]%n", formulaText);
|
2016-03-08 09:39:35 +00:00
|
|
|
// [@IF(@ISERROR(BK24),0,BK24)]
|
|
|
|
// [@IF(D4=0,0,1)]
|
|
|
|
// [@IF(D4=0,0,B32+1)]
|
|
|
|
// [@IF(D4=0,0,1+(D3/100/D4)^D4-1*100)]
|
|
|
|
// [@SUM(C4...F4)]
|
|
|
|
// [+C4-@SUM(C5...C12)]
|
|
|
|
// [+D5/100/12]
|
|
|
|
// [.3*(B4+B7+B8+B9)]
|
|
|
|
// [+N12+(P12*(.2*K12+K9-O12))]
|
|
|
|
|
2016-03-09 10:38:53 +00:00
|
|
|
// Matcher m = cellContents.matcher (formulaText);
|
|
|
|
// while (m.find ())
|
|
|
|
// {
|
|
|
|
// valid = true;
|
|
|
|
// char operator = m.group (1).isEmpty () ? '+' : m.group (1).charAt (0);
|
|
|
|
//
|
|
|
|
// if (m.group (3) != null) // address
|
|
|
|
// {
|
|
|
|
// Address address = new Address (m.group (3));
|
|
|
|
// Cell cell = parent.getCell (address);
|
|
|
|
// if (cell != null)
|
|
|
|
// interim = cell.getValue ();
|
|
|
|
// }
|
|
|
|
// else if (m.group (4) != null) // constant
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
// interim = Double.parseDouble (m.group (4));
|
|
|
|
// }
|
|
|
|
// catch (NumberFormatException e)
|
|
|
|
// {
|
|
|
|
// System.out.printf ("NFE: %s [%s]%n", m.group (4), formulaText);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// // interim = parent.evaluateFunction (m.group (5)); // function
|
|
|
|
// Function function = Function.getInstance (parent, m.group (5));
|
|
|
|
// if (function != null)
|
|
|
|
// interim = function.getValue ();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (operator == '+')
|
|
|
|
// result += interim;
|
|
|
|
// else if (operator == '-')
|
|
|
|
// result -= interim;
|
|
|
|
// else if (operator == '*')
|
|
|
|
// result *= interim;
|
|
|
|
// else if (operator == '/')
|
|
|
|
// result = interim == 0.0 ? 0 : result / interim;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (valid)
|
|
|
|
// {
|
|
|
|
// value = result;
|
|
|
|
// return result;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// System.out.println ("?? " + formulaText);
|
|
|
|
//
|
|
|
|
// return value;
|
2016-03-04 03:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString ()
|
|
|
|
{
|
2016-03-09 10:38:53 +00:00
|
|
|
// String value = repeatingChar == 0
|
|
|
|
// ? label == null ? formulaText == null ? ", Value : " + this.value
|
|
|
|
// : ", Formula: " + formulaText : ", Label : " + label
|
|
|
|
// : ", Repeat : " + repeatingChar;
|
|
|
|
String contents = "";
|
|
|
|
if (label != null)
|
|
|
|
contents = "Labl: " + label;
|
|
|
|
else if (repeatingChar != 0)
|
|
|
|
contents = "Rept: " + repeatingChar;
|
|
|
|
else if (expressionText != null)
|
|
|
|
contents = "Exp : " + expressionText;
|
|
|
|
return String.format ("[Cell:%5s %s]", address, contents);
|
2016-03-04 03:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-06 08:05:32 +00:00
|
|
|
public int compareTo (Cell o)
|
2016-03-04 03:56:28 +00:00
|
|
|
{
|
|
|
|
return address.compareTo (o.address);
|
|
|
|
}
|
|
|
|
}
|