1 package isac.gui.mawen.editor
4 import isac.gui.mawen.syntax.Ast._
8 import edu.tum.cs.isabelle.api.Implementation
9 import java.awt.event._
10 import java.awt.font._
11 import shapeless.Widen
12 import org.drools.xml.ExtensibleXmlParser.Null
13 import scala.collection.immutable.List
14 import isac.gui.mawen.editor.Box.DrawBox
18 * The Java Swing component for a formula
19 * represented by an <code>Ast</code>
21 class AstComponent(var ast: Ast) extends JComponent with AstContainer with MouseListener with MouseMotionListener {
23 addMouseListener(this)
24 addMouseMotionListener(this)
26 var pressedMousePoint : Point = null
27 var marker : Rectangle = new Rectangle()
28 var markedBoxes : List[DrawBox] = List.empty[DrawBox]
30 var mousePositionFunction : (Graphics, Int, Int, String, Box) => Unit = null
32 var box : DrawBox = null
35 addKeyListener(EventUtil.CreateKeyEventHandler(this))
36 addMouseWheelListener(EventUtil.CreateMouseWheelHandler(this))
40 def setAst(ast: Ast) = {
44 def getAst() : Ast = this.ast
49 override def paint(g: Graphics) = {
50 val g2 = g.asInstanceOf[Graphics2D]
52 // enable antialiasing for Text
53 val rh = new RenderingHints(
54 RenderingHints.KEY_TEXT_ANTIALIASING,
55 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
56 g2.setRenderingHints(rh);
62 g.setColor(Color.WHITE)
63 g.fillRect(0, 0, getWidth, getHeight)
66 g.setColor(Color.BLACK)
67 g.translate(Settings.translateX, Settings.translateY)
68 g2.scale(Settings.zoom, Settings.zoom)
70 Settings.fillStringbounds(ast, g2)
72 box = CalcUtil.assembleBoxes(ast, (box: DrawBox) =>
74 if (EventUtil.foreachBoxFunction != null) {
75 EventUtil.foreachBoxFunction(g2, box)
82 setSize((box.width * Settings.zoom).round + 50 + Settings.translateX, Math.max((box.height * Settings.zoom).round, 50) + Settings.translateY)
83 setPreferredSize(getSize)
87 EventUtil.foreachBoxFunction = null
89 g.drawRect(marker.x, marker.y, marker.width, marker.height)
94 def mouseEntered(e: MouseEvent) {
98 def mouseExited(e: MouseEvent ) {
102 def mousePressed(e: MouseEvent ) {
103 val boxAst : Ast = AstInfoUtil.FindBox(ast)
104 if (boxAst != null) {
105 ast =TransformAstUtil.Update(ast, boxAst, TransformAstUtil.UnBox)
109 pressedMousePoint = new Point ( Settings.translateX - e.getX , Settings.translateY - e.getY)
110 marker.setLocation((e.getX / Settings.zoom ).round - (Settings.translateX / Settings.zoom).round,
111 (e.getY / Settings.zoom ).round - (Settings.translateY / Settings.zoom).round)
113 def mouseReleased(e: MouseEvent ) {
114 pressedMousePoint = null
116 var highest : DrawBox = findHighesBoxOf(box, markedBoxes)
117 ast = TransformAstUtil.Update(ast, highest.ast, TransformAstUtil.Box)
119 markedBoxes = List.empty[DrawBox]
122 def mouseClicked(e: MouseEvent ) {
123 if (e.isControlDown()) {
125 EventUtil.foreachBoxFunction = EventUtil.doInBox(e.getPoint,
128 ast = TransformAstUtil.Update(ast, box.ast, TransformAstUtil.AstToCursor)
134 mousePositionFunction = null
141 def mouseDragged(e:MouseEvent) {
142 if (e.isControlDown() && Settings.isdraggingAllowed) {
143 if (pressedMousePoint != null) {
144 Settings.translateX = e.getX + pressedMousePoint.x
145 Settings.translateY = e.getY + pressedMousePoint.y
151 marker.setSize(((e.getX / Settings.zoom).round - marker.x ) - (Settings.translateX / Settings.zoom).round,
152 ((e.getY / Settings.zoom).round - marker.y ) - (Settings.translateY / Settings.zoom).round)
153 EventUtil.foreachBoxFunction = (g, b) => {
154 if (Box.BoxIn(b, marker) && Box.isLeaf(b)) {
155 Box.draw(b, g, false)
156 if (!markedBoxes.contains(b))
157 markedBoxes = b :: markedBoxes
164 def mouseMoved(e: MouseEvent ) {
165 EventUtil.drawBoxAt(e.getPoint())
175 def findHighesBoxOf(rootbox: DrawBox, markedBoxes : List[DrawBox]) : DrawBox = {
177 if (markedBoxes.forall(x => Box.Contains(rootbox,x)) &&
178 ! rootbox.children.exists(x => markedBoxes.forall(y => Box.Contains(x, y)))
182 } else if (markedBoxes.forall(x => Box.Contains(rootbox, x)) &&
183 rootbox.children.exists(x => markedBoxes.forall(y => Box.Contains(x, y)))) {
185 for( box <- rootbox.children) {
186 var resb = findHighesBoxOf(box, markedBoxes)