src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Thu Aug 29 21:49:46 2013 +0200 (2013-08-29 ago)
changeset 53293 fd27b8f5a479
parent 53292 f567c1c7b180
child 53296 65c60c782da5
permissions -rw-r--r--
added action isabelle.complete, using standard jEdit keyboard shortcut;
     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   /* setup for jEdit text area */
    26 
    27   object Text_Area
    28   {
    29     private val key = new Object
    30 
    31     def apply(text_area: JEditTextArea): Option[Completion_Popup.Text_Area] =
    32       text_area.getClientProperty(key) match {
    33         case text_area_completion: Completion_Popup.Text_Area => Some(text_area_completion)
    34         case _ => None
    35       }
    36 
    37     def exit(text_area: JEditTextArea)
    38     {
    39       Swing_Thread.require()
    40       apply(text_area) match {
    41         case None =>
    42         case Some(text_area_completion) =>
    43           text_area_completion.deactivate()
    44           text_area.putClientProperty(key, null)
    45       }
    46     }
    47 
    48     def init(text_area: JEditTextArea): Completion_Popup.Text_Area =
    49     {
    50       exit(text_area)
    51       val text_area_completion = new Text_Area(text_area)
    52       text_area.putClientProperty(key, text_area_completion)
    53       text_area_completion.activate()
    54       text_area_completion
    55     }
    56 
    57     def dismissed(text_area: JEditTextArea): Boolean =
    58     {
    59       Swing_Thread.require()
    60       apply(text_area) match {
    61         case Some(text_area_completion) => text_area_completion.dismissed()
    62         case None => false
    63       }
    64     }
    65   }
    66 
    67   class Text_Area private(text_area: JEditTextArea)
    68   {
    69     private var completion_popup: Option[Completion_Popup] = None
    70 
    71 
    72     /* completion action */
    73 
    74     private def insert(item: Completion.Item)
    75     {
    76       Swing_Thread.require()
    77 
    78       val buffer = text_area.getBuffer
    79       val len = item.original.length
    80       if (buffer.isEditable && len > 0) {
    81         JEdit_Lib.buffer_edit(buffer) {
    82           val caret = text_area.getCaretPosition
    83           JEdit_Lib.try_get_text(buffer, Text.Range(caret - len, caret)) match {
    84             case Some(text) if text == item.original =>
    85               buffer.remove(caret - len, len)
    86               buffer.insert(caret - len, item.replacement)
    87             case _ =>
    88           }
    89         }
    90       }
    91     }
    92 
    93     def action(immediate: Boolean)
    94     {
    95       val view = text_area.getView
    96       val layered = view.getLayeredPane
    97       val buffer = text_area.getBuffer
    98       val painter = text_area.getPainter
    99 
   100       if (buffer.isEditable) {
   101         Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match {
   102           case Some(syntax) =>
   103             val caret = text_area.getCaretPosition
   104             val line = buffer.getLineOfOffset(caret)
   105             val start = buffer.getLineStartOffset(line)
   106             val text = buffer.getSegment(start, caret - start)
   107 
   108             syntax.completion.complete(Isabelle_Encoding.is_active(buffer), text) match {
   109               case Some((_, List(item))) if immediate =>
   110                 insert(item)
   111 
   112               case Some((original, items)) =>
   113                 val font =
   114                   painter.getFont.deriveFont(Rendering.font_size("jedit_popup_font_scale"))
   115 
   116                 val loc1 = text_area.offsetToXY(caret - original.length)
   117                 if (loc1 != null) {
   118                   val loc2 =
   119                     SwingUtilities.convertPoint(painter,
   120                       loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
   121 
   122                   val completion =
   123                     new Completion_Popup(layered, loc2, font, items) {
   124                       override def complete(item: Completion.Item) { insert(item) }
   125                       override def propagate(evt: KeyEvent) {
   126                         JEdit_Lib.propagate_key(view, evt)
   127                         input(evt)
   128                       }
   129                       override def refocus() { text_area.requestFocus }
   130                     }
   131                   completion_popup = Some(completion)
   132                   completion.show_popup()
   133                 }
   134               case None =>
   135             }
   136           case None =>
   137         }
   138       }
   139     }
   140 
   141 
   142     /* input key events */
   143 
   144     def input(evt: KeyEvent)
   145     {
   146       Swing_Thread.require()
   147 
   148       if (PIDE.options.bool("jedit_completion")) {
   149         if (!evt.isConsumed) {
   150           dismissed()
   151           if (PIDE.options.seconds("jedit_completion_delay").is_zero) {
   152             input_delay.revoke()
   153             action(PIDE.options.bool("jedit_completion_immediate"))
   154           }
   155           else input_delay.invoke()
   156         }
   157       }
   158     }
   159 
   160     private val input_delay =
   161       Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
   162         action(false)
   163       }
   164 
   165 
   166     /* dismiss popup */
   167 
   168     def dismissed(): Boolean =
   169     {
   170       Swing_Thread.require()
   171 
   172       completion_popup match {
   173         case Some(completion) =>
   174           completion.hide_popup()
   175           completion_popup = None
   176           true
   177         case None =>
   178           false
   179       }
   180     }
   181 
   182 
   183     /* activation */
   184 
   185     private val outer_key_listener =
   186       JEdit_Lib.key_listener(key_typed = input _)
   187 
   188     private def activate()
   189     {
   190       text_area.addKeyListener(outer_key_listener)
   191     }
   192 
   193     private def deactivate()
   194     {
   195       dismissed()
   196       text_area.removeKeyListener(outer_key_listener)
   197     }
   198   }
   199 }
   200 
   201 
   202 class Completion_Popup private(
   203   layered: JLayeredPane,
   204   location: Point,
   205   font: Font,
   206   items: List[Completion.Item]) extends JPanel(new BorderLayout)
   207 {
   208   completion =>
   209 
   210   Swing_Thread.require()
   211   require(!items.isEmpty)
   212 
   213 
   214   /* actions */
   215 
   216   def complete(item: Completion.Item) { }
   217   def propagate(evt: KeyEvent) { }
   218   def refocus() { }
   219 
   220 
   221   /* list view */
   222 
   223   private val list_view = new ListView(items)
   224   list_view.font = font
   225   list_view.selection.intervalMode = ListView.IntervalMode.Single
   226   list_view.peer.setFocusTraversalKeysEnabled(false)
   227   list_view.peer.setVisibleRowCount(items.length min 8)
   228   list_view.peer.setSelectedIndex(0)
   229 
   230   private def complete_selected(): Boolean =
   231   {
   232     list_view.selection.items.toList match {
   233       case item :: _ => complete(item); true
   234       case _ => false
   235     }
   236   }
   237 
   238   private def move_items(n: Int)
   239   {
   240     val i = list_view.peer.getSelectedIndex
   241     val j = ((i + n) min (items.length - 1)) max 0
   242     if (i != j) {
   243       list_view.peer.setSelectedIndex(j)
   244       list_view.peer.ensureIndexIsVisible(j)
   245     }
   246   }
   247 
   248   private def move_pages(n: Int)
   249   {
   250     val page_size = list_view.peer.getVisibleRowCount - 1
   251     move_items(page_size * n)
   252   }
   253 
   254 
   255   /* event handling */
   256 
   257   private val inner_key_listener =
   258     JEdit_Lib.key_listener(
   259       key_pressed = (e: KeyEvent) =>
   260         {
   261           if (!e.isConsumed) {
   262             e.getKeyCode match {
   263               case KeyEvent.VK_TAB =>
   264                 if (complete_selected()) e.consume
   265                 hide_popup()
   266               case KeyEvent.VK_ESCAPE =>
   267                 hide_popup()
   268                 e.consume
   269               case KeyEvent.VK_UP => move_items(-1); e.consume
   270               case KeyEvent.VK_DOWN => move_items(1); e.consume
   271               case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
   272               case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
   273               case _ =>
   274                 if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
   275                   hide_popup()
   276             }
   277           }
   278           propagate(e)
   279         },
   280       key_typed = propagate _
   281     )
   282 
   283   list_view.peer.addKeyListener(inner_key_listener)
   284 
   285   list_view.peer.addMouseListener(new MouseAdapter {
   286     override def mouseClicked(e: MouseEvent) {
   287       if (complete_selected()) e.consume
   288       hide_popup()
   289     }
   290   })
   291 
   292   list_view.peer.addFocusListener(new FocusAdapter {
   293     override def focusLost(e: FocusEvent) { hide_popup() }
   294   })
   295 
   296 
   297   /* main content */
   298 
   299   override def getFocusTraversalKeysEnabled = false
   300 
   301   completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
   302 
   303 
   304   /* popup */
   305 
   306   private val popup =
   307   {
   308     val screen = JEdit_Lib.screen_location(layered, location)
   309     val size =
   310     {
   311       val geometry = JEdit_Lib.window_geometry(completion, completion)
   312       val bounds = Rendering.popup_bounds
   313       val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
   314       val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
   315       new Dimension(w, h)
   316     }
   317     new Popup(layered, completion, screen.relative(layered, size), size)
   318   }
   319 
   320   private def show_popup()
   321   {
   322     popup.show
   323     list_view.requestFocus
   324   }
   325 
   326   private def hide_popup()
   327   {
   328     val had_focus = list_view.peer.isFocusOwner
   329     popup.hide
   330     if (had_focus) refocus()
   331   }
   332 }
   333