package uk.ac.lkl.migen.mockup.shapebuilder.ui.tool.shape;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.*;
import uk.ac.lkl.common.ui.NotifyingLine;
import uk.ac.lkl.common.util.value.DoubleValue;
import uk.ac.lkl.migen.mockup.shapebuilder.Configuration;
import uk.ac.lkl.migen.mockup.shapebuilder.model.shape.ExpressedShape;
import uk.ac.lkl.migen.mockup.shapebuilder.model.shape.LineExpression;
import uk.ac.lkl.migen.mockup.shapebuilder.ui.*;
public abstract class SelectShapeTool extends ShapeTool {
public SelectShapeTool(ShapePlotter shapePlotter, String buttonText) {
super(shapePlotter, buttonText);
}
protected void initialiseTool() {
super.initialiseTool();
}
@Override
protected void processKeyPressed(KeyEvent e) {
// hack - repeated work here with menu item
boolean deleteShapeEnabled = Configuration.getConfiguration().getBooleanParameter("delete-shape-enabled");
if (! deleteShapeEnabled)
return;
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE
|| e.getKeyCode() == KeyEvent.VK_DELETE) {
ExpressedShape selectedShape = shapePlotter.getSelectedShape();
if (selectedShape == null)
return;
getModel().removeShape(selectedShape);
}
}
protected void paintToolMarks(Graphics2D g2) {
ExpressedShape selectedShape = shapePlotter.getSelectedShape();
if (selectedShape == null)
return;
Rectangle2D.Double bounds = selectedShape.getBounds2D();
bounds.setRect(bounds.getX() * shapePlotter.getGridSize(), bounds
.getY()
* shapePlotter.getGridSize(), bounds.getWidth()
* shapePlotter.getGridSize(), bounds.getHeight()
* shapePlotter.getGridSize());
g2.setColor(Color.RED);
g2.fill(new Rectangle2D.Double(bounds.getX() - 2, bounds.getY() - 2, 4,
4));
g2.fill(new Rectangle2D.Double(bounds.getX() + bounds.getWidth() - 2,
bounds.getY() - 2, 4, 4));
g2.fill(new Rectangle2D.Double(bounds.getX() + bounds.getWidth() - 2,
bounds.getY() + bounds.getHeight() - 2, 4, 4));
g2.fill(new Rectangle2D.Double(bounds.getX() - 2, bounds.getY()
+ bounds.getHeight() - 2, 4, 4));
g2.draw(bounds);
}
protected final void processMouseMoved(MouseEvent e) {
NotifyingLine<DoubleValue> closestLine = shapePlotter.getClosestLine(e);
shapePlotter.setHighlightedLine(closestLine);
doProcessMouseMoved(e);
}
protected void doProcessMouseMoved(MouseEvent e) {
// do nothing
}
protected final void processMouseClicked(MouseEvent e) {
if (e.getClickCount() == 2)
processDoubleClick(e);
else
doProcessMouseClicked(e);
}
private void processDoubleClick(MouseEvent e) {
NotifyingLine<DoubleValue> highlightedLine = shapePlotter.getHighlightedLine();
if (highlightedLine == null)
return;
ExpressedShape shape = shapePlotter.getShape(highlightedLine);
LineExpression lineVariable = new LineExpression(shape, highlightedLine);
getModel().addPaletteExpression(lineVariable);
}
protected void doProcessMouseClicked(MouseEvent e) {
// do nothing
}
}
|