isac-java/src/java/isac/gui/mawen/editor/AstComponent.scala
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 29 Jun 2017 14:06:23 +0200
changeset 5167 7d8922399461
parent 5164 69958bb0692a
parent 5165 cfe43661c515
child 5168 83927534dc1b
child 5169 6f4319f9ccd7
permissions -rw-r--r--
merged
     1 package isac.gui.mawen.editor
     2 
     3 import javax.swing._
     4 import isac.gui.mawen.syntax.Ast._
     5 import java.awt._
     6 import java.awt.geom._
     7 import java.awt.font._
     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
    15 
    16 
    17 /**
    18  * The Java Swing component for a formula
    19  * represented by an <code>Ast</code>
    20  */
    21 class AstComponent(var ast: Ast) extends JComponent with AstContainer with MouseListener with MouseMotionListener {
    22 
    23   addMouseListener(this)
    24   addMouseMotionListener(this)
    25   
    26   var pressedMousePoint : Point = null
    27   var marker : Rectangle = new Rectangle()
    28   var markedBoxes : List[DrawBox] = List.empty[DrawBox]
    29 
    30   var mousePositionFunction : (Graphics, Int, Int, String, Box) => Unit = null
    31   
    32   var box : DrawBox  = null
    33   
    34   setFocusable(true)
    35   addKeyListener(EventUtil.CreateKeyEventHandler(this))
    36   addMouseWheelListener(EventUtil.CreateMouseWheelHandler(this))
    37   
    38   setSize(50,150)
    39   
    40   def setAst(ast: Ast) = {
    41     this.ast = ast
    42     repaint()
    43   }
    44   def getAst() : Ast = this.ast
    45   
    46   
    47   
    48   
    49   override def paint(g: Graphics) = {
    50     val g2 = g.asInstanceOf[Graphics2D]
    51     
    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);
    57     
    58     
    59     
    60     
    61     // white background
    62     g.setColor(Color.WHITE)
    63     g.fillRect(0, 0, getWidth, getHeight)
    64     
    65     
    66     g.setColor(Color.BLACK)
    67     g.translate(Settings.translateX, Settings.translateY)
    68     g2.scale(Settings.zoom, Settings.zoom)
    69     
    70     Settings.fillStringbounds(ast, g2)
    71     
    72     box =  CalcUtil.assembleBoxes(ast, (box: DrawBox) => 
    73         {
    74           if (EventUtil.foreachBoxFunction != null) {
    75             EventUtil.foreachBoxFunction(g2, box)
    76           }
    77         }
    78       )
    79       
    80       BoxUtil.Draw(box , g)
    81       
    82       setSize((box.width * Settings.zoom).round + 50 + Settings.translateX, Math.max((box.height * Settings.zoom).round, 50)  + Settings.translateY)
    83       setPreferredSize(getSize)
    84       
    85       
    86       
    87       EventUtil.foreachBoxFunction = null
    88       
    89       g.drawRect(marker.x, marker.y, marker.width, marker.height)
    90       
    91   }
    92   
    93   
    94 	def mouseEntered(e: MouseEvent) {
    95 		
    96 		
    97 	}
    98 	def mouseExited(e: MouseEvent ) {
    99 		
   100 
   101 	}
   102 	def mousePressed(e: MouseEvent ) {
   103 	  val boxAst : Ast = AstInfoUtil.FindBox(ast)
   104 	  if (boxAst != null) {
   105   	  ast =TransformAstUtil.Update(ast, boxAst, TransformAstUtil.UnBox)    
   106 	  }
   107 	  
   108 	  
   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)
   112 	}
   113 	def mouseReleased(e: MouseEvent ) {
   114 		pressedMousePoint = null
   115 		marker.setSize(0, 0)
   116 		var highest : DrawBox = findHighesBoxOf(box, markedBoxes)
   117 		ast = TransformAstUtil.Update(ast, highest.ast, TransformAstUtil.Box)
   118 		
   119 		markedBoxes = List.empty[DrawBox]
   120 		repaint()
   121 	}
   122 	def mouseClicked(e: MouseEvent ) {
   123 		if (e.isControlDown()) {
   124 		  
   125 		  EventUtil.foreachBoxFunction = EventUtil.doInBox(e.getPoint,
   126 		      (g, box) => {
   127 		        
   128   	        ast = TransformAstUtil.Update(ast, box.ast, TransformAstUtil.AstToCursor)
   129   	        
   130   	        repaint()
   131 		      }
   132 		    );
   133 		}else {
   134 		  mousePositionFunction = null
   135 		}
   136 		repaint()
   137 	}
   138 	
   139 	
   140 	
   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 
   146     	  
   147     	  repaint()
   148   	  }
   149     }
   150     else {
   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 
   158         }
   159       }
   160       repaint()
   161     }
   162 	}
   163 
   164 	def mouseMoved(e: MouseEvent ) {
   165 	  EventUtil.drawBoxAt(e.getPoint())
   166     repaint()
   167 	}
   168   
   169   
   170   
   171   
   172   
   173 	
   174   
   175   def findHighesBoxOf(rootbox: DrawBox, markedBoxes : List[DrawBox]) : DrawBox = {
   176     
   177     if (markedBoxes.forall(x => Box.Contains(rootbox,x)) && 
   178         ! rootbox.children.exists(x => markedBoxes.forall(y => Box.Contains(x, y)))
   179       ) {
   180       return rootbox
   181       
   182     } else if (markedBoxes.forall(x => Box.Contains(rootbox, x)) && 
   183         rootbox.children.exists(x => markedBoxes.forall(y => Box.Contains(x, y)))) {
   184       
   185       for( box <- rootbox.children) {
   186         var resb = findHighesBoxOf(box, markedBoxes)
   187         if (resb != null) {
   188           return resb
   189         }
   190         
   191       }
   192     }
   193     return null
   194   }
   195 }
   196 
   197 
   198 
   199 
   200 
   201 
   202 
   203 
   204 
   205 
   206 
   207 
   208 
   209 
   210