This commit is contained in:
Denis Molony 2017-02-27 20:41:05 +11:00
parent 01d56941f6
commit 2993fbb510
4 changed files with 55 additions and 82 deletions

View File

@ -52,4 +52,43 @@ public abstract class AbstractValue implements Value
return "";
}
}
String getValueText (Value value, int depth)
{
StringBuilder text = new StringBuilder ();
String typeText = " " + value.getTypeText ();
if (value.isValueType (ValueType.VALUE))
{
String valueText = String.format ("%f", value.getValue ());
text.append (String.format ("| %-10s : %-69s |%n", typeText, valueText));
}
else
text.append (
String.format ("| %-10s : %-69s |%n", typeText, value.getValueType ()));
if (value instanceof Expression)
{
text.append (
String.format ("| Expression : %-69s |%n", ((Expression) value).fullText ()));
for (Value v : (Expression) value)
text.append (getValueText (v, depth + 1));
}
else if (value instanceof Function)
{
text.append (
String.format ("| Function : %-69s |%n", ((Function) value).fullText));
for (Value v : (Function) value)
text.append (getValueText (v, depth + 1));
}
else if (value instanceof Condition)
{
text.append (
String.format ("| Condition : %-69s |%n", ((Condition) value).fullText));
for (Value v : (Condition) value)
text.append (getValueText (v, depth + 1));
}
return text.toString ();
}
}

View File

@ -10,8 +10,9 @@ class Cell extends AbstractValue implements Comparable<Cell>
private final Address address;
private final Sheet parent;
private CellType cellType;
private final Format format = new Format ();
// private final Format format = new Format ();
private String expressionText;
private char cellFormat = ' ';
private String repeatingText;
private String repeat = "";
@ -53,7 +54,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
if (formatText.startsWith ("/F"))
{
format.cellFormat = formatText.charAt (2);
cellFormat = formatText.charAt (2);
return;
}
@ -128,23 +129,24 @@ class Cell extends AbstractValue implements Comparable<Cell>
}
// format cell value for output
String getText (int colWidth, char defaultFormat)
String getText (int colWidth, char globalFormat)
{
switch (cellType)
{
case LABEL:
return format.justify (label, colWidth, format.cellFormat);
return Format.justify (label, colWidth, cellFormat);
case REPEATING_CHARACTER:
return format.justify (repeat, colWidth, ' ');
return Format.justify (repeat, colWidth, ' ');
case EMPTY:
return format.justify (empty, colWidth, ' ');
return Format.justify (empty, colWidth, ' ');
case VALUE:
if (value == null)
calculate ();
return format.format (value, defaultFormat, colWidth);
char formatChar = cellFormat != ' ' ? cellFormat : globalFormat;
return Format.format (value, formatChar, colWidth);
default:
assert false;
@ -215,7 +217,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
text.append (line);
text.append ("\n");
text.append (String.format ("| %-21s %s %17s |%n", address.getText (),
address.getDetails (), "Format : " + format.cellFormat));
address.getDetails (), "Format : " + cellFormat));
text.append (line);
text.append ("\n");
@ -249,45 +251,6 @@ class Cell extends AbstractValue implements Comparable<Cell>
return text.toString ();
}
private String getValueText (Value value, int depth)
{
StringBuilder text = new StringBuilder ();
String typeText = " " + value.getTypeText ();
if (value.isValueType (ValueType.VALUE))
{
String valueText = String.format ("%f", value.getValue ());
text.append (String.format ("| %-10s : %-69s |%n", typeText, valueText));
}
else
text.append (
String.format ("| %-10s : %-69s |%n", typeText, value.getValueType ()));
if (value instanceof Expression)
{
text.append (
String.format ("| Expression : %-69s |%n", ((Expression) value).fullText ()));
for (Value v : (Expression) value)
text.append (getValueText (v, depth + 1));
}
else if (value instanceof Function)
{
text.append (
String.format ("| Function : %-69s |%n", ((Function) value).fullText));
for (Value v : (Function) value)
text.append (getValueText (v, depth + 1));
}
else if (value instanceof Condition)
{
text.append (
String.format ("| Condition : %-69s |%n", ((Condition) value).fullText));
for (Value v : (Condition) value)
text.append (getValueText (v, depth + 1));
}
return text.toString ();
}
@Override
public String toString ()
{

View File

@ -7,17 +7,11 @@ import com.bytezone.diskbrowser.visicalc.Value.ValueType;
public class Format
{
private static final DecimalFormat nf = new DecimalFormat ("#####0.00");
char cellFormat = ' ';
String format (Value value, char defaultFormat, int colWidth)
static String format (Value value, char formatChar, int colWidth)
{
char formatChar = cellFormat != ' ' ? cellFormat : defaultFormat;
if (!value.isValueType (ValueType.VALUE))
{
// char formatChar = format.cellFormat != ' ' ? format.cellFormat : defaultFormat;
return justify (value.getText (), colWidth, formatChar);
}
if (formatChar == 'I')
{
@ -38,7 +32,7 @@ public class Format
else
{
// this could be improved
String numberFormat = String.format ("%%%d.3f", colWidth + 4);
String numberFormat = String.format ("%%%d.5f", colWidth + 6);
String val = String.format (numberFormat, value.getValue ());
while (val.endsWith ("0"))
val = ' ' + val.substring (0, val.length () - 1);
@ -50,7 +44,7 @@ public class Format
}
}
String justify (String text, int colWidth, char format)
static String justify (String text, int colWidth, char format)
{
// right justify
if (format == 'R' || format == '$' || format == 'I')

View File

@ -21,8 +21,7 @@ public class Sheet
private final Map<Integer, Cell> columnOrderCells = new TreeMap<Integer, Cell> ();
private final List<String> lines = new ArrayList<String> ();
// private Cell currentCell = null;
private char defaultFormat;
private char globalFormat;
private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> ();
private int columnWidth = 12;
@ -173,9 +172,6 @@ public class Sheet
calculate (recalculationOrder);
calculate (recalculationOrder);
}
if (false)
printDebug ();
}
private void calculate (char order)
@ -309,7 +305,7 @@ public class Sheet
columnWidth = Integer.parseInt (line.substring (3));
break;
case 'F':
defaultFormat = line.charAt (3);
globalFormat = line.charAt (3);
break;
default:
System.out.printf ("Unknown global format [%s]%n", line);
@ -413,7 +409,7 @@ public class Sheet
if (columnWidths.containsKey (cellAddress.column))
colWidth = columnWidths.get (cellAddress.column);
text.append (cell.getText (colWidth, defaultFormat));
text.append (cell.getText (colWidth, globalFormat));
}
if (debug)
@ -437,23 +433,4 @@ public class Sheet
return text.toString ();
}
private void printDebug ()
{
System.out.println ();
System.out.println ("Lines:");
for (String line : lines)
System.out.println (line);
System.out.println ();
System.out.println ("Cells:");
for (Cell cell : rowOrderCells.values ())
System.out.println (cell.getDebugText ());
System.out.println ();
System.out.println ("Column widths:");
System.out.printf ("Default width : %3d%n", columnWidth);
for (Map.Entry<Integer, Integer> entry : columnWidths.entrySet ())
System.out.printf (" column %3d: %3d%n", entry.getKey (), entry.getValue ());
}
}