2016-03-09 10:38:53 +00:00
|
|
|
package com.bytezone.diskbrowser.visicalc;
|
|
|
|
|
2016-03-12 22:38:03 +00:00
|
|
|
class If extends Function
|
2016-03-09 10:38:53 +00:00
|
|
|
{
|
2017-03-14 11:28:52 +00:00
|
|
|
private final String conditionText;
|
2016-03-09 10:38:53 +00:00
|
|
|
private final String textTrue;
|
|
|
|
private final String textFalse;
|
|
|
|
|
2017-03-14 11:28:52 +00:00
|
|
|
private final Condition condition;
|
2017-03-03 23:41:08 +00:00
|
|
|
private final Expression expTrue;
|
|
|
|
private final Expression expFalse;
|
2016-03-10 02:39:23 +00:00
|
|
|
|
2017-03-14 11:28:52 +00:00
|
|
|
public If (Sheet parent, Cell cell, String text)
|
2016-03-09 10:38:53 +00:00
|
|
|
{
|
2017-03-14 11:28:52 +00:00
|
|
|
super (parent, cell, text);
|
2016-03-09 10:38:53 +00:00
|
|
|
|
2016-03-12 22:33:18 +00:00
|
|
|
int pos1 = functionText.indexOf (',');
|
2017-03-14 11:28:52 +00:00
|
|
|
int pos2 = -1;
|
|
|
|
if (pos1 < 0)
|
|
|
|
throw new IllegalArgumentException ("Not enough parameters for IF: " + text);
|
2016-03-16 06:15:39 +00:00
|
|
|
|
2017-03-14 11:28:52 +00:00
|
|
|
conditionText = functionText.substring (0, pos1);
|
|
|
|
condition = new Condition (parent, cell, conditionText);
|
|
|
|
// System.out.printf ("Cond : %s%n", conditionText);
|
2017-02-26 10:44:10 +00:00
|
|
|
values.add (condition);
|
2016-03-16 06:15:39 +00:00
|
|
|
|
2017-03-14 11:28:52 +00:00
|
|
|
if (functionText.charAt (pos1 + 1) == '@')
|
|
|
|
{
|
|
|
|
textTrue = Expression.getBalancedText (functionText.substring (pos1 + 1));
|
|
|
|
// System.out.printf ("True : %s%n", textTrue);
|
|
|
|
expTrue = new Expression (parent, cell, textTrue);
|
|
|
|
pos2 = functionText.indexOf (',', pos1 + textTrue.length () + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos2 = functionText.indexOf (',', pos1 + 1);
|
|
|
|
textTrue = functionText.substring (pos1 + 1, pos2);
|
|
|
|
// System.out.printf ("True : %s%n", textTrue);
|
|
|
|
expTrue = new Expression (parent, cell, functionText.substring (pos1 + 1, pos2));
|
|
|
|
}
|
2017-03-03 23:41:08 +00:00
|
|
|
values.add (expTrue);
|
2017-03-14 11:28:52 +00:00
|
|
|
|
|
|
|
if (pos2 < 0)
|
|
|
|
throw new IllegalArgumentException ("Not enough parameters for IF: " + text);
|
|
|
|
|
|
|
|
textFalse = functionText.substring (pos2 + 1);
|
|
|
|
// System.out.printf ("False: %s%n", textFalse);
|
|
|
|
expFalse = new Expression (parent, cell, textFalse);
|
2017-03-03 23:41:08 +00:00
|
|
|
values.add (expFalse);
|
2017-03-14 11:28:52 +00:00
|
|
|
|
|
|
|
// System.out.println ();
|
2016-03-09 10:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-02-28 20:39:26 +00:00
|
|
|
public void calculate ()
|
2016-03-09 10:38:53 +00:00
|
|
|
{
|
2016-03-16 19:32:25 +00:00
|
|
|
valueType = ValueType.VALUE;
|
2017-02-26 10:44:10 +00:00
|
|
|
condition.calculate ();
|
2016-03-16 19:32:25 +00:00
|
|
|
|
2017-02-26 10:44:10 +00:00
|
|
|
if (condition.getValue () == 1)
|
2016-03-10 02:39:23 +00:00
|
|
|
{
|
2016-03-19 05:31:30 +00:00
|
|
|
expTrue.calculate ();
|
|
|
|
|
2017-02-25 03:56:22 +00:00
|
|
|
if (!expTrue.isValueType (ValueType.VALUE))
|
2016-03-16 19:32:25 +00:00
|
|
|
valueType = expTrue.getValueType ();
|
|
|
|
else
|
|
|
|
value = expTrue.getValue ();
|
2016-03-10 02:39:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-19 05:31:30 +00:00
|
|
|
expFalse.calculate ();
|
|
|
|
|
2017-02-25 03:56:22 +00:00
|
|
|
if (!expFalse.isValueType (ValueType.VALUE))
|
2016-03-16 19:32:25 +00:00
|
|
|
valueType = expFalse.getValueType ();
|
|
|
|
else
|
|
|
|
value = expFalse.getValue ();
|
2016-03-10 02:39:23 +00:00
|
|
|
}
|
2016-03-09 10:38:53 +00:00
|
|
|
}
|
2016-03-14 08:58:54 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString ()
|
|
|
|
{
|
|
|
|
return String.format ("[IF:%s, True:%s, False:%s]", condition, textTrue, textFalse);
|
|
|
|
}
|
2016-03-09 10:38:53 +00:00
|
|
|
}
|