Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

PositionLayout in Scala

By: fedarius on Aug 6th, 2011  |  syntax: Scala  |  size: 1.71 KB  |  hits: 62  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class PositionPanel extends Panel with LayoutContainer {
  2.   import PositionPanel._
  3.  
  4.   type Constraints = Seq[Tuple2[Property.PropertyValue, _ >: Double with Float with Int]]
  5.  
  6.   def layoutManager = peer.getLayout.asInstanceOf[PositionLayout]
  7.   override lazy val peer = new javax.swing.JPanel(new PositionLayout) with SuperMixin
  8.  
  9.   def areValid(c: Constraints): (Boolean, String) = (true, "")
  10.  
  11.   def constraintsFor (comp: Component) = {
  12.      var cs = layoutManager.getConstraints (comp.peer)
  13.      var seq: Constraints = Nil
  14.      cs foreach {
  15.        c => seq = seq :+ (c.property, c.value)
  16.      }
  17.      seq
  18.   }
  19.  
  20.   def add(c: Component, l: Constraints) {
  21.     layoutManager.addLayoutComponent (c.peer, convertConstraints (convertConstraints (l)))
  22.   }
  23. }
  24.  
  25. object Test extends SimpleSwingApplication {
  26.   val button1 = new Flex4Button
  27.  
  28.   def top = new MainFrame {
  29.     title = "Test"
  30.     preferredSize = new Dimension(860, 400)
  31.  
  32.     contents = new FlowPanel {
  33.       background = Color.RED
  34.  
  35.       contents += button1
  36.  
  37.     }
  38.   }
  39.  
  40.   def main: Unit = main(Array())
  41. }
  42.  
  43. object PositionTest extends SimpleSwingApplication {
  44.   val button2 = new Button("Button")
  45.  
  46.   val positionPanel = new PositionPanel {
  47.     preferredSize = new Dimension(200, 200)
  48.     background = Color.BLUE
  49.  
  50.     add(
  51.       button2,
  52.       Seq((PositionPanel.Property.Left, 10), (PositionPanel.Property.Right, 10)))
  53.   }
  54.  
  55.   def top = new MainFrame {
  56.     title = "Test"
  57.     preferredSize = new Dimension(860, 400)
  58.  
  59.     contents = new FlowPanel {
  60.       contents += positionPanel
  61.     }
  62.  
  63.     positionPanel.revalidate
  64.  
  65.     println (this)
  66.     println (contents(0))
  67.     println (positionPanel)
  68.   }
  69.   def main: Unit = main(Array())
  70. }