src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Fri Aug 30 13:24:14 2013 +0200 (2013-08-30 ago)
changeset 53325 ffabc0083786
parent 53322 a4cd032172e0
child 53337 b3817a0e3211
permissions -rw-r--r--
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
     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 decode = Isabelle_Encoding.is_active(buffer)
   110             syntax.completion.complete(decode, explicit, text) match {
   111               case Some(result) =>
   112                 if (result.unique && result.items.head.immediate && immediate)
   113                   insert(result.items.head)
   114                 else {
   115                   val font =
   116                     painter.getFont.deriveFont(Rendering.font_size("jedit_popup_font_scale"))
   117 
   118                   val loc1 = text_area.offsetToXY(caret - result.original.length)
   119                   if (loc1 != null) {
   120                     val loc2 =
   121                       SwingUtilities.convertPoint(painter,
   122                         loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
   123 
   124                     val completion =
   125                       new Completion_Popup(layered, loc2, font, result.items) {
   126                         override def complete(item: Completion.Item) { insert(item) }
   127                         override def propagate(evt: KeyEvent) {
   128                           JEdit_Lib.propagate_key(view, evt)
   129                           input(evt)
   130                         }
   131                         override def refocus() { text_area.requestFocus }
   132                       }
   133                     completion_popup = Some(completion)
   134                     completion.show_popup()
   135                   }
   136                 }
   137               case None =>
   138             }
   139           case None =>
   140         }
   141       }
   142     }
   143 
   144 
   145     /* input key events */
   146 
   147     def input(evt: KeyEvent)
   148     {
   149       Swing_Thread.require()
   150 
   151       if (PIDE.options.bool("jedit_completion")) {
   152         if (!evt.isConsumed) {
   153           dismissed()
   154 
   155           val mod = evt.getModifiers
   156           val special =
   157             evt.getKeyChar == '\b' ||
   158             // cf. 5.1.0/jEdit/org/gjt/sp/jedit/gui/KeyEventWorkaround.java
   159             (mod & InputEvent.CTRL_MASK) != 0 && (mod & InputEvent.ALT_MASK) == 0 ||
   160             (mod & InputEvent.CTRL_MASK) == 0 && (mod & InputEvent.ALT_MASK) != 0 &&
   161               !Debug.ALT_KEY_PRESSED_DISABLED ||
   162             (mod & InputEvent.META_MASK) != 0
   163 
   164           if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
   165             input_delay.revoke()
   166             action(immediate = PIDE.options.bool("jedit_completion_immediate"))
   167           }
   168           else input_delay.invoke()
   169         }
   170       }
   171     }
   172 
   173     private val input_delay =
   174       Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
   175         action()
   176       }
   177 
   178 
   179     /* dismiss popup */
   180 
   181     def dismissed(): Boolean =
   182     {
   183       Swing_Thread.require()
   184 
   185       completion_popup match {
   186         case Some(completion) =>
   187           completion.hide_popup()
   188           completion_popup = None
   189           true
   190         case None =>
   191           false
   192       }
   193     }
   194 
   195 
   196     /* activation */
   197 
   198     private val outer_key_listener =
   199       JEdit_Lib.key_listener(key_typed = input _)
   200 
   201     private def activate()
   202     {
   203       text_area.addKeyListener(outer_key_listener)
   204     }
   205 
   206     private def deactivate()
   207     {
   208       dismissed()
   209       text_area.removeKeyListener(outer_key_listener)
   210     }
   211   }
   212 }
   213 
   214 
   215 class Completion_Popup private(
   216   layered: JLayeredPane,
   217   location: Point,
   218   font: Font,
   219   items: List[Completion.Item]) extends JPanel(new BorderLayout)
   220 {
   221   completion =>
   222 
   223   Swing_Thread.require()
   224   require(!items.isEmpty)
   225 
   226 
   227   /* actions */
   228 
   229   def complete(item: Completion.Item) { }
   230   def propagate(evt: KeyEvent) { }
   231   def refocus() { }
   232 
   233 
   234   /* list view */
   235 
   236   private val list_view = new ListView(items)
   237   list_view.font = font
   238   list_view.selection.intervalMode = ListView.IntervalMode.Single
   239   list_view.peer.setFocusTraversalKeysEnabled(false)
   240   list_view.peer.setVisibleRowCount(items.length min 8)
   241   list_view.peer.setSelectedIndex(0)
   242 
   243   private def complete_selected(): Boolean =
   244   {
   245     list_view.selection.items.toList match {
   246       case item :: _ => complete(item); true
   247       case _ => false
   248     }
   249   }
   250 
   251   private def move_items(n: Int)
   252   {
   253     val i = list_view.peer.getSelectedIndex
   254     val j = ((i + n) min (items.length - 1)) max 0
   255     if (i != j) {
   256       list_view.peer.setSelectedIndex(j)
   257       list_view.peer.ensureIndexIsVisible(j)
   258     }
   259   }
   260 
   261   private def move_pages(n: Int)
   262   {
   263     val page_size = list_view.peer.getVisibleRowCount - 1
   264     move_items(page_size * n)
   265   }
   266 
   267 
   268   /* event handling */
   269 
   270   private val inner_key_listener =
   271     JEdit_Lib.key_listener(
   272       key_pressed = (e: KeyEvent) =>
   273         {
   274           if (!e.isConsumed) {
   275             e.getKeyCode match {
   276               case KeyEvent.VK_TAB =>
   277                 if (complete_selected()) e.consume
   278                 hide_popup()
   279               case KeyEvent.VK_ESCAPE =>
   280                 hide_popup()
   281                 e.consume
   282               case KeyEvent.VK_UP => move_items(-1); e.consume
   283               case KeyEvent.VK_DOWN => move_items(1); e.consume
   284               case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
   285               case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
   286               case _ =>
   287                 if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
   288                   hide_popup()
   289             }
   290           }
   291           propagate(e)
   292         },
   293       key_typed = propagate _
   294     )
   295 
   296   list_view.peer.addKeyListener(inner_key_listener)
   297 
   298   list_view.peer.addMouseListener(new MouseAdapter {
   299     override def mouseClicked(e: MouseEvent) {
   300       if (complete_selected()) e.consume
   301       hide_popup()
   302     }
   303   })
   304 
   305   list_view.peer.addFocusListener(new FocusAdapter {
   306     override def focusLost(e: FocusEvent) { hide_popup() }
   307   })
   308 
   309 
   310   /* main content */
   311 
   312   override def getFocusTraversalKeysEnabled = false
   313   completion.setBorder(new LineBorder(Color.BLACK))
   314   completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
   315 
   316 
   317   /* popup */
   318 
   319   private val popup =
   320   {
   321     val screen = JEdit_Lib.screen_location(layered, location)
   322     val size =
   323     {
   324       val geometry = JEdit_Lib.window_geometry(completion, completion)
   325       val bounds = Rendering.popup_bounds
   326       val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
   327       val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
   328       new Dimension(w, h)
   329     }
   330     new Popup(layered, completion, screen.relative(layered, size), size)
   331   }
   332 
   333   private def show_popup()
   334   {
   335     popup.show
   336     list_view.requestFocus
   337   }
   338 
   339   private def hide_popup()
   340   {
   341     val had_focus = list_view.peer.isFocusOwner
   342     popup.hide
   343     if (had_focus) refocus()
   344   }
   345 }
   346