src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Tue, 27 Aug 2013 20:45:02 +0200
changeset 53233 4b422e5d64fd
parent 53232 4a3762693452
child 53234 ea4abbbe1702
permissions -rw-r--r--
some actual completion via outer syntax; disable old SideKick completion for "isabelle" mode;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/completion_popup.scala
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     3
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     4
Completion popup based on javax.swing.PopupFactory.
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     5
*/
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     6
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     8
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
     9
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    10
import isabelle._
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    11
53229
6ce8328d7912 explicit "hidden" operation with focus management;
wenzelm
parents: 53228
diff changeset
    12
import java.awt.{Font, Point, BorderLayout, Dimension}
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
    13
import java.awt.event.{KeyEvent, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent}
53228
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    14
import javax.swing.{JPanel, JComponent, PopupFactory, SwingUtilities}
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    15
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    16
import scala.swing.{ListView, ScrollPane}
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    17
import scala.swing.event.MouseClicked
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    18
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    19
import org.gjt.sp.jedit.View
53228
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    20
import org.gjt.sp.jedit.textarea.JEditTextArea
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    21
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    22
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    23
object Completion_Popup
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    24
{
53233
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    25
  private sealed case class Item(original: String, replacement: String, description: String)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    26
  { override def toString: String = description }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    27
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    28
  private def complete_item(text_area: JEditTextArea, item: Item)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    29
  {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    30
    Swing_Thread.require()
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    31
53233
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    32
    val buffer = text_area.getBuffer
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    33
    val len = item.original.length
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    34
    if (buffer.isEditable && len > 0) {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    35
      JEdit_Lib.buffer_edit(buffer) {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    36
        val caret = text_area.getCaretPosition
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    37
        JEdit_Lib.try_get_text(buffer, Text.Range(caret - len, caret)) match {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    38
          case Some(text) if text == item.original =>
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    39
            buffer.remove(caret - len, len)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    40
            buffer.insert(caret - len, item.replacement)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    41
          case _ =>
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    42
        }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    43
      }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    44
    }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    45
  }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    46
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    47
  def input_text_area(text_area: JEditTextArea, get_syntax: => Option[Outer_Syntax], evt: KeyEvent)
53228
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    48
  {
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    49
    Swing_Thread.require()
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    50
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    51
    val buffer = text_area.getBuffer
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    52
    if (buffer.isEditable) {
53233
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    53
      get_syntax match {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    54
        case None =>
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    55
        case Some(syntax) =>
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    56
          val view = text_area.getView
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    57
          val painter = text_area.getPainter
53228
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    58
53233
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    59
          val caret = text_area.getCaretPosition
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    60
          val completion =
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    61
          {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    62
            val line = buffer.getLineOfOffset(caret)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    63
            val start = buffer.getLineStartOffset(line)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    64
            val text = buffer.getSegment(start, caret - start)
53228
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    65
53233
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    66
            syntax.completion.complete(text) match {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    67
              case None => None
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    68
              case Some((word, cs)) =>
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    69
                val ds =
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    70
                  (if (Isabelle_Encoding.is_active(buffer))
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    71
                    cs.map(Symbol.decode(_)).sorted
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    72
                   else cs).filter(_ != word)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    73
                if (ds.isEmpty) None
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    74
                else Some((word, ds.map(s => Item(word, s, s))))
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    75
            }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    76
          }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    77
          completion match {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    78
            case None =>
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    79
            case Some((original, items)) =>
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    80
              val popup_font =
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    81
                painter.getFont.deriveFont(
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    82
                  (painter.getFont.getSize2D * PIDE.options.real("jedit_popup_font_scale")).toFloat
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    83
                    max 10.0f)
53229
6ce8328d7912 explicit "hidden" operation with focus management;
wenzelm
parents: 53228
diff changeset
    84
53233
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    85
              val location = text_area.offsetToXY(caret - original.length)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    86
              if (location != null) {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    87
                location.y = location.y + painter.getFontMetrics.getHeight
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    88
                SwingUtilities.convertPointToScreen(location, painter)
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    89
                new Completion_Popup(view.getRootPane, popup_font, location, items) {
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    90
                  override def complete(item: Item) { complete_item(text_area, item) }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    91
                  override def propagate(evt: KeyEvent) { JEdit_Lib.propagate_key(view, evt) }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    92
                  override def hidden() { text_area.requestFocus }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    93
                }
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
    94
              }
53232
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
    95
          }
53228
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    96
      }
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    97
    }
f6c6688961db some key event handling in the manner of SideKickBindings, SideKickCompletionPopup;
wenzelm
parents: 53226
diff changeset
    98
  }
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
    99
}
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   100
53233
4b422e5d64fd some actual completion via outer syntax;
wenzelm
parents: 53232
diff changeset
   101
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   102
class Completion_Popup private(
53229
6ce8328d7912 explicit "hidden" operation with focus management;
wenzelm
parents: 53228
diff changeset
   103
  root: JComponent,
6ce8328d7912 explicit "hidden" operation with focus management;
wenzelm
parents: 53228
diff changeset
   104
  popup_font: Font,
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   105
  screen_point: Point,
53232
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   106
  items: List[Completion_Popup.Item]) extends JPanel(new BorderLayout)
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   107
{
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   108
  completion =>
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   109
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   110
  Swing_Thread.require()
53232
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   111
  require(!items.isEmpty)
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   112
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   113
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   114
  /* actions */
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   115
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   116
  def complete(item: Completion_Popup.Item) { }
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   117
  def propagate(evt: KeyEvent) { }
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   118
  def hidden() { }
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   119
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   120
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   121
  /* list view */
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   122
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   123
  private val list_view = new ListView(items)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   124
  {
53229
6ce8328d7912 explicit "hidden" operation with focus management;
wenzelm
parents: 53228
diff changeset
   125
    font = popup_font
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   126
  }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   127
  list_view.selection.intervalMode = ListView.IntervalMode.Single
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   128
  list_view.peer.setFocusTraversalKeysEnabled(false)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   129
  list_view.peer.setVisibleRowCount(items.length min 8)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   130
  list_view.peer.setSelectedIndex(0)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   131
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   132
  private def complete_selected(): Boolean =
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   133
  {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   134
    list_view.selection.items.toList match {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   135
      case item :: _ => complete(item); true
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   136
      case _ => false
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   137
    }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   138
  }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   139
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   140
  private def move_items(n: Int)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   141
  {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   142
    val i = list_view.peer.getSelectedIndex
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   143
    val j = ((i + n) min (items.length - 1)) max 0
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   144
    if (i != j) {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   145
      list_view.peer.setSelectedIndex(j)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   146
      list_view.peer.ensureIndexIsVisible(j)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   147
    }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   148
  }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   149
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   150
  private def move_pages(n: Int)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   151
  {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   152
    val page_size = list_view.peer.getVisibleRowCount - 1
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   153
    move_items(page_size * n)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   154
  }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   155
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   156
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   157
  /* event handling */
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   158
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   159
  private val key_listener =
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   160
    JEdit_Lib.key_listener(
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   161
      workaround = false,
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   162
      key_pressed = (e: KeyEvent) =>
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   163
        {
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   164
          if (!e.isConsumed) {
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   165
            e.getKeyCode match {
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   166
              case KeyEvent.VK_TAB =>
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   167
                if (complete_selected()) e.consume
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   168
                hide_popup()
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   169
              case KeyEvent.VK_ESCAPE =>
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   170
                hide_popup()
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   171
                e.consume
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   172
              case KeyEvent.VK_UP => move_items(-1); e.consume
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   173
              case KeyEvent.VK_DOWN => move_items(1); e.consume
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   174
              case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   175
              case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   176
              case _ =>
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   177
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   178
                  hide_popup()
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   179
            }
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   180
          }
53232
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   181
          propagate(e)
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   182
        },
53232
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   183
      key_typed = propagate _
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   184
    )
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   185
53226
9cf8e2263ca7 more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents: 53023
diff changeset
   186
  list_view.peer.addKeyListener(key_listener)
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   187
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   188
  list_view.peer.addMouseListener(new MouseAdapter {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   189
    override def mouseClicked(e: MouseEvent) {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   190
      if (complete_selected()) e.consume
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   191
      hide_popup()
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   192
    }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   193
  })
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   194
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   195
  list_view.peer.addFocusListener(new FocusAdapter {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   196
    override def focusLost(e: FocusEvent) { hide_popup() }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   197
  })
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   198
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   199
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   200
  /* main content */
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   201
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   202
  override def getFocusTraversalKeysEnabled = false
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   203
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   204
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   205
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   206
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   207
  /* popup */
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   208
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   209
  private val popup =
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   210
  {
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   211
    val screen_bounds = JEdit_Lib.screen_bounds(screen_point)
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   212
53230
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   213
    val x0 = root.getLocationOnScreen.x
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   214
    val y0 = root.getLocationOnScreen.y
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   215
    val w0 = root.getWidth
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   216
    val h0 = root.getHeight
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   217
53230
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   218
    val (w, h) =
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   219
    {
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   220
      val geometry = JEdit_Lib.window_geometry(completion, completion)
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   221
      val bounds = Rendering.popup_bounds
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   222
      val w = geometry.width min (screen_bounds.width * bounds).toInt min w0
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   223
      val h = geometry.height min (screen_bounds.height * bounds).toInt min h0
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   224
      (w, h)
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   225
    }
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   226
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   227
    val (x, y) =
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   228
    {
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   229
      val x1 = x0 + w0 - w
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   230
      val y1 = y0 + h0 - h
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   231
      val x2 = screen_point.x min (screen_bounds.x + screen_bounds.width - w)
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   232
      val y2 = screen_point.y min (screen_bounds.y + screen_bounds.height - h)
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   233
      ((x2 min x1) max x0, (y2 min y1) max y0)
6589ff56cc3c determine completion geometry like tooltip;
wenzelm
parents: 53229
diff changeset
   234
    }
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   235
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   236
    completion.setSize(new Dimension(w, h))
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   237
    completion.setPreferredSize(new Dimension(w, h))
53229
6ce8328d7912 explicit "hidden" operation with focus management;
wenzelm
parents: 53228
diff changeset
   238
    PopupFactory.getSharedInstance.getPopup(root, completion, x, y)
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   239
  }
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   240
53232
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   241
  private def hide_popup()
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   242
  {
53229
6ce8328d7912 explicit "hidden" operation with focus management;
wenzelm
parents: 53228
diff changeset
   243
    popup.hide
6ce8328d7912 explicit "hidden" operation with focus management;
wenzelm
parents: 53228
diff changeset
   244
    hidden()
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   245
  }
53232
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   246
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   247
  popup.show
4a3762693452 tuned signature;
wenzelm
parents: 53231
diff changeset
   248
  list_view.requestFocus
53023
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   249
}
f127e949389f Completion popup based on javax.swing.PopupFactory, which has better cross-platform chances than JWindow (cf. org/gjt/jedit/gui/CompletionPopup.java);
wenzelm
parents:
diff changeset
   250