src/Tools/jEdit/src/prover/MarkupNode.scala
author immler@in.tum.de
Wed Apr 22 17:35:49 2009 +0200 (2009-04-22)
changeset 34554 7dc6c231da40
parent 34517 163cda249619
child 34555 7c001369956a
permissions -rw-r--r--
abs. stops, markup nodes depend on doc-version;
fixed missing positions in ProofDocument.text_changed;
relink only changed commands in ProofDocument.token_changed
immler@34393
     1
/*
wenzelm@34407
     2
 * Document markup nodes, with connection to Swing tree model
immler@34393
     3
 *
wenzelm@34407
     4
 * @author Fabian Immler, TU Munich
immler@34393
     5
 */
immler@34393
     6
immler@34393
     7
package isabelle.prover
immler@34393
     8
immler@34554
     9
import isabelle.proofdocument.ProofDocument
immler@34554
    10
immler@34400
    11
import sidekick.IAsset
immler@34393
    12
import javax.swing._
immler@34393
    13
import javax.swing.text.Position
immler@34400
    14
import javax.swing.tree._
immler@34393
    15
immler@34401
    16
object MarkupNode {
immler@34401
    17
immler@34554
    18
  def markup2default_node(node: MarkupNode, cmd: Command, doc: ProofDocument) : DefaultMutableTreeNode = {
immler@34401
    19
wenzelm@34480
    20
    implicit def int2pos(offset: Int): Position =
immler@34554
    21
      new Position { def getOffset = offset; override def toString = offset.toString}
immler@34401
    22
wenzelm@34480
    23
    object RelativeAsset extends IAsset {
immler@34401
    24
      override def getIcon : Icon = null
immler@34514
    25
      override def getShortString : String = node.kind
immler@34514
    26
      override def getLongString : String = node.desc
immler@34514
    27
      override def getName : String = node.id
immler@34514
    28
      override def setName (name : String) = ()
immler@34401
    29
      override def setStart(start : Position) = ()
immler@34554
    30
      override def getStart : Position = node.abs_start(doc)
immler@34401
    31
      override def setEnd(end : Position) = ()
immler@34554
    32
      override def getEnd : Position = node.abs_stop(doc)
immler@34554
    33
      override def toString = node.id + ": " + node.kind + "[" + getStart + " - " + getEnd + "]"
immler@34401
    34
    }
immler@34393
    35
immler@34401
    36
    new DefaultMutableTreeNode(RelativeAsset)
immler@34401
    37
  }
immler@34401
    38
}
immler@34401
    39
immler@34514
    40
class MarkupNode (val base : Command, val start : Int, val stop : Int,
immler@34514
    41
                    val id : String, val kind : String, val desc : String) {
immler@34401
    42
immler@34554
    43
  def swing_node(doc: ProofDocument) : DefaultMutableTreeNode = {
immler@34554
    44
    val node = MarkupNode.markup2default_node (this, base, doc)
immler@34554
    45
    for (c <- children) node add c.swing_node(doc)
immler@34554
    46
    node
immler@34554
    47
  }
immler@34554
    48
    
immler@34554
    49
  def abs_start(doc: ProofDocument) = base.start(doc) + start
immler@34554
    50
  def abs_stop(doc: ProofDocument) = base.start(doc) + stop
immler@34401
    51
immler@34401
    52
  var parent : MarkupNode = null
immler@34401
    53
  def orphan = parent == null
immler@34401
    54
immler@34554
    55
  var children : List[MarkupNode] = Nil
immler@34514
    56
immler@34401
    57
  private def add(child : MarkupNode) {
immler@34401
    58
    child parent = this
immler@34554
    59
    children = (child :: children) sort ((a, b) => a.start < b.start)
immler@34393
    60
  }
immler@34393
    61
immler@34401
    62
  private def remove(nodes : List[MarkupNode]) {
immler@34554
    63
    children = children diff nodes
immler@34401
    64
  }
immler@34393
    65
immler@34401
    66
  def dfs : List[MarkupNode] = {
immler@34401
    67
    var all = Nil : List[MarkupNode]
wenzelm@34503
    68
    for (child <- children)
immler@34401
    69
      all = child.dfs ::: all
immler@34401
    70
    all = this :: all
immler@34401
    71
    all
immler@34401
    72
  }
immler@34401
    73
immler@34514
    74
  def leafs: List[MarkupNode] = {
immler@34514
    75
    if (children == Nil) return List(this)
immler@34514
    76
    else return children flatMap (_.leafs)
immler@34514
    77
  }
immler@34514
    78
immler@34514
    79
  def flatten: List[MarkupNode] = {
immler@34514
    80
    var next_x = start
immler@34514
    81
    if(children.length == 0) List(this)
immler@34514
    82
    else {
immler@34514
    83
      val filled_gaps = for {
immler@34514
    84
        child <- children
immler@34514
    85
        markups = if (next_x < child.start) {
immler@34517
    86
          new MarkupNode(base, next_x, child.start, id, kind, "") :: child.flatten
immler@34514
    87
        } else child.flatten
immler@34514
    88
        update = (next_x = child.stop)
immler@34514
    89
        markup <- markups
immler@34514
    90
      } yield markup
immler@34517
    91
      if (next_x < stop) filled_gaps + new MarkupNode(base, next_x, stop, id, kind, "")
immler@34514
    92
      else filled_gaps
immler@34514
    93
    }
immler@34514
    94
  }
immler@34514
    95
immler@34401
    96
  def insert(new_child : MarkupNode) : Unit = {
wenzelm@34503
    97
    if (new_child fitting_into this) {
wenzelm@34503
    98
      for (child <- children) {
wenzelm@34503
    99
        if (new_child fitting_into child)
immler@34401
   100
          child insert new_child
immler@34401
   101
      }
wenzelm@34503
   102
      if (new_child orphan) {
immler@34401
   103
        // new_child did not fit into children of this
immler@34401
   104
        // -> insert new_child between this and its children
wenzelm@34503
   105
        for (child <- children) {
wenzelm@34503
   106
          if (child fitting_into new_child) {
immler@34401
   107
            new_child add child
immler@34400
   108
          }
immler@34400
   109
        }
immler@34401
   110
        this add new_child
immler@34401
   111
        this remove new_child.children
immler@34400
   112
      }
immler@34400
   113
    } else {
immler@34514
   114
      System.err.println("ignored nonfitting markup " + new_child.id + new_child.kind + new_child.desc
immler@34514
   115
                         + "(" +new_child.start + ", "+ new_child.stop + ")")
immler@34400
   116
    }
immler@34400
   117
  }
immler@34393
   118
immler@34401
   119
  // does this fit into node?
immler@34401
   120
  def fitting_into(node : MarkupNode) = node.start <= this.start &&
immler@34514
   121
    node.stop >= this.stop
immler@34514
   122
immler@34517
   123
  override def toString = "([" + start + " - " + stop + "] " + id + "( " + kind + "): " + desc
immler@34393
   124
}