/*
* Copyright (C) 2011 Alasdair C. Hamilton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package ket.math.purpose.function;
import java.awt.*;
import java.util.*;
import ket.display.*;
import ket.display.LatexTools;
import ket.display.box.Box;
import ket.display.box.BoxGraphic;
import ket.display.box.BoxList;
import ket.display.box.BoxTools;
import ket.display.box.BoxWord;
import ket.math.*;
import ket.math.convert.Like;
import ket.math.purpose.NumberValue;
import ket.math.purpose.SymbolicState;
import ket.math.purpose.Word;
/**
* A specific mathematical operation that acts on arguments in a case-specific
* way.
*/
public class FunctionFork extends FunctionForm {
@Override
public String toLatex(Vector<Argument> args) {
int columns = args.size()/2 + args.size() % 2; // i.e round up
if (columns<2) {
columns = 2;
}
Argument[][] table = new Argument[columns][2];
for (int i=0; i<args.size(); i++) {
table[i/2][i%2] = args.get(i);
}
if (table!=null) {
return LatexTools.tableToLatex(table, "\\{", "."); // Null elements become blanks.;
} else {
return super.toLatex(args);
}
}
/*>
@Override
public String toHTML(Vector<Argument> args) {
Argument[][] table = asTable(args);
if (table!=null) {
// TODO: Finish.
return super.toHTML(args);
} else {
return super.toHTML(args);
}
}
*/
private Box[][] asTable(Vector<Argument> args, ColourScheme colourScheme) {
int columns = args.size()/2 + args.size() % 2;
if (columns<2) {
columns = 2;
}
Vector<Box> boxArgs = BoxTools.padArgs(args, 2*columns, colourScheme);
Box[][] table = new Box[columns][3];
for (int i=0; i<2*columns; i+=2) {
table[i/2][0] = boxArgs.get(i);
table[i/2][1] = new BoxWord(null, " ", Box.X_CENTRE_ALIGN|Box.Y_CENTRE_ALIGN);
table[i/2][2] = boxArgs.get(i+1);
}
return table;
}
@Override
public Box toBox(Argument argument, long settings, ColourScheme colourScheme, Vector<Argument> args) {
Box[][] table = asTable(args, colourScheme);
if (table!=null) {
Box matrixWithoutBrackets = BoxTools.gridToBox(argument, table);
BoxGraphic open = new BoxGraphic(argument, 0L, BoxGraphic.OPEN_PARENTHESIS);
BoxList result = BoxTools.centredHorizontalBoxList(argument, settings, open, matrixWithoutBrackets);
open.setProperty(Box.PRESERVE_ASPECT_RATIO|Box.VERTICAL_STRETCH|Box.PLAIN_FONT);
result.setProperty(settings); //n
return result;
} else {
return super.toBox(argument, settings, colourScheme, args);
}
}
}