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