author | wenzelm |
Wed, 21 Nov 2012 14:06:59 +0100 | |
changeset 50146 | 03f38212442a |
parent 49844 | 19ea3242ec37 |
child 50167 | 4f2b5b2a9ad5 |
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, |
|
49730 | 26 |
parent: JComponent, |
49702 | 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) |
49730 | 29 |
extends JWindow(JEdit_Lib.parent_window(parent) 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) { |
|
49842 | 58 |
setBackground(rendering.tooltip_color) |
49728 | 59 |
registerKeyboardAction(action_listener, "close_all", |
49705 | 60 |
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED) |
61 |
||
49702 | 62 |
override def getFocusTraversalKeysEnabled(): Boolean = false |
63 |
}) |
|
49705 | 64 |
window.getRootPane.setBorder(new LineBorder(Color.BLACK)) |
49702 | 65 |
|
49725 | 66 |
|
67 |
/* pretty text area */ |
|
68 |
||
49703
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
69 |
val pretty_text_area = new Pretty_Text_Area(view) |
49706 | 70 |
pretty_text_area.getPainter.setBackground(rendering.tooltip_color) |
50146 | 71 |
pretty_text_area.resize(Isabelle.font_family(), |
72 |
Isabelle.font_size("jedit_tooltip_font_scale").round) |
|
49703
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
73 |
pretty_text_area.update(rendering.snapshot, body) |
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
74 |
|
49728 | 75 |
pretty_text_area.registerKeyboardAction(action_listener, "close_all", |
76 |
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED) |
|
77 |
||
49706 | 78 |
window.add(pretty_text_area) |
49708 | 79 |
|
49725 | 80 |
|
81 |
/* controls */ |
|
82 |
||
49727 | 83 |
private val close = new Label { |
84 |
icon = Isabelle_Rendering.tooltip_close_icon |
|
85 |
tooltip = "Close tooltip window" |
|
86 |
listenTo(mouse.clicks) |
|
87 |
reactions += { case _: MouseClicked => window.dispose() } |
|
88 |
} |
|
89 |
||
49726 | 90 |
private val detach = new Label { |
91 |
icon = Isabelle_Rendering.tooltip_detach_icon |
|
92 |
tooltip = "Detach tooltip window" |
|
93 |
listenTo(mouse.clicks) |
|
94 |
reactions += { |
|
95 |
case _: MouseClicked => Info_Dockable(view, rendering.snapshot, body); window.dispose() |
|
96 |
} |
|
97 |
} |
|
98 |
||
49842 | 99 |
private val controls = new FlowPanel(FlowPanel.Alignment.Left)(close, detach) { |
100 |
background = rendering.tooltip_color |
|
101 |
} |
|
49725 | 102 |
window.add(controls.peer, BorderLayout.NORTH) |
103 |
||
104 |
||
105 |
/* window geometry */ |
|
106 |
||
49708 | 107 |
{ |
49707 | 108 |
val font_metrics = pretty_text_area.getPainter.getFontMetrics |
109 |
val margin = Isabelle.options.int("jedit_tooltip_margin") // FIXME via rendering?! |
|
110 |
val lines = // FIXME avoid redundant formatting |
|
111 |
XML.traverse_text(Pretty.formatted(body, margin, Pretty.font_metric(font_metrics)))(0)( |
|
112 |
(n: Int, s: String) => n + s.iterator.filter(_ == '\n').length) |
|
113 |
||
114 |
val screen = Toolkit.getDefaultToolkit.getScreenSize |
|
115 |
val w = (font_metrics.charWidth(Pretty.spc) * margin) min (screen.width / 2) |
|
49844 | 116 |
val h = (font_metrics.getHeight * (lines + 2) + 25) min (screen.height / 2) |
49707 | 117 |
window.setSize(w, h) |
118 |
} |
|
49706 | 119 |
|
49709 | 120 |
{ |
49730 | 121 |
val point = new Point(mouse_x, mouse_y) |
122 |
SwingUtilities.convertPointToScreen(point, parent) |
|
49709 | 123 |
|
124 |
val screen = Toolkit.getDefaultToolkit.getScreenSize |
|
125 |
val x = point.x min (screen.width - window.getWidth) |
|
126 |
val y = point.y min (screen.height - window.getHeight) |
|
127 |
window.setLocation(x, y) |
|
128 |
} |
|
129 |
||
49705 | 130 |
window.setVisible(true) |
49707 | 131 |
pretty_text_area.refresh() // FIXME avoid redundant formatting |
49702 | 132 |
} |
133 |