/*
* 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 geom.Offset;
import java.util.Vector;
// import java.awt.*; TODO: Expand
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import ket.*;
import ket.math.*;
import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxText;
import ket.display.box.BoxFactory;
import geom.Position;
/**
* A single equation may be displayed on its own and its setup and painting is
* controlled by this class.
*/
public class NetworkDisplay implements Display {
final MathCollection mathCollection;
/**
* When the equation is drawn, record its box from which the arguments
* associated with a mouse click may be determined.
*/
Box equationBox;
Box labelBox;
Position actualEquationTopLeft;
public NetworkDisplay(MathCollection mathCollection) {
this.mathCollection = mathCollection;
}
@Override
public Box findDeepestBox(Position p) {
if (equationBox==null) {
return null;
}
return equationBox.findDeepestBox(p);
}
@Override
public Argument findDeepestArgument(Position p) {
if (equationBox==null) {
return null;
}
return equationBox.findDeepestArgument(p);
}
@Override
public Vector<Integer> findVisibleEquationIndices() {
Vector<Integer> indices = new Vector<Integer>();
if (equationBox==null) return indices;
Argument a = equationBox.getArgument();
if (a==null) return indices;
Integer index = a.getEquationIndex();
if (index==null) return indices;
indices.add(index);
return indices;
}
@Override
public Equation pointToEquation(Position p) {
Argument argument = equationBox.getArgument();
if (argument==null) {
return null;
}
return argument.getEquation();
}
@Override
public boolean generateBoxes(Graphics2D g2D, ColourScheme colourScheme, int fontSize, Offset panelRectangle) {
// TODO: Handle multiple equation selection.
Equation equation = mathCollection.getCursor().getEquation();
Argument root = equation.getRoot();
if (equation.isText()) {
equationBox = equation.toBox(colourScheme);
} else {
equationBox = BoxFactory.verticalContainer(root, colourScheme);
}
labelBox = equation.getLabelBox(colourScheme);
labelBox.setProperty(Box.RIGHT_ALIGN);
labelBox.setProperty(Box.Y_CENTRE_ALIGN);
equationBox.setupInnerRectangle(fontSize);
double minimumHeight = equationBox.getInnerRectangle().height;
double shapeWidth = panelRectangle.width - KetPanel.BORDER_OFFSET.width;
Offset equationRectangle = new Offset(shapeWidth, minimumHeight);
labelBox.setup(fontSize, equationRectangle);
Offset windowWithoutLabel = new Offset(shapeWidth-labelBox.getInnerRectangle().width, minimumHeight);
equationBox.setupOuterRectangle(windowWithoutLabel);
//- equationBox.setBackground(true);
double panelHeightOfNextBox;
if (equation.isText()) {
panelHeightOfNextBox = KetPanel.TOP_BORDER_SIZE;
} else {
double centre = (panelRectangle.height-KetPanel.BORDER_OFFSET.height) / 2.0;
double heightAboveCentre = equationBox.getOuterRectangle().height / 2.0;
panelHeightOfNextBox = centre - heightAboveCentre;
}
actualEquationTopLeft = new Position(KetPanel.LEFT_BORDER_SIZE, panelHeightOfNextBox);
return true;
}
/**
* Draw a single equation in the centre of the screen or text to the
* top left. In either case a label is also displayed.
*/
@Override
public void paint(Graphics2D g2D, ColourScheme colourScheme, int fontSize, Offset panelRectangle) {
if (labelBox==null || equationBox==null) {
// (Just in case) If ket panel is painted before boxes have been created in generate boxes.
return;
}
// labelBox.paint(g2D, actualEquationTopLeft);
equationBox.paint(g2D, actualEquationTopLeft, colourScheme);
}
@Override
public boolean isArgumentVisible(Argument argument) {
return equationBox.containsArgument(argument);
}
@Override
public boolean requiresRepaint() {
return false;
}
@Override
public void noteChange(Graphics2D g2D, ColourScheme colourScheme, Argument before, Equation afterEquation, int fontSize, Offset panelRectangle) {
// Do nothing?
}
}