author | wenzelm |
Sat, 01 Mar 2014 19:39:27 +0100 | |
changeset 55825 | 694833e3e4a0 |
parent 55813 | 08a1c860bc12 |
child 55828 | 42ac3cfb89f6 |
permissions | -rw-r--r-- |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Tools/jEdit/src/completion_popup.scala |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
3 |
|
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
4 |
Completion popup. |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
5 |
*/ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
6 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
7 |
package isabelle.jedit |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
8 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
9 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
10 |
import isabelle._ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
11 |
|
53297 | 12 |
import java.awt.{Color, Font, Point, BorderLayout, Dimension} |
53784 | 13 |
import java.awt.event.{KeyEvent, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent} |
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
14 |
import javax.swing.{JPanel, JComponent, JLayeredPane, SwingUtilities} |
53297 | 15 |
import javax.swing.border.LineBorder |
53848
8d7029eb0c31
disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents:
53784
diff
changeset
|
16 |
import javax.swing.text.DefaultCaret |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
17 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
18 |
import scala.swing.{ListView, ScrollPane} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
19 |
import scala.swing.event.MouseClicked |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
20 |
|
53784 | 21 |
import org.gjt.sp.jedit.View |
55712 | 22 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea} |
53784 | 23 |
import org.gjt.sp.jedit.gui.{HistoryTextField, KeyEventWorkaround} |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
24 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
25 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
26 |
object Completion_Popup |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
27 |
{ |
53784 | 28 |
/** jEdit text area **/ |
53235
1b6a44859549
register single instance within root, in order to get rid of old popup as user continues typing;
wenzelm
parents:
53234
diff
changeset
|
29 |
|
53242 | 30 |
object Text_Area |
53233 | 31 |
{ |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
32 |
private val key = new Object |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
33 |
|
55712 | 34 |
def apply(text_area: TextArea): Option[Completion_Popup.Text_Area] = |
35 |
{ |
|
36 |
Swing_Thread.require() |
|
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
37 |
text_area.getClientProperty(key) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
38 |
case text_area_completion: Completion_Popup.Text_Area => Some(text_area_completion) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
39 |
case _ => None |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
40 |
} |
55712 | 41 |
} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
42 |
|
55712 | 43 |
def active_range(text_area: TextArea): Option[Text.Range] = |
44 |
apply(text_area) match { |
|
45 |
case Some(text_area_completion) => text_area_completion.active_range |
|
46 |
case None => None |
|
55711 | 47 |
} |
48 |
||
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
49 |
def exit(text_area: JEditTextArea) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
50 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
51 |
Swing_Thread.require() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
52 |
apply(text_area) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
53 |
case None => |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
54 |
case Some(text_area_completion) => |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
55 |
text_area_completion.deactivate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
56 |
text_area.putClientProperty(key, null) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
57 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
58 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
59 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
60 |
def init(text_area: JEditTextArea): Completion_Popup.Text_Area = |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
61 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
62 |
exit(text_area) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
63 |
val text_area_completion = new Text_Area(text_area) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
64 |
text_area.putClientProperty(key, text_area_completion) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
65 |
text_area_completion.activate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
66 |
text_area_completion |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
67 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
68 |
|
55712 | 69 |
def dismissed(text_area: TextArea): Boolean = |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
70 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
71 |
Swing_Thread.require() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
72 |
apply(text_area) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
73 |
case Some(text_area_completion) => text_area_completion.dismissed() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
74 |
case None => false |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
75 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
76 |
} |
53272 | 77 |
} |
78 |
||
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
79 |
class Text_Area private(text_area: JEditTextArea) |
53272 | 80 |
{ |
55711 | 81 |
// owned by Swing thread |
53272 | 82 |
private var completion_popup: Option[Completion_Popup] = None |
83 |
||
55711 | 84 |
def active_range: Option[Text.Range] = |
85 |
completion_popup match { |
|
86 |
case Some(completion) => |
|
87 |
completion.active_range match { |
|
88 |
case Some((range, _)) if completion.isDisplayable => Some(range) |
|
89 |
case _ => None |
|
90 |
} |
|
91 |
case None => None |
|
92 |
} |
|
93 |
||
53272 | 94 |
|
55767
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
95 |
/* caret */ |
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
96 |
|
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
97 |
def before_caret_range(rendering: Rendering): Text.Range = |
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
98 |
{ |
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
99 |
val snapshot = rendering.snapshot |
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
100 |
val former_caret = snapshot.revert(text_area.getCaretPosition) |
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
101 |
snapshot.convert(Text.Range(former_caret - 1, former_caret)) |
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
102 |
} |
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
103 |
|
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
104 |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
105 |
/* rendering */ |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
106 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
107 |
def rendering(rendering: Rendering, line_range: Text.Range): Option[Text.Info[Color]] = |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
108 |
{ |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
109 |
active_range match { |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
110 |
case Some(range) => range.try_restrict(line_range) |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
111 |
case None => |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
112 |
val buffer = text_area.getBuffer |
55767
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
113 |
if (line_range.contains(text_area.getCaretPosition)) { |
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
114 |
before_caret_range(rendering).try_restrict(line_range) match { |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
115 |
case Some(range) if !range.is_singularity => |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
116 |
rendering.completion_names(range) match { |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
117 |
case Some(names) => Some(names.range) |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
118 |
case None => |
55752 | 119 |
syntax_completion(false, Some(rendering)) match { |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
120 |
case Some(result) => Some(result.range) |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
121 |
case None => None |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
122 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
123 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
124 |
case _ => None |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
125 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
126 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
127 |
else None |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
128 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
129 |
}.map(range => Text.Info(range, rendering.completion_color)) |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
130 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
131 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
132 |
/* syntax completion */ |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
133 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
134 |
def syntax_completion( |
55752 | 135 |
explicit: Boolean, opt_rendering: Option[Rendering] = None): Option[Completion.Result] = |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
136 |
{ |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
137 |
val buffer = text_area.getBuffer |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
138 |
val history = PIDE.completion_history.value |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
139 |
val decode = Isabelle_Encoding.is_active(buffer) |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
140 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
141 |
Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match { |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
142 |
case Some(syntax) => |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
143 |
val caret = text_area.getCaretPosition |
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
144 |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
145 |
val line = buffer.getLineOfOffset(caret) |
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
146 |
val line_start = buffer.getLineStartOffset(line) |
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
147 |
val line_length = (buffer.getLineEndOffset(line) min buffer.getLength) - line_start |
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
148 |
val line_text = buffer.getSegment(line_start, line_length) |
55748 | 149 |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
150 |
val context = |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
151 |
(opt_rendering orElse PIDE.document_view(text_area).map(_.get_rendering()) match { |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
152 |
case Some(rendering) => |
55767
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
153 |
rendering.language_context(before_caret_range(rendering)) |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
154 |
case None => None |
55749 | 155 |
}) getOrElse syntax.language_context |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
156 |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
157 |
syntax.completion.complete( |
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
158 |
history, decode, explicit, line_start, line_text, caret - line_start, true, context) |
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
159 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
160 |
case None => None |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
161 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
162 |
} |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
163 |
|
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
164 |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
165 |
/* completion action */ |
53273 | 166 |
|
53275 | 167 |
private def insert(item: Completion.Item) |
53242 | 168 |
{ |
169 |
Swing_Thread.require() |
|
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
170 |
|
53242 | 171 |
val buffer = text_area.getBuffer |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
172 |
val range = item.range |
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
173 |
if (buffer.isEditable && range.length > 0) { |
53242 | 174 |
JEdit_Lib.buffer_edit(buffer) { |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
175 |
JEdit_Lib.try_get_text(buffer, range) match { |
53242 | 176 |
case Some(text) if text == item.original => |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
177 |
buffer.remove(range.start, range.length) |
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
178 |
buffer.insert(range.start, item.replacement) |
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
179 |
text_area.moveCaretPosition(range.start + item.replacement.length + item.move) |
53242 | 180 |
case _ => |
181 |
} |
|
53233 | 182 |
} |
183 |
} |
|
184 |
} |
|
53242 | 185 |
|
53322 | 186 |
def action(immediate: Boolean = false, explicit: Boolean = false) |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
187 |
{ |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
188 |
val view = text_area.getView |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
189 |
val layered = view.getLayeredPane |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
190 |
val buffer = text_area.getBuffer |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
191 |
val painter = text_area.getPainter |
53273 | 192 |
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
193 |
val history = PIDE.completion_history.value |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
194 |
val decode = Isabelle_Encoding.is_active(buffer) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
195 |
|
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
196 |
def open_popup(result: Completion.Result) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
197 |
{ |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
198 |
val font = |
55825 | 199 |
painter.getFont.deriveFont( |
200 |
Font_Info.main_size(PIDE.options.real("jedit_popup_font_scale"))) |
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
201 |
|
55711 | 202 |
val range = result.range |
203 |
def invalidate(): Unit = JEdit_Lib.invalidate_range(text_area, range) |
|
204 |
||
205 |
val loc1 = text_area.offsetToXY(range.start) |
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
206 |
if (loc1 != null) { |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
207 |
val loc2 = |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
208 |
SwingUtilities.convertPoint(painter, |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
209 |
loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
210 |
|
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
211 |
val completion = |
55711 | 212 |
new Completion_Popup(Some((range, invalidate _)), layered, loc2, font, result.items) { |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
213 |
override def complete(item: Completion.Item) { |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
214 |
PIDE.completion_history.update(item) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
215 |
insert(item) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
216 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
217 |
override def propagate(evt: KeyEvent) { |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
218 |
JEdit_Lib.propagate_key(view, evt) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
219 |
input(evt) |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
220 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
221 |
override def refocus() { text_area.requestFocus } |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
222 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
223 |
completion_popup = Some(completion) |
55711 | 224 |
invalidate() |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
225 |
completion.show_popup() |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
226 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
227 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
228 |
|
55752 | 229 |
def semantic_completion(): Option[Completion.Result] = |
230 |
if (explicit) { |
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
231 |
PIDE.document_view(text_area) match { |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
232 |
case Some(doc_view) => |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
233 |
val rendering = doc_view.get_rendering() |
55767
96ddf9bf12ac
more precise before_caret_range (looking both in space and time);
wenzelm
parents:
55752
diff
changeset
|
234 |
rendering.completion_names(before_caret_range(rendering)) match { |
55752 | 235 |
case None => None |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
236 |
case Some(names) => |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
237 |
JEdit_Lib.try_get_text(buffer, names.range) match { |
55752 | 238 |
case Some(original) => names.complete(history, decode, original) |
239 |
case None => None |
|
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
240 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
241 |
} |
55752 | 242 |
case None => None |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
243 |
} |
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
244 |
} |
55752 | 245 |
else None |
55693
93ba0085e888
try explicit semantic completion before syntax completion;
wenzelm
parents:
55692
diff
changeset
|
246 |
|
55752 | 247 |
if (buffer.isEditable) { |
248 |
semantic_completion() orElse syntax_completion(explicit) match { |
|
55747
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
249 |
case Some(result) => |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
250 |
result.items match { |
bef19c929ba5
more completion rendering: active, semantic, syntactic;
wenzelm
parents:
55712
diff
changeset
|
251 |
case List(item) if result.unique && item.immediate && immediate => |
55752 | 252 |
insert(item) |
253 |
case _ :: _ => |
|
254 |
open_popup(result) |
|
255 |
case _ => |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
256 |
} |
55752 | 257 |
case None => |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
258 |
} |
55752 | 259 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
260 |
} |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
261 |
|
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
262 |
|
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
263 |
/* input key events */ |
53273 | 264 |
|
53272 | 265 |
def input(evt: KeyEvent) |
53242 | 266 |
{ |
267 |
Swing_Thread.require() |
|
53233 | 268 |
|
53273 | 269 |
if (PIDE.options.bool("jedit_completion")) { |
270 |
if (!evt.isConsumed) { |
|
271 |
dismissed() |
|
53397
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
272 |
if (evt.getKeyChar != '\b') { |
53784 | 273 |
val special = JEdit_Lib.special_key(evt) |
53397
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
274 |
if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) { |
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
275 |
input_delay.revoke() |
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
276 |
action(immediate = PIDE.options.bool("jedit_completion_immediate")) |
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
277 |
} |
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
278 |
else input_delay.invoke() |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
279 |
} |
53242 | 280 |
} |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
281 |
} |
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
282 |
} |
53273 | 283 |
|
284 |
private val input_delay = |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
285 |
Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) { |
53322 | 286 |
action() |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
287 |
} |
53273 | 288 |
|
289 |
||
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
290 |
/* dismiss popup */ |
53273 | 291 |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
292 |
def dismissed(): Boolean = |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
293 |
{ |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
294 |
Swing_Thread.require() |
53273 | 295 |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
296 |
completion_popup match { |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
297 |
case Some(completion) => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
298 |
completion.hide_popup() |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
299 |
completion_popup = None |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
300 |
true |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
301 |
case None => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
302 |
false |
53273 | 303 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
304 |
} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
305 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
306 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
307 |
/* activation */ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
308 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
309 |
private val outer_key_listener = |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
310 |
JEdit_Lib.key_listener(key_typed = input _) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
311 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
312 |
private def activate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
313 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
314 |
text_area.addKeyListener(outer_key_listener) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
315 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
316 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
317 |
private def deactivate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
318 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
319 |
dismissed() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
320 |
text_area.removeKeyListener(outer_key_listener) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
321 |
} |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
322 |
} |
53784 | 323 |
|
324 |
||
325 |
||
326 |
/** history text field **/ |
|
327 |
||
328 |
class History_Text_Field( |
|
329 |
name: String = null, |
|
330 |
instant_popups: Boolean = false, |
|
331 |
enter_adds_to_history: Boolean = true, |
|
332 |
syntax: Outer_Syntax = Outer_Syntax.init) extends |
|
333 |
HistoryTextField(name, instant_popups, enter_adds_to_history) |
|
334 |
{ |
|
335 |
text_field => |
|
336 |
||
53848
8d7029eb0c31
disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents:
53784
diff
changeset
|
337 |
// see https://forums.oracle.com/thread/1361677 |
8d7029eb0c31
disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents:
53784
diff
changeset
|
338 |
if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret) |
53784 | 339 |
|
55711 | 340 |
// owned by Swing thread |
53784 | 341 |
private var completion_popup: Option[Completion_Popup] = None |
342 |
||
343 |
||
344 |
/* dismiss */ |
|
345 |
||
346 |
private def dismissed(): Boolean = |
|
347 |
{ |
|
348 |
completion_popup match { |
|
349 |
case Some(completion) => |
|
350 |
completion.hide_popup() |
|
351 |
completion_popup = None |
|
352 |
true |
|
353 |
case None => |
|
354 |
false |
|
355 |
} |
|
356 |
} |
|
357 |
||
358 |
||
359 |
/* insert */ |
|
360 |
||
361 |
private def insert(item: Completion.Item) |
|
362 |
{ |
|
363 |
Swing_Thread.require() |
|
364 |
||
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
365 |
val range = item.range |
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
366 |
if (text_field.isEditable && range.length > 0) { |
53784 | 367 |
val content = text_field.getText |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
368 |
JEdit_Lib.try_get_text(content, range) match { |
53784 | 369 |
case Some(text) if text == item.original => |
370 |
text_field.setText( |
|
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
371 |
content.substring(0, range.start) + |
53784 | 372 |
item.replacement + |
55692
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
373 |
content.substring(range.stop)) |
19e8b00684f7
more explicit Completion.Item.range, independently of caret;
wenzelm
parents:
55690
diff
changeset
|
374 |
text_field.getCaret.setDot(range.start + item.replacement.length + item.move) |
53784 | 375 |
case _ => |
376 |
} |
|
377 |
} |
|
378 |
} |
|
379 |
||
380 |
||
381 |
/* completion action */ |
|
382 |
||
383 |
def action() |
|
384 |
{ |
|
385 |
GUI.layered_pane(text_field) match { |
|
386 |
case Some(layered) if text_field.isEditable => |
|
387 |
val history = PIDE.completion_history.value |
|
388 |
||
389 |
val caret = text_field.getCaret.getDot |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
390 |
val text = text_field.getText |
55748 | 391 |
|
55749 | 392 |
val context = syntax.language_context |
55748 | 393 |
|
55813
08a1c860bc12
allow completion within a word (or symbol), like semantic completion;
wenzelm
parents:
55767
diff
changeset
|
394 |
syntax.completion.complete(history, true, false, 0, text, caret, false, context) match { |
53784 | 395 |
case Some(result) => |
396 |
val fm = text_field.getFontMetrics(text_field.getFont) |
|
397 |
val loc = |
|
398 |
SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered) |
|
399 |
||
55711 | 400 |
val completion = |
401 |
new Completion_Popup(None, layered, loc, text_field.getFont, result.items) |
|
53784 | 402 |
{ |
403 |
override def complete(item: Completion.Item) { |
|
404 |
PIDE.completion_history.update(item) |
|
405 |
insert(item) |
|
406 |
} |
|
407 |
override def propagate(evt: KeyEvent) { |
|
408 |
if (!evt.isConsumed) text_field.processKeyEvent(evt) |
|
409 |
} |
|
410 |
override def refocus() { text_field.requestFocus } |
|
411 |
} |
|
412 |
completion_popup = Some(completion) |
|
413 |
completion.show_popup() |
|
414 |
||
415 |
case None => |
|
416 |
} |
|
417 |
case _ => |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
||
422 |
/* process key event */ |
|
423 |
||
424 |
private def process(evt: KeyEvent) |
|
425 |
{ |
|
426 |
if (PIDE.options.bool("jedit_completion")) { |
|
427 |
dismissed() |
|
428 |
if (evt.getKeyChar != '\b') { |
|
429 |
val special = JEdit_Lib.special_key(evt) |
|
430 |
if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) { |
|
431 |
process_delay.revoke() |
|
432 |
action() |
|
433 |
} |
|
434 |
else process_delay.invoke() |
|
435 |
} |
|
436 |
} |
|
437 |
} |
|
438 |
||
439 |
private val process_delay = |
|
440 |
Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) { |
|
441 |
action() |
|
442 |
} |
|
443 |
||
444 |
override def processKeyEvent(evt0: KeyEvent) |
|
445 |
{ |
|
446 |
val evt = KeyEventWorkaround.processKeyEvent(evt0) |
|
447 |
if (evt != null) { |
|
448 |
evt.getID match { |
|
449 |
case KeyEvent.KEY_PRESSED => |
|
450 |
val key_code = evt.getKeyCode |
|
451 |
if (key_code == KeyEvent.VK_ESCAPE) { |
|
452 |
if (dismissed()) evt.consume |
|
453 |
} |
|
454 |
case KeyEvent.KEY_TYPED => |
|
455 |
super.processKeyEvent(evt) |
|
456 |
process(evt) |
|
457 |
evt.consume |
|
458 |
case _ => |
|
459 |
} |
|
460 |
if (!evt.isConsumed) super.processKeyEvent(evt) |
|
461 |
} |
|
462 |
} |
|
463 |
} |
|
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
464 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
465 |
|
53233 | 466 |
|
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
467 |
class Completion_Popup private( |
55711 | 468 |
val active_range: Option[(Text.Range, () => Unit)], |
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
469 |
layered: JLayeredPane, |
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
470 |
location: Point, |
53272 | 471 |
font: Font, |
53275 | 472 |
items: List[Completion.Item]) extends JPanel(new BorderLayout) |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
473 |
{ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
474 |
completion => |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
475 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
476 |
Swing_Thread.require() |
53405 | 477 |
|
53232 | 478 |
require(!items.isEmpty) |
53405 | 479 |
val multi = items.length > 1 |
53232 | 480 |
|
481 |
||
482 |
/* actions */ |
|
483 |
||
53275 | 484 |
def complete(item: Completion.Item) { } |
53232 | 485 |
def propagate(evt: KeyEvent) { } |
53234
ea4abbbe1702
more careful refocus operation: do not reset focus if it was already lost (relevant when activating a different GUI component, for example);
wenzelm
parents:
53233
diff
changeset
|
486 |
def refocus() { } |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
487 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
488 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
489 |
/* list view */ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
490 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
491 |
private val list_view = new ListView(items) |
53272 | 492 |
list_view.font = font |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
493 |
list_view.selection.intervalMode = ListView.IntervalMode.Single |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
494 |
list_view.peer.setFocusTraversalKeysEnabled(false) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
495 |
list_view.peer.setVisibleRowCount(items.length min 8) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
496 |
list_view.peer.setSelectedIndex(0) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
497 |
|
53398
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
498 |
for (cond <- |
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
499 |
List(JComponent.WHEN_FOCUSED, |
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
500 |
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, |
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
501 |
JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null) |
f8b150e8778b
remove Swing input map, which might bind keys in unexpected ways (e.g. LEFT/RIGHT in singleton list);
wenzelm
parents:
53397
diff
changeset
|
502 |
|
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
503 |
private def complete_selected(): Boolean = |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
504 |
{ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
505 |
list_view.selection.items.toList match { |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
506 |
case item :: _ => complete(item); true |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
507 |
case _ => false |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
508 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
509 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
510 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
511 |
private def move_items(n: Int) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
512 |
{ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
513 |
val i = list_view.peer.getSelectedIndex |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
514 |
val j = ((i + n) min (items.length - 1)) max 0 |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
515 |
if (i != j) { |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
516 |
list_view.peer.setSelectedIndex(j) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
517 |
list_view.peer.ensureIndexIsVisible(j) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
518 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
519 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
520 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
521 |
private def move_pages(n: Int) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
522 |
{ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
523 |
val page_size = list_view.peer.getVisibleRowCount - 1 |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
524 |
move_items(page_size * n) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
525 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
526 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
527 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
528 |
/* event handling */ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
529 |
|
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
530 |
private val inner_key_listener = |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
531 |
JEdit_Lib.key_listener( |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
532 |
key_pressed = (e: KeyEvent) => |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
533 |
{ |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
534 |
if (!e.isConsumed) { |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
535 |
e.getKeyCode match { |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
536 |
case KeyEvent.VK_TAB => |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
537 |
if (complete_selected()) e.consume |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
538 |
hide_popup() |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
539 |
case KeyEvent.VK_ESCAPE => |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
540 |
hide_popup() |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
541 |
e.consume |
53405 | 542 |
case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi => |
53407 | 543 |
move_items(-1) |
544 |
e.consume |
|
53405 | 545 |
case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi => |
53407 | 546 |
move_items(1) |
547 |
e.consume |
|
53405 | 548 |
case KeyEvent.VK_PAGE_UP if multi => |
53407 | 549 |
move_pages(-1) |
550 |
e.consume |
|
53405 | 551 |
case KeyEvent.VK_PAGE_DOWN if multi => |
53407 | 552 |
move_pages(1) |
553 |
e.consume |
|
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
554 |
case _ => |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
555 |
if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown) |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
556 |
hide_popup() |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
557 |
} |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
558 |
} |
53232 | 559 |
propagate(e) |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
560 |
}, |
53232 | 561 |
key_typed = propagate _ |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
562 |
) |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
563 |
|
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
564 |
list_view.peer.addKeyListener(inner_key_listener) |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
565 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
566 |
list_view.peer.addMouseListener(new MouseAdapter { |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
567 |
override def mouseClicked(e: MouseEvent) { |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
568 |
if (complete_selected()) e.consume |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
569 |
hide_popup() |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
570 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
571 |
}) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
572 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
573 |
list_view.peer.addFocusListener(new FocusAdapter { |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
574 |
override def focusLost(e: FocusEvent) { hide_popup() } |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
575 |
}) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
576 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
577 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
578 |
/* main content */ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
579 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
580 |
override def getFocusTraversalKeysEnabled = false |
53297 | 581 |
completion.setBorder(new LineBorder(Color.BLACK)) |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
582 |
completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent]) |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
583 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
584 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
585 |
/* popup */ |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
586 |
|
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
587 |
private val popup = |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
588 |
{ |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
589 |
val screen = JEdit_Lib.screen_location(layered, location) |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
590 |
val size = |
53230 | 591 |
{ |
592 |
val geometry = JEdit_Lib.window_geometry(completion, completion) |
|
593 |
val bounds = Rendering.popup_bounds |
|
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
594 |
val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
595 |
val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight |
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
596 |
new Dimension(w, h) |
53230 | 597 |
} |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
598 |
new Popup(layered, completion, screen.relative(layered, size), size) |
53246
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
599 |
} |
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
600 |
|
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
601 |
private def show_popup() |
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
602 |
{ |
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
603 |
popup.show |
8d34caf5bf82
more elementary Popup via JLayeredPane -- avoid javax.swing.PopupFactory with its many problems and dangers of accidental HeavyWeightPopup (especially on Mac OS X);
wenzelm
parents:
53244
diff
changeset
|
604 |
list_view.requestFocus |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
605 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
606 |
|
54377
750561986828
added jedit_completion_dismiss_delay for hide_popup, which helps to avoid loosing key events on old popup (no change of default behavior);
wenzelm
parents:
54376
diff
changeset
|
607 |
private val hide_popup_delay = |
750561986828
added jedit_completion_dismiss_delay for hide_popup, which helps to avoid loosing key events on old popup (no change of default behavior);
wenzelm
parents:
54376
diff
changeset
|
608 |
Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_dismiss_delay")) { |
55711 | 609 |
active_range match { case Some((_, invalidate)) => invalidate() case _ => } |
54377
750561986828
added jedit_completion_dismiss_delay for hide_popup, which helps to avoid loosing key events on old popup (no change of default behavior);
wenzelm
parents:
54376
diff
changeset
|
610 |
popup.hide |
750561986828
added jedit_completion_dismiss_delay for hide_popup, which helps to avoid loosing key events on old popup (no change of default behavior);
wenzelm
parents:
54376
diff
changeset
|
611 |
} |
750561986828
added jedit_completion_dismiss_delay for hide_popup, which helps to avoid loosing key events on old popup (no change of default behavior);
wenzelm
parents:
54376
diff
changeset
|
612 |
|
53232 | 613 |
private def hide_popup() |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
614 |
{ |
54376
3eb84b6b0353
transfer focus before closing old component -- avoid intermediate focus switch to root component, which is actually visible e.g. on Windows;
wenzelm
parents:
53987
diff
changeset
|
615 |
if (list_view.peer.isFocusOwner) refocus() |
54377
750561986828
added jedit_completion_dismiss_delay for hide_popup, which helps to avoid loosing key events on old popup (no change of default behavior);
wenzelm
parents:
54376
diff
changeset
|
616 |
|
55711 | 617 |
if (PIDE.options.seconds("jedit_completion_dismiss_delay").is_zero) { |
618 |
active_range match { case Some((_, invalidate)) => invalidate() case _ => } |
|
54377
750561986828
added jedit_completion_dismiss_delay for hide_popup, which helps to avoid loosing key events on old popup (no change of default behavior);
wenzelm
parents:
54376
diff
changeset
|
619 |
popup.hide |
55711 | 620 |
} |
54377
750561986828
added jedit_completion_dismiss_delay for hide_popup, which helps to avoid loosing key events on old popup (no change of default behavior);
wenzelm
parents:
54376
diff
changeset
|
621 |
else hide_popup_delay.invoke() |
53023
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
622 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
623 |
} |
f127e949389f
Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff
changeset
|
624 |