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 ""; 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 Address address;
private final Sheet parent; private final Sheet parent;
private CellType cellType; private CellType cellType;
private final Format format = new Format (); // private final Format format = new Format ();
private String expressionText; private String expressionText;
private char cellFormat = ' ';
private String repeatingText; private String repeatingText;
private String repeat = ""; private String repeat = "";
@ -53,7 +54,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
if (formatText.startsWith ("/F")) if (formatText.startsWith ("/F"))
{ {
format.cellFormat = formatText.charAt (2); cellFormat = formatText.charAt (2);
return; return;
} }
@ -128,23 +129,24 @@ class Cell extends AbstractValue implements Comparable<Cell>
} }
// format cell value for output // format cell value for output
String getText (int colWidth, char defaultFormat) String getText (int colWidth, char globalFormat)
{ {
switch (cellType) switch (cellType)
{ {
case LABEL: case LABEL:
return format.justify (label, colWidth, format.cellFormat); return Format.justify (label, colWidth, cellFormat);
case REPEATING_CHARACTER: case REPEATING_CHARACTER:
return format.justify (repeat, colWidth, ' '); return Format.justify (repeat, colWidth, ' ');
case EMPTY: case EMPTY:
return format.justify (empty, colWidth, ' '); return Format.justify (empty, colWidth, ' ');
case VALUE: case VALUE:
if (value == null) if (value == null)
calculate (); calculate ();
return format.format (value, defaultFormat, colWidth); char formatChar = cellFormat != ' ' ? cellFormat : globalFormat;
return Format.format (value, formatChar, colWidth);
default: default:
assert false; assert false;
@ -215,7 +217,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
text.append (line); text.append (line);
text.append ("\n"); text.append ("\n");
text.append (String.format ("| %-21s %s %17s |%n", address.getText (), text.append (String.format ("| %-21s %s %17s |%n", address.getText (),
address.getDetails (), "Format : " + format.cellFormat)); address.getDetails (), "Format : " + cellFormat));
text.append (line); text.append (line);
text.append ("\n"); text.append ("\n");
@ -249,45 +251,6 @@ class Cell extends AbstractValue implements Comparable<Cell>
return text.toString (); 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 @Override
public String toString () public String toString ()
{ {

View File

@ -7,17 +7,11 @@ import com.bytezone.diskbrowser.visicalc.Value.ValueType;
public class Format public class Format
{ {
private static final DecimalFormat nf = new DecimalFormat ("#####0.00"); 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)) if (!value.isValueType (ValueType.VALUE))
{
// char formatChar = format.cellFormat != ' ' ? format.cellFormat : defaultFormat;
return justify (value.getText (), colWidth, formatChar); return justify (value.getText (), colWidth, formatChar);
}
if (formatChar == 'I') if (formatChar == 'I')
{ {
@ -38,7 +32,7 @@ public class Format
else else
{ {
// this could be improved // 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 ()); String val = String.format (numberFormat, value.getValue ());
while (val.endsWith ("0")) while (val.endsWith ("0"))
val = ' ' + val.substring (0, val.length () - 1); 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 // right justify
if (format == 'R' || format == '$' || format == 'I') 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 Map<Integer, Cell> columnOrderCells = new TreeMap<Integer, Cell> ();
private final List<String> lines = new ArrayList<String> (); private final List<String> lines = new ArrayList<String> ();
// private Cell currentCell = null; private char globalFormat;
private char defaultFormat;
private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> (); private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> ();
private int columnWidth = 12; private int columnWidth = 12;
@ -173,9 +172,6 @@ public class Sheet
calculate (recalculationOrder); calculate (recalculationOrder);
calculate (recalculationOrder); calculate (recalculationOrder);
} }
if (false)
printDebug ();
} }
private void calculate (char order) private void calculate (char order)
@ -309,7 +305,7 @@ public class Sheet
columnWidth = Integer.parseInt (line.substring (3)); columnWidth = Integer.parseInt (line.substring (3));
break; break;
case 'F': case 'F':
defaultFormat = line.charAt (3); globalFormat = line.charAt (3);
break; break;
default: default:
System.out.printf ("Unknown global format [%s]%n", line); System.out.printf ("Unknown global format [%s]%n", line);
@ -413,7 +409,7 @@ public class Sheet
if (columnWidths.containsKey (cellAddress.column)) if (columnWidths.containsKey (cellAddress.column))
colWidth = columnWidths.get (cellAddress.column); colWidth = columnWidths.get (cellAddress.column);
text.append (cell.getText (colWidth, defaultFormat)); text.append (cell.getText (colWidth, globalFormat));
} }
if (debug) if (debug)
@ -437,23 +433,4 @@ public class Sheet
return text.toString (); 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 ());
}
} }