YourKit Java Profiler |
JFacets /
SwingDemoThis 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.
We'll use "panel builder facets" in order to create our swing panels for two roles :
Using the profiles inheritance, we'll iterate on some user profiles ( Here we go ! Main classHere's the content of our // 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 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; } } BuilderStandardThe panel builder that we assign to the @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; } } PanelBuilderAdminThe panel builder that we assign to the @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; } } ConclusionThis 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 ! |