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

401 lines
8.9 KiB
Java
Raw Normal View History

2016-03-04 03:56:28 +00:00
package com.bytezone.diskbrowser.visicalc;
2017-03-23 13:30:41 +00:00
class Cell implements Value, 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;
2017-03-23 13:30:41 +00:00
private String fullText;
2017-02-27 09:41:05 +00:00
private char cellFormat = ' ';
2016-03-04 03:56:28 +00:00
private String repeat = "";
2017-03-20 11:02:05 +00:00
private boolean calculated;
2016-03-13 10:57:20 +00:00
2017-03-23 13:30:41 +00:00
private CellType cellType;
private String repeatingText; // REPEATING_CHARACTER
private String label; // LABEL
private Value 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
{
this.parent = parent;
this.address = address;
2017-02-25 03:56:22 +00:00
cellType = CellType.EMPTY;
2017-03-20 07:17:46 +00:00
}
void reset ()
{
2017-03-20 11:02:05 +00:00
calculated = 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-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-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;
2016-03-13 10:57:20 +00:00
}
else
{
2017-03-23 13:30:41 +00:00
fullText = command;
cellType = CellType.VALUE;
2017-03-14 11:28:52 +00:00
try
{
2017-03-23 13:30:41 +00:00
value = new Expression (this, fullText).reduce ();
2017-03-14 11:28:52 +00:00
}
catch (IllegalArgumentException e)
{
2017-03-23 13:30:41 +00:00
value = new Error (this, "@ERROR");
2017-03-14 11:28:52 +00:00
}
2016-03-04 03:56:28 +00:00
}
2016-03-09 10:38:53 +00:00
2016-03-10 09:21:47 +00:00
if (false)
2017-03-23 13:30:41 +00:00
setTestData (0);
}
private void setTestData (int choice)
{
// FUTURE.VC
if (choice == 1)
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)
2017-03-23 13:30:41 +00:00
fullText = "1000";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 131)
2017-03-23 13:30:41 +00:00
fullText = "10.5";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 195)
2017-03-23 13:30:41 +00:00
fullText = "12";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 259)
2017-03-23 13:30:41 +00:00
fullText = "8";
2017-02-25 10:30:56 +00:00
}
2016-03-10 09:21:47 +00:00
// IRA.VC
2017-03-23 13:30:41 +00:00
if (choice == 2)
2017-02-25 10:30:56 +00:00
{
System.out.println ("****** Hardcoded values ******");
2017-03-03 23:41:08 +00:00
if (address.getRowKey () == 66)
2017-03-23 13:30:41 +00:00
fullText = "10";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 130)
2017-03-23 13:30:41 +00:00
fullText = "30";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 194)
2017-03-23 13:30:41 +00:00
fullText = "65";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 258)
2017-03-23 13:30:41 +00:00
fullText = "1000";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 386)
2017-03-23 13:30:41 +00:00
fullText = "15";
2017-02-25 10:30:56 +00:00
}
2016-03-11 01:52:22 +00:00
// CARLOAN.VC
2017-03-23 13:30:41 +00:00
if (choice == 3)
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)
2017-03-23 13:30:41 +00:00
fullText = "9375";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 131)
2017-03-23 13:30:41 +00:00
fullText = "4500";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 195)
2017-03-23 13:30:41 +00:00
fullText = "24";
2017-03-03 23:41:08 +00:00
else if (address.getRowKey () == 259)
2017-03-23 13:30:41 +00:00
fullText = "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-03-20 11:02:05 +00:00
String getFormattedText (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-03-23 13:30:41 +00:00
switch (getValueResult ())
2017-03-03 10:24:23 +00:00
{
2017-03-23 13:30:41 +00:00
case ERROR:
case NA:
if (fmtChar == ' ')
fmtChar = 'R';
return " " + Format.justify (value.getText (), colWidth - 1, fmtChar);
case VALID:
switch (getValueType ())
{
case BOOLEAN:
if (fmtChar != 'L')
fmtChar = 'R';
return Format.justify (value.getText (), colWidth, fmtChar);
case NUMBER:
if (colWidth == 1)
return ".";
return " " + Format.format (value, fmtChar, colWidth - 1);
default:
assert false;
return "Impossible";
}
default:
assert false;
return "Impossible";
2017-03-03 10:24:23 +00:00
}
2017-02-25 03:56:22 +00:00
default:
assert false;
2017-03-23 13:30:41 +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
2017-03-23 13:30:41 +00:00
public void calculate ()
2016-03-11 21:56:02 +00:00
{
2017-03-23 13:30:41 +00:00
if (cellType == CellType.VALUE && !calculated)
{
value.calculate ();
calculated = true;
}
2016-03-11 21:56:02 +00:00
}
2016-03-16 19:32:25 +00:00
@Override
2017-03-23 13:30:41 +00:00
public boolean isValid ()
2016-03-16 19:32:25 +00:00
{
2017-03-23 13:30:41 +00:00
return cellType == CellType.VALUE ? value.isValid () : true;
2016-03-16 19:32:25 +00:00
}
2016-03-09 10:38:53 +00:00
@Override
2017-03-23 13:30:41 +00:00
public ValueResult getValueResult ()
2016-03-09 10:38:53 +00:00
{
2017-03-23 13:30:41 +00:00
return cellType == CellType.VALUE ? value.getValueResult () : ValueResult.VALID;
2016-03-16 06:15:39 +00:00
}
2016-03-14 20:09:04 +00:00
2016-03-11 21:56:02 +00:00
@Override
2017-03-23 13:30:41 +00:00
public ValueType getValueType ()
2016-03-11 21:56:02 +00:00
{
2017-03-23 13:30:41 +00:00
return cellType == CellType.VALUE ? value.getValueType () : ValueType.NUMBER;
2016-03-11 21:56:02 +00:00
}
2016-03-16 06:15:39 +00:00
@Override
2017-03-23 13:30:41 +00:00
public double getDouble ()
2016-03-11 21:56:02 +00:00
{
2017-03-23 13:30:41 +00:00
return cellType == CellType.VALUE ? value.getDouble () : 0;
2017-02-25 03:56:22 +00:00
}
2017-03-23 13:30:41 +00:00
@Override
public boolean getBoolean ()
2017-02-25 03:56:22 +00:00
{
2017-03-23 13:30:41 +00:00
return cellType == CellType.VALUE ? value.getBoolean () : false;
}
2017-02-25 03:56:22 +00:00
2017-03-23 13:30:41 +00:00
@Override
public String getFullText ()
{
2017-02-25 03:56:22 +00:00
switch (cellType)
2016-03-16 19:32:25 +00:00
{
2017-02-25 03:56:22 +00:00
case LABEL:
2017-03-23 13:30:41 +00:00
return label;
2017-02-25 03:56:22 +00:00
case REPEATING_CHARACTER:
2017-03-23 13:30:41 +00:00
return repeatingText;
2017-02-25 03:56:22 +00:00
case EMPTY:
2017-03-23 13:30:41 +00:00
return "Empty Cell";
2017-02-25 03:56:22 +00:00
case VALUE:
2017-03-23 13:30:41 +00:00
return value.getFullText ();
2017-02-25 03:56:22 +00:00
default:
2017-03-23 13:30:41 +00:00
return "impossible";
2016-03-16 19:32:25 +00:00
}
2017-03-23 13:30:41 +00:00
}
2017-02-25 03:56:22 +00:00
2017-03-23 13:30:41 +00:00
@Override
public String getType ()
{
return "Cell";
2017-02-25 03:56:22 +00:00
}
2017-03-23 13:30:41 +00:00
@Override
public String getText ()
{
// cell points to another cell which is ERROR or NA
assert cellType == CellType.VALUE;
// assert !value.isValid ();
return value.getText ();
}
Value getValue ()
{
return value;
}
// public String getDebugText ()
// {
// StringBuilder text = new StringBuilder ();
// text.append (line);
// text.append ("\n");
// text.append (String.format ("| %-11s %s Format: %s |%n",
// address.getText (), address.getDetails (), cellFormat));
// text.append (line);
// text.append ("\n");
//
// switch (cellType)
// {
// 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
// text.append (((AbstractValue) value).getValueText (0));
// break;
//
// default:
// text.append ("Unknown CellType: " + cellType + "\n");
// }
//
// 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;
2017-02-25 03:56:22 +00:00
case EMPTY:
contents = "Empty";
2017-03-23 13:30:41 +00:00
break;
case VALUE:
switch (value.getValueType ())
{
case NUMBER:
contents = "Num : " + fullText;
break;
case BOOLEAN:
contents = "Bool: " + fullText;
break;
}
// contents = "Exp : " + expressionText;
break;
2016-03-19 05:31:30 +00:00
}
2016-03-13 10:57:20 +00:00
2017-03-23 13:30:41 +00:00
return String.format ("Cell:%5s %s", address.getText (), 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);
}
}