Created
April 1, 2012 13:45
-
-
Save delexi/2275388 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import info.clearthought.layout.{ TableLayout, TableLayoutConstants, TableLayoutConstraints } | |
| import scala.swing.{ Button, Component, LayoutContainer, MainFrame, Panel, SimpleSwingApplication } | |
| object TablePanel { | |
| sealed class CellJustification(val value: Int) | |
| case object Left extends CellJustification(TableLayoutConstants.LEFT) | |
| case object Top extends CellJustification(TableLayoutConstants.TOP) | |
| case object Center extends CellJustification(TableLayoutConstants.CENTER) | |
| case object Full extends CellJustification(TableLayoutConstants.FULL) | |
| case object Bottom extends CellJustification(TableLayoutConstants.BOTTOM) | |
| case object Right extends CellJustification(TableLayoutConstants.RIGHT) | |
| case object Leading extends CellJustification(TableLayoutConstants.LEADING) | |
| case object Trailing extends CellJustification(TableLayoutConstants.TRAILING) | |
| sealed class RowColJustification(val value: Double) | |
| case object Fill extends RowColJustification(TableLayoutConstants.FILL) | |
| case object Preferred extends RowColJustification(TableLayoutConstants.PREFERRED) | |
| case object Minimum extends RowColJustification(TableLayoutConstants.MINIMUM) | |
| implicit def tuple2ToTableConstraints(tuple: Tuple2[Int, Int]): TableLayoutConstraints = | |
| new TableLayoutConstraints(tuple._1, tuple._2, tuple._1, tuple._2, Full.value, Full.value) | |
| implicit def tuple4ToTableConstraints(tuple: Tuple4[Int, Int, Int, Int]): TableLayoutConstraints = | |
| new TableLayoutConstraints(tuple._1, tuple._2, tuple._3, tuple._4, Full.value, Full.value) | |
| implicit def tuple4JustiToTableConstraints(tuple: Tuple4[Int, Int, CellJustification, CellJustification]): TableLayoutConstraints = | |
| new TableLayoutConstraints(tuple._1, tuple._2, tuple._1, tuple._2, tuple._3.value, tuple._4.value) | |
| implicit def rowColJustification2Double(rcj: RowColJustification): Double = rcj.value | |
| } | |
| class TablePanel(layoutSpecs: Array[Array[Double]]) extends Panel with LayoutContainer { | |
| type Constraints = TableLayoutConstraints | |
| override lazy val peer = new javax.swing.JPanel(new TableLayout(layoutSpecs)) with SuperMixin | |
| protected def layoutManager = peer.getLayout.asInstanceOf[TableLayout] | |
| protected def constraintsFor(comp: Component) = | |
| layoutManager.getConstraints(comp.peer).asInstanceOf[Constraints] | |
| protected def add(comp: Component, cs: Constraints) = { | |
| peer.add(comp.peer, cs) | |
| } | |
| protected def areValid(cs: Constraints) = (true, "") | |
| } | |
| object TableLayoutTest extends SimpleSwingApplication { | |
| import TablePanel._ | |
| import TablePanel.{ CellJustification, RowColJustification } | |
| javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel") | |
| def top = new MainFrame { | |
| title = "TableLayoutTest" | |
| val layoutSpecs: Array[Array[Double]] = Array(Array(100, 100, Fill), Array(0.5, 0.5)) | |
| contents = new TablePanel(layoutSpecs) { | |
| layout += new Button { | |
| text = "0,0,1,0" | |
| } -> ((0, 0, 1, 0)) | |
| layout += new Button { | |
| text = "2,0,2,1" | |
| } -> ((2, 0, 2, 1)) | |
| layout += new Button { | |
| text = "0,1,left,bottom" | |
| } -> ((0, 1, Left, Bottom)) | |
| } | |
| visible = true | |
| } | |
| } |
Author
Zur doppelten Klammer: Irgendwann heute hatte ich es scho mit einfacher Klammerung versucht, das hat der Kompiler nicht genommen, weil er dachte das seien Parameter für "->". Aber ich denke mal das liegt daran, dass zu dem Zeitpunkt die implicits noch nicht richtig funktioniert haben.
Danke für den/das Review :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bei
button -> ((0,0,1,0))ist ein Klammerpaar unnötig.Tuple2[Int, Int]kann auch als(Int, Int)geschrieben werden.*Justificationsolltest du noch abstrakt machen.Ansonsten sieht es gut aus.