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