+1! I created a rough-draft BXMLCompiler back in the day and never completed it. It used variables whose names were determined by counters, but the generated code you have is SO much easier to read.
-T On Mon, Dec 27, 2010 at 9:03 AM, Greg Brown <[email protected]> wrote: > Great approach! Is that representative of what your BXML->Java compiler > generates? I really like how it uses anonymous inner classes as the > "builder" mechanism. > > On Dec 27, 2010, at 7:41 AM, calathus wrote: > > Greg, > I considered implementing Pivot builder in Scala following scala-squib, but > I found a few issues for this type of approach. > Basically, it will depend on more runtime binding, so it will reduce the > advantage of statically typed language. > After I implemented bxml to Java converter, it became clear that the > translation can be done more directly reflecting the original BXML > structure, also without using variable in translated Java codes. > > For example, detail_pane.bxml can be translated in Java as following. This > style may be used instead of BXML. > Do you have any opinion for this approach? > > public static Object getComponent() { > try { > return new BoxPane() {{ > setOrientation(Orientation.VERTICAL); > setStyles("{fill:true}"); > add(new Label() {{ > setTextKey("companyName"); > setStyles("{font:{size:12, bold:true}}"); > }}); > add(new Separator() {{ > }}); > add(new Form() {{ > setStyles("{padding:0, fill:true, showFlagIcons:false, > showFlagHighlight:false, leftAlignLabels:true}"); > getSections().add(new Form.Section() {{ > final ValueMapping valueMapping_5 = new > ValueMapping(); > final ChangeMapping changeMapping_6 = new > ChangeMapping(); > final VolumeMapping volumeMapping_7 = new > VolumeMapping(); > add(new Label() {{ > setTextKey("value"); > setTextBindMapping(valueMapping_5); > setStyles("{horizontalAlignment:'right'}"); > Form.setLabel(this, "value"); > }}); > add(new Label() {{ > setTextKey("change"); > setTextBindMapping(changeMapping_6); > setStyles("{horizontalAlignment:'right'}"); > Form.setLabel(this, "change"); > }}); > add(new Label() {{ > setTextKey("openingValue"); > setTextBindMapping(valueMapping_5); > setStyles("{horizontalAlignment:'right'}"); > Form.setLabel(this, "openingValue"); > }}); > add(new Label() {{ > setTextKey("highValue"); > setTextBindMapping(valueMapping_5); > setStyles("{horizontalAlignment:'right'}"); > Form.setLabel(this, "highValue"); > }}); > add(new Label() {{ > setTextKey("lowValue"); > setTextBindMapping(valueMapping_5); > setStyles("{horizontalAlignment:'right'}"); > Form.setLabel(this, "lowValue"); > }}); > add(new Label() {{ > setTextKey("volume"); > setTextBindMapping(volumeMapping_7); > setStyles("{horizontalAlignment:'right'}"); > Form.setLabel(this, "volume"); > }}); > }}); > }}); > }}; > } catch (Exception e) { > e.printStackTrace(); > throw new RuntimeException(e); > } > } > > ----- > detail_pane.bxml > > <BoxPane orientation="vertical" styles="{fill:true}" > xmlns:bxml="http://pivot.apache.org/bxml" > xmlns:stocktracker="org.apache.pivot.tutorials.stocktracker" > xmlns="org.apache.pivot.wtk"> > <Label textKey="companyName" styles="{font:{size:12, bold:true}}"/> > > <Separator/> > > <Form styles="{padding:0, fill:true, showFlagIcons:false, > showFlagHighlight:false, > leftAlignLabels:true}"> > <Form.Section> > <bxml:define> > <stocktracker:ValueMapping bxml:id="valueMapping"/> > <stocktracker:ChangeMapping bxml:id="changeMapping"/> > <stocktracker:VolumeMapping bxml:id="volumeMapping"/> > </bxml:define> > > <Label bxml:id="valueLabel" Form.label="%value" > textKey="value" textBindMapping="$valueMapping" > styles="{horizontalAlignment:'right'}"/> > <Label bxml:id="changeLabel" Form.label="%change" > textKey="change" textBindMapping="$changeMapping" > styles="{horizontalAlignment:'right'}"/> > <Label bxml:id="openingValueLabel" Form.label="%openingValue" > textKey="openingValue" textBindMapping="$valueMapping" > styles="{horizontalAlignment:'right'}"/> > <Label bxml:id="highValueLabel" Form.label="%highValue" > textKey="highValue" textBindMapping="$valueMapping" > styles="{horizontalAlignment:'right'}"/> > <Label bxml:id="lowValueLabel" Form.label="%lowValue" > textKey="lowValue" textBindMapping="$valueMapping" > styles="{horizontalAlignment:'right'}"/> > <Label bxml:id="volumeLabel" Form.label="%volume" > textKey="volume" textBindMapping="$volumeMapping" > styles="{horizontalAlignment:'right'}"/> > </Form.Section> > </Form> > </BoxPane> > > On Tue, Dec 21, 2010 at 11:06 AM, calathus <[email protected]> wrote: > >> Greg, >> There were a few questions, and I did not have investigated these lines >> much, so I searched the similar projects. >> >> For the CRUD stuff, I will answer later. >> >> For the Scala Pivot builder approach, as you and Clint suggested, I was >> thinking something similar o Gvoovy's SwingBuilder approach. >> Right now, Scala 2.8.1's scala.swing library is not taking this type of >> builder approach. So I looked for some other library. >> I found squib project: >> http://code.google.com/p/scala-squib/ >> >> >> Unfortunately, Scala is still evolving language, this squid project could >> not be compiled in the latest 2.8.1, but with a few code changes, I could >> resolved these errors. (but somehow how, running demo did not work with ant) >> >> So If we change Swing API to Pivot API, we will be able to have similar >> builder. >> Here is a sample code from the squib project: >> >> import java.awt.GridBagConstraints._ >> import java.beans.{PropertyChangeListener, PropertyChangeEvent} >> import java.lang.reflect.Method >> import javax.swing._ >> import javax.swing.event._ >> >> import scala.reflect.BeanProperty >> >> import net.miginfocom.swing.MigLayout >> >> package tfd.scala.squib.demo { >> >> import tfd.scala.squib._ >> import tfd.scala.squib.event._ >> >> object RGBSliders extends Application { >> class RGBCanvasComponent extends JComponent { >> private var red:Int = 0 >> private var green:Int = 0 >> private var blue:Int = 0 >> >> def setRed(value:Int) = { >> if (value != red) { >> red = value >> repaint() >> } >> } >> >> def setGreen(value:Int) = { >> if (value != green) { >> green = value >> repaint() >> } >> } >> >> def setBlue(value:Int) = { >> if (value != blue) { >> blue = value >> repaint() >> } >> } >> >> override def paint(g: Graphics):Unit = { >> g.setColor(new Color(red, green, blue)) >> val d = getSize() >> g.fillRect(0,0, d.width, d.height) >> } >> } >> >> object rgbcanvas extends BuiltComponent[RGBCanvasComponent] { >> def newComponent = new RGBCanvasComponent() >> } >> >> lazy val rgb = rgbcanvas.id("rgbcanvas") >> >> frame( >> 'title->"RGB Selector", >> 'visible -> true, >> 'defaultCloseOperation -> JFrame.EXIT_ON_CLOSE, >> 'layout -> new MigLayout("fill, wrap 3", "align right, grow", >> "align top, grow"), >> contents( >> label("Red:"), >> slider("red", 'maximum -> 255, 'minimum-> 0, 'value->0, >> sliderValueChanged(&rgb.setRed)), >> rgbcanvas("rgbcanvas", >> 'preferredSize -> new Dimension(100,100), >> 'red -> 0, >> 'green -> 0, >> 'blue -> 0 >> ) -> "spany 3", >> label("Green:"), >> slider("green", 'maximum -> 255, 'minimum-> 0, 'value->0, >> sliderValueChanged(&rgb.setGreen)), >> label("Blue:"), >> slider("blue", 'maximum -> 255, 'minimum-> 0, 'value->0, >> sliderValueChanged(&rgb.setBlue)) >> ) >> ).pack >> } >> } >> >> >> On Mon, Dec 20, 2010 at 6:04 AM, Greg Brown <[email protected]> wrote: >> >>> As Chris mentioned, Pivot does not currently provide a BXML to Java >>> compiler. We actually prototyped one a while back, but it was never >>> completed due to other priorities. >>> >>> Generating byte code from BXML might be tough, but converting it to a >>> Java source file would probably be pretty easy. Let me know if you are >>> interested in trying to write something like this - I would be happy to try >>> to help when I can. >>> >>> G >>> >>> On Dec 19, 2010, at 10:26 PM, calathus wrote: >>> >>> > Hi, >>> > I'm looking for a tool to convert bxml file to equivalent Java source >>> program. >>> > Are there such tools? >>> > >>> > I just started using pivot, but many of sample is based on bxml. >>> > I think in order to develop a UI library with Java generic class, XML >>> file based approach is not appropriate. >>> > >>> > And xml based approach will reduce static error detection capability. >>> > I liked to see more examples using Java classes to define GUI without >>> relying on bxml files. >>> > >>> > -- >>> > Cheers, >>> > calathus >>> > >>> > >>> > >>> >>> >> >> >> -- >> Cheers, >> calathus >> >> >> >> > > > -- > Cheers, > calathus > > > > >
