src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Wed Sep 04 17:40:07 2013 +0200 (2013-09-04 ago)
changeset 53407 2add7d4c85bd
parent 53405 ed2b48af04d9
child 53784 322a3ff42b33
permissions -rw-r--r--
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@53296
    13
import java.awt.event.{InputEvent, KeyEvent, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent}
wenzelm@53246
    14
import javax.swing.{JPanel, JComponent, JLayeredPane, SwingUtilities}
wenzelm@53297
    15
import javax.swing.border.LineBorder
wenzelm@53023
    16
wenzelm@53023
    17
import scala.swing.{ListView, ScrollPane}
wenzelm@53023
    18
import scala.swing.event.MouseClicked
wenzelm@53023
    19
wenzelm@53296
    20
import org.gjt.sp.jedit.{View, Debug}
wenzelm@53228
    21
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@53023
    22
wenzelm@53023
    23
wenzelm@53023
    24
object Completion_Popup
wenzelm@53023
    25
{
wenzelm@53272
    26
  /* setup for jEdit text area */
wenzelm@53235
    27
wenzelm@53242
    28
  object Text_Area
wenzelm@53233
    29
  {
wenzelm@53274
    30
    private val key = new Object
wenzelm@53274
    31
wenzelm@53274
    32
    def apply(text_area: JEditTextArea): Option[Completion_Popup.Text_Area] =
wenzelm@53274
    33
      text_area.getClientProperty(key) match {
wenzelm@53274
    34
        case text_area_completion: Completion_Popup.Text_Area => Some(text_area_completion)
wenzelm@53274
    35
        case _ => None
wenzelm@53274
    36
      }
wenzelm@53274
    37
wenzelm@53274
    38
    def exit(text_area: JEditTextArea)
wenzelm@53274
    39
    {
wenzelm@53274
    40
      Swing_Thread.require()
wenzelm@53274
    41
      apply(text_area) match {
wenzelm@53274
    42
        case None =>
wenzelm@53274
    43
        case Some(text_area_completion) =>
wenzelm@53274
    44
          text_area_completion.deactivate()
wenzelm@53274
    45
          text_area.putClientProperty(key, null)
wenzelm@53274
    46
      }
wenzelm@53274
    47
    }
wenzelm@53274
    48
wenzelm@53274
    49
    def init(text_area: JEditTextArea): Completion_Popup.Text_Area =
wenzelm@53274
    50
    {
wenzelm@53274
    51
      exit(text_area)
wenzelm@53274
    52
      val text_area_completion = new Text_Area(text_area)
wenzelm@53274
    53
      text_area.putClientProperty(key, text_area_completion)
wenzelm@53274
    54
      text_area_completion.activate()
wenzelm@53274
    55
      text_area_completion
wenzelm@53274
    56
    }
wenzelm@53274
    57
wenzelm@53274
    58
    def dismissed(text_area: JEditTextArea): Boolean =
wenzelm@53274
    59
    {
wenzelm@53274
    60
      Swing_Thread.require()
wenzelm@53274
    61
      apply(text_area) match {
wenzelm@53274
    62
        case Some(text_area_completion) => text_area_completion.dismissed()
wenzelm@53274
    63
        case None => false
wenzelm@53274
    64
      }
wenzelm@53274
    65
    }
wenzelm@53272
    66
  }
wenzelm@53272
    67
wenzelm@53274
    68
  class Text_Area private(text_area: JEditTextArea)
wenzelm@53272
    69
  {
wenzelm@53272
    70
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53272
    71
wenzelm@53272
    72
wenzelm@53292
    73
    /* completion action */
wenzelm@53273
    74
wenzelm@53275
    75
    private def insert(item: Completion.Item)
wenzelm@53242
    76
    {
wenzelm@53242
    77
      Swing_Thread.require()
wenzelm@53023
    78
wenzelm@53242
    79
      val buffer = text_area.getBuffer
wenzelm@53242
    80
      val len = item.original.length
wenzelm@53242
    81
      if (buffer.isEditable && len > 0) {
wenzelm@53242
    82
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@53242
    83
          val caret = text_area.getCaretPosition
wenzelm@53242
    84
          JEdit_Lib.try_get_text(buffer, Text.Range(caret - len, caret)) match {
wenzelm@53242
    85
            case Some(text) if text == item.original =>
wenzelm@53242
    86
              buffer.remove(caret - len, len)
wenzelm@53242
    87
              buffer.insert(caret - len, item.replacement)
wenzelm@53242
    88
            case _ =>
wenzelm@53242
    89
          }
wenzelm@53233
    90
        }
wenzelm@53233
    91
      }
wenzelm@53233
    92
    }
wenzelm@53242
    93
wenzelm@53322
    94
    def action(immediate: Boolean = false, explicit: Boolean = false)
wenzelm@53292
    95
    {
wenzelm@53292
    96
      val view = text_area.getView
wenzelm@53292
    97
      val layered = view.getLayeredPane
wenzelm@53292
    98
      val buffer = text_area.getBuffer
wenzelm@53292
    99
      val painter = text_area.getPainter
wenzelm@53273
   100
wenzelm@53292
   101
      if (buffer.isEditable) {
wenzelm@53292
   102
        Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match {
wenzelm@53292
   103
          case Some(syntax) =>
wenzelm@53292
   104
            val caret = text_area.getCaretPosition
wenzelm@53292
   105
            val line = buffer.getLineOfOffset(caret)
wenzelm@53292
   106
            val start = buffer.getLineStartOffset(line)
wenzelm@53292
   107
            val text = buffer.getSegment(start, caret - start)
wenzelm@53292
   108
wenzelm@53337
   109
            val history = PIDE.completion_history.value
wenzelm@53322
   110
            val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@53337
   111
            syntax.completion.complete(history, decode, explicit, text) match {
wenzelm@53325
   112
              case Some(result) =>
wenzelm@53325
   113
                if (result.unique && result.items.head.immediate && immediate)
wenzelm@53325
   114
                  insert(result.items.head)
wenzelm@53325
   115
                else {
wenzelm@53325
   116
                  val font =
wenzelm@53325
   117
                    painter.getFont.deriveFont(Rendering.font_size("jedit_popup_font_scale"))
wenzelm@53292
   118
wenzelm@53325
   119
                  val loc1 = text_area.offsetToXY(caret - result.original.length)
wenzelm@53325
   120
                  if (loc1 != null) {
wenzelm@53325
   121
                    val loc2 =
wenzelm@53325
   122
                      SwingUtilities.convertPoint(painter,
wenzelm@53325
   123
                        loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
wenzelm@53292
   124
wenzelm@53325
   125
                    val completion =
wenzelm@53325
   126
                      new Completion_Popup(layered, loc2, font, result.items) {
wenzelm@53337
   127
                        override def complete(item: Completion.Item) {
wenzelm@53337
   128
                          PIDE.completion_history.update(item)
wenzelm@53337
   129
                          insert(item)
wenzelm@53337
   130
                        }
wenzelm@53325
   131
                        override def propagate(evt: KeyEvent) {
wenzelm@53325
   132
                          JEdit_Lib.propagate_key(view, evt)
wenzelm@53325
   133
                          input(evt)
wenzelm@53325
   134
                        }
wenzelm@53325
   135
                        override def refocus() { text_area.requestFocus }
wenzelm@53292
   136
                      }
wenzelm@53325
   137
                    completion_popup = Some(completion)
wenzelm@53325
   138
                    completion.show_popup()
wenzelm@53325
   139
                  }
wenzelm@53292
   140
                }
wenzelm@53292
   141
              case None =>
wenzelm@53292
   142
            }
wenzelm@53292
   143
          case None =>
wenzelm@53292
   144
        }
wenzelm@53292
   145
      }
wenzelm@53292
   146
    }
wenzelm@53292
   147
wenzelm@53292
   148
wenzelm@53292
   149
    /* input key events */
wenzelm@53273
   150
wenzelm@53272
   151
    def input(evt: KeyEvent)
wenzelm@53242
   152
    {
wenzelm@53242
   153
      Swing_Thread.require()
wenzelm@53233
   154
wenzelm@53273
   155
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   156
        if (!evt.isConsumed) {
wenzelm@53273
   157
          dismissed()
wenzelm@53397
   158
          if (evt.getKeyChar != '\b') {
wenzelm@53397
   159
            val mod = evt.getModifiers
wenzelm@53397
   160
            val special =
wenzelm@53397
   161
              // cf. 5.1.0/jEdit/org/gjt/sp/jedit/gui/KeyEventWorkaround.java
wenzelm@53397
   162
              (mod & InputEvent.CTRL_MASK) != 0 && (mod & InputEvent.ALT_MASK) == 0 ||
wenzelm@53397
   163
              (mod & InputEvent.CTRL_MASK) == 0 && (mod & InputEvent.ALT_MASK) != 0 &&
wenzelm@53397
   164
                !Debug.ALT_KEY_PRESSED_DISABLED ||
wenzelm@53397
   165
              (mod & InputEvent.META_MASK) != 0
wenzelm@53296
   166
wenzelm@53397
   167
            if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53397
   168
              input_delay.revoke()
wenzelm@53397
   169
              action(immediate = PIDE.options.bool("jedit_completion_immediate"))
wenzelm@53397
   170
            }
wenzelm@53397
   171
            else input_delay.invoke()
wenzelm@53292
   172
          }
wenzelm@53242
   173
        }
wenzelm@53228
   174
      }
wenzelm@53228
   175
    }
wenzelm@53273
   176
wenzelm@53273
   177
    private val input_delay =
wenzelm@53292
   178
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53322
   179
        action()
wenzelm@53292
   180
      }
wenzelm@53273
   181
wenzelm@53273
   182
wenzelm@53292
   183
    /* dismiss popup */
wenzelm@53273
   184
wenzelm@53292
   185
    def dismissed(): Boolean =
wenzelm@53292
   186
    {
wenzelm@53292
   187
      Swing_Thread.require()
wenzelm@53273
   188
wenzelm@53292
   189
      completion_popup match {
wenzelm@53292
   190
        case Some(completion) =>
wenzelm@53292
   191
          completion.hide_popup()
wenzelm@53292
   192
          completion_popup = None
wenzelm@53292
   193
          true
wenzelm@53292
   194
        case None =>
wenzelm@53292
   195
          false
wenzelm@53273
   196
      }
wenzelm@53292
   197
    }
wenzelm@53274
   198
wenzelm@53274
   199
wenzelm@53274
   200
    /* activation */
wenzelm@53274
   201
wenzelm@53274
   202
    private val outer_key_listener =
wenzelm@53274
   203
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   204
wenzelm@53274
   205
    private def activate()
wenzelm@53274
   206
    {
wenzelm@53274
   207
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   208
    }
wenzelm@53274
   209
wenzelm@53274
   210
    private def deactivate()
wenzelm@53274
   211
    {
wenzelm@53274
   212
      dismissed()
wenzelm@53274
   213
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   214
    }
wenzelm@53228
   215
  }
wenzelm@53023
   216
}
wenzelm@53023
   217
wenzelm@53233
   218
wenzelm@53023
   219
class Completion_Popup private(
wenzelm@53246
   220
  layered: JLayeredPane,
wenzelm@53246
   221
  location: Point,
wenzelm@53272
   222
  font: Font,
wenzelm@53275
   223
  items: List[Completion.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   224
{
wenzelm@53023
   225
  completion =>
wenzelm@53023
   226
wenzelm@53023
   227
  Swing_Thread.require()
wenzelm@53405
   228
wenzelm@53232
   229
  require(!items.isEmpty)
wenzelm@53405
   230
  val multi = items.length > 1
wenzelm@53232
   231
wenzelm@53232
   232
wenzelm@53232
   233
  /* actions */
wenzelm@53232
   234
wenzelm@53275
   235
  def complete(item: Completion.Item) { }
wenzelm@53232
   236
  def propagate(evt: KeyEvent) { }
wenzelm@53234
   237
  def refocus() { }
wenzelm@53023
   238
wenzelm@53023
   239
wenzelm@53023
   240
  /* list view */
wenzelm@53023
   241
wenzelm@53023
   242
  private val list_view = new ListView(items)
wenzelm@53272
   243
  list_view.font = font
wenzelm@53023
   244
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   245
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   246
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   247
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   248
wenzelm@53398
   249
  for (cond <-
wenzelm@53398
   250
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   251
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   252
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   253
wenzelm@53023
   254
  private def complete_selected(): Boolean =
wenzelm@53023
   255
  {
wenzelm@53023
   256
    list_view.selection.items.toList match {
wenzelm@53023
   257
      case item :: _ => complete(item); true
wenzelm@53023
   258
      case _ => false
wenzelm@53023
   259
    }
wenzelm@53023
   260
  }
wenzelm@53023
   261
wenzelm@53023
   262
  private def move_items(n: Int)
wenzelm@53023
   263
  {
wenzelm@53023
   264
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   265
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   266
    if (i != j) {
wenzelm@53023
   267
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   268
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   269
    }
wenzelm@53023
   270
  }
wenzelm@53023
   271
wenzelm@53023
   272
  private def move_pages(n: Int)
wenzelm@53023
   273
  {
wenzelm@53023
   274
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   275
    move_items(page_size * n)
wenzelm@53023
   276
  }
wenzelm@53023
   277
wenzelm@53023
   278
wenzelm@53023
   279
  /* event handling */
wenzelm@53023
   280
wenzelm@53274
   281
  private val inner_key_listener =
wenzelm@53226
   282
    JEdit_Lib.key_listener(
wenzelm@53226
   283
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   284
        {
wenzelm@53226
   285
          if (!e.isConsumed) {
wenzelm@53226
   286
            e.getKeyCode match {
wenzelm@53226
   287
              case KeyEvent.VK_TAB =>
wenzelm@53226
   288
                if (complete_selected()) e.consume
wenzelm@53226
   289
                hide_popup()
wenzelm@53226
   290
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   291
                hide_popup()
wenzelm@53226
   292
                e.consume
wenzelm@53405
   293
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   294
                move_items(-1)
wenzelm@53407
   295
                e.consume
wenzelm@53405
   296
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   297
                move_items(1)
wenzelm@53407
   298
                e.consume
wenzelm@53405
   299
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   300
                move_pages(-1)
wenzelm@53407
   301
                e.consume
wenzelm@53405
   302
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   303
                move_pages(1)
wenzelm@53407
   304
                e.consume
wenzelm@53226
   305
              case _ =>
wenzelm@53226
   306
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   307
                  hide_popup()
wenzelm@53226
   308
            }
wenzelm@53226
   309
          }
wenzelm@53232
   310
          propagate(e)
wenzelm@53226
   311
        },
wenzelm@53232
   312
      key_typed = propagate _
wenzelm@53226
   313
    )
wenzelm@53023
   314
wenzelm@53274
   315
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   316
wenzelm@53023
   317
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   318
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   319
      if (complete_selected()) e.consume
wenzelm@53023
   320
      hide_popup()
wenzelm@53023
   321
    }
wenzelm@53023
   322
  })
wenzelm@53023
   323
wenzelm@53023
   324
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   325
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   326
  })
wenzelm@53023
   327
wenzelm@53023
   328
wenzelm@53023
   329
  /* main content */
wenzelm@53023
   330
wenzelm@53023
   331
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   332
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   333
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   334
wenzelm@53023
   335
wenzelm@53023
   336
  /* popup */
wenzelm@53023
   337
wenzelm@53023
   338
  private val popup =
wenzelm@53023
   339
  {
wenzelm@53247
   340
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   341
    val size =
wenzelm@53230
   342
    {
wenzelm@53230
   343
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   344
      val bounds = Rendering.popup_bounds
wenzelm@53247
   345
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   346
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   347
      new Dimension(w, h)
wenzelm@53230
   348
    }
wenzelm@53247
   349
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   350
  }
wenzelm@53246
   351
wenzelm@53246
   352
  private def show_popup()
wenzelm@53246
   353
  {
wenzelm@53246
   354
    popup.show
wenzelm@53246
   355
    list_view.requestFocus
wenzelm@53023
   356
  }
wenzelm@53023
   357
wenzelm@53232
   358
  private def hide_popup()
wenzelm@53023
   359
  {
wenzelm@53234
   360
    val had_focus = list_view.peer.isFocusOwner
wenzelm@53229
   361
    popup.hide
wenzelm@53234
   362
    if (had_focus) refocus()
wenzelm@53023
   363
  }
wenzelm@53023
   364
}
wenzelm@53023
   365