author | immler@in.tum.de |
Tue, 02 Jun 2009 19:00:58 +0200 | |
changeset 34590 | 320b33f30cae |
parent 34577 | aef72e071725 |
child 34597 | a0c84b0edb9a |
permissions | -rw-r--r-- |
34393 | 1 |
/* |
34407 | 2 |
* Document markup nodes, with connection to Swing tree model |
34393 | 3 |
* |
34407 | 4 |
* @author Fabian Immler, TU Munich |
34393 | 5 |
*/ |
6 |
||
7 |
package isabelle.prover |
|
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 | 12 |
import javax.swing._ |
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 | 15 |
|
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
16 |
abstract class MarkupInfo |
34577 | 17 |
case class RootInfo() extends MarkupInfo |
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
18 |
case class OuterInfo(highlight: String) extends MarkupInfo {override def toString = highlight} |
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
19 |
case class HighlightInfo(highlight: String) extends MarkupInfo {override def toString = highlight} |
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
20 |
case class TypeInfo(type_kind: String) extends MarkupInfo {override def toString = type_kind} |
34568
b517d0607297
implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents:
34564
diff
changeset
|
21 |
case class RefInfo(file: Option[String], line: Option[Int], |
b517d0607297
implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents:
34564
diff
changeset
|
22 |
command_id: Option[String], offset: Option[Int]) extends MarkupInfo { |
b517d0607297
implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents:
34564
diff
changeset
|
23 |
override def toString = (file, line, command_id, offset).toString |
b517d0607297
implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents:
34564
diff
changeset
|
24 |
} |
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
25 |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
26 |
object MarkupNode { |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
27 |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
28 |
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
|
29 |
|
34480 | 30 |
implicit def int2pos(offset: Int): Position = |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
31 |
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
|
32 |
|
34480 | 33 |
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
|
34 |
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
|
35 |
override def getShortString : String = node.content |
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
36 |
override def getLongString : String = node.info.toString |
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
37 |
override def getName : String = node.id |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
38 |
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
|
39 |
override def setStart(start : Position) = () |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
40 |
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
|
41 |
override def setEnd(end : Position) = () |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
} |
34393 | 45 |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
46 |
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
|
47 |
} |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
48 |
} |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
49 |
|
34560 | 50 |
class MarkupNode (val base: Command, val start: Int, val stop: Int, |
34557 | 51 |
val children: List[MarkupNode], |
34568
b517d0607297
implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents:
34564
diff
changeset
|
52 |
val id: String, val content: String, val info: MarkupInfo) { |
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
53 |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
54 |
def swing_node(doc: ProofDocument) : DefaultMutableTreeNode = { |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
55 |
val node = MarkupNode.markup2default_node (this, base, doc) |
34557 | 56 |
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
|
57 |
node |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
58 |
} |
34557 | 59 |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
|
34557 | 63 |
def set_children(newchildren: List[MarkupNode]): MarkupNode = |
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
64 |
new MarkupNode(base, start, stop, newchildren, id, content, info) |
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
65 |
|
34557 | 66 |
def add(child : MarkupNode) = set_children ((child :: children) sort ((a, b) => a.start < b.start)) |
34393 | 67 |
|
34557 | 68 |
def remove(nodes : List[MarkupNode]) = set_children(children diff nodes) |
34393 | 69 |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
70 |
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
|
71 |
var all = Nil : List[MarkupNode] |
34503 | 72 |
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
|
73 |
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
|
74 |
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
|
75 |
all |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
76 |
} |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
77 |
|
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
78 |
def leafs: List[MarkupNode] = { |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
79 |
if (children == Nil) return List(this) |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
80 |
else return children flatMap (_.leafs) |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
81 |
} |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
82 |
|
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
83 |
def flatten: List[MarkupNode] = { |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
84 |
var next_x = start |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
85 |
if(children.length == 0) List(this) |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
86 |
else { |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
87 |
val filled_gaps = for { |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
88 |
child <- children |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
89 |
markups = if (next_x < child.start) { |
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
90 |
new MarkupNode(base, next_x, child.start, Nil, id, content, info) :: child.flatten |
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
91 |
} else child.flatten |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
92 |
update = (next_x = child.stop) |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
93 |
markup <- markups |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
94 |
} yield markup |
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
95 |
if (next_x < stop) filled_gaps + new MarkupNode(base, next_x, stop, Nil, id, content, info) |
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
96 |
else filled_gaps |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
97 |
} |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
98 |
} |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
99 |
|
34559 | 100 |
def filter(p: (MarkupNode => Boolean)): List[MarkupNode] = { |
101 |
val filtered = children.flatMap(_.filter(p)) |
|
102 |
if (p(this)) List(this set_children(filtered)) |
|
103 |
else filtered |
|
104 |
} |
|
105 |
||
34557 | 106 |
def +(new_child : MarkupNode) : MarkupNode = { |
34503 | 107 |
if (new_child fitting_into this) { |
34561 | 108 |
var inserted = false |
109 |
val new_children = children.map(c => if((new_child fitting_into c) && !inserted) {inserted = true; c + new_child} else c) |
|
110 |
if (!inserted) { |
|
34557 | 111 |
// new_child did not fit into children of this -> insert new_child between this and its children |
34561 | 112 |
val fitting = children filter(_ fitting_into new_child) |
113 |
(this remove fitting) add ((new_child /: fitting) (_ + _)) |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
114 |
} |
34557 | 115 |
else this set_children new_children |
34400
1b61a92f8675
MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents:
34393
diff
changeset
|
116 |
} else { |
34590 | 117 |
System.err.println("ignored nonfitting markup: " + new_child) |
118 |
this |
|
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 |
} |
34393 | 121 |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
122 |
// does this fit into node? |
34557 | 123 |
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
|
124 |
|
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
125 |
override def toString = "([" + start + " - " + stop + "] " + id + "( " + content + "): " + info.toString |
34393 | 126 |
} |