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