src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Sat Feb 22 15:07:33 2014 +0100 (2014-02-22 ago)
changeset 55666 cc350eb1087e
parent 55616 25a7a998852a
child 55690 d73949233c2e
permissions -rw-r--r--
refined language context: antiquotes;
support default completions, with move of caret position;
tuned signature;
wenzelm@53023
     1
/*  Title:      Tools/jEdit/src/completion_popup.scala
wenzelm@53023
     2
    Author:     Makarius
wenzelm@53023
     3
wenzelm@53246
     4
Completion popup.
wenzelm@53023
     5
*/
wenzelm@53023
     6
wenzelm@53023
     7
package isabelle.jedit
wenzelm@53023
     8
wenzelm@53023
     9
wenzelm@53023
    10
import isabelle._
wenzelm@53023
    11
wenzelm@53297
    12
import java.awt.{Color, Font, Point, BorderLayout, Dimension}
wenzelm@53784
    13
import java.awt.event.{KeyEvent, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent}
wenzelm@53246
    14
import javax.swing.{JPanel, JComponent, JLayeredPane, SwingUtilities}
wenzelm@53297
    15
import javax.swing.border.LineBorder
wenzelm@53848
    16
import javax.swing.text.DefaultCaret
wenzelm@53023
    17
wenzelm@53023
    18
import scala.swing.{ListView, ScrollPane}
wenzelm@53023
    19
import scala.swing.event.MouseClicked
wenzelm@53023
    20
wenzelm@53784
    21
import org.gjt.sp.jedit.View
wenzelm@53228
    22
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@53784
    23
import org.gjt.sp.jedit.gui.{HistoryTextField, KeyEventWorkaround}
wenzelm@53023
    24
wenzelm@53023
    25
wenzelm@53023
    26
object Completion_Popup
wenzelm@53023
    27
{
wenzelm@53784
    28
  /** jEdit text area **/
wenzelm@53235
    29
wenzelm@53242
    30
  object Text_Area
wenzelm@53233
    31
  {
wenzelm@53274
    32
    private val key = new Object
wenzelm@53274
    33
wenzelm@53274
    34
    def apply(text_area: JEditTextArea): Option[Completion_Popup.Text_Area] =
wenzelm@53274
    35
      text_area.getClientProperty(key) match {
wenzelm@53274
    36
        case text_area_completion: Completion_Popup.Text_Area => Some(text_area_completion)
wenzelm@53274
    37
        case _ => None
wenzelm@53274
    38
      }
wenzelm@53274
    39
wenzelm@53274
    40
    def exit(text_area: JEditTextArea)
wenzelm@53274
    41
    {
wenzelm@53274
    42
      Swing_Thread.require()
wenzelm@53274
    43
      apply(text_area) match {
wenzelm@53274
    44
        case None =>
wenzelm@53274
    45
        case Some(text_area_completion) =>
wenzelm@53274
    46
          text_area_completion.deactivate()
wenzelm@53274
    47
          text_area.putClientProperty(key, null)
wenzelm@53274
    48
      }
wenzelm@53274
    49
    }
wenzelm@53274
    50
wenzelm@53274
    51
    def init(text_area: JEditTextArea): Completion_Popup.Text_Area =
wenzelm@53274
    52
    {
wenzelm@53274
    53
      exit(text_area)
wenzelm@53274
    54
      val text_area_completion = new Text_Area(text_area)
wenzelm@53274
    55
      text_area.putClientProperty(key, text_area_completion)
wenzelm@53274
    56
      text_area_completion.activate()
wenzelm@53274
    57
      text_area_completion
wenzelm@53274
    58
    }
wenzelm@53274
    59
wenzelm@53274
    60
    def dismissed(text_area: JEditTextArea): Boolean =
wenzelm@53274
    61
    {
wenzelm@53274
    62
      Swing_Thread.require()
wenzelm@53274
    63
      apply(text_area) match {
wenzelm@53274
    64
        case Some(text_area_completion) => text_area_completion.dismissed()
wenzelm@53274
    65
        case None => false
wenzelm@53274
    66
      }
wenzelm@53274
    67
    }
wenzelm@53272
    68
  }
wenzelm@53272
    69
wenzelm@53274
    70
  class Text_Area private(text_area: JEditTextArea)
wenzelm@53272
    71
  {
wenzelm@53272
    72
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53272
    73
wenzelm@53272
    74
wenzelm@53292
    75
    /* completion action */
wenzelm@53273
    76
wenzelm@53275
    77
    private def insert(item: Completion.Item)
wenzelm@53242
    78
    {
wenzelm@53242
    79
      Swing_Thread.require()
wenzelm@53023
    80
wenzelm@53242
    81
      val buffer = text_area.getBuffer
wenzelm@53242
    82
      val len = item.original.length
wenzelm@53242
    83
      if (buffer.isEditable && len > 0) {
wenzelm@53242
    84
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@53242
    85
          val caret = text_area.getCaretPosition
wenzelm@53242
    86
          JEdit_Lib.try_get_text(buffer, Text.Range(caret - len, caret)) match {
wenzelm@53242
    87
            case Some(text) if text == item.original =>
wenzelm@53242
    88
              buffer.remove(caret - len, len)
wenzelm@53242
    89
              buffer.insert(caret - len, item.replacement)
wenzelm@55666
    90
              if (item.move != 0)
wenzelm@55666
    91
                text_area.moveCaretPosition(text_area.getCaretPosition + item.move)
wenzelm@53242
    92
            case _ =>
wenzelm@53242
    93
          }
wenzelm@53233
    94
        }
wenzelm@53233
    95
      }
wenzelm@53233
    96
    }
wenzelm@53242
    97
wenzelm@53322
    98
    def action(immediate: Boolean = false, explicit: Boolean = false)
wenzelm@53292
    99
    {
wenzelm@53292
   100
      val view = text_area.getView
wenzelm@53292
   101
      val layered = view.getLayeredPane
wenzelm@53292
   102
      val buffer = text_area.getBuffer
wenzelm@53292
   103
      val painter = text_area.getPainter
wenzelm@53273
   104
wenzelm@53292
   105
      if (buffer.isEditable) {
wenzelm@53292
   106
        Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match {
wenzelm@53292
   107
          case Some(syntax) =>
wenzelm@53292
   108
            val caret = text_area.getCaretPosition
wenzelm@53292
   109
            val line = buffer.getLineOfOffset(caret)
wenzelm@53292
   110
            val start = buffer.getLineStartOffset(line)
wenzelm@53292
   111
            val text = buffer.getSegment(start, caret - start)
wenzelm@53292
   112
wenzelm@53337
   113
            val history = PIDE.completion_history.value
wenzelm@53322
   114
            val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@55615
   115
            val context =
wenzelm@55616
   116
              (PIDE.document_view(text_area) match {
wenzelm@55616
   117
                case None => None
wenzelm@55615
   118
                case Some(doc_view) => doc_view.get_rendering().completion_context(caret)
wenzelm@55616
   119
              }) getOrElse syntax.completion_context
wenzelm@55616
   120
wenzelm@55615
   121
            syntax.completion.complete(history, decode, explicit, text, context) match {
wenzelm@53325
   122
              case Some(result) =>
wenzelm@53325
   123
                if (result.unique && result.items.head.immediate && immediate)
wenzelm@53325
   124
                  insert(result.items.head)
wenzelm@53325
   125
                else {
wenzelm@53325
   126
                  val font =
wenzelm@53325
   127
                    painter.getFont.deriveFont(Rendering.font_size("jedit_popup_font_scale"))
wenzelm@53292
   128
wenzelm@53325
   129
                  val loc1 = text_area.offsetToXY(caret - result.original.length)
wenzelm@53325
   130
                  if (loc1 != null) {
wenzelm@53325
   131
                    val loc2 =
wenzelm@53325
   132
                      SwingUtilities.convertPoint(painter,
wenzelm@53325
   133
                        loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
wenzelm@53292
   134
wenzelm@53325
   135
                    val completion =
wenzelm@53325
   136
                      new Completion_Popup(layered, loc2, font, result.items) {
wenzelm@53337
   137
                        override def complete(item: Completion.Item) {
wenzelm@53337
   138
                          PIDE.completion_history.update(item)
wenzelm@53337
   139
                          insert(item)
wenzelm@53337
   140
                        }
wenzelm@53325
   141
                        override def propagate(evt: KeyEvent) {
wenzelm@53325
   142
                          JEdit_Lib.propagate_key(view, evt)
wenzelm@53325
   143
                          input(evt)
wenzelm@53325
   144
                        }
wenzelm@53325
   145
                        override def refocus() { text_area.requestFocus }
wenzelm@53292
   146
                      }
wenzelm@53325
   147
                    completion_popup = Some(completion)
wenzelm@53325
   148
                    completion.show_popup()
wenzelm@53325
   149
                  }
wenzelm@53292
   150
                }
wenzelm@53292
   151
              case None =>
wenzelm@53292
   152
            }
wenzelm@53292
   153
          case None =>
wenzelm@53292
   154
        }
wenzelm@53292
   155
      }
wenzelm@53292
   156
    }
wenzelm@53292
   157
wenzelm@53292
   158
wenzelm@53292
   159
    /* input key events */
wenzelm@53273
   160
wenzelm@53272
   161
    def input(evt: KeyEvent)
wenzelm@53242
   162
    {
wenzelm@53242
   163
      Swing_Thread.require()
wenzelm@53233
   164
wenzelm@53273
   165
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   166
        if (!evt.isConsumed) {
wenzelm@53273
   167
          dismissed()
wenzelm@53397
   168
          if (evt.getKeyChar != '\b') {
wenzelm@53784
   169
            val special = JEdit_Lib.special_key(evt)
wenzelm@53397
   170
            if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53397
   171
              input_delay.revoke()
wenzelm@53397
   172
              action(immediate = PIDE.options.bool("jedit_completion_immediate"))
wenzelm@53397
   173
            }
wenzelm@53397
   174
            else input_delay.invoke()
wenzelm@53292
   175
          }
wenzelm@53242
   176
        }
wenzelm@53228
   177
      }
wenzelm@53228
   178
    }
wenzelm@53273
   179
wenzelm@53273
   180
    private val input_delay =
wenzelm@53292
   181
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53322
   182
        action()
wenzelm@53292
   183
      }
wenzelm@53273
   184
wenzelm@53273
   185
wenzelm@53292
   186
    /* dismiss popup */
wenzelm@53273
   187
wenzelm@53292
   188
    def dismissed(): Boolean =
wenzelm@53292
   189
    {
wenzelm@53292
   190
      Swing_Thread.require()
wenzelm@53273
   191
wenzelm@53292
   192
      completion_popup match {
wenzelm@53292
   193
        case Some(completion) =>
wenzelm@53292
   194
          completion.hide_popup()
wenzelm@53292
   195
          completion_popup = None
wenzelm@53292
   196
          true
wenzelm@53292
   197
        case None =>
wenzelm@53292
   198
          false
wenzelm@53273
   199
      }
wenzelm@53292
   200
    }
wenzelm@53274
   201
wenzelm@53274
   202
wenzelm@53274
   203
    /* activation */
wenzelm@53274
   204
wenzelm@53274
   205
    private val outer_key_listener =
wenzelm@53274
   206
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   207
wenzelm@53274
   208
    private def activate()
wenzelm@53274
   209
    {
wenzelm@53274
   210
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   211
    }
wenzelm@53274
   212
wenzelm@53274
   213
    private def deactivate()
wenzelm@53274
   214
    {
wenzelm@53274
   215
      dismissed()
wenzelm@53274
   216
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   217
    }
wenzelm@53228
   218
  }
wenzelm@53784
   219
wenzelm@53784
   220
wenzelm@53784
   221
wenzelm@53784
   222
  /** history text field **/
wenzelm@53784
   223
wenzelm@53784
   224
  class History_Text_Field(
wenzelm@53784
   225
    name: String = null,
wenzelm@53784
   226
    instant_popups: Boolean = false,
wenzelm@53784
   227
    enter_adds_to_history: Boolean = true,
wenzelm@53784
   228
    syntax: Outer_Syntax = Outer_Syntax.init) extends
wenzelm@53784
   229
    HistoryTextField(name, instant_popups, enter_adds_to_history)
wenzelm@53784
   230
  {
wenzelm@53784
   231
    text_field =>
wenzelm@53784
   232
wenzelm@53848
   233
    // see https://forums.oracle.com/thread/1361677
wenzelm@53848
   234
    if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret)
wenzelm@53784
   235
wenzelm@53784
   236
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53784
   237
wenzelm@53784
   238
wenzelm@53784
   239
    /* dismiss */
wenzelm@53784
   240
wenzelm@53784
   241
    private def dismissed(): Boolean =
wenzelm@53784
   242
    {
wenzelm@53784
   243
      completion_popup match {
wenzelm@53784
   244
        case Some(completion) =>
wenzelm@53784
   245
          completion.hide_popup()
wenzelm@53784
   246
          completion_popup = None
wenzelm@53784
   247
          true
wenzelm@53784
   248
        case None =>
wenzelm@53784
   249
          false
wenzelm@53784
   250
      }
wenzelm@53784
   251
    }
wenzelm@53784
   252
wenzelm@53784
   253
wenzelm@53784
   254
    /* insert */
wenzelm@53784
   255
wenzelm@53784
   256
    private def insert(item: Completion.Item)
wenzelm@53784
   257
    {
wenzelm@53784
   258
      Swing_Thread.require()
wenzelm@53784
   259
wenzelm@53784
   260
      val len = item.original.length
wenzelm@53784
   261
      if (text_field.isEditable && len > 0) {
wenzelm@53784
   262
        val caret = text_field.getCaret.getDot
wenzelm@53784
   263
        val content = text_field.getText
wenzelm@53784
   264
        JEdit_Lib.try_get_text(content, Text.Range(caret - len, caret)) match {
wenzelm@53784
   265
          case Some(text) if text == item.original =>
wenzelm@53784
   266
            text_field.setText(
wenzelm@53784
   267
              content.substring(0, caret - len) +
wenzelm@53784
   268
              item.replacement +
wenzelm@53784
   269
              content.substring(caret))
wenzelm@55666
   270
            text_field.getCaret.setDot(caret - len + item.replacement.length + item.move)
wenzelm@53784
   271
          case _ =>
wenzelm@53784
   272
        }
wenzelm@53784
   273
      }
wenzelm@53784
   274
    }
wenzelm@53784
   275
wenzelm@53784
   276
wenzelm@53784
   277
    /* completion action */
wenzelm@53784
   278
wenzelm@53784
   279
    def action()
wenzelm@53784
   280
    {
wenzelm@53784
   281
      GUI.layered_pane(text_field) match {
wenzelm@53784
   282
        case Some(layered) if text_field.isEditable =>
wenzelm@53784
   283
          val history = PIDE.completion_history.value
wenzelm@53784
   284
wenzelm@53784
   285
          val caret = text_field.getCaret.getDot
wenzelm@53784
   286
          val text = text_field.getText.substring(0, caret)
wenzelm@53784
   287
wenzelm@55615
   288
          syntax.completion.complete(
wenzelm@55616
   289
              history, decode = true, explicit = false, text, syntax.completion_context) match {
wenzelm@53784
   290
            case Some(result) =>
wenzelm@53784
   291
              val fm = text_field.getFontMetrics(text_field.getFont)
wenzelm@53784
   292
              val loc =
wenzelm@53784
   293
                SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered)
wenzelm@53784
   294
wenzelm@53851
   295
              val completion = new Completion_Popup(layered, loc, text_field.getFont, result.items)
wenzelm@53784
   296
              {
wenzelm@53784
   297
                override def complete(item: Completion.Item) {
wenzelm@53784
   298
                  PIDE.completion_history.update(item)
wenzelm@53784
   299
                  insert(item)
wenzelm@53784
   300
                }
wenzelm@53784
   301
                override def propagate(evt: KeyEvent) {
wenzelm@53784
   302
                  if (!evt.isConsumed) text_field.processKeyEvent(evt)
wenzelm@53784
   303
                }
wenzelm@53784
   304
                override def refocus() { text_field.requestFocus }
wenzelm@53784
   305
              }
wenzelm@53784
   306
              completion_popup = Some(completion)
wenzelm@53784
   307
              completion.show_popup()
wenzelm@53784
   308
wenzelm@53784
   309
            case None =>
wenzelm@53784
   310
          }
wenzelm@53784
   311
        case _ =>
wenzelm@53784
   312
      }
wenzelm@53784
   313
    }
wenzelm@53784
   314
wenzelm@53784
   315
wenzelm@53784
   316
    /* process key event */
wenzelm@53784
   317
wenzelm@53784
   318
    private def process(evt: KeyEvent)
wenzelm@53784
   319
    {
wenzelm@53784
   320
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53784
   321
        dismissed()
wenzelm@53784
   322
        if (evt.getKeyChar != '\b') {
wenzelm@53784
   323
          val special = JEdit_Lib.special_key(evt)
wenzelm@53784
   324
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53784
   325
            process_delay.revoke()
wenzelm@53784
   326
            action()
wenzelm@53784
   327
          }
wenzelm@53784
   328
          else process_delay.invoke()
wenzelm@53784
   329
        }
wenzelm@53784
   330
      }
wenzelm@53784
   331
    }
wenzelm@53784
   332
wenzelm@53784
   333
    private val process_delay =
wenzelm@53784
   334
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53784
   335
        action()
wenzelm@53784
   336
      }
wenzelm@53784
   337
wenzelm@53784
   338
    override def processKeyEvent(evt0: KeyEvent)
wenzelm@53784
   339
    {
wenzelm@53784
   340
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53784
   341
      if (evt != null) {
wenzelm@53784
   342
        evt.getID match {
wenzelm@53784
   343
          case KeyEvent.KEY_PRESSED =>
wenzelm@53784
   344
            val key_code = evt.getKeyCode
wenzelm@53784
   345
            if (key_code == KeyEvent.VK_ESCAPE) {
wenzelm@53784
   346
              if (dismissed()) evt.consume
wenzelm@53784
   347
            }
wenzelm@53784
   348
          case KeyEvent.KEY_TYPED =>
wenzelm@53784
   349
            super.processKeyEvent(evt)
wenzelm@53784
   350
            process(evt)
wenzelm@53784
   351
            evt.consume
wenzelm@53784
   352
          case _ =>
wenzelm@53784
   353
        }
wenzelm@53784
   354
        if (!evt.isConsumed) super.processKeyEvent(evt)
wenzelm@53784
   355
      }
wenzelm@53784
   356
    }
wenzelm@53784
   357
  }
wenzelm@53023
   358
}
wenzelm@53023
   359
wenzelm@53233
   360
wenzelm@53023
   361
class Completion_Popup private(
wenzelm@53246
   362
  layered: JLayeredPane,
wenzelm@53246
   363
  location: Point,
wenzelm@53272
   364
  font: Font,
wenzelm@53275
   365
  items: List[Completion.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   366
{
wenzelm@53023
   367
  completion =>
wenzelm@53023
   368
wenzelm@53023
   369
  Swing_Thread.require()
wenzelm@53405
   370
wenzelm@53232
   371
  require(!items.isEmpty)
wenzelm@53405
   372
  val multi = items.length > 1
wenzelm@53232
   373
wenzelm@53232
   374
wenzelm@53232
   375
  /* actions */
wenzelm@53232
   376
wenzelm@53275
   377
  def complete(item: Completion.Item) { }
wenzelm@53232
   378
  def propagate(evt: KeyEvent) { }
wenzelm@53234
   379
  def refocus() { }
wenzelm@53023
   380
wenzelm@53023
   381
wenzelm@53023
   382
  /* list view */
wenzelm@53023
   383
wenzelm@53023
   384
  private val list_view = new ListView(items)
wenzelm@53272
   385
  list_view.font = font
wenzelm@53023
   386
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   387
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   388
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   389
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   390
wenzelm@53398
   391
  for (cond <-
wenzelm@53398
   392
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   393
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   394
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   395
wenzelm@53023
   396
  private def complete_selected(): Boolean =
wenzelm@53023
   397
  {
wenzelm@53023
   398
    list_view.selection.items.toList match {
wenzelm@53023
   399
      case item :: _ => complete(item); true
wenzelm@53023
   400
      case _ => false
wenzelm@53023
   401
    }
wenzelm@53023
   402
  }
wenzelm@53023
   403
wenzelm@53023
   404
  private def move_items(n: Int)
wenzelm@53023
   405
  {
wenzelm@53023
   406
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   407
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   408
    if (i != j) {
wenzelm@53023
   409
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   410
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   411
    }
wenzelm@53023
   412
  }
wenzelm@53023
   413
wenzelm@53023
   414
  private def move_pages(n: Int)
wenzelm@53023
   415
  {
wenzelm@53023
   416
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   417
    move_items(page_size * n)
wenzelm@53023
   418
  }
wenzelm@53023
   419
wenzelm@53023
   420
wenzelm@53023
   421
  /* event handling */
wenzelm@53023
   422
wenzelm@53274
   423
  private val inner_key_listener =
wenzelm@53226
   424
    JEdit_Lib.key_listener(
wenzelm@53226
   425
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   426
        {
wenzelm@53226
   427
          if (!e.isConsumed) {
wenzelm@53226
   428
            e.getKeyCode match {
wenzelm@53226
   429
              case KeyEvent.VK_TAB =>
wenzelm@53226
   430
                if (complete_selected()) e.consume
wenzelm@53226
   431
                hide_popup()
wenzelm@53226
   432
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   433
                hide_popup()
wenzelm@53226
   434
                e.consume
wenzelm@53405
   435
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   436
                move_items(-1)
wenzelm@53407
   437
                e.consume
wenzelm@53405
   438
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   439
                move_items(1)
wenzelm@53407
   440
                e.consume
wenzelm@53405
   441
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   442
                move_pages(-1)
wenzelm@53407
   443
                e.consume
wenzelm@53405
   444
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   445
                move_pages(1)
wenzelm@53407
   446
                e.consume
wenzelm@53226
   447
              case _ =>
wenzelm@53226
   448
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   449
                  hide_popup()
wenzelm@53226
   450
            }
wenzelm@53226
   451
          }
wenzelm@53232
   452
          propagate(e)
wenzelm@53226
   453
        },
wenzelm@53232
   454
      key_typed = propagate _
wenzelm@53226
   455
    )
wenzelm@53023
   456
wenzelm@53274
   457
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   458
wenzelm@53023
   459
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   460
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   461
      if (complete_selected()) e.consume
wenzelm@53023
   462
      hide_popup()
wenzelm@53023
   463
    }
wenzelm@53023
   464
  })
wenzelm@53023
   465
wenzelm@53023
   466
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   467
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   468
  })
wenzelm@53023
   469
wenzelm@53023
   470
wenzelm@53023
   471
  /* main content */
wenzelm@53023
   472
wenzelm@53023
   473
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   474
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   475
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   476
wenzelm@53023
   477
wenzelm@53023
   478
  /* popup */
wenzelm@53023
   479
wenzelm@53023
   480
  private val popup =
wenzelm@53023
   481
  {
wenzelm@53247
   482
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   483
    val size =
wenzelm@53230
   484
    {
wenzelm@53230
   485
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   486
      val bounds = Rendering.popup_bounds
wenzelm@53247
   487
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   488
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   489
      new Dimension(w, h)
wenzelm@53230
   490
    }
wenzelm@53247
   491
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   492
  }
wenzelm@53246
   493
wenzelm@53246
   494
  private def show_popup()
wenzelm@53246
   495
  {
wenzelm@53246
   496
    popup.show
wenzelm@53246
   497
    list_view.requestFocus
wenzelm@53023
   498
  }
wenzelm@53023
   499
wenzelm@54377
   500
  private val hide_popup_delay =
wenzelm@54377
   501
    Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_dismiss_delay")) {
wenzelm@54377
   502
      popup.hide
wenzelm@54377
   503
    }
wenzelm@54377
   504
wenzelm@53232
   505
  private def hide_popup()
wenzelm@53023
   506
  {
wenzelm@54376
   507
    if (list_view.peer.isFocusOwner) refocus()
wenzelm@54377
   508
wenzelm@54377
   509
    if (PIDE.options.seconds("jedit_completion_dismiss_delay").is_zero)
wenzelm@54377
   510
      popup.hide
wenzelm@54377
   511
    else hide_popup_delay.invoke()
wenzelm@53023
   512
  }
wenzelm@53023
   513
}
wenzelm@53023
   514