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