/*
* 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 ketUI.panel;
import java.awt.*;
import java.util.*;
import geom.Offset;
import geom.Position;
import ket.*;
import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxText;
import ket.math.*;
/**
* This class stores enough box information to draw a box at its current
* location. To draw an equation, it is converted to an equationBox, a label
* box and has a top left point relative to which it is drawn.
*/
class BoxEquation implements Comparable<BoxEquation> {
final Box equation;
final Box label;
Position topLeft;
public BoxEquation(Box equation, Box label) {
assert equation!=null : "BoxEquation: '(Box) equation' is null.";
assert label!=null : "BoxEquation: '(Box) label' is null.";
this.equation = equation;
this.label = label;
this.topLeft = null;
}
public BoxEquation(Box equation, Box label, Position topLeft) {
assert equation!=null : "BoxEquation: '(Box) equation' is null.";
//D assert label!=null : "BoxEquation: '(Box) label' is null.";
assert topLeft!=null : "TopLeft is null.";
this.equation = equation;
this.label = label;
this.topLeft = topLeft;
}
public Position getTopLeft() {
assert isValid();
assert topLeft!=null : "topLeft is being accessed while null.";
return topLeft;
}
public void setTopLeft(Position topLeft) {
this.topLeft = topLeft;
}
public Box getEquationBox() {
assert equation!=null;
return equation;
}
public Equation getEquation() {
assert equation!=null;
Argument argument = equation.getArgument();
assert argument!=null;
return argument.getEquation();
}
public Box getLabel() {
return label;
}
public void paint(Graphics2D g2D, ColourScheme colourScheme) {
label.paint(g2D, topLeft, colourScheme);
/*-
// TODO: Check that the box fits the screen.
// TODO: How does BoxText's wordwrap interact with this?
if (equation.getInnerRectangle().width < equation.getOuterRectangle().width) {
equation.paint(g2D, topLeft);
} else{
// The equation is too wide.
// TODO: Implement.
equation.paint(g2D, topLeft);
}
*/
equation.paint(g2D, topLeft, colourScheme);
}
public Box findDeepestBox(Position p) {
Box equationMatch = equation.findDeepestBox(p);
if (equationMatch!=null && equationMatch.getArgument()!=null) {
return equationMatch;
}
Box labelMatch = label.findDeepestBox(p);
if (labelMatch!=null && labelMatch.getArgument()!=null) {
return equation;
}
return null;
}
public Argument findDeepestArgument(Position p) {
Argument equationMatch = equation.findDeepestArgument(p);
if (equationMatch!=null) {
return equationMatch;
}
Box labelMatch = label.findDeepestBox(p);
if (labelMatch!=null && labelMatch.getArgument()!=null) {
Argument root = equation.getArgument();
if (root!=null) {
return root;
}
}
return null;
}
public double getWidth() {
return equation.getInnerRectangle().width;
}
public double getFullWidth() {
return equation.getInnerRectangle().width + label.getInnerRectangle().width;
}
public double getHeight() {
return equation.getInnerRectangle().height;
}
// WARNING: This is a special case, it sorts by equation-box width and not equation index.
public int compareTo(BoxEquation that) {
//- return (int) (this.getEquationBox().getInnerRectangle().width - that.getEquationBox().getInnerRectangle().width);
return (int) (this.getWidth() - that.getWidth());
}
public boolean isValid() {
return topLeft!=null && label!=null && equation!=null;
}
public String toString() {
String string = "";
/*
string += "boxEquation::equation: " + equation + "\n";
string += " label: " + label + "\n";
string += " topLeft: " + topLeft;
*/
string = equation.getArgument().toString();
return string;
}
}