This commit is contained in:
Denis Molony 2016-03-13 21:57:20 +11:00
parent f7e9854dc8
commit eaf7449696
3 changed files with 88 additions and 65 deletions

View File

@ -22,7 +22,7 @@ public class VisicalcFile extends AbstractFile
text.append ("Visicalc : " + name + "\n"); text.append ("Visicalc : " + name + "\n");
text.append ("Cells : " + sheet.size () + "\n\n"); text.append ("Cells : " + sheet.size () + "\n\n");
text.append (sheet.getCells ()); text.append (sheet.getTextDisplay ());
text.append ("\n\n"); text.append ("\n\n");
text.append (sheet.getLines ()); text.append (sheet.getLines ());

View File

@ -8,15 +8,21 @@ class Cell implements Comparable<Cell>, Value
final Address address; final Address address;
private final Sheet parent; private final Sheet parent;
private CellType type;
private char format = ' '; private char format = ' ';
private char repeatingChar; private char repeatingChar;
private String repeat = ""; private String repeat = "";
private String label; private String label;
private String expressionText; private String expressionText;
private Value value; private Value value;
// private boolean hasValue;
enum CellType
{
LABEL, REPEATING_CHARACTER, VALUE
}
public Cell (Sheet parent, Address address) public Cell (Sheet parent, Address address)
{ {
@ -41,6 +47,7 @@ class Cell implements Comparable<Cell>, Value
repeatingChar = format.charAt (2); repeatingChar = format.charAt (2);
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
repeat += repeatingChar; repeat += repeatingChar;
type = CellType.REPEATING_CHARACTER;
} }
else else
System.out.printf ("Unexpected format [%s]%n", format); System.out.printf ("Unexpected format [%s]%n", format);
@ -48,14 +55,15 @@ class Cell implements Comparable<Cell>, Value
void setValue (String command) void setValue (String command)
{ {
switch (command.charAt (0)) if (command.charAt (0) == '"')
{ {
case '"':
label = command.substring (1); label = command.substring (1);
break; type = CellType.LABEL;
}
default: else
{
expressionText = command; expressionText = command;
type = CellType.VALUE;
} }
// FUTURE.VC // FUTURE.VC
@ -96,13 +104,24 @@ class Cell implements Comparable<Cell>, Value
expressionText = "D9*G5/(1-((1+G5)^-D4))"; expressionText = "D9*G5/(1-((1+G5)^-D4))";
} }
char getFormat ()
{
return format;
}
String getText (int colWidth, char defaultFormat) String getText (int colWidth, char defaultFormat)
{ {
// cell may have been formatted but no value set
if (type == null)
{
System.out.println (this);
return justify ("", colWidth);
}
switch (type)
{
case LABEL:
return justify (label, colWidth);
case REPEATING_CHARACTER:
return justify (repeat, colWidth);
case VALUE:
if (hasValue ()) if (hasValue ())
if (format == 'I') if (format == 'I')
{ {
@ -117,6 +136,7 @@ class Cell implements Comparable<Cell>, Value
else if (format == '*') else if (format == '*')
{ {
String graphFormat = String.format ("%%-%d.%ds", colWidth, colWidth); String graphFormat = String.format ("%%-%d.%ds", colWidth, colWidth);
// this is not finished
return String.format (graphFormat, "********************"); return String.format (graphFormat, "********************");
} }
else else
@ -132,26 +152,21 @@ class Cell implements Comparable<Cell>, Value
val = val.substring (val.length () - colWidth); val = val.substring (val.length () - colWidth);
return val; return val;
} }
}
return getError ();
}
String text; private String justify (String text, int colWidth)
if (label != null) {
text = label;
else if (repeatingChar > 0)
text = repeat;
else
text = "?";
if (format == 'R') if (format == 'R')
{ {
String labelFormat = String.format ("%%%d.%ds", colWidth, colWidth); String labelFormat = String.format ("%%%d.%ds", colWidth, colWidth);
return (String.format (labelFormat, text)); return (String.format (labelFormat, text));
} }
else
{
String labelFormat = String.format ("%%-%d.%ds", colWidth, colWidth); String labelFormat = String.format ("%%-%d.%ds", colWidth, colWidth);
return (String.format (labelFormat, text)); return (String.format (labelFormat, text));
} }
}
@Override @Override
public boolean hasValue () public boolean hasValue ()
@ -197,12 +212,20 @@ class Cell implements Comparable<Cell>, Value
public String toString () public String toString ()
{ {
String contents = ""; String contents = "";
if (label != null) if (type != null)
switch (type)
{
case LABEL:
contents = "Labl: " + label; contents = "Labl: " + label;
else if (repeatingChar != 0) break;
case REPEATING_CHARACTER:
contents = "Rept: " + repeatingChar; contents = "Rept: " + repeatingChar;
else if (expressionText != null) break;
case VALUE:
contents = "Exp : " + expressionText; contents = "Exp : " + expressionText;
break;
}
return String.format ("[Cell:%5s %s]", address, contents); return String.format ("[Cell:%5s %s]", address, contents);
} }

View File

@ -151,7 +151,7 @@ public class Sheet implements Iterable<Cell>
ptr += length + 1; // +1 for end-of-line token ptr += length + 1; // +1 for end-of-line token
} }
if (true) if (false)
{ {
System.out.println (); System.out.println ();
System.out.println ("Lines:"); System.out.println ("Lines:");
@ -286,7 +286,7 @@ public class Sheet implements Iterable<Cell>
if (!line.isEmpty ()) if (!line.isEmpty ())
currentCell.setValue (line); // expression currentCell.setValue (line); // expression
if (true) if (false)
System.out.printf ("[%s][%-3s][%s]%n", currentCell.address, format, line); System.out.printf ("[%s][%-3s][%s]%n", currentCell.address, format, line);
} }
@ -332,7 +332,7 @@ public class Sheet implements Iterable<Cell>
return text.toString (); return text.toString ();
} }
public String getCells () public String getTextDisplay ()
{ {
StringBuilder text = new StringBuilder (); StringBuilder text = new StringBuilder ();
String longLine; String longLine;