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