src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Tue Feb 25 22:13:57 2014 +0100 (2014-02-25 ago)
changeset 55752 43d0e2a34c9d
parent 55749 75a48dc4383e
child 55767 96ddf9bf12ac
permissions -rw-r--r--
uniform insert vs. popup policy;
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@55712
    22
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
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@55712
    34
    def apply(text_area: TextArea): Option[Completion_Popup.Text_Area] =
wenzelm@55712
    35
    {
wenzelm@55712
    36
      Swing_Thread.require()
wenzelm@53274
    37
      text_area.getClientProperty(key) match {
wenzelm@53274
    38
        case text_area_completion: Completion_Popup.Text_Area => Some(text_area_completion)
wenzelm@53274
    39
        case _ => None
wenzelm@53274
    40
      }
wenzelm@55712
    41
    }
wenzelm@53274
    42
wenzelm@55712
    43
    def active_range(text_area: TextArea): Option[Text.Range] =
wenzelm@55712
    44
      apply(text_area) match {
wenzelm@55712
    45
        case Some(text_area_completion) => text_area_completion.active_range
wenzelm@55712
    46
        case None => None
wenzelm@55711
    47
      }
wenzelm@55711
    48
wenzelm@53274
    49
    def exit(text_area: JEditTextArea)
wenzelm@53274
    50
    {
wenzelm@53274
    51
      Swing_Thread.require()
wenzelm@53274
    52
      apply(text_area) match {
wenzelm@53274
    53
        case None =>
wenzelm@53274
    54
        case Some(text_area_completion) =>
wenzelm@53274
    55
          text_area_completion.deactivate()
wenzelm@53274
    56
          text_area.putClientProperty(key, null)
wenzelm@53274
    57
      }
wenzelm@53274
    58
    }
wenzelm@53274
    59
wenzelm@53274
    60
    def init(text_area: JEditTextArea): Completion_Popup.Text_Area =
wenzelm@53274
    61
    {
wenzelm@53274
    62
      exit(text_area)
wenzelm@53274
    63
      val text_area_completion = new Text_Area(text_area)
wenzelm@53274
    64
      text_area.putClientProperty(key, text_area_completion)
wenzelm@53274
    65
      text_area_completion.activate()
wenzelm@53274
    66
      text_area_completion
wenzelm@53274
    67
    }
wenzelm@53274
    68
wenzelm@55712
    69
    def dismissed(text_area: TextArea): Boolean =
wenzelm@53274
    70
    {
wenzelm@53274
    71
      Swing_Thread.require()
wenzelm@53274
    72
      apply(text_area) match {
wenzelm@53274
    73
        case Some(text_area_completion) => text_area_completion.dismissed()
wenzelm@53274
    74
        case None => false
wenzelm@53274
    75
      }
wenzelm@53274
    76
    }
wenzelm@53272
    77
  }
wenzelm@53272
    78
wenzelm@53274
    79
  class Text_Area private(text_area: JEditTextArea)
wenzelm@53272
    80
  {
wenzelm@55711
    81
    // owned by Swing thread
wenzelm@53272
    82
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53272
    83
wenzelm@55711
    84
    def active_range: Option[Text.Range] =
wenzelm@55711
    85
      completion_popup match {
wenzelm@55711
    86
        case Some(completion) =>
wenzelm@55711
    87
          completion.active_range match {
wenzelm@55711
    88
            case Some((range, _)) if completion.isDisplayable => Some(range)
wenzelm@55711
    89
            case _ => None
wenzelm@55711
    90
          }
wenzelm@55711
    91
        case None => None
wenzelm@55711
    92
      }
wenzelm@55711
    93
wenzelm@53272
    94
wenzelm@55747
    95
    /* rendering */
wenzelm@55747
    96
wenzelm@55747
    97
    def rendering(rendering: Rendering, line_range: Text.Range): Option[Text.Info[Color]] =
wenzelm@55747
    98
    {
wenzelm@55747
    99
      active_range match {
wenzelm@55747
   100
        case Some(range) => range.try_restrict(line_range)
wenzelm@55747
   101
        case None =>
wenzelm@55747
   102
          val buffer = text_area.getBuffer
wenzelm@55747
   103
          val caret = text_area.getCaretPosition
wenzelm@55747
   104
wenzelm@55747
   105
          if (line_range.contains(caret)) {
wenzelm@55747
   106
            JEdit_Lib.stretch_point_range(buffer, caret).try_restrict(line_range) match {
wenzelm@55747
   107
              case Some(range) if !range.is_singularity =>
wenzelm@55747
   108
                rendering.completion_names(range) match {
wenzelm@55747
   109
                  case Some(names) => Some(names.range)
wenzelm@55747
   110
                  case None =>
wenzelm@55752
   111
                    syntax_completion(false, Some(rendering)) match {
wenzelm@55747
   112
                      case Some(result) => Some(result.range)
wenzelm@55747
   113
                      case None => None
wenzelm@55747
   114
                    }
wenzelm@55747
   115
                }
wenzelm@55747
   116
              case _ => None
wenzelm@55747
   117
            }
wenzelm@55747
   118
          }
wenzelm@55747
   119
          else None
wenzelm@55747
   120
      }
wenzelm@55747
   121
    }.map(range => Text.Info(range, rendering.completion_color))
wenzelm@55747
   122
wenzelm@55747
   123
wenzelm@55747
   124
    /* syntax completion */
wenzelm@55747
   125
wenzelm@55747
   126
    def syntax_completion(
wenzelm@55752
   127
      explicit: Boolean, opt_rendering: Option[Rendering] = None): Option[Completion.Result] =
wenzelm@55747
   128
    {
wenzelm@55747
   129
      val buffer = text_area.getBuffer
wenzelm@55747
   130
      val history = PIDE.completion_history.value
wenzelm@55747
   131
      val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@55747
   132
wenzelm@55747
   133
      Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match {
wenzelm@55747
   134
        case Some(syntax) =>
wenzelm@55747
   135
          val caret = text_area.getCaretPosition
wenzelm@55747
   136
          val line = buffer.getLineOfOffset(caret)
wenzelm@55747
   137
          val start = buffer.getLineStartOffset(line)
wenzelm@55747
   138
          val text = buffer.getSegment(start, caret - start)
wenzelm@55747
   139
wenzelm@55748
   140
          val word_context =
wenzelm@55748
   141
            Completion.word_context(
wenzelm@55748
   142
              JEdit_Lib.try_get_text(buffer, JEdit_Lib.point_range(buffer, caret)))
wenzelm@55748
   143
wenzelm@55747
   144
          val context =
wenzelm@55747
   145
            (opt_rendering orElse PIDE.document_view(text_area).map(_.get_rendering()) match {
wenzelm@55747
   146
              case Some(rendering) =>
wenzelm@55749
   147
                rendering.language_context(JEdit_Lib.stretch_point_range(buffer, caret))
wenzelm@55747
   148
              case None => None
wenzelm@55749
   149
            }) getOrElse syntax.language_context
wenzelm@55747
   150
wenzelm@55748
   151
          syntax.completion.complete(history, decode, explicit, start, text, word_context, context)
wenzelm@55747
   152
wenzelm@55747
   153
        case None => None
wenzelm@55747
   154
      }
wenzelm@55747
   155
    }
wenzelm@55747
   156
wenzelm@55747
   157
wenzelm@53292
   158
    /* completion action */
wenzelm@53273
   159
wenzelm@53275
   160
    private def insert(item: Completion.Item)
wenzelm@53242
   161
    {
wenzelm@53242
   162
      Swing_Thread.require()
wenzelm@53023
   163
wenzelm@53242
   164
      val buffer = text_area.getBuffer
wenzelm@55692
   165
      val range = item.range
wenzelm@55692
   166
      if (buffer.isEditable && range.length > 0) {
wenzelm@53242
   167
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@55692
   168
          JEdit_Lib.try_get_text(buffer, range) match {
wenzelm@53242
   169
            case Some(text) if text == item.original =>
wenzelm@55692
   170
              buffer.remove(range.start, range.length)
wenzelm@55692
   171
              buffer.insert(range.start, item.replacement)
wenzelm@55692
   172
              text_area.moveCaretPosition(range.start + item.replacement.length + item.move)
wenzelm@53242
   173
            case _ =>
wenzelm@53242
   174
          }
wenzelm@53233
   175
        }
wenzelm@53233
   176
      }
wenzelm@53233
   177
    }
wenzelm@53242
   178
wenzelm@53322
   179
    def action(immediate: Boolean = false, explicit: Boolean = false)
wenzelm@53292
   180
    {
wenzelm@53292
   181
      val view = text_area.getView
wenzelm@53292
   182
      val layered = view.getLayeredPane
wenzelm@53292
   183
      val buffer = text_area.getBuffer
wenzelm@53292
   184
      val painter = text_area.getPainter
wenzelm@55693
   185
      val caret = text_area.getCaretPosition
wenzelm@53273
   186
wenzelm@55693
   187
      val history = PIDE.completion_history.value
wenzelm@55693
   188
      val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@55693
   189
wenzelm@55693
   190
      def open_popup(result: Completion.Result)
wenzelm@55693
   191
      {
wenzelm@55693
   192
        val font =
wenzelm@55693
   193
          painter.getFont.deriveFont(Rendering.font_size("jedit_popup_font_scale"))
wenzelm@55693
   194
wenzelm@55711
   195
        val range = result.range
wenzelm@55711
   196
        def invalidate(): Unit = JEdit_Lib.invalidate_range(text_area, range)
wenzelm@55711
   197
wenzelm@55711
   198
        val loc1 = text_area.offsetToXY(range.start)
wenzelm@55693
   199
        if (loc1 != null) {
wenzelm@55693
   200
          val loc2 =
wenzelm@55693
   201
            SwingUtilities.convertPoint(painter,
wenzelm@55693
   202
              loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
wenzelm@55693
   203
wenzelm@55693
   204
          val completion =
wenzelm@55711
   205
            new Completion_Popup(Some((range, invalidate _)), layered, loc2, font, result.items) {
wenzelm@55693
   206
              override def complete(item: Completion.Item) {
wenzelm@55693
   207
                PIDE.completion_history.update(item)
wenzelm@55693
   208
                insert(item)
wenzelm@55693
   209
              }
wenzelm@55693
   210
              override def propagate(evt: KeyEvent) {
wenzelm@55693
   211
                JEdit_Lib.propagate_key(view, evt)
wenzelm@55693
   212
                input(evt)
wenzelm@55693
   213
              }
wenzelm@55693
   214
              override def refocus() { text_area.requestFocus }
wenzelm@55693
   215
            }
wenzelm@55693
   216
          completion_popup = Some(completion)
wenzelm@55711
   217
          invalidate()
wenzelm@55693
   218
          completion.show_popup()
wenzelm@55693
   219
        }
wenzelm@55693
   220
      }
wenzelm@55693
   221
wenzelm@55752
   222
      def semantic_completion(): Option[Completion.Result] =
wenzelm@55752
   223
        if (explicit) {
wenzelm@55693
   224
          PIDE.document_view(text_area) match {
wenzelm@55693
   225
            case Some(doc_view) =>
wenzelm@55693
   226
              val rendering = doc_view.get_rendering()
wenzelm@55693
   227
              rendering.completion_names(JEdit_Lib.stretch_point_range(buffer, caret)) match {
wenzelm@55752
   228
                case None => None
wenzelm@55693
   229
                case Some(names) =>
wenzelm@55693
   230
                  JEdit_Lib.try_get_text(buffer, names.range) match {
wenzelm@55752
   231
                    case Some(original) => names.complete(history, decode, original)
wenzelm@55752
   232
                    case None => None
wenzelm@55693
   233
                  }
wenzelm@55693
   234
              }
wenzelm@55752
   235
            case None => None
wenzelm@55693
   236
          }
wenzelm@55693
   237
        }
wenzelm@55752
   238
        else None
wenzelm@55693
   239
wenzelm@55752
   240
      if (buffer.isEditable) {
wenzelm@55752
   241
        semantic_completion() orElse syntax_completion(explicit) match {
wenzelm@55747
   242
          case Some(result) =>
wenzelm@55747
   243
            result.items match {
wenzelm@55747
   244
              case List(item) if result.unique && item.immediate && immediate =>
wenzelm@55752
   245
                insert(item)
wenzelm@55752
   246
              case _ :: _ =>
wenzelm@55752
   247
                open_popup(result)
wenzelm@55752
   248
              case _ =>
wenzelm@53292
   249
            }
wenzelm@55752
   250
          case None =>
wenzelm@53292
   251
        }
wenzelm@55752
   252
      }
wenzelm@53292
   253
    }
wenzelm@53292
   254
wenzelm@53292
   255
wenzelm@53292
   256
    /* input key events */
wenzelm@53273
   257
wenzelm@53272
   258
    def input(evt: KeyEvent)
wenzelm@53242
   259
    {
wenzelm@53242
   260
      Swing_Thread.require()
wenzelm@53233
   261
wenzelm@53273
   262
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   263
        if (!evt.isConsumed) {
wenzelm@53273
   264
          dismissed()
wenzelm@53397
   265
          if (evt.getKeyChar != '\b') {
wenzelm@53784
   266
            val special = JEdit_Lib.special_key(evt)
wenzelm@53397
   267
            if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53397
   268
              input_delay.revoke()
wenzelm@53397
   269
              action(immediate = PIDE.options.bool("jedit_completion_immediate"))
wenzelm@53397
   270
            }
wenzelm@53397
   271
            else input_delay.invoke()
wenzelm@53292
   272
          }
wenzelm@53242
   273
        }
wenzelm@53228
   274
      }
wenzelm@53228
   275
    }
wenzelm@53273
   276
wenzelm@53273
   277
    private val input_delay =
wenzelm@53292
   278
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53322
   279
        action()
wenzelm@53292
   280
      }
wenzelm@53273
   281
wenzelm@53273
   282
wenzelm@53292
   283
    /* dismiss popup */
wenzelm@53273
   284
wenzelm@53292
   285
    def dismissed(): Boolean =
wenzelm@53292
   286
    {
wenzelm@53292
   287
      Swing_Thread.require()
wenzelm@53273
   288
wenzelm@53292
   289
      completion_popup match {
wenzelm@53292
   290
        case Some(completion) =>
wenzelm@53292
   291
          completion.hide_popup()
wenzelm@53292
   292
          completion_popup = None
wenzelm@53292
   293
          true
wenzelm@53292
   294
        case None =>
wenzelm@53292
   295
          false
wenzelm@53273
   296
      }
wenzelm@53292
   297
    }
wenzelm@53274
   298
wenzelm@53274
   299
wenzelm@53274
   300
    /* activation */
wenzelm@53274
   301
wenzelm@53274
   302
    private val outer_key_listener =
wenzelm@53274
   303
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   304
wenzelm@53274
   305
    private def activate()
wenzelm@53274
   306
    {
wenzelm@53274
   307
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   308
    }
wenzelm@53274
   309
wenzelm@53274
   310
    private def deactivate()
wenzelm@53274
   311
    {
wenzelm@53274
   312
      dismissed()
wenzelm@53274
   313
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   314
    }
wenzelm@53228
   315
  }
wenzelm@53784
   316
wenzelm@53784
   317
wenzelm@53784
   318
wenzelm@53784
   319
  /** history text field **/
wenzelm@53784
   320
wenzelm@53784
   321
  class History_Text_Field(
wenzelm@53784
   322
    name: String = null,
wenzelm@53784
   323
    instant_popups: Boolean = false,
wenzelm@53784
   324
    enter_adds_to_history: Boolean = true,
wenzelm@53784
   325
    syntax: Outer_Syntax = Outer_Syntax.init) extends
wenzelm@53784
   326
    HistoryTextField(name, instant_popups, enter_adds_to_history)
wenzelm@53784
   327
  {
wenzelm@53784
   328
    text_field =>
wenzelm@53784
   329
wenzelm@53848
   330
    // see https://forums.oracle.com/thread/1361677
wenzelm@53848
   331
    if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret)
wenzelm@53784
   332
wenzelm@55711
   333
    // owned by Swing thread
wenzelm@53784
   334
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53784
   335
wenzelm@53784
   336
wenzelm@53784
   337
    /* dismiss */
wenzelm@53784
   338
wenzelm@53784
   339
    private def dismissed(): Boolean =
wenzelm@53784
   340
    {
wenzelm@53784
   341
      completion_popup match {
wenzelm@53784
   342
        case Some(completion) =>
wenzelm@53784
   343
          completion.hide_popup()
wenzelm@53784
   344
          completion_popup = None
wenzelm@53784
   345
          true
wenzelm@53784
   346
        case None =>
wenzelm@53784
   347
          false
wenzelm@53784
   348
      }
wenzelm@53784
   349
    }
wenzelm@53784
   350
wenzelm@53784
   351
wenzelm@53784
   352
    /* insert */
wenzelm@53784
   353
wenzelm@53784
   354
    private def insert(item: Completion.Item)
wenzelm@53784
   355
    {
wenzelm@53784
   356
      Swing_Thread.require()
wenzelm@53784
   357
wenzelm@55692
   358
      val range = item.range
wenzelm@55692
   359
      if (text_field.isEditable && range.length > 0) {
wenzelm@53784
   360
        val content = text_field.getText
wenzelm@55692
   361
        JEdit_Lib.try_get_text(content, range) match {
wenzelm@53784
   362
          case Some(text) if text == item.original =>
wenzelm@53784
   363
            text_field.setText(
wenzelm@55692
   364
              content.substring(0, range.start) +
wenzelm@53784
   365
              item.replacement +
wenzelm@55692
   366
              content.substring(range.stop))
wenzelm@55692
   367
            text_field.getCaret.setDot(range.start + item.replacement.length + item.move)
wenzelm@53784
   368
          case _ =>
wenzelm@53784
   369
        }
wenzelm@53784
   370
      }
wenzelm@53784
   371
    }
wenzelm@53784
   372
wenzelm@53784
   373
wenzelm@53784
   374
    /* completion action */
wenzelm@53784
   375
wenzelm@53784
   376
    def action()
wenzelm@53784
   377
    {
wenzelm@53784
   378
      GUI.layered_pane(text_field) match {
wenzelm@53784
   379
        case Some(layered) if text_field.isEditable =>
wenzelm@53784
   380
          val history = PIDE.completion_history.value
wenzelm@53784
   381
wenzelm@53784
   382
          val caret = text_field.getCaret.getDot
wenzelm@53784
   383
          val text = text_field.getText.substring(0, caret)
wenzelm@53784
   384
wenzelm@55748
   385
          val word_context =
wenzelm@55748
   386
            Completion.word_context(JEdit_Lib.try_get_text(text_field.getText,
wenzelm@55748
   387
              Text.Range(caret, caret + 1)))  // FIXME proper point range!?
wenzelm@55748
   388
wenzelm@55749
   389
          val context = syntax.language_context
wenzelm@55748
   390
wenzelm@55748
   391
          syntax.completion.complete(history, true, false, 0, text, word_context, context) match {
wenzelm@53784
   392
            case Some(result) =>
wenzelm@53784
   393
              val fm = text_field.getFontMetrics(text_field.getFont)
wenzelm@53784
   394
              val loc =
wenzelm@53784
   395
                SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered)
wenzelm@53784
   396
wenzelm@55711
   397
              val completion =
wenzelm@55711
   398
                new Completion_Popup(None, layered, loc, text_field.getFont, result.items)
wenzelm@53784
   399
              {
wenzelm@53784
   400
                override def complete(item: Completion.Item) {
wenzelm@53784
   401
                  PIDE.completion_history.update(item)
wenzelm@53784
   402
                  insert(item)
wenzelm@53784
   403
                }
wenzelm@53784
   404
                override def propagate(evt: KeyEvent) {
wenzelm@53784
   405
                  if (!evt.isConsumed) text_field.processKeyEvent(evt)
wenzelm@53784
   406
                }
wenzelm@53784
   407
                override def refocus() { text_field.requestFocus }
wenzelm@53784
   408
              }
wenzelm@53784
   409
              completion_popup = Some(completion)
wenzelm@53784
   410
              completion.show_popup()
wenzelm@53784
   411
wenzelm@53784
   412
            case None =>
wenzelm@53784
   413
          }
wenzelm@53784
   414
        case _ =>
wenzelm@53784
   415
      }
wenzelm@53784
   416
    }
wenzelm@53784
   417
wenzelm@53784
   418
wenzelm@53784
   419
    /* process key event */
wenzelm@53784
   420
wenzelm@53784
   421
    private def process(evt: KeyEvent)
wenzelm@53784
   422
    {
wenzelm@53784
   423
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53784
   424
        dismissed()
wenzelm@53784
   425
        if (evt.getKeyChar != '\b') {
wenzelm@53784
   426
          val special = JEdit_Lib.special_key(evt)
wenzelm@53784
   427
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53784
   428
            process_delay.revoke()
wenzelm@53784
   429
            action()
wenzelm@53784
   430
          }
wenzelm@53784
   431
          else process_delay.invoke()
wenzelm@53784
   432
        }
wenzelm@53784
   433
      }
wenzelm@53784
   434
    }
wenzelm@53784
   435
wenzelm@53784
   436
    private val process_delay =
wenzelm@53784
   437
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53784
   438
        action()
wenzelm@53784
   439
      }
wenzelm@53784
   440
wenzelm@53784
   441
    override def processKeyEvent(evt0: KeyEvent)
wenzelm@53784
   442
    {
wenzelm@53784
   443
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53784
   444
      if (evt != null) {
wenzelm@53784
   445
        evt.getID match {
wenzelm@53784
   446
          case KeyEvent.KEY_PRESSED =>
wenzelm@53784
   447
            val key_code = evt.getKeyCode
wenzelm@53784
   448
            if (key_code == KeyEvent.VK_ESCAPE) {
wenzelm@53784
   449
              if (dismissed()) evt.consume
wenzelm@53784
   450
            }
wenzelm@53784
   451
          case KeyEvent.KEY_TYPED =>
wenzelm@53784
   452
            super.processKeyEvent(evt)
wenzelm@53784
   453
            process(evt)
wenzelm@53784
   454
            evt.consume
wenzelm@53784
   455
          case _ =>
wenzelm@53784
   456
        }
wenzelm@53784
   457
        if (!evt.isConsumed) super.processKeyEvent(evt)
wenzelm@53784
   458
      }
wenzelm@53784
   459
    }
wenzelm@53784
   460
  }
wenzelm@53023
   461
}
wenzelm@53023
   462
wenzelm@53233
   463
wenzelm@53023
   464
class Completion_Popup private(
wenzelm@55711
   465
  val active_range: Option[(Text.Range, () => Unit)],
wenzelm@53246
   466
  layered: JLayeredPane,
wenzelm@53246
   467
  location: Point,
wenzelm@53272
   468
  font: Font,
wenzelm@53275
   469
  items: List[Completion.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   470
{
wenzelm@53023
   471
  completion =>
wenzelm@53023
   472
wenzelm@53023
   473
  Swing_Thread.require()
wenzelm@53405
   474
wenzelm@53232
   475
  require(!items.isEmpty)
wenzelm@53405
   476
  val multi = items.length > 1
wenzelm@53232
   477
wenzelm@53232
   478
wenzelm@53232
   479
  /* actions */
wenzelm@53232
   480
wenzelm@53275
   481
  def complete(item: Completion.Item) { }
wenzelm@53232
   482
  def propagate(evt: KeyEvent) { }
wenzelm@53234
   483
  def refocus() { }
wenzelm@53023
   484
wenzelm@53023
   485
wenzelm@53023
   486
  /* list view */
wenzelm@53023
   487
wenzelm@53023
   488
  private val list_view = new ListView(items)
wenzelm@53272
   489
  list_view.font = font
wenzelm@53023
   490
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   491
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   492
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   493
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   494
wenzelm@53398
   495
  for (cond <-
wenzelm@53398
   496
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   497
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   498
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   499
wenzelm@53023
   500
  private def complete_selected(): Boolean =
wenzelm@53023
   501
  {
wenzelm@53023
   502
    list_view.selection.items.toList match {
wenzelm@53023
   503
      case item :: _ => complete(item); true
wenzelm@53023
   504
      case _ => false
wenzelm@53023
   505
    }
wenzelm@53023
   506
  }
wenzelm@53023
   507
wenzelm@53023
   508
  private def move_items(n: Int)
wenzelm@53023
   509
  {
wenzelm@53023
   510
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   511
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   512
    if (i != j) {
wenzelm@53023
   513
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   514
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   515
    }
wenzelm@53023
   516
  }
wenzelm@53023
   517
wenzelm@53023
   518
  private def move_pages(n: Int)
wenzelm@53023
   519
  {
wenzelm@53023
   520
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   521
    move_items(page_size * n)
wenzelm@53023
   522
  }
wenzelm@53023
   523
wenzelm@53023
   524
wenzelm@53023
   525
  /* event handling */
wenzelm@53023
   526
wenzelm@53274
   527
  private val inner_key_listener =
wenzelm@53226
   528
    JEdit_Lib.key_listener(
wenzelm@53226
   529
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   530
        {
wenzelm@53226
   531
          if (!e.isConsumed) {
wenzelm@53226
   532
            e.getKeyCode match {
wenzelm@53226
   533
              case KeyEvent.VK_TAB =>
wenzelm@53226
   534
                if (complete_selected()) e.consume
wenzelm@53226
   535
                hide_popup()
wenzelm@53226
   536
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   537
                hide_popup()
wenzelm@53226
   538
                e.consume
wenzelm@53405
   539
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   540
                move_items(-1)
wenzelm@53407
   541
                e.consume
wenzelm@53405
   542
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   543
                move_items(1)
wenzelm@53407
   544
                e.consume
wenzelm@53405
   545
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   546
                move_pages(-1)
wenzelm@53407
   547
                e.consume
wenzelm@53405
   548
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   549
                move_pages(1)
wenzelm@53407
   550
                e.consume
wenzelm@53226
   551
              case _ =>
wenzelm@53226
   552
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   553
                  hide_popup()
wenzelm@53226
   554
            }
wenzelm@53226
   555
          }
wenzelm@53232
   556
          propagate(e)
wenzelm@53226
   557
        },
wenzelm@53232
   558
      key_typed = propagate _
wenzelm@53226
   559
    )
wenzelm@53023
   560
wenzelm@53274
   561
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   562
wenzelm@53023
   563
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   564
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   565
      if (complete_selected()) e.consume
wenzelm@53023
   566
      hide_popup()
wenzelm@53023
   567
    }
wenzelm@53023
   568
  })
wenzelm@53023
   569
wenzelm@53023
   570
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   571
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   572
  })
wenzelm@53023
   573
wenzelm@53023
   574
wenzelm@53023
   575
  /* main content */
wenzelm@53023
   576
wenzelm@53023
   577
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   578
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   579
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   580
wenzelm@53023
   581
wenzelm@53023
   582
  /* popup */
wenzelm@53023
   583
wenzelm@53023
   584
  private val popup =
wenzelm@53023
   585
  {
wenzelm@53247
   586
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   587
    val size =
wenzelm@53230
   588
    {
wenzelm@53230
   589
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   590
      val bounds = Rendering.popup_bounds
wenzelm@53247
   591
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   592
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   593
      new Dimension(w, h)
wenzelm@53230
   594
    }
wenzelm@53247
   595
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   596
  }
wenzelm@53246
   597
wenzelm@53246
   598
  private def show_popup()
wenzelm@53246
   599
  {
wenzelm@53246
   600
    popup.show
wenzelm@53246
   601
    list_view.requestFocus
wenzelm@53023
   602
  }
wenzelm@53023
   603
wenzelm@54377
   604
  private val hide_popup_delay =
wenzelm@54377
   605
    Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_dismiss_delay")) {
wenzelm@55711
   606
      active_range match { case Some((_, invalidate)) => invalidate() case _ => }
wenzelm@54377
   607
      popup.hide
wenzelm@54377
   608
    }
wenzelm@54377
   609
wenzelm@53232
   610
  private def hide_popup()
wenzelm@53023
   611
  {
wenzelm@54376
   612
    if (list_view.peer.isFocusOwner) refocus()
wenzelm@54377
   613
wenzelm@55711
   614
    if (PIDE.options.seconds("jedit_completion_dismiss_delay").is_zero) {
wenzelm@55711
   615
      active_range match { case Some((_, invalidate)) => invalidate() case _ => }
wenzelm@54377
   616
      popup.hide
wenzelm@55711
   617
    }
wenzelm@54377
   618
    else hide_popup_delay.invoke()
wenzelm@53023
   619
  }
wenzelm@53023
   620
}
wenzelm@53023
   621