src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Tue Aug 27 22:20:11 2013 +0200 (2013-08-27 ago)
changeset 53236 837a6ef61fe9
parent 53235 1b6a44859549
child 53237 6bfe54791591
permissions -rw-r--r--
de-register completion first, which is important to make new popup reliably;
     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       workaround = false,
   190       key_pressed = (e: KeyEvent) =>
   191         {
   192           if (!e.isConsumed) {
   193             e.getKeyCode match {
   194               case KeyEvent.VK_TAB =>
   195                 if (complete_selected()) e.consume
   196                 hide_popup()
   197               case KeyEvent.VK_ESCAPE =>
   198                 hide_popup()
   199                 e.consume
   200               case KeyEvent.VK_UP => move_items(-1); e.consume
   201               case KeyEvent.VK_DOWN => move_items(1); e.consume
   202               case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
   203               case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
   204               case _ =>
   205                 if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
   206                   hide_popup()
   207             }
   208           }
   209           propagate(e)
   210         },
   211       key_typed = propagate _
   212     )
   213 
   214   list_view.peer.addKeyListener(key_listener)
   215 
   216   list_view.peer.addMouseListener(new MouseAdapter {
   217     override def mouseClicked(e: MouseEvent) {
   218       if (complete_selected()) e.consume
   219       hide_popup()
   220     }
   221   })
   222 
   223   list_view.peer.addFocusListener(new FocusAdapter {
   224     override def focusLost(e: FocusEvent) { hide_popup() }
   225   })
   226 
   227 
   228   /* main content */
   229 
   230   override def getFocusTraversalKeysEnabled = false
   231 
   232   completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
   233 
   234 
   235   /* popup */
   236 
   237   private val popup =
   238   {
   239     val screen_bounds = JEdit_Lib.screen_bounds(screen_point)
   240 
   241     val x0 = root.getLocationOnScreen.x
   242     val y0 = root.getLocationOnScreen.y
   243     val w0 = root.getWidth
   244     val h0 = root.getHeight
   245 
   246     val (w, h) =
   247     {
   248       val geometry = JEdit_Lib.window_geometry(completion, completion)
   249       val bounds = Rendering.popup_bounds
   250       val w = geometry.width min (screen_bounds.width * bounds).toInt min w0
   251       val h = geometry.height min (screen_bounds.height * bounds).toInt min h0
   252       (w, h)
   253     }
   254 
   255     val (x, y) =
   256     {
   257       val x1 = x0 + w0 - w
   258       val y1 = y0 + h0 - h
   259       val x2 = screen_point.x min (screen_bounds.x + screen_bounds.width - w)
   260       val y2 = screen_point.y min (screen_bounds.y + screen_bounds.height - h)
   261       ((x2 min x1) max x0, (y2 min y1) max y0)
   262     }
   263 
   264     completion.setSize(new Dimension(w, h))
   265     completion.setPreferredSize(new Dimension(w, h))
   266     PopupFactory.getSharedInstance.getPopup(root, completion, x, y)
   267   }
   268 
   269   private def hide_popup()
   270   {
   271     val had_focus = list_view.peer.isFocusOwner
   272     popup.hide
   273     if (had_focus) refocus()
   274   }
   275 
   276   popup.show
   277   list_view.requestFocus
   278 }
   279