author | wenzelm |
Sun, 07 Oct 2012 16:26:31 +0200 | |
changeset 49728 | 74f36dab2b62 |
parent 49727 | 2fe56b600698 |
child 49730 | e0d98ff3c0db |
permissions | -rw-r--r-- |
49702 | 1 |
/* Title: Tools/jEdit/src/pretty_tooltip.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Enhanced tooltip window based on Pretty_Text_Area. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
49712 | 12 |
import java.awt.{Toolkit, Color, Point, BorderLayout, Window} |
49705 | 13 |
import java.awt.event.{ActionListener, ActionEvent, KeyEvent, WindowEvent, WindowAdapter} |
14 |
import javax.swing.{SwingUtilities, JWindow, JPanel, JComponent, KeyStroke} |
|
49702 | 15 |
import javax.swing.border.LineBorder |
16 |
||
49725 | 17 |
import scala.swing.{FlowPanel, Label} |
18 |
import scala.swing.event.MouseClicked |
|
19 |
||
49702 | 20 |
import org.gjt.sp.jedit.View |
21 |
import org.gjt.sp.jedit.textarea.TextArea |
|
22 |
||
23 |
||
24 |
class Pretty_Tooltip( |
|
25 |
view: View, |
|
26 |
text_area: TextArea, |
|
27 |
rendering: Isabelle_Rendering, |
|
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49709
diff
changeset
|
28 |
mouse_x: Int, mouse_y: Int, body: XML.Body) |
49712 | 29 |
extends JWindow(JEdit_Lib.parent_window(text_area) getOrElse view) |
49702 | 30 |
{ |
49705 | 31 |
window => |
32 |
||
49725 | 33 |
Swing_Thread.require() |
34 |
||
35 |
||
49705 | 36 |
window.addWindowFocusListener(new WindowAdapter { |
49712 | 37 |
override def windowLostFocus(e: WindowEvent) { |
38 |
if (!Window.getWindows.exists(w => |
|
39 |
w.isDisplayable && JEdit_Lib.ancestors(w).exists(_ == window))) |
|
40 |
window.dispose() |
|
41 |
} |
|
49702 | 42 |
}) |
49705 | 43 |
|
49728 | 44 |
private val action_listener = new ActionListener { |
45 |
def actionPerformed(e: ActionEvent) { |
|
46 |
e.getActionCommand match { |
|
47 |
case "close_all" => |
|
48 |
Window.getWindows foreach { |
|
49 |
case c: Pretty_Tooltip => c.dispose |
|
50 |
case _ => |
|
51 |
} |
|
52 |
case _ => |
|
49705 | 53 |
} |
54 |
} |
|
49728 | 55 |
} |
56 |
||
57 |
window.setContentPane(new JPanel(new BorderLayout) { |
|
58 |
registerKeyboardAction(action_listener, "close_all", |
|
49705 | 59 |
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED) |
60 |
||
49702 | 61 |
override def getFocusTraversalKeysEnabled(): Boolean = false |
62 |
}) |
|
49705 | 63 |
window.getRootPane.setBorder(new LineBorder(Color.BLACK)) |
49702 | 64 |
|
49725 | 65 |
|
66 |
/* pretty text area */ |
|
67 |
||
49703
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
68 |
val pretty_text_area = new Pretty_Text_Area(view) |
49706 | 69 |
pretty_text_area.getPainter.setBackground(rendering.tooltip_color) |
49703
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
70 |
pretty_text_area.resize( |
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
71 |
Isabelle.font_family(), Isabelle.font_size("jedit_tooltip_font_scale").round) |
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
72 |
pretty_text_area.update(rendering.snapshot, body) |
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
73 |
|
49728 | 74 |
pretty_text_area.registerKeyboardAction(action_listener, "close_all", |
75 |
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED) |
|
76 |
||
49706 | 77 |
window.add(pretty_text_area) |
49708 | 78 |
|
49725 | 79 |
|
80 |
/* controls */ |
|
81 |
||
49727 | 82 |
private val close = new Label { |
83 |
icon = Isabelle_Rendering.tooltip_close_icon |
|
84 |
tooltip = "Close tooltip window" |
|
85 |
listenTo(mouse.clicks) |
|
86 |
reactions += { case _: MouseClicked => window.dispose() } |
|
87 |
} |
|
88 |
||
49726 | 89 |
private val detach = new Label { |
90 |
icon = Isabelle_Rendering.tooltip_detach_icon |
|
91 |
tooltip = "Detach tooltip window" |
|
92 |
listenTo(mouse.clicks) |
|
93 |
reactions += { |
|
94 |
case _: MouseClicked => Info_Dockable(view, rendering.snapshot, body); window.dispose() |
|
95 |
} |
|
96 |
} |
|
97 |
||
49727 | 98 |
private val controls = new FlowPanel(FlowPanel.Alignment.Left)(close, detach) |
49725 | 99 |
window.add(controls.peer, BorderLayout.NORTH) |
100 |
||
101 |
||
102 |
/* window geometry */ |
|
103 |
||
49708 | 104 |
{ |
49707 | 105 |
val font_metrics = pretty_text_area.getPainter.getFontMetrics |
106 |
val margin = Isabelle.options.int("jedit_tooltip_margin") // FIXME via rendering?! |
|
107 |
val lines = // FIXME avoid redundant formatting |
|
108 |
XML.traverse_text(Pretty.formatted(body, margin, Pretty.font_metric(font_metrics)))(0)( |
|
109 |
(n: Int, s: String) => n + s.iterator.filter(_ == '\n').length) |
|
110 |
||
111 |
val screen = Toolkit.getDefaultToolkit.getScreenSize |
|
112 |
val w = (font_metrics.charWidth(Pretty.spc) * margin) min (screen.width / 2) |
|
49725 | 113 |
val h = (font_metrics.getHeight * (lines + 3)) min (screen.height / 2) |
49707 | 114 |
window.setSize(w, h) |
115 |
} |
|
49706 | 116 |
|
49709 | 117 |
{ |
118 |
val container = text_area.getPainter |
|
119 |
val font_metrics = container.getFontMetrics |
|
120 |
val point = new Point(mouse_x, mouse_y + font_metrics.getHeight / 2) |
|
121 |
SwingUtilities.convertPointToScreen(point, container) |
|
122 |
||
123 |
val screen = Toolkit.getDefaultToolkit.getScreenSize |
|
124 |
val x = point.x min (screen.width - window.getWidth) |
|
125 |
val y = point.y min (screen.height - window.getHeight) |
|
126 |
window.setLocation(x, y) |
|
127 |
} |
|
128 |
||
49705 | 129 |
window.setVisible(true) |
49707 | 130 |
pretty_text_area.refresh() // FIXME avoid redundant formatting |
49702 | 131 |
} |
132 |