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