author | wenzelm |
Thu, 20 Feb 2014 12:53:12 +0100 | |
changeset 55615 | bf4bbe72f740 |
parent 54377 | 750561986828 |
child 55616 | 25a7a998852a |
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 |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
22 |
import org.gjt.sp.jedit.textarea.JEditTextArea |
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 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
34 |
def apply(text_area: JEditTextArea): Option[Completion_Popup.Text_Area] = |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
35 |
text_area.getClientProperty(key) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
36 |
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
|
37 |
case _ => None |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
38 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
39 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
40 |
def exit(text_area: JEditTextArea) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
41 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
42 |
Swing_Thread.require() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
43 |
apply(text_area) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
44 |
case None => |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
45 |
case Some(text_area_completion) => |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
46 |
text_area_completion.deactivate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
47 |
text_area.putClientProperty(key, null) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
48 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
49 |
} |
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 |
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
|
52 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
53 |
exit(text_area) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
54 |
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
|
55 |
text_area.putClientProperty(key, text_area_completion) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
56 |
text_area_completion.activate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
57 |
text_area_completion |
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 dismissed(text_area: JEditTextArea): Boolean = |
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 |
Swing_Thread.require() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
63 |
apply(text_area) match { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
64 |
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
|
65 |
case None => false |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
66 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
67 |
} |
53272 | 68 |
} |
69 |
||
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
70 |
class Text_Area private(text_area: JEditTextArea) |
53272 | 71 |
{ |
72 |
private var completion_popup: Option[Completion_Popup] = None |
|
73 |
||
74 |
||
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
75 |
/* completion action */ |
53273 | 76 |
|
53275 | 77 |
private def insert(item: Completion.Item) |
53242 | 78 |
{ |
79 |
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
|
80 |
|
53242 | 81 |
val buffer = text_area.getBuffer |
82 |
val len = item.original.length |
|
83 |
if (buffer.isEditable && len > 0) { |
|
84 |
JEdit_Lib.buffer_edit(buffer) { |
|
85 |
val caret = text_area.getCaretPosition |
|
86 |
JEdit_Lib.try_get_text(buffer, Text.Range(caret - len, caret)) match { |
|
87 |
case Some(text) if text == item.original => |
|
88 |
buffer.remove(caret - len, len) |
|
89 |
buffer.insert(caret - len, item.replacement) |
|
90 |
case _ => |
|
91 |
} |
|
53233 | 92 |
} |
93 |
} |
|
94 |
} |
|
53242 | 95 |
|
53322 | 96 |
def action(immediate: Boolean = false, explicit: Boolean = false) |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
97 |
{ |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
98 |
val view = text_area.getView |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
99 |
val layered = view.getLayeredPane |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
100 |
val buffer = text_area.getBuffer |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
101 |
val painter = text_area.getPainter |
53273 | 102 |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
103 |
if (buffer.isEditable) { |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
104 |
Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match { |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
105 |
case Some(syntax) => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
106 |
val caret = text_area.getCaretPosition |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
107 |
val line = buffer.getLineOfOffset(caret) |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
108 |
val start = buffer.getLineStartOffset(line) |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
109 |
val text = buffer.getSegment(start, caret - start) |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
110 |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
111 |
val history = PIDE.completion_history.value |
53322 | 112 |
val decode = Isabelle_Encoding.is_active(buffer) |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
54377
diff
changeset
|
113 |
val context = |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
54377
diff
changeset
|
114 |
PIDE.document_view(text_area) match { |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
54377
diff
changeset
|
115 |
case None => Completion.Context.default |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
54377
diff
changeset
|
116 |
case Some(doc_view) => doc_view.get_rendering().completion_context(caret) |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
54377
diff
changeset
|
117 |
} |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
54377
diff
changeset
|
118 |
syntax.completion.complete(history, decode, explicit, text, context) match { |
53325
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
119 |
case Some(result) => |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
120 |
if (result.unique && result.items.head.immediate && immediate) |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
121 |
insert(result.items.head) |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
122 |
else { |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
123 |
val font = |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
124 |
painter.getFont.deriveFont(Rendering.font_size("jedit_popup_font_scale")) |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
125 |
|
53325
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
126 |
val loc1 = text_area.offsetToXY(caret - result.original.length) |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
127 |
if (loc1 != null) { |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
128 |
val loc2 = |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
129 |
SwingUtilities.convertPoint(painter, |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
130 |
loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered) |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
131 |
|
53325
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
132 |
val completion = |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
133 |
new Completion_Popup(layered, loc2, font, result.items) { |
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
134 |
override def complete(item: Completion.Item) { |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
135 |
PIDE.completion_history.update(item) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
136 |
insert(item) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
137 |
} |
53325
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
138 |
override def propagate(evt: KeyEvent) { |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
139 |
JEdit_Lib.propagate_key(view, evt) |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
140 |
input(evt) |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
141 |
} |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
142 |
override def refocus() { text_area.requestFocus } |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
143 |
} |
53325
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
144 |
completion_popup = Some(completion) |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
145 |
completion.show_popup() |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53322
diff
changeset
|
146 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
147 |
} |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
148 |
case None => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
149 |
} |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
150 |
case None => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
151 |
} |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
152 |
} |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
153 |
} |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
154 |
|
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
155 |
|
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
156 |
/* input key events */ |
53273 | 157 |
|
53272 | 158 |
def input(evt: KeyEvent) |
53242 | 159 |
{ |
160 |
Swing_Thread.require() |
|
53233 | 161 |
|
53273 | 162 |
if (PIDE.options.bool("jedit_completion")) { |
163 |
if (!evt.isConsumed) { |
|
164 |
dismissed() |
|
53397
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
165 |
if (evt.getKeyChar != '\b') { |
53784 | 166 |
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
|
167 |
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
|
168 |
input_delay.revoke() |
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
169 |
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
|
170 |
} |
b179cdfa9d82
no completion on backspace -- too intrusive, e.g. when deleting keywords;
wenzelm
parents:
53337
diff
changeset
|
171 |
else input_delay.invoke() |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
172 |
} |
53242 | 173 |
} |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
174 |
} |
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
175 |
} |
53273 | 176 |
|
177 |
private val input_delay = |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
178 |
Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) { |
53322 | 179 |
action() |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
180 |
} |
53273 | 181 |
|
182 |
||
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
183 |
/* dismiss popup */ |
53273 | 184 |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
185 |
def dismissed(): Boolean = |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
186 |
{ |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
187 |
Swing_Thread.require() |
53273 | 188 |
|
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
189 |
completion_popup match { |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
190 |
case Some(completion) => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
191 |
completion.hide_popup() |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
192 |
completion_popup = None |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
193 |
true |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
194 |
case None => |
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
195 |
false |
53273 | 196 |
} |
53292
f567c1c7b180
option to insert unique completion immediately into buffer;
wenzelm
parents:
53275
diff
changeset
|
197 |
} |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
198 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
199 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
200 |
/* activation */ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
201 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
202 |
private val outer_key_listener = |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
203 |
JEdit_Lib.key_listener(key_typed = input _) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
204 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
205 |
private def activate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
206 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
207 |
text_area.addKeyListener(outer_key_listener) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
208 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
209 |
|
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
210 |
private def deactivate() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
211 |
{ |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
212 |
dismissed() |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
213 |
text_area.removeKeyListener(outer_key_listener) |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
214 |
} |
53228
f6c6688961db
some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents:
53226
diff
changeset
|
215 |
} |
53784 | 216 |
|
217 |
||
218 |
||
219 |
/** history text field **/ |
|
220 |
||
221 |
class History_Text_Field( |
|
222 |
name: String = null, |
|
223 |
instant_popups: Boolean = false, |
|
224 |
enter_adds_to_history: Boolean = true, |
|
225 |
syntax: Outer_Syntax = Outer_Syntax.init) extends |
|
226 |
HistoryTextField(name, instant_popups, enter_adds_to_history) |
|
227 |
{ |
|
228 |
text_field => |
|
229 |
||
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
|
230 |
// 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
|
231 |
if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret) |
53784 | 232 |
|
233 |
private var completion_popup: Option[Completion_Popup] = None |
|
234 |
||
235 |
||
236 |
/* dismiss */ |
|
237 |
||
238 |
private def dismissed(): Boolean = |
|
239 |
{ |
|
240 |
completion_popup match { |
|
241 |
case Some(completion) => |
|
242 |
completion.hide_popup() |
|
243 |
completion_popup = None |
|
244 |
true |
|
245 |
case None => |
|
246 |
false |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
||
251 |
/* insert */ |
|
252 |
||
253 |
private def insert(item: Completion.Item) |
|
254 |
{ |
|
255 |
Swing_Thread.require() |
|
256 |
||
257 |
val len = item.original.length |
|
258 |
if (text_field.isEditable && len > 0) { |
|
259 |
val caret = text_field.getCaret.getDot |
|
260 |
val content = text_field.getText |
|
261 |
JEdit_Lib.try_get_text(content, Text.Range(caret - len, caret)) match { |
|
262 |
case Some(text) if text == item.original => |
|
263 |
text_field.setText( |
|
264 |
content.substring(0, caret - len) + |
|
265 |
item.replacement + |
|
266 |
content.substring(caret)) |
|
53987 | 267 |
text_field.getCaret.setDot(caret - len + item.replacement.length) |
53784 | 268 |
case _ => |
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
||
274 |
/* completion action */ |
|
275 |
||
276 |
def action() |
|
277 |
{ |
|
278 |
GUI.layered_pane(text_field) match { |
|
279 |
case Some(layered) if text_field.isEditable => |
|
280 |
val history = PIDE.completion_history.value |
|
281 |
||
282 |
val caret = text_field.getCaret.getDot |
|
283 |
val text = text_field.getText.substring(0, caret) |
|
284 |
||
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
54377
diff
changeset
|
285 |
syntax.completion.complete( |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
54377
diff
changeset
|
286 |
history, decode = true, explicit = false, text, Completion.Context.default) match { |
53784 | 287 |
case Some(result) => |
288 |
val fm = text_field.getFontMetrics(text_field.getFont) |
|
289 |
val loc = |
|
290 |
SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered) |
|
291 |
||
53851 | 292 |
val completion = new Completion_Popup(layered, loc, text_field.getFont, result.items) |
53784 | 293 |
{ |
294 |
override def complete(item: Completion.Item) { |
|
295 |
PIDE.completion_history.update(item) |
|
296 |
insert(item) |
|
297 |
} |
|
298 |
override def propagate(evt: KeyEvent) { |
|
299 |
if (!evt.isConsumed) text_field.processKeyEvent(evt) |
|
300 |
} |
|
301 |
override def refocus() { text_field.requestFocus } |
|
302 |
} |
|
303 |
completion_popup = Some(completion) |
|
304 |
completion.show_popup() |
|
305 |
||
306 |
case None => |
|
307 |
} |
|
308 |
case _ => |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
||
313 |
/* process key event */ |
|
314 |
||
315 |
private def process(evt: KeyEvent) |
|
316 |
{ |
|
317 |
if (PIDE.options.bool("jedit_completion")) { |
|
318 |
dismissed() |
|
319 |
if (evt.getKeyChar != '\b') { |
|
320 |
val special = JEdit_Lib.special_key(evt) |
|
321 |
if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) { |
|
322 |
process_delay.revoke() |
|
323 |
action() |
|
324 |
} |
|
325 |
else process_delay.invoke() |
|
326 |
} |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
private val process_delay = |
|
331 |
Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) { |
|
332 |
action() |
|
333 |
} |
|
334 |
||
335 |
override def processKeyEvent(evt0: KeyEvent) |
|
336 |
{ |
|
337 |
val evt = KeyEventWorkaround.processKeyEvent(evt0) |
|
338 |
if (evt != null) { |
|
339 |
evt.getID match { |
|
340 |
case KeyEvent.KEY_PRESSED => |
|
341 |
val key_code = evt.getKeyCode |
|
342 |
if (key_code == KeyEvent.VK_ESCAPE) { |
|
343 |
if (dismissed()) evt.consume |
|
344 |
} |
|
345 |
case KeyEvent.KEY_TYPED => |
|
346 |
super.processKeyEvent(evt) |
|
347 |
process(evt) |
|
348 |
evt.consume |
|
349 |
case _ => |
|
350 |
} |
|
351 |
if (!evt.isConsumed) super.processKeyEvent(evt) |
|
352 |
} |
|
353 |
} |
|
354 |
} |
|
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
|
355 |
} |
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
|
356 |
|
53233 | 357 |
|
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
|
358 |
class Completion_Popup private( |
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
|
359 |
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
|
360 |
location: Point, |
53272 | 361 |
font: Font, |
53275 | 362 |
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
|
363 |
{ |
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
|
364 |
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
|
365 |
|
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
|
366 |
Swing_Thread.require() |
53405 | 367 |
|
53232 | 368 |
require(!items.isEmpty) |
53405 | 369 |
val multi = items.length > 1 |
53232 | 370 |
|
371 |
||
372 |
/* actions */ |
|
373 |
||
53275 | 374 |
def complete(item: Completion.Item) { } |
53232 | 375 |
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
|
376 |
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
|
377 |
|
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
|
378 |
|
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
|
379 |
/* 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
|
380 |
|
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
|
381 |
private val list_view = new ListView(items) |
53272 | 382 |
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
|
383 |
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
|
384 |
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
|
385 |
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
|
386 |
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
|
387 |
|
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
|
388 |
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
|
389 |
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
|
390 |
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
|
391 |
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
|
392 |
|
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
|
393 |
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
|
394 |
{ |
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
|
395 |
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
|
396 |
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
|
397 |
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
|
398 |
} |
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
|
399 |
} |
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
|
400 |
|
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
|
401 |
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
|
402 |
{ |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
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
|
408 |
} |
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
|
409 |
} |
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
|
410 |
|
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
|
411 |
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
|
412 |
{ |
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
|
413 |
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
|
414 |
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
|
415 |
} |
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
|
416 |
|
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
|
417 |
|
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
|
418 |
/* 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
|
419 |
|
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
420 |
private val inner_key_listener = |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
421 |
JEdit_Lib.key_listener( |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
422 |
key_pressed = (e: KeyEvent) => |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
423 |
{ |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
424 |
if (!e.isConsumed) { |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
425 |
e.getKeyCode match { |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
426 |
case KeyEvent.VK_TAB => |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
427 |
if (complete_selected()) e.consume |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
428 |
hide_popup() |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
429 |
case KeyEvent.VK_ESCAPE => |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
430 |
hide_popup() |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
431 |
e.consume |
53405 | 432 |
case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi => |
53407 | 433 |
move_items(-1) |
434 |
e.consume |
|
53405 | 435 |
case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi => |
53407 | 436 |
move_items(1) |
437 |
e.consume |
|
53405 | 438 |
case KeyEvent.VK_PAGE_UP if multi => |
53407 | 439 |
move_pages(-1) |
440 |
e.consume |
|
53405 | 441 |
case KeyEvent.VK_PAGE_DOWN if multi => |
53407 | 442 |
move_pages(1) |
443 |
e.consume |
|
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
444 |
case _ => |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
445 |
if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown) |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
446 |
hide_popup() |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
447 |
} |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
448 |
} |
53232 | 449 |
propagate(e) |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
450 |
}, |
53232 | 451 |
key_typed = propagate _ |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53023
diff
changeset
|
452 |
) |
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
|
453 |
|
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53273
diff
changeset
|
454 |
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
|
455 |
|
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
|
456 |
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
|
457 |
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
|
458 |
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
|
459 |
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
|
460 |
} |
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
|
461 |
}) |
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
|
462 |
|
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
|
463 |
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
|
464 |
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
|
465 |
}) |
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
|
466 |
|
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 |
|
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
|
468 |
/* 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
|
469 |
|
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
|
470 |
override def getFocusTraversalKeysEnabled = false |
53297 | 471 |
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
|
472 |
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
|
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 |
|
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 |
/* 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
|
476 |
|
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
|
477 |
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
|
478 |
{ |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
479 |
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
|
480 |
val size = |
53230 | 481 |
{ |
482 |
val geometry = JEdit_Lib.window_geometry(completion, completion) |
|
483 |
val bounds = Rendering.popup_bounds |
|
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
484 |
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
|
485 |
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
|
486 |
new Dimension(w, h) |
53230 | 487 |
} |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53246
diff
changeset
|
488 |
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
|
489 |
} |
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
|
490 |
|
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
|
491 |
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
|
492 |
{ |
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
|
493 |
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
|
494 |
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
|
495 |
} |
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 |
|
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
|
497 |
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
|
498 |
Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_dismiss_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
|
499 |
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
|
500 |
} |
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
|
501 |
|
53232 | 502 |
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
|
503 |
{ |
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
|
504 |
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
|
505 |
|
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
|
506 |
if (PIDE.options.seconds("jedit_completion_dismiss_delay").is_zero) |
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
|
507 |
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
|
508 |
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
|
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 |