author | immler@in.tum.de |
Thu, 27 Aug 2009 10:51:09 +0200 | |
changeset 34676 | 9e725d34df7b |
parent 34659 | e888d0cdda9c |
child 34701 | 80b0add08eef |
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 |
34582 | 18 |
case class HighlightInfo(highlight: String) extends |
19 |
MarkupInfo { override def toString = highlight } |
|
20 |
case class TypeInfo(type_kind: String) extends |
|
21 |
MarkupInfo { override def toString = type_kind } |
|
34568
b517d0607297
implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents:
34564
diff
changeset
|
22 |
case class RefInfo(file: Option[String], line: Option[Int], |
34582 | 23 |
command_id: Option[String], offset: Option[Int]) extends MarkupInfo { |
24 |
override def toString = (file, line, command_id, offset).toString |
|
25 |
} |
|
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
26 |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
27 |
object MarkupNode { |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
28 |
|
34582 | 29 |
def markup2default_node(node: MarkupNode, cmd: Command, doc: ProofDocument) |
30 |
: DefaultMutableTreeNode = { |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
31 |
|
34480 | 32 |
implicit def int2pos(offset: Int): Position = |
34582 | 33 |
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
|
34 |
|
34480 | 35 |
object RelativeAsset extends IAsset { |
34582 | 36 |
override def getIcon: Icon = null |
37 |
override def getShortString: String = node.content |
|
38 |
override def getLongString: String = node.info.toString |
|
39 |
override def getName: String = node.id |
|
40 |
override def setName(name: String) = () |
|
41 |
override def setStart(start: Position) = () |
|
42 |
override def getStart: Position = node.abs_start(doc) |
|
43 |
override def setEnd(end: Position) = () |
|
44 |
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
|
45 |
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
|
46 |
} |
34393 | 47 |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
48 |
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
|
49 |
} |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
50 |
} |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
51 |
|
34582 | 52 |
class MarkupNode(val base: Command, val start: Int, val stop: Int, |
53 |
val children: List[MarkupNode], |
|
54 |
val id: String, val content: String, val info: MarkupInfo) |
|
55 |
{ |
|
34656 | 56 |
|
34582 | 57 |
def swing_node(doc: ProofDocument): DefaultMutableTreeNode = { |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
58 |
val node = MarkupNode.markup2default_node (this, base, doc) |
34557 | 59 |
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
|
60 |
node |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
61 |
} |
34557 | 62 |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34517
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
|
34557 | 66 |
def set_children(newchildren: List[MarkupNode]): MarkupNode = |
34564
850dc36d4926
let MarkupNode carry arbitrary information
immler@in.tum.de
parents:
34561
diff
changeset
|
67 |
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
|
68 |
|
34659 | 69 |
private def add(child: MarkupNode) = |
34582 | 70 |
set_children ((child :: children) sort ((a, b) => a.start < b.start)) |
34393 | 71 |
|
34582 | 72 |
def remove(nodes: List[MarkupNode]) = set_children(children diff nodes) |
34393 | 73 |
|
34582 | 74 |
def dfs: List[MarkupNode] = { |
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
75 |
var all = Nil : List[MarkupNode] |
34503 | 76 |
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
|
77 |
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
|
78 |
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
|
79 |
all |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
80 |
} |
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
81 |
|
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
82 |
def leafs: List[MarkupNode] = { |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
83 |
if (children == Nil) return List(this) |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
84 |
else return children flatMap (_.leafs) |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
85 |
} |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
86 |
|
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
87 |
def flatten: List[MarkupNode] = { |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
88 |
var next_x = start |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
89 |
if(children.length == 0) List(this) |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
90 |
else { |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
91 |
val filled_gaps = for { |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
92 |
child <- children |
34582 | 93 |
markups = |
94 |
if (next_x < child.start) { |
|
95 |
new MarkupNode(base, next_x, child.start, Nil, id, content, info) :: child.flatten |
|
96 |
} else child.flatten |
|
34514
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
97 |
update = (next_x = child.stop) |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
98 |
markup <- markups |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
99 |
} yield markup |
34582 | 100 |
if (next_x < stop) |
101 |
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
|
102 |
else filled_gaps |
2104a836b415
renamed fields of MarkupNode; implemented flatten and leafs
immler@in.tum.de
parents:
34503
diff
changeset
|
103 |
} |
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 |
|
34582 | 106 |
def filter(p: MarkupNode => Boolean): List[MarkupNode] = { |
34559 | 107 |
val filtered = children.flatMap(_.filter(p)) |
108 |
if (p(this)) List(this set_children(filtered)) |
|
109 |
else filtered |
|
110 |
} |
|
111 |
||
34582 | 112 |
def +(new_child: MarkupNode): MarkupNode = { |
34503 | 113 |
if (new_child fitting_into this) { |
34561 | 114 |
var inserted = false |
34582 | 115 |
val new_children = |
116 |
children.map(c => |
|
117 |
if ((new_child fitting_into c) && !inserted) {inserted = true; c + new_child} |
|
118 |
else c) |
|
34561 | 119 |
if (!inserted) { |
34582 | 120 |
// new_child did not fit into children of this |
121 |
// -> insert new_child between this and its children |
|
34561 | 122 |
val fitting = children filter(_ fitting_into new_child) |
123 |
(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
|
124 |
} |
34557 | 125 |
else this set_children new_children |
34400
1b61a92f8675
MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents:
34393
diff
changeset
|
126 |
} else { |
34590 | 127 |
System.err.println("ignored nonfitting markup: " + new_child) |
128 |
this |
|
34400
1b61a92f8675
MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents:
34393
diff
changeset
|
129 |
} |
1b61a92f8675
MarkupNode instead of DefaultMutableTreeNode and RelativeAsset
immler@in.tum.de
parents:
34393
diff
changeset
|
130 |
} |
34393 | 131 |
|
34401
44241a37b74a
structure of markup-tree in scala, keep track of swing-nodes in background
immler@in.tum.de
parents:
34400
diff
changeset
|
132 |
// does this fit into node? |
34582 | 133 |
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
|
134 |
|
34582 | 135 |
override def toString = |
136 |
"([" + start + " - " + stop + "] " + id + "( " + content + "): " + info.toString |
|
34393 | 137 |
} |