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

186 lines
4.6 KiB
Java
Raw Normal View History

2016-03-04 03:56:28 +00:00
package com.bytezone.diskbrowser.visicalc;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
2017-02-18 09:54:24 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2016-03-04 03:56:28 +00:00
class Range implements Iterable<Address>
{
2017-03-14 11:28:52 +00:00
private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}");
2017-02-18 09:54:24 +00:00
private static final Pattern rangePattern =
Pattern.compile ("([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})");
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
2017-03-03 23:41:08 +00:00
private Address from, to;
private final List<Address> range = new ArrayList<Address> ();
private final Sheet parent;
2016-03-04 03:56:28 +00:00
2017-03-15 00:41:45 +00:00
private boolean isHorizontal;
2017-02-18 09:54:24 +00:00
2017-03-15 00:41:45 +00:00
public Range (Sheet parent, Cell cell, String rangeText)
2016-03-04 03:56:28 +00:00
{
2017-03-03 10:24:23 +00:00
this.parent = parent;
2016-03-04 03:56:28 +00:00
2017-03-15 00:41:45 +00:00
Matcher m = rangePattern.matcher (rangeText);
if (m.find ())
{
from = new Address (m.group (1), m.group (2));
to = new Address (m.group (3), m.group (4));
isHorizontal = from.rowMatches (to);
populateRange ();
}
else
throw new IllegalArgumentException ();
2017-03-03 10:24:23 +00:00
2017-02-18 09:54:24 +00:00
}
2017-03-15 00:41:45 +00:00
public Range (Sheet parent, Address from, Address to)
2017-02-18 09:54:24 +00:00
{
2017-03-03 10:24:23 +00:00
this.parent = parent;
2017-03-15 00:41:45 +00:00
this.from = from;
this.to = to;
2017-03-03 10:24:23 +00:00
2017-03-15 00:41:45 +00:00
isHorizontal = from.rowMatches (to);
populateRange ();
2017-02-18 09:54:24 +00:00
}
2017-03-15 00:41:45 +00:00
// public Range (Sheet parent, String[] cells)
// {
// this.parent = parent;
//
// for (String s : cells)
// range.add (new Address (s));
//
// createCells ();
// }
private void populateRange ()
2017-02-18 09:54:24 +00:00
{
2016-03-04 03:56:28 +00:00
range.add (from);
2017-03-15 00:41:45 +00:00
parent.getCell (from);
2017-02-18 09:54:24 +00:00
Address tempFrom = from;
2016-03-04 03:56:28 +00:00
2017-03-03 23:41:08 +00:00
if (from.rowMatches (to))
2016-03-04 03:56:28 +00:00
while (from.compareTo (to) < 0)
{
from = from.nextColumn ();
range.add (from);
2017-03-15 00:41:45 +00:00
parent.getCell (from);
2016-03-04 03:56:28 +00:00
}
2017-03-03 23:41:08 +00:00
else if (from.columnMatches (to))
2016-03-04 03:56:28 +00:00
while (from.compareTo (to) < 0)
{
from = from.nextRow ();
range.add (from);
2017-03-15 00:41:45 +00:00
parent.getCell (from);
2016-03-04 03:56:28 +00:00
}
else
throw new InvalidParameterException ();
2017-03-03 10:24:23 +00:00
2017-02-18 09:54:24 +00:00
from = tempFrom;
2017-03-15 00:41:45 +00:00
// createCells ();
2016-03-04 03:56:28 +00:00
}
2017-03-15 00:41:45 +00:00
// private void createCells ()
// {
// for (Address address : range)
// parent.getCell (address); // ensure that the cell exists
// }
2016-03-07 04:37:01 +00:00
boolean isHorizontal ()
{
2017-03-15 00:41:45 +00:00
// Address first = range.get (0);
// Address last = range.get (range.size () - 1);
// return first.rowMatches (last);
return isHorizontal;
2016-03-07 04:37:01 +00:00
}
boolean isVertical ()
{
2017-03-15 00:41:45 +00:00
// Address first = range.get (0);
// Address last = range.get (range.size () - 1);
// return first.columnMatches (last);
return !isHorizontal;
2016-03-07 04:37:01 +00:00
}
2017-02-18 09:54:24 +00:00
@Override
public Iterator<Address> iterator ()
{
return range.iterator ();
}
2017-03-03 10:24:23 +00:00
public int size ()
{
2017-03-15 00:41:45 +00:00
// int total = 0;
//
// for (Address address : range)
// if (parent.getCell (address) != null)
// ++total;
2017-03-03 10:24:23 +00:00
2017-03-15 00:41:45 +00:00
return range.size ();
2017-03-03 10:24:23 +00:00
}
2017-03-15 00:41:45 +00:00
// private void setRange (String text)
// {
// Matcher m = rangePattern.matcher (text);
// if (m.find ())
// {
// from = new Address (m.group (1), m.group (2));
// to = new Address (m.group (3), m.group (4));
// populateRange ();
// return;
// }
//
// // m = addressList.matcher (text);
// // if (m.find ())
// // {
// // System.out.printf ("Address list: %s%n", text);
// // String[] cells = m.group (1).split (",");
// // for (String s : cells)
// // {
// // System.out.println (s);
// // if (cellAddress.matcher (s).matches ())
// // range.add (new Address (s));
// // else
// // {
// // System.out.println ("Not a cell address: " + s);
// // }
// // }
// //
// // System.out.println ();
// // return;
// // }
//
// int pos = text.indexOf ("...");
// if (pos > 0)
// {
// String fromAddress = text.substring (0, pos);
// String toAddress = text.substring (pos + 3);
// from = new Address (fromAddress);
// to = new Address (toAddress);
// populateRange ();
// return;
// }
//
// System.out.printf ("null range [%s]%n", text);
// }
2017-02-18 09:54:24 +00:00
2016-03-04 03:56:28 +00:00
@Override
public String toString ()
{
if (from == null || to == null)
{
StringBuilder text = new StringBuilder ();
for (Address address : range)
2017-03-03 23:41:08 +00:00
text.append (address.getText () + ",");
2016-03-04 03:56:28 +00:00
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
return text.toString ();
}
2017-03-03 23:41:08 +00:00
return String.format (" %s -> %s", from.getText (), to.getText ());
2016-03-04 03:56:28 +00:00
}
}