src/Tools/jEdit/src/prover/MarkupNode.scala
author immler@in.tum.de
Wed, 29 Apr 2009 16:08:22 +0200
changeset 34557 647a2430d1cd
parent 34555 7c001369956a
child 34558 668fae39d86d
permissions -rw-r--r--
immutable markup-nodes; more seperate nodes in command; restructured handling of markups in prover
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
     1
/*
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34402
diff changeset
     2
 * Document markup nodes, with connection to Swing tree model
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
     3
 *
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34402
diff changeset
     4
 * @author Fabian Immler, TU Munich
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
     5
 */
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
     6
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
     7
package isabelle.prover
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
     8
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
     9
import isabelle.proofdocument.ProofDocument
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    10
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
    11
import sidekick.IAsset
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    12
import javax.swing._
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    13
import javax.swing.text.Position
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
    14
import javax.swing.tree._
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    15
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    16
object MarkupNode {
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    17
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    18
  def markup2default_node(node: MarkupNode, cmd: Command, doc: ProofDocument) : DefaultMutableTreeNode = {
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    19
34480
017fae24829f simplified implicit convertion Int => Position;
wenzelm
parents: 34407
diff changeset
    20
    implicit def int2pos(offset: Int): Position =
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    21
      new Position { def getOffset = offset; override def toString = offset.toString}
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    22
34480
017fae24829f simplified implicit convertion Int => Position;
wenzelm
parents: 34407
diff changeset
    23
    object RelativeAsset extends IAsset {
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    24
      override def getIcon : Icon = null
34555
7c001369956a included information on ML status messages in Sidekick's status-window
immler@in.tum.de
parents: 34554
diff changeset
    25
      override def getShortString : String = node.content
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    26
      override def getLongString : String = node.desc
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    27
      override def getName : String = node.id
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    28
      override def setName (name : String) = ()
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    29
      override def setStart(start : Position) = ()
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    30
      override def getStart : Position = node.abs_start(doc)
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    31
      override def setEnd(end : Position) = ()
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    32
      override def getEnd : Position = node.abs_stop(doc)
34555
7c001369956a included information on ML status messages in Sidekick's status-window
immler@in.tum.de
parents: 34554
diff changeset
    33
      override def toString = node.id + ": " + node.content + "[" + getStart + " - " + getEnd + "]"
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    34
    }
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    35
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    36
    new DefaultMutableTreeNode(RelativeAsset)
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    37
  }
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    38
}
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    39
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    40
class MarkupNode (val base : Command, val start : Int, val stop : Int,
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    41
                  val children: List[MarkupNode],
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    42
                  val id : String, val content : String, val desc : String) {
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    43
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    44
  def swing_node(doc: ProofDocument) : DefaultMutableTreeNode = {
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    45
    val node = MarkupNode.markup2default_node (this, base, doc)
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    46
    children.map(node add _.swing_node(doc))
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    47
    node
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    48
  }
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    49
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    50
  def abs_start(doc: ProofDocument) = base.start(doc) + start
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34517
diff changeset
    51
  def abs_stop(doc: ProofDocument) = base.start(doc) + stop
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    52
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    53
  def set_children(newchildren: List[MarkupNode]): MarkupNode =
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    54
    new MarkupNode(base, start, stop, newchildren, id, content, desc)
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    55
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    56
  def add(child : MarkupNode) = set_children ((child :: children) sort ((a, b) => a.start < b.start))
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    57
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    58
  def remove(nodes : List[MarkupNode]) = set_children(children diff nodes)
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    59
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    60
  def dfs : List[MarkupNode] = {
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    61
    var all = Nil : List[MarkupNode]
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
    62
    for (child <- children)
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    63
      all = child.dfs ::: all
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    64
    all = this :: all
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    65
    all
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    66
  }
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    67
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    68
  def leafs: List[MarkupNode] = {
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    69
    if (children == Nil) return List(this)
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    70
    else return children flatMap (_.leafs)
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    71
  }
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    72
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    73
  def flatten: List[MarkupNode] = {
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    74
    var next_x = start
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    75
    if(children.length == 0) List(this)
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    76
    else {
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    77
      val filled_gaps = for {
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    78
        child <- children
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    79
        markups = if (next_x < child.start) {
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    80
          new MarkupNode(base, next_x, child.start, Nil, id, content, "") :: child.flatten
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    81
        } else child.flatten
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    82
        update = (next_x = child.stop)
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    83
        markup <- markups
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    84
      } yield markup
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    85
      if (next_x < stop) filled_gaps + new MarkupNode(base, next_x, stop, Nil, id, content, "")
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    86
      else filled_gaps
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    87
    }
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    88
  }
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    89
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    90
  def +(new_child : MarkupNode) : MarkupNode = {
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
    91
    if (new_child fitting_into this) {
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    92
      val new_children = children.map(c => if((new_child fitting_into c)) c + new_child else c)
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    93
      if (new_children == children) {
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    94
        // new_child did not fit into children of this -> insert new_child between this and its children
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    95
        val (fitting, nonfitting) = children span(_ fitting_into new_child)
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    96
        this remove fitting add ((new_child /: fitting) (_ add _))
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    97
      }
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
    98
      else this set_children new_children
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
    99
    } else {
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
   100
      error("ignored nonfitting markup " + new_child.id + new_child.content + new_child.desc
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   101
                         + "(" +new_child.start + ", "+ new_child.stop + ")")
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
   102
    }
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
   103
  }
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
   104
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   105
  // does this fit into node?
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34555
diff changeset
   106
  def fitting_into(node : MarkupNode) = node.start <= this.start && node.stop >= this.stop
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   107
34555
7c001369956a included information on ML status messages in Sidekick's status-window
immler@in.tum.de
parents: 34554
diff changeset
   108
  override def toString = "([" + start + " - " + stop + "] " + id + "( " + content + "): " + desc
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
   109
}