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