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