FindBox now returns Option[Ast]
authormmahringer <s1520454056@students.fh-hagenberg.at>
Tue, 12 Sep 2017 13:34:41 +0200
changeset 52131f3173a59903
parent 5212 51971c00458f
child 5214 733780043a0a
FindBox now returns Option[Ast]
isac-java/src/java/isac/gui/mawen/editor/AstComponent.scala
isac-java/src/java/isac/gui/mawen/editor/AstInfoUtil.scala
isac-java/src/java/isac/gui/mawen/editor/BoxUtil.scala
isac-java/src/java/isac/gui/mawen/editor/EditingUtil.scala
isac-java/src/java/isac/gui/mawen/editor/EventUtil.scala
isac-java/src/java/isac/gui/mawen/editor/TransformAstUtil.scala
     1.1 --- a/isac-java/src/java/isac/gui/mawen/editor/AstComponent.scala	Tue Sep 12 12:02:50 2017 +0200
     1.2 +++ b/isac-java/src/java/isac/gui/mawen/editor/AstComponent.scala	Tue Sep 12 13:34:41 2017 +0200
     1.3 @@ -71,7 +71,7 @@
     1.4            }
     1.5          }
     1.6        )
     1.7 -      BoxUtil.Draw(box , g)
     1.8 +      BoxUtil.draw(box , g)
     1.9        setSize((box.width * Settings.zoom).round + 50 + Settings.translateX, Math.max((box.height * Settings.zoom).round, 50)  + Settings.translateY)
    1.10        setPreferredSize(getSize)
    1.11        EventUtil.foreachBoxFunction = null
    1.12 @@ -82,10 +82,13 @@
    1.13  	def mouseEntered(e: MouseEvent) {}
    1.14  	def mouseExited(e: MouseEvent ) {}
    1.15  	def mousePressed(e: MouseEvent ) {
    1.16 -	  val boxAst : Ast = AstInfoUtil.FindBox(ast)
    1.17 -	  if (boxAst != null) {
    1.18 -  	  ast =TransformAstUtil.Update(ast, boxAst, TransformAstUtil.UnBox)    
    1.19 +	  AstInfoUtil.FindBox(ast) match {
    1.20 +	    case Some(boxAst) => {
    1.21 +	      ast =TransformAstUtil.Update(ast, boxAst, TransformAstUtil.UnBox)    
    1.22 +	    }
    1.23 +	    case None => {}
    1.24  	  }
    1.25 +	 
    1.26  	  val cursorAst : Ast = AstInfoUtil.FindCursor(ast)
    1.27  	  if (cursorAst != null) {
    1.28    	  ast =TransformAstUtil.Update(ast, cursorAst , TransformAstUtil.CursorToAst)    
    1.29 @@ -108,10 +111,10 @@
    1.30  	}
    1.31  	def mouseClicked(e: MouseEvent ) {
    1.32  	  if (e.isControlDown()) {
    1.33 -	    val boxAst : Ast = AstInfoUtil.FindBox(ast)  
    1.34 -  	  if (boxAst != null) {
    1.35 -    	  ast =TransformAstUtil.Update(ast, boxAst, TransformAstUtil.UnBox)    
    1.36 -  	  }
    1.37 +	    AstInfoUtil.FindBox(ast) match {
    1.38 +	      case Some(boxAst) =>  ast =TransformAstUtil.Update(ast, boxAst, TransformAstUtil.UnBox)
    1.39 +	      case None => {}
    1.40 +	    }
    1.41  		  EventUtil.foreachBoxFunction = EventUtil.doInBox(e.getPoint,
    1.42  		      (g, box) => {
    1.43    	        ast = TransformAstUtil.Update(ast, box.ast, TransformAstUtil.AstToCursor)
     2.1 --- a/isac-java/src/java/isac/gui/mawen/editor/AstInfoUtil.scala	Tue Sep 12 12:02:50 2017 +0200
     2.2 +++ b/isac-java/src/java/isac/gui/mawen/editor/AstInfoUtil.scala	Tue Sep 12 13:34:41 2017 +0200
     2.3 @@ -93,17 +93,20 @@
     2.4      case Variable(str) => false
     2.5      case Appl(asts) => asts.exists(x => hasBox(x))
     2.6    }
     2.7 -   //TODOWN ..: Option[Ast] = ...
     2.8 -  def FindBox(ast: Ast) : Ast = ast match {
     2.9 -     case Appl(List(Constant(str), a)) if str.startsWith("BOX") => ast
    2.10 +  def FindBox(ast: Ast) : Option[Ast] = ast match {
    2.11 +     case Appl(List(Constant(str), a)) if str.startsWith("BOX") => Some(ast)
    2.12       case Appl(asts)                                            => {
    2.13 -       val l = asts.map(x => FindBox(x)).filter(y => y != null)
    2.14 +       val l = asts.map(x => FindBox(x))
    2.15 +       l.foreach(x => x match {
    2.16 +         case Some(a) => return x
    2.17 +         case None => {}
    2.18 +       })
    2.19         if (l != null && l.length >0) {
    2.20           return l.head
    2.21         }
    2.22 -       return null
    2.23 +       return None
    2.24       }
    2.25 -     case ast                                                   => null
    2.26 +     case ast                                                   => None
    2.27    }
    2.28    //TODOWN ..: Option[Ast] = ...
    2.29    def FindCursor(ast: Ast) : Ast = ast match {
     3.1 --- a/isac-java/src/java/isac/gui/mawen/editor/BoxUtil.scala	Tue Sep 12 12:02:50 2017 +0200
     3.2 +++ b/isac-java/src/java/isac/gui/mawen/editor/BoxUtil.scala	Tue Sep 12 13:34:41 2017 +0200
     3.3 @@ -19,19 +19,18 @@
     3.4   */
     3.5  object BoxUtil {
     3.6  
     3.7 -  // TODOWN rename "draw"
     3.8 -  def Draw(box : DrawBox, g: Graphics) : Unit = box.name match {
     3.9 +  def draw(box : DrawBox, g: Graphics) : Unit = box.name match {
    3.10      case "GAP"                         => drawGap(g, box) 
    3.11      case "CURSOR"                      => drawCursor(g, box)
    3.12      case "Groups.plus_class.plus"      => drawPlus(g, box)   //TODOWN "+" 
    3.13      case "Fields.inverse_class.divide" => drawDivide(g, box) //TODOWN "/"
    3.14      case ""                            => {
    3.15        drawBox(box, g)
    3.16 -      box.children.foreach( Draw(_, g) )
    3.17 +      box.children.foreach( draw(_, g) )
    3.18      }
    3.19      case _ =>  {
    3.20        drawString(g, box)
    3.21 -      box.children.foreach( Draw(_, g) )
    3.22 +      box.children.foreach( draw(_, g) )
    3.23      }
    3.24    }
    3.25    def drawGap(g: Graphics, box : DrawBox) {
    3.26 @@ -46,7 +45,7 @@
    3.27          g.asInstanceOf[Graphics2D].drawLine(box.x, box.y, box.x, box.y - box.height)
    3.28  
    3.29          g.setColor(Color.BLACK)
    3.30 -        if (box.children.length >= 1) Draw(box.children.head, g)
    3.31 +        if (box.children.length >= 1) draw(box.children.head, g)
    3.32        }
    3.33        case Appl(List(a, Constant("CURSOR"))) => {
    3.34          g.setColor(Color.RED)
    3.35 @@ -55,12 +54,12 @@
    3.36          
    3.37          g.setColor(Color.BLACK)
    3.38          if (box.children.length >= 1)
    3.39 -          Draw(box.children.head, g)
    3.40 +          draw(box.children.head, g)
    3.41        }
    3.42      }
    3.43      case _ => {
    3.44        if (box.children.length >= 1)
    3.45 -          Draw(box.children.head, g)
    3.46 +          draw(box.children.head, g)
    3.47      }
    3.48    }
    3.49    // TODOWN layout_class "linear"
     4.1 --- a/isac-java/src/java/isac/gui/mawen/editor/EditingUtil.scala	Tue Sep 12 12:02:50 2017 +0200
     4.2 +++ b/isac-java/src/java/isac/gui/mawen/editor/EditingUtil.scala	Tue Sep 12 13:34:41 2017 +0200
     4.3 @@ -69,15 +69,14 @@
     4.4        case _ => None
     4.5      }
     4.6      case KeyEvent.VK_DELETE => Some(Appl(List(Constant("CURSOR"),Constant("GAP"))))
     4.7 -    case KeyEvent.VK_F2 => {
     4.8 -      val b = AstInfoUtil.FindBox(c.getAst())
     4.9 -      if (b == null) {
    4.10 -        return None
    4.11 +    case KeyEvent.VK_F2 => AstInfoUtil.FindBox(c.getAst()) match {
    4.12 +      case None => None
    4.13 +      case Some(b) => {
    4.14 +        c.setAst(TransformAstUtil.Update(c.getAst(), b, (a) => a match {
    4.15 +          case Appl(List(Constant(str), ast)) if str.startsWith("BOX") => Appl(List(Constant("CURSOR"), ast))
    4.16 +        }))
    4.17 +        None
    4.18        }
    4.19 -      c.setAst(TransformAstUtil.Update(c.getAst(), b, (a) => a match {
    4.20 -        case Appl(List(Constant(str), ast)) if str.startsWith("BOX") => Appl(List(Constant("CURSOR"), ast))
    4.21 -      }))
    4.22 -      None
    4.23      }
    4.24    }
    4.25    def delimerState(c: AstContainer, input: Character, cursorAst: Ast) : Option[Ast] =  AstInfoUtil.AstOfCursor(cursorAst) match {
     5.1 --- a/isac-java/src/java/isac/gui/mawen/editor/EventUtil.scala	Tue Sep 12 12:02:50 2017 +0200
     5.2 +++ b/isac-java/src/java/isac/gui/mawen/editor/EventUtil.scala	Tue Sep 12 13:34:41 2017 +0200
     5.3 @@ -80,7 +80,10 @@
     5.4           }else if (event.getKeyCode == KeyEvent.VK_UP) {
     5.5             TransformAstUtil.BoxParent(c)
     5.6           }
     5.7 -        EditorListenerHandler.fireNotifysBraille(c, AstInfoUtil.FindBox(c.getAst()))
     5.8 +         AstInfoUtil.FindBox(c.getAst()) match {
     5.9 +           case Some(boxAst) => EditorListenerHandler.fireNotifysBraille(c, boxAst)
    5.10 +           case None => {}
    5.11 +         }
    5.12        } else if (AstInfoUtil.hasCursor(c.getAst())) {
    5.13          EditingUtil.parse(c, -event.getKeyCode)
    5.14        }
    5.15 @@ -93,16 +96,19 @@
    5.16        }else if (event.getKeyCode == KeyEvent.VK_UP) {
    5.17           TransformAstUtil.CursorParent(c)
    5.18        }else if (event.isControlDown() && event.getKeyCode == KeyEvent.VK_ENTER) {
    5.19 -        val boxAst : Ast = AstInfoUtil.FindBox(c.getAst())
    5.20 -    	  if (boxAst != null) {
    5.21 -      	  c.setAst(TransformAstUtil.Update(c.getAst(), boxAst, TransformAstUtil.UnBox))
    5.22 -    	  }
    5.23 +        AstInfoUtil.FindBox(c.getAst()) match {
    5.24 +          case Some(boxAst ) =>  {
    5.25 +        	  c.setAst(TransformAstUtil.Update(c.getAst(), boxAst, TransformAstUtil.UnBox))
    5.26 +      	  }
    5.27 +          case None => {}
    5.28 +        }
    5.29      	  val cursorAst : Ast = AstInfoUtil.FindCursor(c.getAst())
    5.30      	  if (cursorAst != null) {
    5.31        	  c.setAst(TransformAstUtil.Update(c.getAst(), cursorAst , TransformAstUtil.CursorToAst))
    5.32      	  }
    5.33          EditorListenerHandler.fireNotifyIsaCheck(c)
    5.34 -      }else if (event.isControlDown() && event.getKeyCode == KeyEvent.VK_V) {
    5.35 +        
    5.36 +       }else if (event.isControlDown() && event.getKeyCode == KeyEvent.VK_V) {
    5.37          // !!!! Dirty Code !!!!
    5.38          c.setAst(Variable(Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor).asInstanceOf[String]))
    5.39          EditorListenerHandler.fireNotifyIsaCheck(c)
     6.1 --- a/isac-java/src/java/isac/gui/mawen/editor/TransformAstUtil.scala	Tue Sep 12 12:02:50 2017 +0200
     6.2 +++ b/isac-java/src/java/isac/gui/mawen/editor/TransformAstUtil.scala	Tue Sep 12 13:34:41 2017 +0200
     6.3 @@ -33,55 +33,59 @@
     6.4       case _ =>  throw new AstException("UnBox, Box has a false structure(required: Appl(List(Constant(\"BOX.1\"), ast), get: " + raw_string_of(ast) + ")")
     6.5    }
     6.6    def BoxNextChild(c: AstContainer) = {
     6.7 -     val boxAst = AstInfoUtil.FindBox(c.getAst())
     6.8 -     if (boxAst != null) {// check if there is a box
     6.9 -       val boxAstElem  = TransformAstUtil.UnBox(boxAst) // get ast from Appl(Cursor, ast)
    6.10 -       val unboxedroot = TransformAstUtil.Update(c.getAst(), boxAst, TransformAstUtil.UnBox) // remove Box from Root Ast
    6.11 -       AstInfoUtil.Parent(unboxedroot, boxAstElem) match {
    6.12 -         case Some(parentAst) => c.setAst(TransformAstUtil.Update(unboxedroot, AstInfoUtil.NextChild(parentAst, boxAstElem), TransformAstUtil.Box))
    6.13 -         case None => {}
    6.14 +     AstInfoUtil.FindBox(c.getAst()) match {
    6.15 +       case None => {}
    6.16 +       case Some(boxAst) => {// check if there is a box
    6.17 +         val boxAstElem  = TransformAstUtil.UnBox(boxAst) // get ast from Appl(Cursor, ast)
    6.18 +         val unboxedroot = TransformAstUtil.Update(c.getAst(), boxAst, TransformAstUtil.UnBox) // remove Box from Root Ast
    6.19 +         AstInfoUtil.Parent(unboxedroot, boxAstElem) match {
    6.20 +           case Some(parentAst) => c.setAst(TransformAstUtil.Update(unboxedroot, AstInfoUtil.NextChild(parentAst, boxAstElem), TransformAstUtil.Box))
    6.21 +           case None => {}
    6.22 +         }
    6.23         }
    6.24 -       
    6.25 -      
    6.26       }
    6.27    }
    6.28 -  def BoxPrevChild(c: AstContainer) = {
    6.29 -     val boxAst = AstInfoUtil.FindBox(c.getAst())
    6.30 -     if (boxAst != null) {// check if there is a box
    6.31 +  def BoxPrevChild(c: AstContainer) =  AstInfoUtil.FindBox(c.getAst()) match {
    6.32 +    case None => {}
    6.33 +    case Some(boxAst) => {
    6.34         val boxAstElem  = TransformAstUtil.UnBox(boxAst)
    6.35         val unboxedAst = TransformAstUtil.Update(c.getAst(), boxAst, TransformAstUtil.UnBox)
    6.36         AstInfoUtil.Parent(unboxedAst, boxAstElem) match {
    6.37           case Some(parentAst) => c.setAst(TransformAstUtil.Update(unboxedAst, AstInfoUtil.PrevChild(parentAst, boxAstElem), TransformAstUtil.Box))
    6.38           case None => {}
    6.39         }
    6.40 -       
    6.41       }
    6.42    }
    6.43 -  def BoxParent(c: AstContainer) = {
    6.44 -     val boxAst = AstInfoUtil.FindBox(c.getAst())
    6.45 -     if (boxAst != null) {// check if there is a box
    6.46 +  def BoxParent(c: AstContainer) = AstInfoUtil.FindBox(c.getAst()) match{
    6.47 +    case None => {}
    6.48 +    case Some(boxAst) =>  {// check if there is a box
    6.49         val boxAstElem  = TransformAstUtil.UnBox(boxAst)
    6.50         val unboxedAst = TransformAstUtil.Update(c.getAst(), boxAst, TransformAstUtil.UnBox)
    6.51         AstInfoUtil.Parent(unboxedAst, boxAstElem) match {
    6.52           case Some(parentAst) => c.setAst(TransformAstUtil.Update(unboxedAst, parentAst, TransformAstUtil.Box))
    6.53           case None => {}
    6.54         }
    6.55 -       
    6.56       }
    6.57 +    
    6.58    }
    6.59    def BoxChild(c: AstContainer) = {
    6.60 -     val boxAst = AstInfoUtil.FindBox(c.getAst())
    6.61 -     if (boxAst != null) { // check if there is a box
    6.62 -       val boxAstElem  = TransformAstUtil.UnBox(boxAst)
    6.63 -       if (AstInfoUtil.FirstChild(boxAstElem) != null) {
    6.64 -         val unboxedAst = TransformAstUtil.Update(c.getAst(), boxAst, TransformAstUtil.UnBox)
    6.65 -          AstInfoUtil.FirstChild(boxAstElem) match {
    6.66 -           case Some(a) => c.setAst(TransformAstUtil.Update(unboxedAst, a, TransformAstUtil.Box))
    6.67 -           case None => {}
    6.68 +     AstInfoUtil.FindBox(c.getAst()) match {
    6.69 +       case None => {}
    6.70 +       case Some(boxAst) => {
    6.71 +         val boxAstElem  = TransformAstUtil.UnBox(boxAst)
    6.72 +         if (AstInfoUtil.FirstChild(boxAstElem) != null) {
    6.73 +           val unboxedAst = TransformAstUtil.Update(c.getAst(), boxAst, TransformAstUtil.UnBox)
    6.74 +            AstInfoUtil.FirstChild(boxAstElem) match {
    6.75 +             case Some(a) => c.setAst(TransformAstUtil.Update(unboxedAst, a, TransformAstUtil.Box))
    6.76 +             case None => {}
    6.77 +           }
    6.78 +           
    6.79           }
    6.80 -         
    6.81         }
    6.82       }
    6.83 +    
    6.84 +       
    6.85 +     
    6.86    }
    6.87    def CursorNextChild(c: AstContainer) = {
    6.88       val boxAst = AstInfoUtil.FindCursor(c.getAst())