register single instance within root, in order to get rid of old popup as user continues typing;
authorwenzelm
Tue Aug 27 22:00:35 2013 +0200 (2013-08-27 ago)
changeset 532351b6a44859549
parent 53234 ea4abbbe1702
child 53236 837a6ef61fe9
register single instance within root, in order to get rid of old popup as user continues typing;
src/Tools/jEdit/src/completion_popup.scala
     1.1 --- a/src/Tools/jEdit/src/completion_popup.scala	Tue Aug 27 20:58:53 2013 +0200
     1.2 +++ b/src/Tools/jEdit/src/completion_popup.scala	Tue Aug 27 22:00:35 2013 +0200
     1.3 @@ -22,10 +22,29 @@
     1.4  
     1.5  object Completion_Popup
     1.6  {
     1.7 +  /* items */
     1.8 +
     1.9    private sealed case class Item(original: String, replacement: String, description: String)
    1.10    { override def toString: String = description }
    1.11  
    1.12 -  private def complete_item(text_area: JEditTextArea, item: Item)
    1.13 +
    1.14 +  /* register single instance within root */
    1.15 +
    1.16 +  private def register(root: JComponent, completion: Completion_Popup)
    1.17 +  {
    1.18 +    Swing_Thread.require()
    1.19 +
    1.20 +    root.getClientProperty(Completion_Popup) match {
    1.21 +      case old_completion: Completion_Popup => old_completion.hide_popup
    1.22 +      case _ =>
    1.23 +    }
    1.24 +    root.putClientProperty(Completion_Popup, completion)
    1.25 +  }
    1.26 +
    1.27 +
    1.28 +  /* jEdit text area operations */
    1.29 +
    1.30 +  private def complete_text_area(text_area: JEditTextArea, item: Item)
    1.31    {
    1.32      Swing_Thread.require()
    1.33  
    1.34 @@ -48,16 +67,16 @@
    1.35    {
    1.36      Swing_Thread.require()
    1.37  
    1.38 +    val view = text_area.getView
    1.39 +    val root = view.getRootPane
    1.40      val buffer = text_area.getBuffer
    1.41 +    val painter = text_area.getPainter
    1.42 +
    1.43      if (buffer.isEditable) {
    1.44        get_syntax match {
    1.45 -        case None =>
    1.46          case Some(syntax) =>
    1.47 -          val view = text_area.getView
    1.48 -          val painter = text_area.getPainter
    1.49 -
    1.50            val caret = text_area.getCaretPosition
    1.51 -          val completion =
    1.52 +          val result =
    1.53            {
    1.54              val line = buffer.getLineOfOffset(caret)
    1.55              val start = buffer.getLineStartOffset(line)
    1.56 @@ -74,8 +93,7 @@
    1.57                  else Some((word, ds.map(s => Item(word, s, s))))
    1.58              }
    1.59            }
    1.60 -          completion match {
    1.61 -            case None =>
    1.62 +          result match {
    1.63              case Some((original, items)) =>
    1.64                val popup_font =
    1.65                  painter.getFont.deriveFont(
    1.66 @@ -86,15 +104,25 @@
    1.67                if (location != null) {
    1.68                  location.y = location.y + painter.getFontMetrics.getHeight
    1.69                  SwingUtilities.convertPointToScreen(location, painter)
    1.70 -                new Completion_Popup(view.getRootPane, popup_font, location, items) {
    1.71 -                  override def complete(item: Item) { complete_item(text_area, item) }
    1.72 -                  override def propagate(evt: KeyEvent) { JEdit_Lib.propagate_key(view, evt) }
    1.73 -                  override def refocus() { text_area.requestFocus }
    1.74 -                }
    1.75 +
    1.76 +                val completion =
    1.77 +                  new Completion_Popup(root, popup_font, location, items) {
    1.78 +                    override def complete(item: Item) { complete_text_area(text_area, item) }
    1.79 +                    override def propagate(e: KeyEvent) {
    1.80 +                      JEdit_Lib.propagate_key(view, e)
    1.81 +                      if (!e.isConsumed) input_text_area(text_area, get_syntax, e)
    1.82 +                    }
    1.83 +                    override def refocus() { text_area.requestFocus }
    1.84 +                  }
    1.85 +                register(root, completion)
    1.86                }
    1.87 +              else register(root, null)
    1.88 +            case None => register(root, null)
    1.89            }
    1.90 +        case None => register(root, null)
    1.91        }
    1.92      }
    1.93 +    else register(root, null)
    1.94    }
    1.95  }
    1.96