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