src/Tools/jEdit/src/prover/MarkupNode.scala
author immler@in.tum.de
Mon, 02 Feb 2009 23:05:25 +0100
changeset 34517 163cda249619
parent 34514 2104a836b415
child 34554 7dc6c231da40
permissions -rw-r--r--
implemented markTokens; moved choose_color to DynamicTokenMarker and extended to choose_style
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
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
     9
import sidekick.IAsset
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    10
import javax.swing._
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    11
import javax.swing.text.Position
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
    12
import javax.swing.tree._
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    13
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    14
object MarkupNode {
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    15
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    16
  def markup2default_node(node : MarkupNode) : DefaultMutableTreeNode = {
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    17
34480
017fae24829f simplified implicit convertion Int => Position;
wenzelm
parents: 34407
diff changeset
    18
    implicit def int2pos(offset: Int): Position =
017fae24829f simplified implicit convertion Int => Position;
wenzelm
parents: 34407
diff changeset
    19
      new Position { def getOffset = offset }
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    20
34480
017fae24829f simplified implicit convertion Int => Position;
wenzelm
parents: 34407
diff changeset
    21
    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
    22
      override def getIcon : Icon = null
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    23
      override def getShortString : String = node.kind
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    24
      override def getLongString : String = node.desc
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    25
      override def getName : String = node.id
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    26
      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
    27
      override def setStart(start : Position) = ()
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    28
      override def getStart : Position = node.abs_start
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 setEnd(end : Position) = ()
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    30
      override def getEnd : Position = node.abs_stop
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    31
      override def toString = node.id + ": " + node.kind + "[" + node.start + " - " + node.stop + "]"
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    32
    }
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    33
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    34
    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
    35
  }
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    36
}
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    37
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    38
class MarkupNode (val base : Command, val start : Int, val stop : Int,
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    39
                    val id : String, val kind : 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
    40
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    41
  val swing_node : DefaultMutableTreeNode = MarkupNode.markup2default_node (this)
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    42
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    43
  var parent : MarkupNode = null
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    44
  def orphan = parent == null
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    45
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    46
  def length = stop - start
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    47
  def abs_start = base.start + start
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    48
  def abs_stop = base.start + stop
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    49
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    50
  private var children_cell : List[MarkupNode] = Nil
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    51
  //track changes in swing_node
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    52
  def children = children_cell
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    53
  def children_= (cs : List[MarkupNode]) = {
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    54
    swing_node.removeAllChildren
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
    55
    for (c <- cs) swing_node add c.swing_node
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    56
    children_cell = cs
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    57
  }
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    58
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    59
  private def add(child : MarkupNode) {
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    60
    child parent = this
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    61
    children_cell = (child :: children) sort ((a, b) => a.start < b.start)
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    62
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    63
    swing_node add child.swing_node
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    64
  }
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    65
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    66
  private def remove(nodes : List[MarkupNode]) {
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    67
    children_cell = children diff nodes
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    68
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
    69
      for (node <- nodes) try {
34402
bd8d70cd9baf information on command-phase left of scrollbar
immler@in.tum.de
parents: 34401
diff changeset
    70
        swing_node remove node.swing_node
bd8d70cd9baf information on command-phase left of scrollbar
immler@in.tum.de
parents: 34401
diff changeset
    71
      } catch { case e : IllegalArgumentException =>
bd8d70cd9baf information on command-phase left of scrollbar
immler@in.tum.de
parents: 34401
diff changeset
    72
        System.err.println(e.toString)
bd8d70cd9baf information on command-phase left of scrollbar
immler@in.tum.de
parents: 34401
diff changeset
    73
        case e => throw e
bd8d70cd9baf information on command-phase left of scrollbar
immler@in.tum.de
parents: 34401
diff changeset
    74
      }
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    75
  }
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
    76
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    77
  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
    78
    var all = Nil : List[MarkupNode]
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
    79
    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
    80
      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
    81
    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
    82
    all
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    83
  }
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
    84
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    85
  def leafs: List[MarkupNode] = {
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    86
    if (children == Nil) return List(this)
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    87
    else return children flatMap (_.leafs)
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
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    90
  def flatten: List[MarkupNode] = {
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    91
    var next_x = start
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    92
    if(children.length == 0) List(this)
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    93
    else {
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    94
      val filled_gaps = for {
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    95
        child <- children
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    96
        markups = if (next_x < child.start) {
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34514
diff changeset
    97
          new MarkupNode(base, next_x, child.start, id, kind, "") :: child.flatten
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    98
        } else child.flatten
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
    99
        update = (next_x = child.stop)
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   100
        markup <- markups
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   101
      } yield markup
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34514
diff changeset
   102
      if (next_x < stop) filled_gaps + new MarkupNode(base, next_x, stop, id, kind, "")
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   103
      else filled_gaps
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   104
    }
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   105
  }
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   106
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   107
  def insert(new_child : MarkupNode) : Unit = {
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
   108
    if (new_child fitting_into this) {
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
   109
      for (child <- children) {
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
   110
        if (new_child fitting_into child)
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   111
          child insert new_child
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   112
      }
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
   113
      if (new_child orphan) {
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   114
        // new_child did not fit into children of this
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   115
        // -> insert new_child between this and its children
34503
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
   116
        for (child <- children) {
7d0726f19d04 tuned whitespace;
wenzelm
parents: 34480
diff changeset
   117
          if (child fitting_into new_child) {
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   118
            new_child add child
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
   119
          }
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
   120
        }
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   121
        this add new_child
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   122
        this remove new_child.children
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
   123
      }
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
   124
    } else {
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   125
      System.err.println("ignored nonfitting markup " + new_child.id + new_child.kind + new_child.desc
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   126
                         + "(" +new_child.start + ", "+ new_child.stop + ")")
34400
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
   127
    }
1b61a92f8675 MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents: 34393
diff changeset
   128
  }
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
   129
34401
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   130
  // does this fit into node?
44241a37b74a structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents: 34400
diff changeset
   131
  def fitting_into(node : MarkupNode) = node.start <= this.start &&
34514
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   132
    node.stop >= this.stop
2104a836b415 renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents: 34503
diff changeset
   133
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34514
diff changeset
   134
  override def toString = "([" + start + " - " + stop + "] " + id + "( " + kind + "): " + desc
34393
f0e1608a774f basic tree structure for sidekick
immler@in.tum.de
parents:
diff changeset
   135
}