« Previous day (Mar 30, 2005) | Main | Next day (Apr 1, 2005) »

20050331 Thursday March 31, 2005

Creating Tabbed Menus

Somebody asked on the forum how to create a Tab menu using standard JSF components.

Here's one way you can do it. It's not easy (yet!) but works. The solution involves the following steps:

First go ahead and create your TabFragment. In it, drop a gridpanel. Let's say you want 6 pages to be part of this tab. Set the columns property to 6 - this will ensure that the components are laid out in a single horizontal row.

Now drop 6 hyperlinks into the page. The tab text for the nested output text components should be the displayed tab names. The action properties for the action links should be logical navigation case names - these can be anything, as long as you use the same names in your navigation file. (Note that each link action you dropped results in two separate components - the link, which has an action property, and a nested output text, which has the text string. Use the application outline when in doubt.)

Next, go and create all six pages that will be part of the tabbed set. On each page, include the tab fragment. Next go to the navigation file, switch to the XML file view (because wildcards are not yet well supported in the design view), and add a rule like this - use cut & paste:

    <navigation-rule>
        <from-view-id>*</from-view-id>
        <navigation-case>
            <from-outcome>viewPreferences</from-outcome>
            <to-view-id>/ViewPreferences.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

This assumes that one of the links in the tabbed fragment has the action attribute set to "viewPreferences", and when clicked, this will navigate to the ViewPreferences.jsp page.

Repeat the above for all the hyperlinks in your tabbed fragment such that each has a page, a corresponding link, and a corresponding navigation rule.

Voila! You're ready to run - you now have a functional (but ugly) tabbed window set up. You should be able to run and navigate the tabs as expected.

The next thing you'll want to do is create some sort of visual feedback for the tabs, such that exactly one tab is shown as "current" (e.g. it has a tabbed border). Likewise, you'll probably want to change the link styling for this tab (so it looks like plain text) and for the other tabs (so they look less like hyperlinks).

We can achieve this with CSS. Let's say you add these two rules to your stylesheet:

.selected { }
.notselected { }
We'll tweak these later. Now the big question is: How do we get the tab fragment to automatically style the different portions of the tabbed fragments according to which tab is selected?

Go to your tabbed fragment's backing bean, and add some code like this:

    // XXX You MUST keep this in sync with the actual links included in the page fragment,
    // and navigation destinations in the navigation.xml file!
    static String[] tabs = { "/Page1.jsp",
                             "/Page2.jsp",
                             "/Page3.jsp",
                             "/Page4.jsp",
                             "/Page5.jsp",
                             "/ViewPreferences.jsp"};

    private int getSelectedIndex() {
         FacesContext fc = getContext();
         String viewId = fc.getViewRoot().getViewId();
         for (int i = 0; i < tabs.length; i++) {
             if (viewId.equals(tabs[i])) {
                 return i;
             }
         }
         return-1;
    }

    public String getColClasses() {
        StringBuffer sb = new StringBuffer();
        int sel = getSelectedIndex();
        for (int i = 0; i < sel; i++) {
            sb.append("notselected,");
        }
        sb.append("selected,");
        for (int i = sel+1; i < tabs.length; i++) {
            sb.append("notselected,");
        }
        return sb.toString();
    }
The whole point of this code is to create a property in the page fragment, called "colClasses", which will return something like "notselected selected notselected notselected notselected notselected" if for example the second tab is selected. And how do we know which tab is selected? It's obviously the page that is including the tabbed fragment being rendered! So the getSelectedIndex() method goes out and finds out what the root page name is, then looks that up in its tabbed page list (which you need to edit obviously), and based on this computes a comma-separated list of style class names.

The list of style class names can be bound directly to the grid panel in the page fragment which is including the links. Go and select it, then set its columnClasses property to the value binding expression #{TabFragment.colClasses}. (You can do this by right clicking on the grid panel and choosing Property Bindings... too, then drill into your page fragment and locate the colClasses name. Don't forget to hit Apply.)

We're almost done. We now apply different styles to the different cells rendered for the tabs in the tab fragment. Now we just need to play with CSS to create tab-like effects.

Here are some things you'll want to try:

I wish I could share my stylesheet with you but I used an existing proprietary one. If anyone does work on this and wants to publish their solution feel free to append a comment! Good luck and let me know how it works out!

(2005-03-31 22:19:15.0) Permalink Comments [5]

Hippie Completion

I read in Cendric Beaust's blog that the latest Eclipse snapshot now has "hippie completion". This is an editor feature where you press a keybinding and the editor automatically completes the word you're typing for you, by searching around in your current document (and if not found) in other documents.

NetBeans has had this feature for at least five years. Since Creator is built on top of NetBeans, you'll find it in Creator too.

Hippie completion is also known as hippie expansion and originated in Emacs. See for example the XEmacs documentation.

It is bound to Ctrl-K (and on the Mac, Command-K). Go ahead and try it - it's the best way to figure out how it works. If the word you want to complete has many possible matches, hit Ctrl-K repeatedly to cycle through the matches. For example, in a typical Creator file, if you type "Ht" and hit Ctrl-K, it's going to offer HtmlCommandButton, HtmlOutputText, etc. if you have buttons and text components on the page. Now aren't you glad you didn't have to type that?

While I'm on the topic of editor shortcuts, another feature you may not know about it "Go Back". Let's say you've jumped around in your editor files, by clicking on error links in the output window, or by using "Go to declaration" (alt-g if I remember correctly), etc. You can "Go Back" just like in a browser by hitting Alt-K. And to move forward again, hit Alt-L. (And yes, these have toolbar buttons in Creator, and in recent NetBeans 4.1 builds as well.)

(2005-03-31 20:07:55.0) Permalink Comments [2]