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