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