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

282 lines
6.3 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 DecimalFormat nf = new DecimalFormat ("#####0.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-27 09:41:05 +00:00
// private final Format format = new Format ();
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-02-25 03:56:22 +00:00
super ("Cell " + address.text);
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;
}
Address getAddress ()
{
return address;
2016-03-15 20:06:04 +00:00
}
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-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
{
expressionText = command;
2016-08-01 01:22:34 +00:00
cellType = CellType.VALUE;
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 ******");
2016-03-15 20:06:04 +00:00
if (address.rowKey == 67)
2016-03-10 09:21:47 +00:00
expressionText = "1000";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 131)
2016-03-10 09:21:47 +00:00
expressionText = "10.5";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 195)
2016-03-10 09:21:47 +00:00
expressionText = "12";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 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 ******");
2016-03-15 20:06:04 +00:00
if (address.rowKey == 66)
2016-03-10 09:21:47 +00:00
expressionText = "10";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 130)
2016-03-10 09:21:47 +00:00
expressionText = "30";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 194)
2016-03-10 09:21:47 +00:00
expressionText = "65";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 258)
2016-03-10 09:21:47 +00:00
expressionText = "1000";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 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 ******");
2016-03-15 20:06:04 +00:00
if (address.rowKey == 67)
2016-03-11 01:52:22 +00:00
expressionText = "9375";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 131)
2016-03-11 01:52:22 +00:00
expressionText = "4500";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 195)
2016-03-11 01:52:22 +00:00
expressionText = "24";
2016-03-15 20:06:04 +00:00
else if (address.rowKey == 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
{
2016-08-01 01:22:34 +00:00
switch (cellType)
2016-03-13 10:57:20 +00:00
{
case LABEL:
2017-02-27 09:41:05 +00:00
return Format.justify (label, colWidth, cellFormat);
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-25 03:56:22 +00:00
if (value == null)
calculate ();
2017-02-27 09:41:05 +00:00
char formatChar = cellFormat != ' ' ? cellFormat : globalFormat;
return Format.format (value, formatChar, colWidth);
2016-03-11 22:32:19 +00:00
2017-02-25 03:56:22 +00:00
default:
assert false;
return getText (); // not possible
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-02-25 03:56:22 +00:00
if (value == null)
calculate ();
2016-03-16 06:15:39 +00:00
return value.getValue ();
2016-03-11 21:56:02 +00:00
}
2016-03-16 19:32:25 +00:00
@Override
public ValueType getValueType ()
{
2016-08-01 01:50:43 +00:00
return value.getValueType ();
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-02-25 03:56:22 +00:00
if (value == null)
calculate ();
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-02-25 03:56:22 +00:00
if (value == null)
calculate ();
2016-08-01 05:18:51 +00:00
return value.isValueType (type);
2016-03-11 21:56:02 +00:00
}
2016-08-01 05:18:51 +00:00
public boolean isCellType (CellType type)
2016-07-21 11:28:22 +00:00
{
2016-08-01 01:22:34 +00:00
return cellType == type;
2016-07-21 11:28:22 +00:00
}
2016-03-16 06:15:39 +00:00
@Override
2016-03-17 04:40:43 +00:00
public Value calculate ()
2016-03-11 21:56:02 +00:00
{
2017-02-25 03:56:22 +00:00
if (value != null && value.isValueType (ValueType.VALUE))
2016-07-20 10:35:13 +00:00
return this;
2016-08-01 01:50:43 +00:00
2017-02-25 03:56:22 +00:00
if (value == null)
2016-03-10 02:39:23 +00:00
{
2017-02-25 03:56:22 +00:00
if (expressionText == null)
expressionText = "";
2017-02-26 10:44:10 +00:00
value = new Expression (parent, expressionText).reduce ();
2016-03-10 02:39:23 +00:00
}
2017-02-25 10:30:56 +00:00
2017-02-25 03:56:22 +00:00
value.calculate ();
return this;
}
public String getDebugText ()
{
StringBuilder text = new StringBuilder ();
text.append (line);
text.append ("\n");
text.append (String.format ("| %-21s %s %17s |%n", address.getText (),
2017-02-27 09:41:05 +00:00
address.getDetails (), "Format : " + 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
text.append (getValueText (value, 0));
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
2016-03-09 10:38:53 +00:00
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);
}
}