author | wenzelm |
Fri, 14 Dec 2012 21:26:01 +0100 | |
changeset 50538 | 48cb76b785da |
parent 50501 | 6f41f1646617 |
child 50554 | 0493efcc97e9 |
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 |
||
50167 | 12 |
import java.awt.{Toolkit, Color, Point, BorderLayout, Window, Dimension} |
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, |
50199
6d04e2422769
quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents:
50169
diff
changeset
|
27 |
rendering: Rendering, |
50501
6f41f1646617
more careful handling of Dialog_Result, with active area and color feedback;
wenzelm
parents:
50206
diff
changeset
|
28 |
mouse_x: Int, mouse_y: Int, |
6f41f1646617
more careful handling of Dialog_Result, with active area and color feedback;
wenzelm
parents:
50206
diff
changeset
|
29 |
results: Command.Results, |
6f41f1646617
more careful handling of Dialog_Result, with active area and color feedback;
wenzelm
parents:
50206
diff
changeset
|
30 |
body: XML.Body) |
49730 | 31 |
extends JWindow(JEdit_Lib.parent_window(parent) getOrElse view) |
49702 | 32 |
{ |
49705 | 33 |
window => |
34 |
||
49725 | 35 |
Swing_Thread.require() |
36 |
||
37 |
||
49705 | 38 |
window.addWindowFocusListener(new WindowAdapter { |
49712 | 39 |
override def windowLostFocus(e: WindowEvent) { |
40 |
if (!Window.getWindows.exists(w => |
|
41 |
w.isDisplayable && JEdit_Lib.ancestors(w).exists(_ == window))) |
|
42 |
window.dispose() |
|
43 |
} |
|
49702 | 44 |
}) |
49705 | 45 |
|
49728 | 46 |
private val action_listener = new ActionListener { |
47 |
def actionPerformed(e: ActionEvent) { |
|
48 |
e.getActionCommand match { |
|
49 |
case "close_all" => |
|
50 |
Window.getWindows foreach { |
|
51 |
case c: Pretty_Tooltip => c.dispose |
|
52 |
case _ => |
|
53 |
} |
|
54 |
case _ => |
|
49705 | 55 |
} |
56 |
} |
|
49728 | 57 |
} |
58 |
||
59 |
window.setContentPane(new JPanel(new BorderLayout) { |
|
49842 | 60 |
setBackground(rendering.tooltip_color) |
49728 | 61 |
registerKeyboardAction(action_listener, "close_all", |
49705 | 62 |
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED) |
63 |
||
49702 | 64 |
override def getFocusTraversalKeysEnabled(): Boolean = false |
65 |
}) |
|
49705 | 66 |
window.getRootPane.setBorder(new LineBorder(Color.BLACK)) |
49702 | 67 |
|
49725 | 68 |
|
69 |
/* pretty text area */ |
|
70 |
||
50538
48cb76b785da
init gutter according to view properties, which improves symmetry of windows and allows use of folds etc;
wenzelm
parents:
50501
diff
changeset
|
71 |
val pretty_text_area = new Pretty_Text_Area(view, Some(rendering.tooltip_color)) |
50206 | 72 |
pretty_text_area.resize(Rendering.font_family(), |
73 |
Rendering.font_size("jedit_tooltip_font_scale").round) |
|
50501
6f41f1646617
more careful handling of Dialog_Result, with active area and color feedback;
wenzelm
parents:
50206
diff
changeset
|
74 |
pretty_text_area.update(rendering.snapshot, results, body) |
49703
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
75 |
|
49728 | 76 |
pretty_text_area.registerKeyboardAction(action_listener, "close_all", |
50167 | 77 |
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED) |
49728 | 78 |
|
49706 | 79 |
window.add(pretty_text_area) |
49708 | 80 |
|
49725 | 81 |
|
82 |
/* controls */ |
|
83 |
||
49727 | 84 |
private val close = new Label { |
50199
6d04e2422769
quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents:
50169
diff
changeset
|
85 |
icon = Rendering.tooltip_close_icon |
49727 | 86 |
tooltip = "Close tooltip window" |
87 |
listenTo(mouse.clicks) |
|
88 |
reactions += { case _: MouseClicked => window.dispose() } |
|
89 |
} |
|
90 |
||
49726 | 91 |
private val detach = new Label { |
50199
6d04e2422769
quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents:
50169
diff
changeset
|
92 |
icon = Rendering.tooltip_detach_icon |
49726 | 93 |
tooltip = "Detach tooltip window" |
94 |
listenTo(mouse.clicks) |
|
95 |
reactions += { |
|
50501
6f41f1646617
more careful handling of Dialog_Result, with active area and color feedback;
wenzelm
parents:
50206
diff
changeset
|
96 |
case _: MouseClicked => |
6f41f1646617
more careful handling of Dialog_Result, with active area and color feedback;
wenzelm
parents:
50206
diff
changeset
|
97 |
Info_Dockable(view, rendering.snapshot, results, body) |
6f41f1646617
more careful handling of Dialog_Result, with active area and color feedback;
wenzelm
parents:
50206
diff
changeset
|
98 |
window.dispose() |
49726 | 99 |
} |
100 |
} |
|
101 |
||
49842 | 102 |
private val controls = new FlowPanel(FlowPanel.Alignment.Left)(close, detach) { |
103 |
background = rendering.tooltip_color |
|
104 |
} |
|
49725 | 105 |
window.add(controls.peer, BorderLayout.NORTH) |
106 |
||
107 |
||
108 |
/* window geometry */ |
|
109 |
||
49708 | 110 |
{ |
49707 | 111 |
val font_metrics = pretty_text_area.getPainter.getFontMetrics |
50205 | 112 |
val margin = PIDE.options.int("jedit_tooltip_margin") // FIXME via rendering?! |
50167 | 113 |
val lines = |
49707 | 114 |
XML.traverse_text(Pretty.formatted(body, margin, Pretty.font_metric(font_metrics)))(0)( |
115 |
(n: Int, s: String) => n + s.iterator.filter(_ == '\n').length) |
|
116 |
||
117 |
val screen = Toolkit.getDefaultToolkit.getScreenSize |
|
50167 | 118 |
val w = (font_metrics.charWidth(Pretty.spc) * (margin + 2)) min (screen.width / 2) |
119 |
val h = (font_metrics.getHeight * (lines + 2)) min (screen.height / 2) |
|
120 |
pretty_text_area.setPreferredSize(new Dimension(w, h)) |
|
50169 | 121 |
window.pack |
49707 | 122 |
} |
49706 | 123 |
|
49709 | 124 |
{ |
49730 | 125 |
val point = new Point(mouse_x, mouse_y) |
126 |
SwingUtilities.convertPointToScreen(point, parent) |
|
49709 | 127 |
|
128 |
val screen = Toolkit.getDefaultToolkit.getScreenSize |
|
129 |
val x = point.x min (screen.width - window.getWidth) |
|
130 |
val y = point.y min (screen.height - window.getHeight) |
|
131 |
window.setLocation(x, y) |
|
132 |
} |
|
133 |
||
49705 | 134 |
window.setVisible(true) |
50167 | 135 |
pretty_text_area.refresh() |
49702 | 136 |
} |
137 |