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