src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Tue Aug 27 22:23:40 2013 +0200 (2013-08-27 ago)
changeset 53237 6bfe54791591
parent 53236 837a6ef61fe9
child 53242 142f4fff4e40
permissions -rw-r--r--
enable jEdit KeyEventWorkaround uniformly;
wenzelm@53023
     1
/*  Title:      Tools/jEdit/src/completion_popup.scala
wenzelm@53023
     2
    Author:     Makarius
wenzelm@53023
     3
wenzelm@53023
     4
Completion popup based on javax.swing.PopupFactory.
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@53228
    14
import javax.swing.{JPanel, JComponent, PopupFactory, 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@53235
    25
  /* items */
wenzelm@53235
    26
wenzelm@53233
    27
  private sealed case class Item(original: String, replacement: String, description: String)
wenzelm@53233
    28
  { override def toString: String = description }
wenzelm@53233
    29
wenzelm@53235
    30
wenzelm@53235
    31
  /* register single instance within root */
wenzelm@53235
    32
wenzelm@53235
    33
  private def register(root: JComponent, completion: Completion_Popup)
wenzelm@53235
    34
  {
wenzelm@53235
    35
    Swing_Thread.require()
wenzelm@53235
    36
wenzelm@53235
    37
    root.getClientProperty(Completion_Popup) match {
wenzelm@53235
    38
      case old_completion: Completion_Popup => old_completion.hide_popup
wenzelm@53235
    39
      case _ =>
wenzelm@53235
    40
    }
wenzelm@53235
    41
    root.putClientProperty(Completion_Popup, completion)
wenzelm@53235
    42
  }
wenzelm@53235
    43
wenzelm@53235
    44
wenzelm@53235
    45
  /* jEdit text area operations */
wenzelm@53235
    46
wenzelm@53235
    47
  private def complete_text_area(text_area: JEditTextArea, item: Item)
wenzelm@53233
    48
  {
wenzelm@53233
    49
    Swing_Thread.require()
wenzelm@53023
    50
wenzelm@53233
    51
    val buffer = text_area.getBuffer
wenzelm@53233
    52
    val len = item.original.length
wenzelm@53233
    53
    if (buffer.isEditable && len > 0) {
wenzelm@53233
    54
      JEdit_Lib.buffer_edit(buffer) {
wenzelm@53233
    55
        val caret = text_area.getCaretPosition
wenzelm@53233
    56
        JEdit_Lib.try_get_text(buffer, Text.Range(caret - len, caret)) match {
wenzelm@53233
    57
          case Some(text) if text == item.original =>
wenzelm@53233
    58
            buffer.remove(caret - len, len)
wenzelm@53233
    59
            buffer.insert(caret - len, item.replacement)
wenzelm@53233
    60
          case _ =>
wenzelm@53233
    61
        }
wenzelm@53233
    62
      }
wenzelm@53233
    63
    }
wenzelm@53233
    64
  }
wenzelm@53233
    65
wenzelm@53233
    66
  def input_text_area(text_area: JEditTextArea, get_syntax: => Option[Outer_Syntax], evt: KeyEvent)
wenzelm@53228
    67
  {
wenzelm@53228
    68
    Swing_Thread.require()
wenzelm@53228
    69
wenzelm@53235
    70
    val view = text_area.getView
wenzelm@53235
    71
    val root = view.getRootPane
wenzelm@53228
    72
    val buffer = text_area.getBuffer
wenzelm@53235
    73
    val painter = text_area.getPainter
wenzelm@53235
    74
wenzelm@53236
    75
    register(root, null)
wenzelm@53236
    76
wenzelm@53228
    77
    if (buffer.isEditable) {
wenzelm@53233
    78
      get_syntax match {
wenzelm@53233
    79
        case Some(syntax) =>
wenzelm@53233
    80
          val caret = text_area.getCaretPosition
wenzelm@53235
    81
          val result =
wenzelm@53233
    82
          {
wenzelm@53233
    83
            val line = buffer.getLineOfOffset(caret)
wenzelm@53233
    84
            val start = buffer.getLineStartOffset(line)
wenzelm@53233
    85
            val text = buffer.getSegment(start, caret - start)
wenzelm@53228
    86
wenzelm@53233
    87
            syntax.completion.complete(text) match {
wenzelm@53233
    88
              case Some((word, cs)) =>
wenzelm@53233
    89
                val ds =
wenzelm@53233
    90
                  (if (Isabelle_Encoding.is_active(buffer))
wenzelm@53233
    91
                    cs.map(Symbol.decode(_)).sorted
wenzelm@53233
    92
                   else cs).filter(_ != word)
wenzelm@53233
    93
                if (ds.isEmpty) None
wenzelm@53233
    94
                else Some((word, ds.map(s => Item(word, s, s))))
wenzelm@53236
    95
              case None => None
wenzelm@53233
    96
            }
wenzelm@53233
    97
          }
wenzelm@53235
    98
          result match {
wenzelm@53233
    99
            case Some((original, items)) =>
wenzelm@53233
   100
              val popup_font =
wenzelm@53233
   101
                painter.getFont.deriveFont(
wenzelm@53233
   102
                  (painter.getFont.getSize2D * PIDE.options.real("jedit_popup_font_scale")).toFloat
wenzelm@53233
   103
                    max 10.0f)
wenzelm@53229
   104
wenzelm@53233
   105
              val location = text_area.offsetToXY(caret - original.length)
wenzelm@53233
   106
              if (location != null) {
wenzelm@53233
   107
                location.y = location.y + painter.getFontMetrics.getHeight
wenzelm@53233
   108
                SwingUtilities.convertPointToScreen(location, painter)
wenzelm@53235
   109
wenzelm@53235
   110
                val completion =
wenzelm@53235
   111
                  new Completion_Popup(root, popup_font, location, items) {
wenzelm@53235
   112
                    override def complete(item: Item) { complete_text_area(text_area, item) }
wenzelm@53235
   113
                    override def propagate(e: KeyEvent) {
wenzelm@53235
   114
                      JEdit_Lib.propagate_key(view, e)
wenzelm@53235
   115
                      if (!e.isConsumed) input_text_area(text_area, get_syntax, e)
wenzelm@53235
   116
                    }
wenzelm@53235
   117
                    override def refocus() { text_area.requestFocus }
wenzelm@53235
   118
                  }
wenzelm@53235
   119
                register(root, completion)
wenzelm@53233
   120
              }
wenzelm@53236
   121
            case None =>
wenzelm@53232
   122
          }
wenzelm@53236
   123
        case None =>
wenzelm@53228
   124
      }
wenzelm@53228
   125
    }
wenzelm@53228
   126
  }
wenzelm@53023
   127
}
wenzelm@53023
   128
wenzelm@53233
   129
wenzelm@53023
   130
class Completion_Popup private(
wenzelm@53229
   131
  root: JComponent,
wenzelm@53229
   132
  popup_font: Font,
wenzelm@53023
   133
  screen_point: Point,
wenzelm@53232
   134
  items: List[Completion_Popup.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   135
{
wenzelm@53023
   136
  completion =>
wenzelm@53023
   137
wenzelm@53023
   138
  Swing_Thread.require()
wenzelm@53232
   139
  require(!items.isEmpty)
wenzelm@53232
   140
wenzelm@53232
   141
wenzelm@53232
   142
  /* actions */
wenzelm@53232
   143
wenzelm@53232
   144
  def complete(item: Completion_Popup.Item) { }
wenzelm@53232
   145
  def propagate(evt: KeyEvent) { }
wenzelm@53234
   146
  def refocus() { }
wenzelm@53023
   147
wenzelm@53023
   148
wenzelm@53023
   149
  /* list view */
wenzelm@53023
   150
wenzelm@53023
   151
  private val list_view = new ListView(items)
wenzelm@53023
   152
  {
wenzelm@53229
   153
    font = popup_font
wenzelm@53023
   154
  }
wenzelm@53023
   155
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   156
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   157
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   158
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   159
wenzelm@53023
   160
  private def complete_selected(): Boolean =
wenzelm@53023
   161
  {
wenzelm@53023
   162
    list_view.selection.items.toList match {
wenzelm@53023
   163
      case item :: _ => complete(item); true
wenzelm@53023
   164
      case _ => false
wenzelm@53023
   165
    }
wenzelm@53023
   166
  }
wenzelm@53023
   167
wenzelm@53023
   168
  private def move_items(n: Int)
wenzelm@53023
   169
  {
wenzelm@53023
   170
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   171
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   172
    if (i != j) {
wenzelm@53023
   173
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   174
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   175
    }
wenzelm@53023
   176
  }
wenzelm@53023
   177
wenzelm@53023
   178
  private def move_pages(n: Int)
wenzelm@53023
   179
  {
wenzelm@53023
   180
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   181
    move_items(page_size * n)
wenzelm@53023
   182
  }
wenzelm@53023
   183
wenzelm@53023
   184
wenzelm@53023
   185
  /* event handling */
wenzelm@53023
   186
wenzelm@53226
   187
  private val key_listener =
wenzelm@53226
   188
    JEdit_Lib.key_listener(
wenzelm@53226
   189
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   190
        {
wenzelm@53226
   191
          if (!e.isConsumed) {
wenzelm@53226
   192
            e.getKeyCode match {
wenzelm@53226
   193
              case KeyEvent.VK_TAB =>
wenzelm@53226
   194
                if (complete_selected()) e.consume
wenzelm@53226
   195
                hide_popup()
wenzelm@53226
   196
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   197
                hide_popup()
wenzelm@53226
   198
                e.consume
wenzelm@53226
   199
              case KeyEvent.VK_UP => move_items(-1); e.consume
wenzelm@53226
   200
              case KeyEvent.VK_DOWN => move_items(1); e.consume
wenzelm@53226
   201
              case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
wenzelm@53226
   202
              case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
wenzelm@53226
   203
              case _ =>
wenzelm@53226
   204
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   205
                  hide_popup()
wenzelm@53226
   206
            }
wenzelm@53226
   207
          }
wenzelm@53232
   208
          propagate(e)
wenzelm@53226
   209
        },
wenzelm@53232
   210
      key_typed = propagate _
wenzelm@53226
   211
    )
wenzelm@53023
   212
wenzelm@53226
   213
  list_view.peer.addKeyListener(key_listener)
wenzelm@53023
   214
wenzelm@53023
   215
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   216
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   217
      if (complete_selected()) e.consume
wenzelm@53023
   218
      hide_popup()
wenzelm@53023
   219
    }
wenzelm@53023
   220
  })
wenzelm@53023
   221
wenzelm@53023
   222
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   223
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   224
  })
wenzelm@53023
   225
wenzelm@53023
   226
wenzelm@53023
   227
  /* main content */
wenzelm@53023
   228
wenzelm@53023
   229
  override def getFocusTraversalKeysEnabled = false
wenzelm@53023
   230
wenzelm@53023
   231
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   232
wenzelm@53023
   233
wenzelm@53023
   234
  /* popup */
wenzelm@53023
   235
wenzelm@53023
   236
  private val popup =
wenzelm@53023
   237
  {
wenzelm@53023
   238
    val screen_bounds = JEdit_Lib.screen_bounds(screen_point)
wenzelm@53023
   239
wenzelm@53230
   240
    val x0 = root.getLocationOnScreen.x
wenzelm@53230
   241
    val y0 = root.getLocationOnScreen.y
wenzelm@53230
   242
    val w0 = root.getWidth
wenzelm@53230
   243
    val h0 = root.getHeight
wenzelm@53023
   244
wenzelm@53230
   245
    val (w, h) =
wenzelm@53230
   246
    {
wenzelm@53230
   247
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   248
      val bounds = Rendering.popup_bounds
wenzelm@53230
   249
      val w = geometry.width min (screen_bounds.width * bounds).toInt min w0
wenzelm@53230
   250
      val h = geometry.height min (screen_bounds.height * bounds).toInt min h0
wenzelm@53230
   251
      (w, h)
wenzelm@53230
   252
    }
wenzelm@53230
   253
wenzelm@53230
   254
    val (x, y) =
wenzelm@53230
   255
    {
wenzelm@53230
   256
      val x1 = x0 + w0 - w
wenzelm@53230
   257
      val y1 = y0 + h0 - h
wenzelm@53230
   258
      val x2 = screen_point.x min (screen_bounds.x + screen_bounds.width - w)
wenzelm@53230
   259
      val y2 = screen_point.y min (screen_bounds.y + screen_bounds.height - h)
wenzelm@53230
   260
      ((x2 min x1) max x0, (y2 min y1) max y0)
wenzelm@53230
   261
    }
wenzelm@53023
   262
wenzelm@53023
   263
    completion.setSize(new Dimension(w, h))
wenzelm@53023
   264
    completion.setPreferredSize(new Dimension(w, h))
wenzelm@53229
   265
    PopupFactory.getSharedInstance.getPopup(root, completion, x, y)
wenzelm@53023
   266
  }
wenzelm@53023
   267
wenzelm@53232
   268
  private def hide_popup()
wenzelm@53023
   269
  {
wenzelm@53234
   270
    val had_focus = list_view.peer.isFocusOwner
wenzelm@53229
   271
    popup.hide
wenzelm@53234
   272
    if (had_focus) refocus()
wenzelm@53023
   273
  }
wenzelm@53232
   274
wenzelm@53232
   275
  popup.show
wenzelm@53232
   276
  list_view.requestFocus
wenzelm@53023
   277
}
wenzelm@53023
   278