Recent Changes - Search:


SourceForge Logo









YourKit Java Profiler

SwingDemo

This very little demo shows JFacets at work with a Swing app. The idea is of course to show profiled UIs to end-users based on their profile. These UIs will present some text as well as the current date.

NOTE
Once again, we use the SimpleProfileRepository here for the sake of simplicity, but the principle is similar with any other Profile Repository implementation.

We'll use "panel builder facets" in order to create our swing panels for two roles :

  • standard_role : for users that has a read-only view of the world.
  • admin_role : for users that have full privileges over our application

Using the profiles inheritance, we'll iterate on some user profiles (john and ivar, respecitvely inheriting of standard_role and admin_role in the profiles graph) in order to retrieve a panel builder facet for each of them. Then, we'll invoke this builder to create the views and display them.

Here we go !

Main class

Here's the content of our main method :

// get hold of a JFacets instance
JFacets jFacets = JFacets.get("/net/sourceforge/jfacets/examples/swing/swingDemoAppCtx.xml");

// our target object
Date currentDate = new Date();

// iterate on user names (those are defined in the SimpleProfileRepository as
// childs of standard_role and asmin_role), and create a JFrame using the
// facetized panel builders
String[] profileIds = {"john", "ivar"};
for (int i = 0; i < profileIds.length; i++) {

        JFrame frame = new JFrame("JFacets Swing demo, profile " + profileIds[i]);

        // retrieve a panel builder facet for the
        // supplied profile ID and target object,
        // and build the profiled panel...
        PanelBuilderFacet builder =
                (PanelBuilderFacet)jFacets.getFacet("panelBuilder", profileIds[i], currentDate);
        JPanel panel = builder.buildPanel();

        // show frame
        showFrame(frame, panel);
}

When we launch that main, we can see those two different frames :

john

ivar

So it worked : different panel builders have been retrieved and used for our two users !

Let's look at the code of our facets now...

PanelBuilderFacet (base class)

This is the base class for our facetized panel builders. It factorizes access to the facet context, and defines the abstract contract for panel builders :

public abstract class PanelBuilderFacet implements IFacet {

        private IFacetContext facetContext;

        public abstract JPanel buildPanel();

        public IFacetContext getContext() {
                return facetContext;
        }

        public void setContext(IFacetContext ctx) {
                this.facetContext = ctx;
        }

}

BuilderStandard

The panel builder that we assign to the standard_role (check the @FacetKey annotation) :

@FacetKey(name="panelBuilder", profileId="standard_role",targetObjectType=Date.class)
public class BuilderStandard extends PanelBuilderFacet {

        public JPanel buildPanel() {
                // get hold of the date (passed as the target object of this facet)
                Date date = (Date)getContext().getTargetObject();

                // and build the JPanel to be returned for profile "admin_role"
                JTextPane textPane = new JTextPane();
                textPane.setText("Hello, there !\nAs a standard user, you can't edit anything !");
                textPane.setEditable(false);
                DateFormat df = DateFormat.getDateInstance();
                JLabel label = new JLabel(df.format(date));
                JButton btn = new JButton("Doh !");
                JPanel panel = new JPanel(new BorderLayout());
                panel.add(textPane, BorderLayout.NORTH);
                panel.add(label, BorderLayout.CENTER);
                panel.add(btn, BorderLayout.SOUTH);
                return panel;
        }

}

PanelBuilderAdmin

The panel builder that we assign to the admin_role profile (check the @FacetKey annotation) :

@FacetKey(name="panelBuilder", profileId="admin_role",targetObjectType=Date.class)
public class BuilderAdmin extends PanelBuilderFacet {

        public JPanel buildPanel() {
                // get hold of the date (passed as the target object of this facet)
                Date date = (Date)getContext().getTargetObject();

                // and build the JPanel to be returned for profile "admin_role"
                JTextPane textPane = new JTextPane();
                textPane.setText("Hello, there !\nAs an admin, you can edit stuff...");
                JFormattedTextField jFormattedTextField = new JFormattedTextField(DateFormat.getDateInstance());
                jFormattedTextField.setValue(date);
                JButton btn1 = new JButton("Do");
                JButton btn2 = new JButton("funky");
                JButton btn3 = new JButton("stuff");
                JPanel buttonsPanel = new JPanel();
                buttonsPanel.add(btn1);
                buttonsPanel.add(btn2);
                buttonsPanel.add(btn3);
                JPanel panel = new JPanel(new BorderLayout());
                panel.add(textPane, BorderLayout.NORTH);
                panel.add(jFormattedTextField, BorderLayout.CENTER);
                panel.add(buttonsPanel, BorderLayout.SOUTH);
                return panel;
        }

}

Conclusion

This simpel example shows a practical JFacets example. Instead of using a basic, limited and painful "if/then/else" mechanism in order to hande profiling into some UI code, it's easier and more efficient to wrap the profiled code into some facets !

Edit - History - Print - Recent Changes - Search
Page last modified on April 14, 2007, at 07:49 PM