src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Tue, 27 Aug 2013 15:35:51 +0200
changeset 53229 6ce8328d7912
parent 53228 f6c6688961db
child 53230 6589ff56cc3c
permissions -rw-r--r--
explicit "hidden" operation with focus management; explicit popup_font; just one option jedit_popup_font_scale for tooltip and completion;

/*  Title:      Tools/jEdit/src/completion_popup.scala
    Author:     Makarius

Completion popup based on javax.swing.PopupFactory.
*/

package isabelle.jedit


import isabelle._

import java.awt.{Font, Point, BorderLayout, Dimension}
import java.awt.event.{KeyEvent, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent}
import javax.swing.{JPanel, JComponent, PopupFactory, SwingUtilities}

import scala.swing.{ListView, ScrollPane}
import scala.swing.event.MouseClicked

import org.gjt.sp.jedit.View
import org.gjt.sp.jedit.textarea.JEditTextArea


object Completion_Popup
{
  sealed case class Item(name: String, text: String)
  { override def toString: String = text }

  def apply(
    opt_view: Option[View],
    root: JComponent,
    popup_font: Font,
    screen_point: Point,
    items: List[Item],
    complete: Item => Unit,
    hidden: () => Unit): Completion_Popup =
  {
    Swing_Thread.require()

    require(!items.isEmpty)

    val completion =
      new Completion_Popup(opt_view, root, popup_font, screen_point, items, complete, hidden)
    completion.show_popup()
    completion
  }

  def input_text_area(text_area: JEditTextArea, evt: KeyEvent)
  {
    Swing_Thread.require()

    val buffer = text_area.getBuffer
    if (buffer.isEditable) {
      val view = text_area.getView
      val painter = text_area.getPainter
      val caret = text_area.getCaretPosition

      // FIXME
      def complete(item: Item) { }

      def hidden() { text_area.requestFocus }

      // FIXME
      val token_length = 0
      val items: List[Item] = Nil

      val popup_font =
        painter.getFont.deriveFont(
          (painter.getFont.getSize2D * PIDE.options.real("jedit_popup_font_scale")).toFloat
            max 10.0f)

      if (!items.isEmpty) {
        val location = text_area.offsetToXY(caret - token_length)
        if (location != null) {
          location.y = location.y + painter.getFontMetrics.getHeight
          SwingUtilities.convertPointToScreen(location, painter)
          apply(Some(view), view.getRootPane, popup_font, location, items, complete _, hidden _)
        }
      }
    }
  }
}


class Completion_Popup private(
  opt_view: Option[View],
  root: JComponent,
  popup_font: Font,
  screen_point: Point,
  items: List[Completion_Popup.Item],
  complete: Completion_Popup.Item => Unit,
  hidden: () => Unit) extends JPanel(new BorderLayout)
{
  completion =>

  Swing_Thread.require()


  /* list view */

  private val list_view = new ListView(items)
  {
    font = popup_font
  }
  list_view.selection.intervalMode = ListView.IntervalMode.Single
  list_view.peer.setFocusTraversalKeysEnabled(false)
  list_view.peer.setVisibleRowCount(items.length min 8)
  list_view.peer.setSelectedIndex(0)

  private def complete_selected(): Boolean =
  {
    list_view.selection.items.toList match {
      case item :: _ => complete(item); true
      case _ => false
    }
  }

  private def move_items(n: Int)
  {
    val i = list_view.peer.getSelectedIndex
    val j = ((i + n) min (items.length - 1)) max 0
    if (i != j) {
      list_view.peer.setSelectedIndex(j)
      list_view.peer.ensureIndexIsVisible(j)
    }
  }

  private def move_pages(n: Int)
  {
    val page_size = list_view.peer.getVisibleRowCount - 1
    move_items(page_size * n)
  }


  /* event handling */

  private val key_listener =
    JEdit_Lib.key_listener(
      workaround = false,
      key_pressed = (e: KeyEvent) =>
        {
          if (!e.isConsumed) {
            e.getKeyCode match {
              case KeyEvent.VK_TAB =>
                if (complete_selected()) e.consume
                hide_popup()
              case KeyEvent.VK_ESCAPE =>
                hide_popup()
                e.consume
              case KeyEvent.VK_UP => move_items(-1); e.consume
              case KeyEvent.VK_DOWN => move_items(1); e.consume
              case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
              case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
              case _ =>
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
                  hide_popup()
            }
          }
          if (!e.isConsumed) pass_to_view(e)
        },
      key_typed = (e: KeyEvent) =>
        {
          if (!e.isConsumed) pass_to_view(e)
        }
    )

  private def pass_to_view(e: KeyEvent)
  {
    opt_view match {
      case Some(view) if view.getKeyEventInterceptor == key_listener =>
        try {
          view.setKeyEventInterceptor(null)
          view.getInputHandler().processKeyEvent(e, View.ACTION_BAR, false)
        }
        finally { view.setKeyEventInterceptor(key_listener) }
      case _ =>
    }
  }

  list_view.peer.addKeyListener(key_listener)

  list_view.peer.addMouseListener(new MouseAdapter {
    override def mouseClicked(e: MouseEvent) {
      if (complete_selected()) e.consume
      hide_popup()
    }
  })

  list_view.peer.addFocusListener(new FocusAdapter {
    override def focusLost(e: FocusEvent) { hide_popup() }
  })


  /* main content */

  override def getFocusTraversalKeysEnabled = false

  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])


  /* popup */

  private val popup =
  {
    val screen_bounds = JEdit_Lib.screen_bounds(screen_point)

    val geometry = JEdit_Lib.window_geometry(completion, completion)

    val w = geometry.width min (screen_bounds.width / 2)
    val h = geometry.height min (screen_bounds.height / 2)

    completion.setSize(new Dimension(w, h))
    completion.setPreferredSize(new Dimension(w, h))

    val x = screen_point.x min (screen_bounds.x + screen_bounds.width - w)
    val y = screen_point.y min (screen_bounds.y + screen_bounds.height - h)
    PopupFactory.getSharedInstance.getPopup(root, completion, x, y)
  }

  def show_popup()
  {
    opt_view.foreach(view => view.setKeyEventInterceptor(key_listener))
    popup.show
    list_view.requestFocus
  }

  def hide_popup()
  {
    opt_view match {
      case Some(view) if (view.getKeyEventInterceptor == key_listener) =>
        view.setKeyEventInterceptor(null)
      case _ =>
    }
    popup.hide
    hidden()
  }
}