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