dmolony-DiskBrowser/src/com/bytezone/diskbrowser/visicalc/Cell.java

348 lines
7.8 KiB
Java
Raw Normal View History

2016-03-04 03:56:28 +00:00
package com.bytezone.diskbrowser.visicalc;
2017-02-25 03:56:22 +00:00
class Cell extends AbstractValue implements Comparable<Cell>
2016-03-04 03:56:28 +00:00
{
2017-02-25 03:56:22 +00:00
private static final String line = "+----------------------------------------"
+ "--------------------------------------------+";
2017-02-26 10:44:10 +00:00
private static final String empty = " ";
2016-03-11 22:32:19 +00:00
2017-02-25 03:56:22 +00:00
private final Address address;
2016-03-06 08:05:32 +00:00
private final Sheet parent;
2016-08-01 01:22:34 +00:00
private CellType cellType;
2017-02-25 10:30:56 +00:00
private String expressionText;
2017-02-27 09:41:05 +00:00
private char cellFormat = ' ';
2016-03-13 10:57:20 +00:00
2017-02-25 03:56:22 +00:00
private String repeatingText;
2016-03-04 03:56:28 +00:00
private String repeat = "";
2016-03-11 21:56:02 +00:00
private String label;
private Value value;
2016-03-13 10:57:20 +00:00
enum CellType
{
2017-02-25 03:56:22 +00:00
LABEL, REPEATING_CHARACTER, VALUE, EMPTY
2016-03-13 10:57:20 +00:00
}
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
{
2017-03-03 23:41:08 +00:00
super ("Cell " + address.getText ());
2017-02-25 03:56:22 +00:00
2016-03-04 03:56:28 +00:00
this.parent = parent;
this.address = address;
2017-02-25 03:56:22 +00:00
cellType = CellType.EMPTY;
2017-03-15 04:40:18 +00:00
isVolatile = false;
2017-03-14 11:28:52 +00:00
}
Cell getCell (Address address)
{
return parent.getCell (address);
}
Cell getCell (String addressText)
{
return parent.getCell (addressText);
}
boolean cellExists (Address address)
{
return parent.cellExists (address);
}
2017-03-19 01:03:57 +00:00
Function getFunction (String text)
{
return parent.getFunction (this, text);
}
Value getExpressionValue (String text)
{
return new Expression (this, text).reduce ();
}
2017-03-14 11:28:52 +00:00
boolean isCellType (CellType cellType)
{
return this.cellType == cellType;
}
Sheet getParent ()
{
return parent;
}
2017-03-14 11:28:52 +00:00
@Override
public boolean isVolatile ()
{
return isVolatile;
2017-02-25 03:56:22 +00:00
}
Address getAddress ()
{
return address;
2016-03-15 20:06:04 +00:00
}
2017-03-14 11:28:52 +00:00
String getAddressText ()
{
return address.getText ();
}
2017-02-25 03:56:22 +00:00
void setFormat (String formatText)
2016-03-08 09:39:35 +00:00
{
// /FG - general
// /FD - default
// /FI - integer
2017-02-25 03:56:22 +00:00
// /F$ - two decimal places
2016-03-08 09:39:35 +00:00
// /FL - left justified
// /FR - right justified
2016-03-10 02:39:23 +00:00
// /F* - graph (histogram)
2017-02-26 10:44:10 +00:00
if (formatText.startsWith ("/T")) // lock titles
2017-02-25 03:56:22 +00:00
return;
if (formatText.startsWith ("/F"))
{
2017-02-27 09:41:05 +00:00
cellFormat = formatText.charAt (2);
2017-02-25 03:56:22 +00:00
return;
}
if (formatText.startsWith ("/-"))
2016-03-08 09:39:35 +00:00
{
2017-02-25 03:56:22 +00:00
repeatingText = formatText.substring (2);
2016-03-08 09:39:35 +00:00
for (int i = 0; i < 20; i++)
2017-02-25 03:56:22 +00:00
repeat += repeatingText;
2016-08-01 01:22:34 +00:00
cellType = CellType.REPEATING_CHARACTER;
2017-03-14 11:28:52 +00:00
isVolatile = false;
2017-02-25 03:56:22 +00:00
return;
2016-03-08 09:39:35 +00:00
}
2017-02-25 03:56:22 +00:00
System.out.printf ("Unexpected format [%s]%n", formatText);
2016-03-08 09:39:35 +00:00
}
2016-03-11 21:56:02 +00:00
void setValue (String command)
2016-03-04 03:56:28 +00:00
{
2017-02-28 20:39:26 +00:00
assert cellType == CellType.EMPTY;
2017-02-25 03:56:22 +00:00
if (!command.isEmpty () && command.charAt (0) == '"')
2016-03-04 03:56:28 +00:00
{
2016-03-13 10:57:20 +00:00
label = command.substring (1);
2016-08-01 01:22:34 +00:00
cellType = CellType.LABEL;
2017-03-14 11:28:52 +00:00
isVolatile = false;
2016-03-13 10:57:20 +00:00
}
else
{
2017-03-14 11:28:52 +00:00
try
{
expressionText = command;
2017-03-19 01:03:57 +00:00
value = new Expression (this, expressionText).reduce ();
2017-03-14 11:28:52 +00:00
cellType = CellType.VALUE;
2017-03-15 04:40:18 +00:00
isVolatile = true;
2017-03-14 11:28:52 +00:00
}
catch (IllegalArgumentException e)
{
System.out.println ("ignoring error: " + command);
}
2016-03-04 03:56:28 +00:00
}
2016-03-09 10:38:53 +00:00
// FUTURE.VC
2016-03-10 09:21:47 +00:00
if (false)
2017-02-25 10:30:56 +00:00
{
System.out.println ("****** Hardcoded values ******");
2017-03-03 23:41:08 +00:00
if (address.getRowKey () == 67)
2016-03-10 09:21:47 +00:00
expressionText = "1000";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 131)
2016-03-10 09:21:47 +00:00
expressionText = "10.5";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 195)
2016-03-10 09:21:47 +00:00
expressionText = "12";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 259)
2016-03-10 09:21:47 +00:00
expressionText = "8";
2017-02-25 10:30:56 +00:00
}
2016-03-10 09:21:47 +00:00
// IRA.VC
2017-02-25 10:30:56 +00:00
if (false)
{
System.out.println ("****** Hardcoded values ******");
2017-03-03 23:41:08 +00:00
if (address.getRowKey () == 66)
2016-03-10 09:21:47 +00:00
expressionText = "10";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 130)
2016-03-10 09:21:47 +00:00
expressionText = "30";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 194)
2016-03-10 09:21:47 +00:00
expressionText = "65";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 258)
2016-03-10 09:21:47 +00:00
expressionText = "1000";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 386)
2016-03-10 09:21:47 +00:00
expressionText = "15";
2017-02-25 10:30:56 +00:00
}
2016-03-11 01:52:22 +00:00
// CARLOAN.VC
2016-03-12 22:38:03 +00:00
if (false)
2017-02-25 10:30:56 +00:00
{
System.out.println ("****** Hardcoded values ******");
2017-03-03 23:41:08 +00:00
if (address.getRowKey () == 67)
2016-03-11 01:52:22 +00:00
expressionText = "9375";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 131)
2016-03-11 01:52:22 +00:00
expressionText = "4500";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 195)
2016-03-11 01:52:22 +00:00
expressionText = "24";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 259)
2016-03-11 01:52:22 +00:00
expressionText = "11.9";
2017-02-25 10:30:56 +00:00
}
2016-03-04 03:56:28 +00:00
}
2017-02-25 03:56:22 +00:00
// format cell value for output
2017-02-27 09:41:05 +00:00
String getText (int colWidth, char globalFormat)
2016-03-04 03:56:28 +00:00
{
2017-03-08 09:18:59 +00:00
char fmtChar = cellFormat != ' ' ? cellFormat : globalFormat;
2016-08-01 01:22:34 +00:00
switch (cellType)
2016-03-13 10:57:20 +00:00
{
case LABEL:
2017-03-12 16:47:11 +00:00
if (fmtChar != 'R')
2017-03-08 09:18:59 +00:00
fmtChar = 'L';
return Format.justify (label, colWidth, fmtChar);
2016-03-13 10:57:20 +00:00
case REPEATING_CHARACTER:
2017-02-27 09:41:05 +00:00
return Format.justify (repeat, colWidth, ' ');
2017-02-25 03:56:22 +00:00
case EMPTY:
2017-02-27 09:41:05 +00:00
return Format.justify (empty, colWidth, ' ');
2016-03-13 10:57:20 +00:00
case VALUE:
2017-02-28 20:39:26 +00:00
if (!isValueType (ValueType.VALUE))
2017-03-03 10:24:23 +00:00
{
2017-03-08 09:18:59 +00:00
if (fmtChar == ' ')
fmtChar = 'R';
return " " + Format.justify (value.getText (), colWidth - 1, fmtChar);
2017-03-03 10:24:23 +00:00
}
2017-02-28 20:39:26 +00:00
2017-03-18 00:24:05 +00:00
if (value.isBoolean ()) // consider ValueType of BOOLEAN
{
if (fmtChar != 'L')
fmtChar = 'R';
return Format.justify (value.getText (), colWidth, fmtChar);
}
2017-03-17 11:03:56 +00:00
if (colWidth == 1)
return ".";
2017-03-08 09:18:59 +00:00
return " " + Format.format (value, fmtChar, colWidth - 1);
2016-03-11 22:32:19 +00:00
2017-02-25 03:56:22 +00:00
default:
assert false;
2017-03-08 09:18:59 +00:00
return "Impossible";
2016-03-11 22:32:19 +00:00
}
2016-03-09 10:38:53 +00:00
}
2016-03-05 02:25:15 +00:00
2016-03-11 21:56:02 +00:00
@Override
2016-03-16 06:15:39 +00:00
public double getValue ()
2016-03-11 21:56:02 +00:00
{
2017-03-08 09:18:59 +00:00
return cellType == CellType.VALUE ? value.getValue () : 0;
2016-03-11 21:56:02 +00:00
}
2016-03-16 19:32:25 +00:00
@Override
public ValueType getValueType ()
{
2017-03-15 04:40:18 +00:00
return cellType == CellType.VALUE ? value.getValueType () : ValueType.VALUE;
2016-03-16 19:32:25 +00:00
}
2016-03-09 10:38:53 +00:00
@Override
2016-03-16 06:15:39 +00:00
public String getText ()
2016-03-09 10:38:53 +00:00
{
2017-03-03 10:24:23 +00:00
if (cellType == CellType.EMPTY)
2017-03-15 04:40:18 +00:00
return "MPT";
2017-02-25 03:56:22 +00:00
2017-03-14 11:28:52 +00:00
if (cellType == CellType.LABEL)
return "LBL";
if (cellType == CellType.REPEATING_CHARACTER)
return "RPT";
2017-03-08 09:18:59 +00:00
assert cellType == CellType.VALUE;
2016-03-16 06:15:39 +00:00
return value.getText ();
}
2016-03-14 20:09:04 +00:00
2016-03-11 21:56:02 +00:00
@Override
2016-08-01 05:18:51 +00:00
public boolean isValueType (ValueType type)
2016-03-11 21:56:02 +00:00
{
2017-03-03 23:41:08 +00:00
return type == getValueType ();
2016-03-11 21:56:02 +00:00
}
2016-03-16 06:15:39 +00:00
@Override
2017-02-28 20:39:26 +00:00
public void calculate ()
2016-03-11 21:56:02 +00:00
{
2017-03-08 09:18:59 +00:00
if (cellType == CellType.VALUE)
2017-03-14 11:28:52 +00:00
{
2017-03-15 04:40:18 +00:00
if (isVolatile)
{
value.calculate ();
isVolatile = value.isVolatile ();
}
2017-03-14 11:28:52 +00:00
}
2017-02-25 03:56:22 +00:00
}
public String getDebugText ()
{
StringBuilder text = new StringBuilder ();
text.append (line);
text.append ("\n");
2017-03-14 11:28:52 +00:00
text.append (String.format ("| %-11s %s Volatile: %1.1s Format: %s |%n",
address.getText (), address.getDetails (), isVolatile, cellFormat));
2017-02-25 03:56:22 +00:00
text.append (line);
text.append ("\n");
switch (cellType)
2016-03-16 19:32:25 +00:00
{
2017-02-25 03:56:22 +00:00
case LABEL:
text.append (String.format ("| LABEL : %-69s |%n", label));
break;
case REPEATING_CHARACTER:
text.append (String.format ("| REPEAT : %-69s |%n", repeatingText));
break;
case EMPTY:
text.append (String.format ("| EMPTY : %-69s |%n", ""));
break;
case VALUE:
text.append (String.format ("| VALUE : %-69s |%n", expressionText));
if (value == null)
text.append (String.format ("| Value : %-69s |%n", "null"));
else
2017-02-28 20:39:26 +00:00
text.append (((AbstractValue) value).getValueText (0));
2017-02-25 03:56:22 +00:00
break;
default:
text.append ("Unknown CellType: " + cellType + "\n");
2016-03-16 19:32:25 +00:00
}
2017-02-25 03:56:22 +00:00
text.append (line);
return text.toString ();
}
2016-03-04 03:56:28 +00:00
@Override
public String toString ()
{
2016-03-19 05:31:30 +00:00
String contents = "";
2016-08-01 01:22:34 +00:00
switch (cellType)
2016-03-19 05:31:30 +00:00
{
case LABEL:
contents = "Labl: " + label;
break;
case REPEATING_CHARACTER:
2017-02-25 03:56:22 +00:00
contents = "Rept: " + repeatingText;
2016-03-19 05:31:30 +00:00
break;
case VALUE:
contents = "Exp : " + expressionText;
break;
2017-02-25 03:56:22 +00:00
case EMPTY:
contents = "Empty";
2016-03-19 05:31:30 +00:00
}
2016-03-13 10:57:20 +00:00
2017-03-15 04:40:18 +00:00
return String.format ("[Cell:%5s %s %s]", address, contents,
isVolatile ? "volatile" : "fixed");
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);
}
}