src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Tue Aug 27 17:17:20 2013 +0200 (2013-08-27 ago)
changeset 53232 4a3762693452
parent 53231 423e29f1f304
child 53233 4b422e5d64fd
permissions -rw-r--r--
tuned signature;
     1 /*  Title:      Tools/jEdit/src/completion_popup.scala
     2     Author:     Makarius
     3 
     4 Completion popup based on javax.swing.PopupFactory.
     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, PopupFactory, 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   sealed case class Item(name: String, text: String)
    26   { override def toString: String = text }
    27 
    28   def input_text_area(text_area: JEditTextArea, evt: KeyEvent)
    29   {
    30     Swing_Thread.require()
    31 
    32     val buffer = text_area.getBuffer
    33     if (buffer.isEditable) {
    34       val view = text_area.getView
    35       val painter = text_area.getPainter
    36       val caret = text_area.getCaretPosition
    37 
    38       // FIXME
    39       def complete(item: Item) { }
    40 
    41       // FIXME
    42       val token_length = 0
    43       val items: List[Item] = Nil
    44 
    45       val popup_font =
    46         painter.getFont.deriveFont(
    47           (painter.getFont.getSize2D * PIDE.options.real("jedit_popup_font_scale")).toFloat
    48             max 10.0f)
    49 
    50       if (!items.isEmpty) {
    51         val location = text_area.offsetToXY(caret - token_length)
    52         if (location != null) {
    53           location.y = location.y + painter.getFontMetrics.getHeight
    54           SwingUtilities.convertPointToScreen(location, painter)
    55           new Completion_Popup(view.getRootPane, popup_font, location, items) {
    56             override def propagate(evt: KeyEvent) { JEdit_Lib.propagate_key(view, evt) }
    57             override def hidden() { text_area.requestFocus }
    58           }
    59         }
    60       }
    61     }
    62   }
    63 }
    64 
    65 class Completion_Popup private(
    66   root: JComponent,
    67   popup_font: Font,
    68   screen_point: Point,
    69   items: List[Completion_Popup.Item]) extends JPanel(new BorderLayout)
    70 {
    71   completion =>
    72 
    73   Swing_Thread.require()
    74   require(!items.isEmpty)
    75 
    76 
    77   /* actions */
    78 
    79   def complete(item: Completion_Popup.Item) { }
    80   def propagate(evt: KeyEvent) { }
    81   def hidden() { }
    82 
    83 
    84   /* list view */
    85 
    86   private val list_view = new ListView(items)
    87   {
    88     font = popup_font
    89   }
    90   list_view.selection.intervalMode = ListView.IntervalMode.Single
    91   list_view.peer.setFocusTraversalKeysEnabled(false)
    92   list_view.peer.setVisibleRowCount(items.length min 8)
    93   list_view.peer.setSelectedIndex(0)
    94 
    95   private def complete_selected(): Boolean =
    96   {
    97     list_view.selection.items.toList match {
    98       case item :: _ => complete(item); true
    99       case _ => false
   100     }
   101   }
   102 
   103   private def move_items(n: Int)
   104   {
   105     val i = list_view.peer.getSelectedIndex
   106     val j = ((i + n) min (items.length - 1)) max 0
   107     if (i != j) {
   108       list_view.peer.setSelectedIndex(j)
   109       list_view.peer.ensureIndexIsVisible(j)
   110     }
   111   }
   112 
   113   private def move_pages(n: Int)
   114   {
   115     val page_size = list_view.peer.getVisibleRowCount - 1
   116     move_items(page_size * n)
   117   }
   118 
   119 
   120   /* event handling */
   121 
   122   private val key_listener =
   123     JEdit_Lib.key_listener(
   124       workaround = false,
   125       key_pressed = (e: KeyEvent) =>
   126         {
   127           if (!e.isConsumed) {
   128             e.getKeyCode match {
   129               case KeyEvent.VK_TAB =>
   130                 if (complete_selected()) e.consume
   131                 hide_popup()
   132               case KeyEvent.VK_ESCAPE =>
   133                 hide_popup()
   134                 e.consume
   135               case KeyEvent.VK_UP => move_items(-1); e.consume
   136               case KeyEvent.VK_DOWN => move_items(1); e.consume
   137               case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
   138               case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
   139               case _ =>
   140                 if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
   141                   hide_popup()
   142             }
   143           }
   144           propagate(e)
   145         },
   146       key_typed = propagate _
   147     )
   148 
   149   list_view.peer.addKeyListener(key_listener)
   150 
   151   list_view.peer.addMouseListener(new MouseAdapter {
   152     override def mouseClicked(e: MouseEvent) {
   153       if (complete_selected()) e.consume
   154       hide_popup()
   155     }
   156   })
   157 
   158   list_view.peer.addFocusListener(new FocusAdapter {
   159     override def focusLost(e: FocusEvent) { hide_popup() }
   160   })
   161 
   162 
   163   /* main content */
   164 
   165   override def getFocusTraversalKeysEnabled = false
   166 
   167   completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
   168 
   169 
   170   /* popup */
   171 
   172   private val popup =
   173   {
   174     val screen_bounds = JEdit_Lib.screen_bounds(screen_point)
   175 
   176     val x0 = root.getLocationOnScreen.x
   177     val y0 = root.getLocationOnScreen.y
   178     val w0 = root.getWidth
   179     val h0 = root.getHeight
   180 
   181     val (w, h) =
   182     {
   183       val geometry = JEdit_Lib.window_geometry(completion, completion)
   184       val bounds = Rendering.popup_bounds
   185       val w = geometry.width min (screen_bounds.width * bounds).toInt min w0
   186       val h = geometry.height min (screen_bounds.height * bounds).toInt min h0
   187       (w, h)
   188     }
   189 
   190     val (x, y) =
   191     {
   192       val x1 = x0 + w0 - w
   193       val y1 = y0 + h0 - h
   194       val x2 = screen_point.x min (screen_bounds.x + screen_bounds.width - w)
   195       val y2 = screen_point.y min (screen_bounds.y + screen_bounds.height - h)
   196       ((x2 min x1) max x0, (y2 min y1) max y0)
   197     }
   198 
   199     completion.setSize(new Dimension(w, h))
   200     completion.setPreferredSize(new Dimension(w, h))
   201     PopupFactory.getSharedInstance.getPopup(root, completion, x, y)
   202   }
   203 
   204   private def hide_popup()
   205   {
   206     popup.hide
   207     hidden()
   208   }
   209 
   210   popup.show
   211   list_view.requestFocus
   212 }
   213