src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Thu Aug 29 21:17:46 2013 +0200 (2013-08-29 ago)
changeset 53292 f567c1c7b180
parent 53275 b34aac6511ab
child 53293 fd27b8f5a479
permissions -rw-r--r--
option to insert unique completion immediately into buffer;
     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       else {
   159         dismissed()
   160         input_delay.revoke()
   161       }
   162     }
   163 
   164     private val input_delay =
   165       Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
   166         action(false)
   167       }
   168 
   169 
   170     /* dismiss popup */
   171 
   172     def dismissed(): Boolean =
   173     {
   174       Swing_Thread.require()
   175 
   176       completion_popup match {
   177         case Some(completion) =>
   178           completion.hide_popup()
   179           completion_popup = None
   180           true
   181         case None =>
   182           false
   183       }
   184     }
   185 
   186 
   187     /* activation */
   188 
   189     private val outer_key_listener =
   190       JEdit_Lib.key_listener(key_typed = input _)
   191 
   192     private def activate()
   193     {
   194       text_area.addKeyListener(outer_key_listener)
   195     }
   196 
   197     private def deactivate()
   198     {
   199       dismissed()
   200       text_area.removeKeyListener(outer_key_listener)
   201     }
   202   }
   203 }
   204 
   205 
   206 class Completion_Popup private(
   207   layered: JLayeredPane,
   208   location: Point,
   209   font: Font,
   210   items: List[Completion.Item]) extends JPanel(new BorderLayout)
   211 {
   212   completion =>
   213 
   214   Swing_Thread.require()
   215   require(!items.isEmpty)
   216 
   217 
   218   /* actions */
   219 
   220   def complete(item: Completion.Item) { }
   221   def propagate(evt: KeyEvent) { }
   222   def refocus() { }
   223 
   224 
   225   /* list view */
   226 
   227   private val list_view = new ListView(items)
   228   list_view.font = font
   229   list_view.selection.intervalMode = ListView.IntervalMode.Single
   230   list_view.peer.setFocusTraversalKeysEnabled(false)
   231   list_view.peer.setVisibleRowCount(items.length min 8)
   232   list_view.peer.setSelectedIndex(0)
   233 
   234   private def complete_selected(): Boolean =
   235   {
   236     list_view.selection.items.toList match {
   237       case item :: _ => complete(item); true
   238       case _ => false
   239     }
   240   }
   241 
   242   private def move_items(n: Int)
   243   {
   244     val i = list_view.peer.getSelectedIndex
   245     val j = ((i + n) min (items.length - 1)) max 0
   246     if (i != j) {
   247       list_view.peer.setSelectedIndex(j)
   248       list_view.peer.ensureIndexIsVisible(j)
   249     }
   250   }
   251 
   252   private def move_pages(n: Int)
   253   {
   254     val page_size = list_view.peer.getVisibleRowCount - 1
   255     move_items(page_size * n)
   256   }
   257 
   258 
   259   /* event handling */
   260 
   261   private val inner_key_listener =
   262     JEdit_Lib.key_listener(
   263       key_pressed = (e: KeyEvent) =>
   264         {
   265           if (!e.isConsumed) {
   266             e.getKeyCode match {
   267               case KeyEvent.VK_TAB =>
   268                 if (complete_selected()) e.consume
   269                 hide_popup()
   270               case KeyEvent.VK_ESCAPE =>
   271                 hide_popup()
   272                 e.consume
   273               case KeyEvent.VK_UP => move_items(-1); e.consume
   274               case KeyEvent.VK_DOWN => move_items(1); e.consume
   275               case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
   276               case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
   277               case _ =>
   278                 if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
   279                   hide_popup()
   280             }
   281           }
   282           propagate(e)
   283         },
   284       key_typed = propagate _
   285     )
   286 
   287   list_view.peer.addKeyListener(inner_key_listener)
   288 
   289   list_view.peer.addMouseListener(new MouseAdapter {
   290     override def mouseClicked(e: MouseEvent) {
   291       if (complete_selected()) e.consume
   292       hide_popup()
   293     }
   294   })
   295 
   296   list_view.peer.addFocusListener(new FocusAdapter {
   297     override def focusLost(e: FocusEvent) { hide_popup() }
   298   })
   299 
   300 
   301   /* main content */
   302 
   303   override def getFocusTraversalKeysEnabled = false
   304 
   305   completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
   306 
   307 
   308   /* popup */
   309 
   310   private val popup =
   311   {
   312     val screen = JEdit_Lib.screen_location(layered, location)
   313     val size =
   314     {
   315       val geometry = JEdit_Lib.window_geometry(completion, completion)
   316       val bounds = Rendering.popup_bounds
   317       val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
   318       val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
   319       new Dimension(w, h)
   320     }
   321     new Popup(layered, completion, screen.relative(layered, size), size)
   322   }
   323 
   324   private def show_popup()
   325   {
   326     popup.show
   327     list_view.requestFocus
   328   }
   329 
   330   private def hide_popup()
   331   {
   332     val had_focus = list_view.peer.isFocusOwner
   333     popup.hide
   334     if (had_focus) refocus()
   335   }
   336 }
   337