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