src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Tue Feb 25 20:46:09 2014 +0100 (2014-02-25 ago)
changeset 55748 2e1398b484aa
parent 55747 bef19c929ba5
child 55749 75a48dc4383e
permissions -rw-r--r--
no word completion within word context;
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@55747
   111
                    syntax_completion(Some(rendering), false) 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@55747
   127
      opt_rendering: Option[Rendering], explicit: Boolean): 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@55747
   147
                rendering.completion_context(JEdit_Lib.stretch_point_range(buffer, caret))
wenzelm@55747
   148
              case None => None
wenzelm@55747
   149
            }) getOrElse syntax.completion_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@55693
   222
      def semantic_completion(): Boolean =
wenzelm@55693
   223
        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@55693
   228
                case None => false
wenzelm@55693
   229
                case Some(names) =>
wenzelm@55693
   230
                  JEdit_Lib.try_get_text(buffer, names.range) match {
wenzelm@55693
   231
                    case Some(original) =>
wenzelm@55693
   232
                      names.complete(history, decode, original) match {
wenzelm@55693
   233
                        case Some(result) if !result.items.isEmpty =>
wenzelm@55693
   234
                          open_popup(result)
wenzelm@55693
   235
                          true
wenzelm@55693
   236
                        case _ => false
wenzelm@55693
   237
                      }
wenzelm@55693
   238
                    case None => false
wenzelm@55693
   239
                  }
wenzelm@55693
   240
              }
wenzelm@55693
   241
            case _ => false
wenzelm@55693
   242
          }
wenzelm@55693
   243
        }
wenzelm@55693
   244
wenzelm@55747
   245
      def syntactic_completion(): Boolean =
wenzelm@55747
   246
        syntax_completion(None, explicit) match {
wenzelm@55747
   247
          case Some(result) =>
wenzelm@55747
   248
            result.items match {
wenzelm@55747
   249
              case List(item) if result.unique && item.immediate && immediate =>
wenzelm@55747
   250
                insert(item); true
wenzelm@55747
   251
              case _ :: _ => open_popup(result); true
wenzelm@55747
   252
              case _ => false
wenzelm@53292
   253
            }
wenzelm@55693
   254
          case None => false
wenzelm@53292
   255
        }
wenzelm@55693
   256
wenzelm@55693
   257
      if (buffer.isEditable)
wenzelm@55747
   258
        semantic_completion() || syntactic_completion()
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@53784
   389
          val text = text_field.getText.substring(0, caret)
wenzelm@53784
   390
wenzelm@55748
   391
          val word_context =
wenzelm@55748
   392
            Completion.word_context(JEdit_Lib.try_get_text(text_field.getText,
wenzelm@55748
   393
              Text.Range(caret, caret + 1)))  // FIXME proper point range!?
wenzelm@55748
   394
wenzelm@55748
   395
          val context = syntax.completion_context
wenzelm@55748
   396
wenzelm@55748
   397
          syntax.completion.complete(history, true, false, 0, text, word_context, context) match {
wenzelm@53784
   398
            case Some(result) =>
wenzelm@53784
   399
              val fm = text_field.getFontMetrics(text_field.getFont)
wenzelm@53784
   400
              val loc =
wenzelm@53784
   401
                SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered)
wenzelm@53784
   402
wenzelm@55711
   403
              val completion =
wenzelm@55711
   404
                new Completion_Popup(None, layered, loc, text_field.getFont, result.items)
wenzelm@53784
   405
              {
wenzelm@53784
   406
                override def complete(item: Completion.Item) {
wenzelm@53784
   407
                  PIDE.completion_history.update(item)
wenzelm@53784
   408
                  insert(item)
wenzelm@53784
   409
                }
wenzelm@53784
   410
                override def propagate(evt: KeyEvent) {
wenzelm@53784
   411
                  if (!evt.isConsumed) text_field.processKeyEvent(evt)
wenzelm@53784
   412
                }
wenzelm@53784
   413
                override def refocus() { text_field.requestFocus }
wenzelm@53784
   414
              }
wenzelm@53784
   415
              completion_popup = Some(completion)
wenzelm@53784
   416
              completion.show_popup()
wenzelm@53784
   417
wenzelm@53784
   418
            case None =>
wenzelm@53784
   419
          }
wenzelm@53784
   420
        case _ =>
wenzelm@53784
   421
      }
wenzelm@53784
   422
    }
wenzelm@53784
   423
wenzelm@53784
   424
wenzelm@53784
   425
    /* process key event */
wenzelm@53784
   426
wenzelm@53784
   427
    private def process(evt: KeyEvent)
wenzelm@53784
   428
    {
wenzelm@53784
   429
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53784
   430
        dismissed()
wenzelm@53784
   431
        if (evt.getKeyChar != '\b') {
wenzelm@53784
   432
          val special = JEdit_Lib.special_key(evt)
wenzelm@53784
   433
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53784
   434
            process_delay.revoke()
wenzelm@53784
   435
            action()
wenzelm@53784
   436
          }
wenzelm@53784
   437
          else process_delay.invoke()
wenzelm@53784
   438
        }
wenzelm@53784
   439
      }
wenzelm@53784
   440
    }
wenzelm@53784
   441
wenzelm@53784
   442
    private val process_delay =
wenzelm@53784
   443
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53784
   444
        action()
wenzelm@53784
   445
      }
wenzelm@53784
   446
wenzelm@53784
   447
    override def processKeyEvent(evt0: KeyEvent)
wenzelm@53784
   448
    {
wenzelm@53784
   449
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53784
   450
      if (evt != null) {
wenzelm@53784
   451
        evt.getID match {
wenzelm@53784
   452
          case KeyEvent.KEY_PRESSED =>
wenzelm@53784
   453
            val key_code = evt.getKeyCode
wenzelm@53784
   454
            if (key_code == KeyEvent.VK_ESCAPE) {
wenzelm@53784
   455
              if (dismissed()) evt.consume
wenzelm@53784
   456
            }
wenzelm@53784
   457
          case KeyEvent.KEY_TYPED =>
wenzelm@53784
   458
            super.processKeyEvent(evt)
wenzelm@53784
   459
            process(evt)
wenzelm@53784
   460
            evt.consume
wenzelm@53784
   461
          case _ =>
wenzelm@53784
   462
        }
wenzelm@53784
   463
        if (!evt.isConsumed) super.processKeyEvent(evt)
wenzelm@53784
   464
      }
wenzelm@53784
   465
    }
wenzelm@53784
   466
  }
wenzelm@53023
   467
}
wenzelm@53023
   468
wenzelm@53233
   469
wenzelm@53023
   470
class Completion_Popup private(
wenzelm@55711
   471
  val active_range: Option[(Text.Range, () => Unit)],
wenzelm@53246
   472
  layered: JLayeredPane,
wenzelm@53246
   473
  location: Point,
wenzelm@53272
   474
  font: Font,
wenzelm@53275
   475
  items: List[Completion.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   476
{
wenzelm@53023
   477
  completion =>
wenzelm@53023
   478
wenzelm@53023
   479
  Swing_Thread.require()
wenzelm@53405
   480
wenzelm@53232
   481
  require(!items.isEmpty)
wenzelm@53405
   482
  val multi = items.length > 1
wenzelm@53232
   483
wenzelm@53232
   484
wenzelm@53232
   485
  /* actions */
wenzelm@53232
   486
wenzelm@53275
   487
  def complete(item: Completion.Item) { }
wenzelm@53232
   488
  def propagate(evt: KeyEvent) { }
wenzelm@53234
   489
  def refocus() { }
wenzelm@53023
   490
wenzelm@53023
   491
wenzelm@53023
   492
  /* list view */
wenzelm@53023
   493
wenzelm@53023
   494
  private val list_view = new ListView(items)
wenzelm@53272
   495
  list_view.font = font
wenzelm@53023
   496
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   497
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   498
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   499
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   500
wenzelm@53398
   501
  for (cond <-
wenzelm@53398
   502
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   503
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   504
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   505
wenzelm@53023
   506
  private def complete_selected(): Boolean =
wenzelm@53023
   507
  {
wenzelm@53023
   508
    list_view.selection.items.toList match {
wenzelm@53023
   509
      case item :: _ => complete(item); true
wenzelm@53023
   510
      case _ => false
wenzelm@53023
   511
    }
wenzelm@53023
   512
  }
wenzelm@53023
   513
wenzelm@53023
   514
  private def move_items(n: Int)
wenzelm@53023
   515
  {
wenzelm@53023
   516
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   517
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   518
    if (i != j) {
wenzelm@53023
   519
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   520
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   521
    }
wenzelm@53023
   522
  }
wenzelm@53023
   523
wenzelm@53023
   524
  private def move_pages(n: Int)
wenzelm@53023
   525
  {
wenzelm@53023
   526
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   527
    move_items(page_size * n)
wenzelm@53023
   528
  }
wenzelm@53023
   529
wenzelm@53023
   530
wenzelm@53023
   531
  /* event handling */
wenzelm@53023
   532
wenzelm@53274
   533
  private val inner_key_listener =
wenzelm@53226
   534
    JEdit_Lib.key_listener(
wenzelm@53226
   535
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   536
        {
wenzelm@53226
   537
          if (!e.isConsumed) {
wenzelm@53226
   538
            e.getKeyCode match {
wenzelm@53226
   539
              case KeyEvent.VK_TAB =>
wenzelm@53226
   540
                if (complete_selected()) e.consume
wenzelm@53226
   541
                hide_popup()
wenzelm@53226
   542
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   543
                hide_popup()
wenzelm@53226
   544
                e.consume
wenzelm@53405
   545
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   546
                move_items(-1)
wenzelm@53407
   547
                e.consume
wenzelm@53405
   548
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   549
                move_items(1)
wenzelm@53407
   550
                e.consume
wenzelm@53405
   551
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   552
                move_pages(-1)
wenzelm@53407
   553
                e.consume
wenzelm@53405
   554
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   555
                move_pages(1)
wenzelm@53407
   556
                e.consume
wenzelm@53226
   557
              case _ =>
wenzelm@53226
   558
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   559
                  hide_popup()
wenzelm@53226
   560
            }
wenzelm@53226
   561
          }
wenzelm@53232
   562
          propagate(e)
wenzelm@53226
   563
        },
wenzelm@53232
   564
      key_typed = propagate _
wenzelm@53226
   565
    )
wenzelm@53023
   566
wenzelm@53274
   567
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   568
wenzelm@53023
   569
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   570
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   571
      if (complete_selected()) e.consume
wenzelm@53023
   572
      hide_popup()
wenzelm@53023
   573
    }
wenzelm@53023
   574
  })
wenzelm@53023
   575
wenzelm@53023
   576
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   577
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   578
  })
wenzelm@53023
   579
wenzelm@53023
   580
wenzelm@53023
   581
  /* main content */
wenzelm@53023
   582
wenzelm@53023
   583
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   584
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   585
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   586
wenzelm@53023
   587
wenzelm@53023
   588
  /* popup */
wenzelm@53023
   589
wenzelm@53023
   590
  private val popup =
wenzelm@53023
   591
  {
wenzelm@53247
   592
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   593
    val size =
wenzelm@53230
   594
    {
wenzelm@53230
   595
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   596
      val bounds = Rendering.popup_bounds
wenzelm@53247
   597
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   598
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   599
      new Dimension(w, h)
wenzelm@53230
   600
    }
wenzelm@53247
   601
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   602
  }
wenzelm@53246
   603
wenzelm@53246
   604
  private def show_popup()
wenzelm@53246
   605
  {
wenzelm@53246
   606
    popup.show
wenzelm@53246
   607
    list_view.requestFocus
wenzelm@53023
   608
  }
wenzelm@53023
   609
wenzelm@54377
   610
  private val hide_popup_delay =
wenzelm@54377
   611
    Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_dismiss_delay")) {
wenzelm@55711
   612
      active_range match { case Some((_, invalidate)) => invalidate() case _ => }
wenzelm@54377
   613
      popup.hide
wenzelm@54377
   614
    }
wenzelm@54377
   615
wenzelm@53232
   616
  private def hide_popup()
wenzelm@53023
   617
  {
wenzelm@54376
   618
    if (list_view.peer.isFocusOwner) refocus()
wenzelm@54377
   619
wenzelm@55711
   620
    if (PIDE.options.seconds("jedit_completion_dismiss_delay").is_zero) {
wenzelm@55711
   621
      active_range match { case Some((_, invalidate)) => invalidate() case _ => }
wenzelm@54377
   622
      popup.hide
wenzelm@55711
   623
    }
wenzelm@54377
   624
    else hide_popup_delay.invoke()
wenzelm@53023
   625
  }
wenzelm@53023
   626
}
wenzelm@53023
   627