2008-07-12 09:03:24 UTC
Thierry,
In reverse order:
2. Metawidget is all about re-using *existing* technologies, so I'm not keen on inventing an expression language (EL) such as 'haveLegend=true'. Swing doesn't have an EL, but many of the other environments do. For Swing, this sort of thing would best be handled by a custom Inspector. You'd write an Inspector and add it to your CompositeInspector. The Inspector would say 'if field x is null, then set field y to be hidden'. See MetawidgetAnnotationInspector for an example of inspecting a field to see if it's null (eg. UiLookup.onlyIfNull).
1. Sort of. Not everybody would want this so it's not the 'generic' case. And there are a few different ways you could do it (most cleanly, using the method described in 2 above). However, here's a quick way:
public class Main
{
public static void main( String[] args )
{
// Data model
final Foo foo = new Foo();
// Metawidget
final SwingMetawidget metawidget = new SwingMetawidget();
metawidget.setInspector( new PropertyTypeInspector() );
metawidget.setToInspect( foo );
if ( foo.bar == null )
{
final JButton button = new JButton();
button.setAction( new AbstractAction( "init" ) {
@Override
public void actionPerformed( ActionEvent e )
{
foo.bar = new Bar();
metawidget.remove( button );
metawidget.repaint();
}
} );
button.setName( "bar" );
metawidget.add( button );
}
// JFrame
JFrame frame = new JFrame( "Metawidget Tutorial" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add( metawidget );
frame.setSize( 400, 210 );
frame.setVisible( true );
}
public static class Foo
{
public String name;
public Bar bar;
}
public static class Bar
{
public String baz;
}
}