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

95 lines
2.1 KiB
Java
Raw Normal View History

2016-03-04 03:56:28 +00:00
package com.bytezone.diskbrowser.visicalc;
class Address implements Comparable<Address>
{
2016-03-04 05:27:14 +00:00
private static final int MAX_ROWS = 255;
private static final int MAX_COLUMNS = 64;
2016-03-04 03:56:28 +00:00
int row, column;
2016-03-15 20:06:04 +00:00
int rowKey;
int columnKey;
2016-03-04 03:56:28 +00:00
String text;
public Address (String column, String row)
{
set (column, row);
}
public Address (int column, int row)
{
2016-03-04 05:27:14 +00:00
assert column <= MAX_COLUMNS;
assert row <= MAX_ROWS;
2016-03-04 03:56:28 +00:00
this.row = row;
this.column = column;
2016-03-15 20:06:04 +00:00
rowKey = row * MAX_COLUMNS + column;
columnKey = column * MAX_ROWS + row;
2016-03-04 03:56:28 +00:00
int col1 = column / 26;
int col2 = column % 26;
String col =
col1 > 0 ? (char) ('@' + col1) + ('A' + col2) + "" : (char) ('A' + col2) + "";
text = col + (row + 1);
}
public Address (String address)
{
if (address.charAt (1) < 'A')
set (address.substring (0, 1), address.substring (1));
else
set (address.substring (0, 2), address.substring (2));
}
private void set (String sCol, String sRow)
{
if (sCol.length () == 1)
column = sCol.charAt (0) - 'A';
else if (sCol.length () == 2)
column = (sCol.charAt (0) - '@') * 26 + sCol.charAt (1) - 'A';
else
System.out.println ("Bollocks");
try
{
row = Integer.parseInt (sRow) - 1;
2016-03-15 20:06:04 +00:00
rowKey = row * MAX_COLUMNS + column;
columnKey = column * MAX_ROWS + row;
2016-03-04 03:56:28 +00:00
text = sCol + sRow;
}
catch (NumberFormatException e)
{
System.out.printf ("NFE: %s%n", sRow);
}
}
2016-03-04 05:27:14 +00:00
Address nextRow ()
2016-03-04 03:56:28 +00:00
{
Address next = new Address (column, row + 1);
return next;
}
2016-03-04 05:27:14 +00:00
Address nextColumn ()
2016-03-04 03:56:28 +00:00
{
Address next = new Address (column + 1, row);
return next;
}
2016-03-15 20:06:04 +00:00
// copied from Appleworks Cell
static String getCellName (int row, int column)
{
char c1 = (char) ('A' + column / 26 - 1);
char c2 = (char) ('A' + column % 26);
return "" + (c1 == '@' ? "" : c1) + c2 + row;
}
2016-03-04 03:56:28 +00:00
@Override
public String toString ()
{
2016-03-15 20:06:04 +00:00
return String.format ("%-4s %3d %3d %4d", text, row, column, rowKey);
2016-03-04 03:56:28 +00:00
}
@Override
public int compareTo (Address o)
{
2016-03-15 20:06:04 +00:00
return rowKey - o.rowKey;
2016-03-04 03:56:28 +00:00
}
}