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
|
2017-03-15 07:07:36 +00:00
|
|
|
throw new IllegalArgumentException (rangeText);
|
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
|
|
|
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;
|
2016-03-04 03:56:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 04:37:01 +00:00
|
|
|
boolean isHorizontal ()
|
|
|
|
{
|
2017-03-15 00:41:45 +00:00
|
|
|
return isHorizontal;
|
2016-03-07 04:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean isVertical ()
|
|
|
|
{
|
2017-03-15 00:41:45 +00:00
|
|
|
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
|
|
|
return range.size ();
|
2017-03-03 10:24:23 +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
|
|
|
}
|
|
|
|
}
|