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