src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Fri Nov 08 15:10:16 2013 +0100 (2013-11-08 ago)
changeset 54376 3eb84b6b0353
parent 53987 16a0cd5293d9
child 54377 750561986828
permissions -rw-r--r--
transfer focus before closing old component -- avoid intermediate focus switch to root component, which is actually visible e.g. on Windows;
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@53242
    90
            case _ =>
wenzelm@53242
    91
          }
wenzelm@53233
    92
        }
wenzelm@53233
    93
      }
wenzelm@53233
    94
    }
wenzelm@53242
    95
wenzelm@53322
    96
    def action(immediate: Boolean = false, explicit: Boolean = false)
wenzelm@53292
    97
    {
wenzelm@53292
    98
      val view = text_area.getView
wenzelm@53292
    99
      val layered = view.getLayeredPane
wenzelm@53292
   100
      val buffer = text_area.getBuffer
wenzelm@53292
   101
      val painter = text_area.getPainter
wenzelm@53273
   102
wenzelm@53292
   103
      if (buffer.isEditable) {
wenzelm@53292
   104
        Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match {
wenzelm@53292
   105
          case Some(syntax) =>
wenzelm@53292
   106
            val caret = text_area.getCaretPosition
wenzelm@53292
   107
            val line = buffer.getLineOfOffset(caret)
wenzelm@53292
   108
            val start = buffer.getLineStartOffset(line)
wenzelm@53292
   109
            val text = buffer.getSegment(start, caret - start)
wenzelm@53292
   110
wenzelm@53337
   111
            val history = PIDE.completion_history.value
wenzelm@53322
   112
            val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@53337
   113
            syntax.completion.complete(history, decode, explicit, text) match {
wenzelm@53325
   114
              case Some(result) =>
wenzelm@53325
   115
                if (result.unique && result.items.head.immediate && immediate)
wenzelm@53325
   116
                  insert(result.items.head)
wenzelm@53325
   117
                else {
wenzelm@53325
   118
                  val font =
wenzelm@53325
   119
                    painter.getFont.deriveFont(Rendering.font_size("jedit_popup_font_scale"))
wenzelm@53292
   120
wenzelm@53325
   121
                  val loc1 = text_area.offsetToXY(caret - result.original.length)
wenzelm@53325
   122
                  if (loc1 != null) {
wenzelm@53325
   123
                    val loc2 =
wenzelm@53325
   124
                      SwingUtilities.convertPoint(painter,
wenzelm@53325
   125
                        loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
wenzelm@53292
   126
wenzelm@53325
   127
                    val completion =
wenzelm@53325
   128
                      new Completion_Popup(layered, loc2, font, result.items) {
wenzelm@53337
   129
                        override def complete(item: Completion.Item) {
wenzelm@53337
   130
                          PIDE.completion_history.update(item)
wenzelm@53337
   131
                          insert(item)
wenzelm@53337
   132
                        }
wenzelm@53325
   133
                        override def propagate(evt: KeyEvent) {
wenzelm@53325
   134
                          JEdit_Lib.propagate_key(view, evt)
wenzelm@53325
   135
                          input(evt)
wenzelm@53325
   136
                        }
wenzelm@53325
   137
                        override def refocus() { text_area.requestFocus }
wenzelm@53292
   138
                      }
wenzelm@53325
   139
                    completion_popup = Some(completion)
wenzelm@53325
   140
                    completion.show_popup()
wenzelm@53325
   141
                  }
wenzelm@53292
   142
                }
wenzelm@53292
   143
              case None =>
wenzelm@53292
   144
            }
wenzelm@53292
   145
          case None =>
wenzelm@53292
   146
        }
wenzelm@53292
   147
      }
wenzelm@53292
   148
    }
wenzelm@53292
   149
wenzelm@53292
   150
wenzelm@53292
   151
    /* input key events */
wenzelm@53273
   152
wenzelm@53272
   153
    def input(evt: KeyEvent)
wenzelm@53242
   154
    {
wenzelm@53242
   155
      Swing_Thread.require()
wenzelm@53233
   156
wenzelm@53273
   157
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   158
        if (!evt.isConsumed) {
wenzelm@53273
   159
          dismissed()
wenzelm@53397
   160
          if (evt.getKeyChar != '\b') {
wenzelm@53784
   161
            val special = JEdit_Lib.special_key(evt)
wenzelm@53397
   162
            if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53397
   163
              input_delay.revoke()
wenzelm@53397
   164
              action(immediate = PIDE.options.bool("jedit_completion_immediate"))
wenzelm@53397
   165
            }
wenzelm@53397
   166
            else input_delay.invoke()
wenzelm@53292
   167
          }
wenzelm@53242
   168
        }
wenzelm@53228
   169
      }
wenzelm@53228
   170
    }
wenzelm@53273
   171
wenzelm@53273
   172
    private val input_delay =
wenzelm@53292
   173
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53322
   174
        action()
wenzelm@53292
   175
      }
wenzelm@53273
   176
wenzelm@53273
   177
wenzelm@53292
   178
    /* dismiss popup */
wenzelm@53273
   179
wenzelm@53292
   180
    def dismissed(): Boolean =
wenzelm@53292
   181
    {
wenzelm@53292
   182
      Swing_Thread.require()
wenzelm@53273
   183
wenzelm@53292
   184
      completion_popup match {
wenzelm@53292
   185
        case Some(completion) =>
wenzelm@53292
   186
          completion.hide_popup()
wenzelm@53292
   187
          completion_popup = None
wenzelm@53292
   188
          true
wenzelm@53292
   189
        case None =>
wenzelm@53292
   190
          false
wenzelm@53273
   191
      }
wenzelm@53292
   192
    }
wenzelm@53274
   193
wenzelm@53274
   194
wenzelm@53274
   195
    /* activation */
wenzelm@53274
   196
wenzelm@53274
   197
    private val outer_key_listener =
wenzelm@53274
   198
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   199
wenzelm@53274
   200
    private def activate()
wenzelm@53274
   201
    {
wenzelm@53274
   202
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   203
    }
wenzelm@53274
   204
wenzelm@53274
   205
    private def deactivate()
wenzelm@53274
   206
    {
wenzelm@53274
   207
      dismissed()
wenzelm@53274
   208
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   209
    }
wenzelm@53228
   210
  }
wenzelm@53784
   211
wenzelm@53784
   212
wenzelm@53784
   213
wenzelm@53784
   214
  /** history text field **/
wenzelm@53784
   215
wenzelm@53784
   216
  class History_Text_Field(
wenzelm@53784
   217
    name: String = null,
wenzelm@53784
   218
    instant_popups: Boolean = false,
wenzelm@53784
   219
    enter_adds_to_history: Boolean = true,
wenzelm@53784
   220
    syntax: Outer_Syntax = Outer_Syntax.init) extends
wenzelm@53784
   221
    HistoryTextField(name, instant_popups, enter_adds_to_history)
wenzelm@53784
   222
  {
wenzelm@53784
   223
    text_field =>
wenzelm@53784
   224
wenzelm@53848
   225
    // see https://forums.oracle.com/thread/1361677
wenzelm@53848
   226
    if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret)
wenzelm@53784
   227
wenzelm@53784
   228
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53784
   229
wenzelm@53784
   230
wenzelm@53784
   231
    /* dismiss */
wenzelm@53784
   232
wenzelm@53784
   233
    private def dismissed(): Boolean =
wenzelm@53784
   234
    {
wenzelm@53784
   235
      completion_popup match {
wenzelm@53784
   236
        case Some(completion) =>
wenzelm@53784
   237
          completion.hide_popup()
wenzelm@53784
   238
          completion_popup = None
wenzelm@53784
   239
          true
wenzelm@53784
   240
        case None =>
wenzelm@53784
   241
          false
wenzelm@53784
   242
      }
wenzelm@53784
   243
    }
wenzelm@53784
   244
wenzelm@53784
   245
wenzelm@53784
   246
    /* insert */
wenzelm@53784
   247
wenzelm@53784
   248
    private def insert(item: Completion.Item)
wenzelm@53784
   249
    {
wenzelm@53784
   250
      Swing_Thread.require()
wenzelm@53784
   251
wenzelm@53784
   252
      val len = item.original.length
wenzelm@53784
   253
      if (text_field.isEditable && len > 0) {
wenzelm@53784
   254
        val caret = text_field.getCaret.getDot
wenzelm@53784
   255
        val content = text_field.getText
wenzelm@53784
   256
        JEdit_Lib.try_get_text(content, Text.Range(caret - len, caret)) match {
wenzelm@53784
   257
          case Some(text) if text == item.original =>
wenzelm@53784
   258
            text_field.setText(
wenzelm@53784
   259
              content.substring(0, caret - len) +
wenzelm@53784
   260
              item.replacement +
wenzelm@53784
   261
              content.substring(caret))
wenzelm@53987
   262
            text_field.getCaret.setDot(caret - len + item.replacement.length)
wenzelm@53784
   263
          case _ =>
wenzelm@53784
   264
        }
wenzelm@53784
   265
      }
wenzelm@53784
   266
    }
wenzelm@53784
   267
wenzelm@53784
   268
wenzelm@53784
   269
    /* completion action */
wenzelm@53784
   270
wenzelm@53784
   271
    def action()
wenzelm@53784
   272
    {
wenzelm@53784
   273
      GUI.layered_pane(text_field) match {
wenzelm@53784
   274
        case Some(layered) if text_field.isEditable =>
wenzelm@53784
   275
          val history = PIDE.completion_history.value
wenzelm@53784
   276
wenzelm@53784
   277
          val caret = text_field.getCaret.getDot
wenzelm@53784
   278
          val text = text_field.getText.substring(0, caret)
wenzelm@53784
   279
wenzelm@53784
   280
          syntax.completion.complete(history, decode = true, explicit = false, text) match {
wenzelm@53784
   281
            case Some(result) =>
wenzelm@53784
   282
              val fm = text_field.getFontMetrics(text_field.getFont)
wenzelm@53784
   283
              val loc =
wenzelm@53784
   284
                SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered)
wenzelm@53784
   285
wenzelm@53851
   286
              val completion = new Completion_Popup(layered, loc, text_field.getFont, result.items)
wenzelm@53784
   287
              {
wenzelm@53784
   288
                override def complete(item: Completion.Item) {
wenzelm@53784
   289
                  PIDE.completion_history.update(item)
wenzelm@53784
   290
                  insert(item)
wenzelm@53784
   291
                }
wenzelm@53784
   292
                override def propagate(evt: KeyEvent) {
wenzelm@53784
   293
                  if (!evt.isConsumed) text_field.processKeyEvent(evt)
wenzelm@53784
   294
                }
wenzelm@53784
   295
                override def refocus() { text_field.requestFocus }
wenzelm@53784
   296
              }
wenzelm@53784
   297
              completion_popup = Some(completion)
wenzelm@53784
   298
              completion.show_popup()
wenzelm@53784
   299
wenzelm@53784
   300
            case None =>
wenzelm@53784
   301
          }
wenzelm@53784
   302
        case _ =>
wenzelm@53784
   303
      }
wenzelm@53784
   304
    }
wenzelm@53784
   305
wenzelm@53784
   306
wenzelm@53784
   307
    /* process key event */
wenzelm@53784
   308
wenzelm@53784
   309
    private def process(evt: KeyEvent)
wenzelm@53784
   310
    {
wenzelm@53784
   311
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53784
   312
        dismissed()
wenzelm@53784
   313
        if (evt.getKeyChar != '\b') {
wenzelm@53784
   314
          val special = JEdit_Lib.special_key(evt)
wenzelm@53784
   315
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53784
   316
            process_delay.revoke()
wenzelm@53784
   317
            action()
wenzelm@53784
   318
          }
wenzelm@53784
   319
          else process_delay.invoke()
wenzelm@53784
   320
        }
wenzelm@53784
   321
      }
wenzelm@53784
   322
    }
wenzelm@53784
   323
wenzelm@53784
   324
    private val process_delay =
wenzelm@53784
   325
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53784
   326
        action()
wenzelm@53784
   327
      }
wenzelm@53784
   328
wenzelm@53784
   329
    override def processKeyEvent(evt0: KeyEvent)
wenzelm@53784
   330
    {
wenzelm@53784
   331
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53784
   332
      if (evt != null) {
wenzelm@53784
   333
        evt.getID match {
wenzelm@53784
   334
          case KeyEvent.KEY_PRESSED =>
wenzelm@53784
   335
            val key_code = evt.getKeyCode
wenzelm@53784
   336
            if (key_code == KeyEvent.VK_ESCAPE) {
wenzelm@53784
   337
              if (dismissed()) evt.consume
wenzelm@53784
   338
            }
wenzelm@53784
   339
          case KeyEvent.KEY_TYPED =>
wenzelm@53784
   340
            super.processKeyEvent(evt)
wenzelm@53784
   341
            process(evt)
wenzelm@53784
   342
            evt.consume
wenzelm@53784
   343
          case _ =>
wenzelm@53784
   344
        }
wenzelm@53784
   345
        if (!evt.isConsumed) super.processKeyEvent(evt)
wenzelm@53784
   346
      }
wenzelm@53784
   347
    }
wenzelm@53784
   348
  }
wenzelm@53023
   349
}
wenzelm@53023
   350
wenzelm@53233
   351
wenzelm@53023
   352
class Completion_Popup private(
wenzelm@53246
   353
  layered: JLayeredPane,
wenzelm@53246
   354
  location: Point,
wenzelm@53272
   355
  font: Font,
wenzelm@53275
   356
  items: List[Completion.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   357
{
wenzelm@53023
   358
  completion =>
wenzelm@53023
   359
wenzelm@53023
   360
  Swing_Thread.require()
wenzelm@53405
   361
wenzelm@53232
   362
  require(!items.isEmpty)
wenzelm@53405
   363
  val multi = items.length > 1
wenzelm@53232
   364
wenzelm@53232
   365
wenzelm@53232
   366
  /* actions */
wenzelm@53232
   367
wenzelm@53275
   368
  def complete(item: Completion.Item) { }
wenzelm@53232
   369
  def propagate(evt: KeyEvent) { }
wenzelm@53234
   370
  def refocus() { }
wenzelm@53023
   371
wenzelm@53023
   372
wenzelm@53023
   373
  /* list view */
wenzelm@53023
   374
wenzelm@53023
   375
  private val list_view = new ListView(items)
wenzelm@53272
   376
  list_view.font = font
wenzelm@53023
   377
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   378
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   379
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   380
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   381
wenzelm@53398
   382
  for (cond <-
wenzelm@53398
   383
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   384
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   385
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   386
wenzelm@53023
   387
  private def complete_selected(): Boolean =
wenzelm@53023
   388
  {
wenzelm@53023
   389
    list_view.selection.items.toList match {
wenzelm@53023
   390
      case item :: _ => complete(item); true
wenzelm@53023
   391
      case _ => false
wenzelm@53023
   392
    }
wenzelm@53023
   393
  }
wenzelm@53023
   394
wenzelm@53023
   395
  private def move_items(n: Int)
wenzelm@53023
   396
  {
wenzelm@53023
   397
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   398
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   399
    if (i != j) {
wenzelm@53023
   400
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   401
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   402
    }
wenzelm@53023
   403
  }
wenzelm@53023
   404
wenzelm@53023
   405
  private def move_pages(n: Int)
wenzelm@53023
   406
  {
wenzelm@53023
   407
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   408
    move_items(page_size * n)
wenzelm@53023
   409
  }
wenzelm@53023
   410
wenzelm@53023
   411
wenzelm@53023
   412
  /* event handling */
wenzelm@53023
   413
wenzelm@53274
   414
  private val inner_key_listener =
wenzelm@53226
   415
    JEdit_Lib.key_listener(
wenzelm@53226
   416
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   417
        {
wenzelm@53226
   418
          if (!e.isConsumed) {
wenzelm@53226
   419
            e.getKeyCode match {
wenzelm@53226
   420
              case KeyEvent.VK_TAB =>
wenzelm@53226
   421
                if (complete_selected()) e.consume
wenzelm@53226
   422
                hide_popup()
wenzelm@53226
   423
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   424
                hide_popup()
wenzelm@53226
   425
                e.consume
wenzelm@53405
   426
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   427
                move_items(-1)
wenzelm@53407
   428
                e.consume
wenzelm@53405
   429
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   430
                move_items(1)
wenzelm@53407
   431
                e.consume
wenzelm@53405
   432
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   433
                move_pages(-1)
wenzelm@53407
   434
                e.consume
wenzelm@53405
   435
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   436
                move_pages(1)
wenzelm@53407
   437
                e.consume
wenzelm@53226
   438
              case _ =>
wenzelm@53226
   439
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   440
                  hide_popup()
wenzelm@53226
   441
            }
wenzelm@53226
   442
          }
wenzelm@53232
   443
          propagate(e)
wenzelm@53226
   444
        },
wenzelm@53232
   445
      key_typed = propagate _
wenzelm@53226
   446
    )
wenzelm@53023
   447
wenzelm@53274
   448
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   449
wenzelm@53023
   450
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   451
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   452
      if (complete_selected()) e.consume
wenzelm@53023
   453
      hide_popup()
wenzelm@53023
   454
    }
wenzelm@53023
   455
  })
wenzelm@53023
   456
wenzelm@53023
   457
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   458
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   459
  })
wenzelm@53023
   460
wenzelm@53023
   461
wenzelm@53023
   462
  /* main content */
wenzelm@53023
   463
wenzelm@53023
   464
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   465
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   466
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   467
wenzelm@53023
   468
wenzelm@53023
   469
  /* popup */
wenzelm@53023
   470
wenzelm@53023
   471
  private val popup =
wenzelm@53023
   472
  {
wenzelm@53247
   473
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   474
    val size =
wenzelm@53230
   475
    {
wenzelm@53230
   476
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   477
      val bounds = Rendering.popup_bounds
wenzelm@53247
   478
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   479
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   480
      new Dimension(w, h)
wenzelm@53230
   481
    }
wenzelm@53247
   482
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   483
  }
wenzelm@53246
   484
wenzelm@53246
   485
  private def show_popup()
wenzelm@53246
   486
  {
wenzelm@53246
   487
    popup.show
wenzelm@53246
   488
    list_view.requestFocus
wenzelm@53023
   489
  }
wenzelm@53023
   490
wenzelm@53232
   491
  private def hide_popup()
wenzelm@53023
   492
  {
wenzelm@54376
   493
    if (list_view.peer.isFocusOwner) refocus()
wenzelm@53229
   494
    popup.hide
wenzelm@53023
   495
  }
wenzelm@53023
   496
}
wenzelm@53023
   497