author | wenzelm |
Mon, 17 Sep 2012 20:34:19 +0200 | |
changeset 49411 | 1da54e9bda68 |
parent 49410 | 34acbcc33adf |
child 49697 | ad2bd4e5a029 |
permissions | -rw-r--r-- |
46572 | 1 |
/* Title: Tools/jEdit/src/text_overview.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Swing component for text status overview. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
12 |
import scala.annotation.tailrec |
|
13 |
||
49346 | 14 |
import java.awt.{Graphics, Graphics2D, BorderLayout, Dimension, Color} |
46572 | 15 |
import java.awt.event.{MouseAdapter, MouseEvent} |
16 |
import javax.swing.{JPanel, ToolTipManager} |
|
17 |
||
18 |
||
19 |
class Text_Overview(doc_view: Document_View) extends JPanel(new BorderLayout) |
|
20 |
{ |
|
21 |
private val text_area = doc_view.text_area |
|
22 |
private val buffer = doc_view.model.buffer |
|
23 |
||
24 |
private val WIDTH = 10 |
|
25 |
private val HEIGHT = 2 |
|
26 |
||
27 |
private def lines(): Int = buffer.getLineCount max text_area.getVisibleLines |
|
28 |
||
29 |
setPreferredSize(new Dimension(WIDTH, 0)) |
|
30 |
||
31 |
setRequestFocusEnabled(false) |
|
32 |
||
33 |
addMouseListener(new MouseAdapter { |
|
34 |
override def mousePressed(event: MouseEvent) { |
|
35 |
val line = (event.getY * lines()) / getHeight |
|
36 |
if (line >= 0 && line < text_area.getLineCount) |
|
37 |
text_area.setCaretPosition(text_area.getLineStartOffset(line)) |
|
38 |
} |
|
39 |
}) |
|
40 |
||
41 |
override def addNotify() { |
|
42 |
super.addNotify() |
|
43 |
ToolTipManager.sharedInstance.registerComponent(this) |
|
44 |
} |
|
45 |
||
46 |
override def removeNotify() { |
|
47 |
ToolTipManager.sharedInstance.unregisterComponent(this) |
|
48 |
super.removeNotify |
|
49 |
} |
|
50 |
||
49346 | 51 |
|
52 |
/* painting based on cached result */ |
|
53 |
||
54 |
private var cached_colors: List[(Color, Int, Int)] = Nil |
|
55 |
||
56 |
private var last_snapshot = Document.State.init.snapshot() |
|
49356 | 57 |
private var last_options = Isabelle.options.value |
49346 | 58 |
private var last_line_count = 0 |
59 |
private var last_char_count = 0 |
|
60 |
private var last_L = 0 |
|
61 |
private var last_H = 0 |
|
62 |
||
46572 | 63 |
override def paintComponent(gfx: Graphics) |
64 |
{ |
|
65 |
super.paintComponent(gfx) |
|
66 |
Swing_Thread.assert() |
|
67 |
||
49411 | 68 |
doc_view.rich_text_area.robust_body(()) { |
49406 | 69 |
JEdit_Lib.buffer_lock(buffer) { |
47027
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
wenzelm
parents:
46572
diff
changeset
|
70 |
val snapshot = doc_view.model.snapshot() |
46572 | 71 |
|
49346 | 72 |
if (snapshot.is_outdated) { |
49356 | 73 |
gfx.setColor(Isabelle.options.color_value("outdated_color")) |
49346 | 74 |
gfx.asInstanceOf[Graphics2D].fill(gfx.getClipBounds) |
75 |
} |
|
76 |
else { |
|
77 |
gfx.setColor(getBackground) |
|
78 |
gfx.asInstanceOf[Graphics2D].fill(gfx.getClipBounds) |
|
79 |
||
80 |
val line_count = buffer.getLineCount |
|
81 |
val char_count = buffer.getLength |
|
46572 | 82 |
|
49346 | 83 |
val L = lines() |
84 |
val H = getHeight() |
|
46572 | 85 |
|
49356 | 86 |
val options = Isabelle.options.value |
87 |
||
49346 | 88 |
if (!(line_count == last_line_count && char_count == last_char_count && |
49356 | 89 |
L == last_L && H == last_H && (snapshot eq_markup last_snapshot) && |
90 |
(options eq last_options))) |
|
49346 | 91 |
{ |
49356 | 92 |
val rendering = Isabelle_Rendering(snapshot, options) |
93 |
||
49346 | 94 |
@tailrec def loop(l: Int, h: Int, p: Int, q: Int, colors: List[(Color, Int, Int)]) |
95 |
: List[(Color, Int, Int)] = |
|
96 |
{ |
|
97 |
if (l < line_count && h < H) { |
|
98 |
val p1 = p + H |
|
99 |
val q1 = q + HEIGHT * L |
|
100 |
val (l1, h1) = |
|
101 |
if (p1 >= q1) (l + 1, h + (p1 - q) / L) |
|
102 |
else (l + (q1 - p) / H, h + HEIGHT) |
|
46572 | 103 |
|
49346 | 104 |
val start = buffer.getLineStartOffset(l) |
105 |
val end = |
|
106 |
if (l1 < line_count) buffer.getLineStartOffset(l1) |
|
107 |
else char_count |
|
108 |
val range = Text.Range(start, end) |
|
46572 | 109 |
|
49346 | 110 |
val colors1 = |
49356 | 111 |
(rendering.overview_color(range), colors) match { |
49346 | 112 |
case (Some(color), (old_color, old_h, old_h1) :: rest) |
113 |
if color == old_color && old_h1 == h => (color, old_h, h1) :: rest |
|
114 |
case (Some(color), _) => (color, h, h1) :: colors |
|
115 |
case (None, _) => colors |
|
116 |
} |
|
117 |
loop(l1, h1, p + (l1 - l) * H, q + (h1 - h) * L, colors1) |
|
118 |
} |
|
119 |
else colors.reverse |
|
120 |
} |
|
121 |
cached_colors = loop(0, 0, 0, 0, Nil) |
|
46572 | 122 |
|
49346 | 123 |
last_snapshot = snapshot |
49356 | 124 |
last_options = options |
49346 | 125 |
last_line_count = line_count |
126 |
last_char_count = char_count |
|
127 |
last_L = L |
|
128 |
last_H = H |
|
129 |
} |
|
130 |
||
131 |
for ((color, h, h1) <- cached_colors) { |
|
132 |
gfx.setColor(color) |
|
133 |
gfx.fillRect(0, h, getWidth, h1 - h) |
|
46572 | 134 |
} |
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
} |
|
140 |