How to create Scala swing shell classes using SuperMixin?

I am trying to understand how the following class works from an answer from this thread: Scala Pop-up menu

Since the thread is pretty old, I decided to just start a new question. I'm new to Scala with a Java background, and I wonder how this class works. I read that an object with the same name as a class looks like a class with a singleton object? I'm not sure how this is suitable for reaching the shell ... (why do we need an object?)

And what exactly does the SuperMixin trait do? The API says: “This trait is used to redirect specific calls from a peer to a wrapper and vice versa. Useful for exposing methods that can be configured using overrides.” which doesn't explain the beginner very well.

I would really appreciate it if someone could explain to the newbie how this class and object (it seems to me magically) get me a wrapper class for JPopupMenu and allow me to call the show method, which calls popupMenu on the screen ... and it seems that I can set its contents (contents + = some scala.swing.menuItem) if it is not defined in the class below?

import javax.swing.JPopupMenu
import scala.swing.{ Component, MenuItem }
import scala.swing.SequentialContainer.Wrapper

object PopupMenu {
  private[PopupMenu] trait JPopupMenuMixin { def popupMenuWrapper: PopupMenu }
}

class PopupMenu extends Component with Wrapper {

  override lazy val peer: JPopupMenu = new JPopupMenu with PopupMenu.JPopupMenuMixin with SuperMixin {
    def popupMenuWrapper = PopupMenu.this
  }

  def show(invoker: Component, x: Int, y: Int): Unit = peer.show(invoker.peer, x, y)

  /* Create any other peer methods here */
}
+2
source share
1 answer

PopupMenu , JPopupMenuMixin. "", private[PopupMenu], PopupMenu .

, . popupMenuWrapper, Swing Scala, . , :

import scala.swing._
import javax.swing.JPopupMenu

class PopupMenu extends Component with SequentialContainer.Wrapper {
  override lazy val peer: JPopupMenu = new JPopupMenu with SuperMixin

  def show(invoker: Component, x: Int, y: Int): Unit = peer.show(invoker.peer, x, y)
}

:

val pop = new PopupMenu {
  contents += new MenuItem("Foo")
}
lazy val but: Button = Button("Test") {
  pop.show(but, 0, 0)
}
val f = new Frame {
  contents = but
  pack().centerOnScreen()
  open()
}

, , - scala.swing.Component peer javax.swing. Mixin with SuperMixin , paintComponent, . .

SequentialContainer.Wrapper, contents += .

+2

Source: https://habr.com/ru/post/1526717/


All Articles