Swixml, XML-based domain-specific language for declaring Java Swing GUIs

60 %
40 %
Information about Swixml, XML-based domain-specific language for declaring Java Swing GUIs

Published on December 15, 2008

Author: wolfpaulus

Source: slideshare.net


SwiXml is a small GUI generating engine for Java applications and applets. Graphical User Interfaces are described in XML documents that are parsed at runtime and rendered into javax.swing objects.
Theoretically, you can look at SwiXml as an XML based domain-specific language, allowing to declare Java Swing GUIs. The Swixml engine will then later at runtime, take a GUI declaration and create that User-Interface on the fly, just like a Web Browser would do with an HTML document.
SwiXml doesn\'t introduce any new layout managers or component classes. Instead, it operates directly on the Swing component classes using introspection.

“GUIs are described in XML documents,parsed at runtime, and rendered intojavax.swing components.” [Apache License V.2.0]

<html> <head> .. <link rel="stylesheet" type="text/css" href="/Styles/main.css"/> <script type="text/javascript" src="/Scripts/global.js"/> </head> <body> <div id="content"> .. </div> .. </body></html> Separation of Concerns

Models Knowledge Requirements DeclarativeReal World Architecture Imperative Code

<?xml version="1.0" encoding="UTF-8"?><frame size="440,380" title="Hello SWIXML World"> <panel constraints="BorderLayout.NORTH"> <label font="ARIAL-BOLD-16" foreground="blue" text="Hello World!"/> </panel> <panel constraints="BorderLayout.SOUTH"> <button text="Click Here" /> </panel></frame>

public class Foo { public Foo() throws Exception { new SwingEngine().render( "xml/gui.xml" ).setVisible( true ); } public static void main( String[] args ) throws Exception { new Foo(); }}

<frame size="440,380" title="Hello SWIXML World"> ... ... equivalent to ... JFrame frame = new JFrame(); frame.setSize( new Dimension( 440, 380 ) ); frame.setTitle( “Hello SWIXML World” );

● applet ● label ● slider● button ● list ● spinner● buttongroup ● menu ● splitpane● checkbox ● menubar ● tabbedpane● checkboxmenuitem ● menuitem ● tableElements● combobox ● optionpane ● tableheader● component ● panel ● textarea● desktoppane ● passwordfield ● textfield● dialog ● popupmenu ● textpane● editorpane ● progressbar ● togglebutton● formattedtextfield ● radiobutton ● toolbar● frame ● radiobuttonmenuitem ● tree● hbox ● scrollpane ● vbox● internalframe ● separator ● ... custom tags

public class Foo { public Foo() throws Exception { SwingEngine.getTaglib().registerTag( "Calendar", JCalendar.class ); new SwingEngine().render( "xml/jcal.xml" ).setVisible( true ); } public static void main( String[] args ) throws Exception { new Foo(); }}

<?xml version="1.0" encoding="UTF-8" ?><frame title="Custom Tags" size="400,260"> <panel layout="BorderLayout"> <panel background="white" constraints="BorderLayout.NORTH"> <label font="Comic Sans MS-BOLD-18" foreground="black"> Custom Components in Swixml </label> </panel> <calendar border="TitledBorder(JCalendar)" initclass="java.util.Date"/> </panel></frame>

<frame ... plaf="org.jvnet.substance.skin.SubstanceRavenLookAndFeel"> QuaquaSubstance

<panel> <combobox action=”doSelect” initclass=”com.company.MyModel”/> </panel>.. equivalent to .. initclass() initclass( String s ) initclass.getInstance() JPanel pnl = new JPanel(); JComboBox cbx = new JComboBox(); cbx.setModel ( new com.company.MyModel() ); cbx.setAction( client.doSelect ); pnl.add( cbx );

<frame ... bundle="locale.myBundle" locale="de">1 src 1 com.company MyClass.java ... 1 xml myGUI.xml ... 1 locale myBundle_en.properties myBundle_fr.properties1 myBundle_de.properties

accelerator name disabledicons text Localized displayedmnemonics title icon titleat icons titles Attributes iconimage tooltiptext label tooltiptexts mnemonic ... custom attributes mnemonicsorg.swixml.Parser.LOCALIZED_ATTRIBUTES.add( "myAttribute" )

<label text="beatles_addr" color="blue"/> vs. <label color="blue"> Abbey Road </label>

<label text="My blue label" foreground="blue" font="Verdana-BOLD-18”/> java.swing.Action java.awt.Insets javax.swing.border.Border javax.swing.KeyStroke java.awt.Color java.awt.LayoutManager java.awt.Dimension java.util.Locale Converters java.awt.Font java.awt.Image java.awt.Point java.awt.Rectangle javax.swing.Icon Primitives javax.swing.IconImage ... custom converters

public interface Converter { Object convert( Class type, Attribute attr, Localizer localizer ); Class convertsTo(); }ConverterLibrary.getInstance().register( myConverter );

package org.swixml.converters;import org.jdom.Attribute;import org.swixml.*;import java.awt.*;public class FontConverter implements Converter { public Object convert( Class type, Attribute attr, Localizer localizer ) throws Exception { return attr != null ? Font.decode( attr.getValue() ) : null; } public Class convertsTo() { return Font.class; }}

<?xml version="1.0" encoding="UTF-8" ?><frame ... > <menubar> <menu text="File"> <menuitem id="mi_save" text="Save" actioncommand="AC_SAVE"/> .. <panel border="EtchedBorder"> <button id="btn1" . . . /> ..public class Foo implements ActionListener { private JButton btn1; // auto-assigned by Swixml private JMenuItem mi_save; // auto-assigned by Swixml public Foo() throws Exception { Container c = new SwingEngine( this ).render( "xml/gui.xml" ); mi_save.addActionListener( this ); ..

<?xml version="1.0" encoding="UTF-8" ?><frame ... > <menubar> <menu text="File"> <menuitem text="Open File" action="open"/> <separator/> ..</frame>public class Foo { public Action open = new AbstractAction() { // auto-bound public void actionPerformed( ActionEvent e ) { // by Swixml ... } }; public Foo() throws Exception { new SwingEngine( this ).render( "xml/gui.xml" ).setVisible( true ); ..



Joshua Marinacci, Member of Sun’s Swing Team: “The net result of working with SwixML is delivering better-looking applications in less time, and that is always a good thing.” Ben Galbraith, author of Professional JSP 2.0 and Professional Apache Tomcat: “SwixML is the most mature XUL/Swing offering. If youre looking for a way to define your Swing UIs in XML, SwixML is one of the best choices around.” Rick Jelliffe, CTO of Topologi: “By moving to SwixML, we reduced source code size by 500k and improved maintainability and startup time.” Kate Rhodes: “I think that SwixML is the easiest way to define a Swing layout, period.” Hans Mueller, CTO for Suns Desktop Division: ”When defining Java Swing GUIs declaratively, SwixML is the strongest example.”© 2003-2006 Carlsbad Cubes© 2011 wolfpaulus.com

Thanks for coming

Add a comment