DigitalJoel

2009/09/28

Using ui:insert to include children

Filed under: facelets, JSF — digitaljoel @ 10:20 pm

Let’s say you have a custom facelets component that wraps an h:selectOneListbox. As a part of using this component, you read the list of items that should be displayed in the list from a database table. Your bean reads the results of the database query and creates listItems, that are then passed back to the h:selectOneListbox, through your my:customListbox component. For instance:

            <my:customListbox
              fieldName="itemValue"
              entity="#{myBean}"
              items="#{myDatabaseBean.listItems}" />

In this example, the value of the h:selectOne would be set in myBean.setItemValue, and the list of selectItems would be returned from myDatabaseBean.getListItems.

Now, you decide that you want to provide some defaults for the lists that are in your UI, but not in the database. You could modify myDatabaseBean.getListItems to manually add a custom default selectItem. The value of the defaultItem could be null, or empty string, that way, when the list is displayed, if myBean.getItemValue returns null or empty string, the default value would be selected in the UI. Pretty slick, except for you have to write java in EVERY instance where you are creating your listItems from the database. Now, add in internationalization and your database bean now has to use the correct resource bundle to read the default value display string. That’s not real difficult, but it’s just more java code that has to be introduced everywhere you are creating those lists.

So, is there another way? Here’s what I did.

In my customListbox component I included a well placed, anonymous ui:insert. This magically made my composition include the children of my customListbox in place of the unnamed ui:insert. For instance, within customListbox.xhtml my h:selectOneListbox now looks like this:

    <h:selectOneListbox value="#{entity[fieldName]}">
      <ui:insert />
      <f:selectItems value="#{items}" />
    </h:selectOneListbox>

Then I can include multiple f:selectItem elements as a child of my:customListbox such as:

<my:customListbox
    fieldName="itemValue"
    entity="#{myBean}"
    items="#{myDatabaseBean.listItems}" >
    <f:selectItem itemValue="" itemLabel="#{bundle.defaultBlahValue}" />
</my:customListbox>

Now, I’ve been able to add a custom default value to my custom listbox with a single line in xhtml, and a single resource bundle value, and no java.

Advertisements

2009/09/11

Recovering a lost file in Eclipse

Filed under: eclipse — Tags: — digitaljoel @ 8:46 pm

The other day I was working on my project. I had cygwin open and copied a file to another so I could use it as a template. Unfortunately, I typed the wrong name and overwrote an existing file that I had local modifications in. Because of the local modifications, couldn’t just get it back from source control. So, how could I the contents of the file back?

Since the file was in eclipse, I refreshed the project, then right clicked on the file. Select “Compare To” and “Local History”. I selected a time previous to when I had copied the file over my existing file. The previous revision is on the right side of the comparison, and my new, incorrect stuff is on the left. I was then able to select all on the right and copy it into where it ought to be.

Since that time I have learned that you can right click the file in the Package Explorer, select Replace With, and Local History or Previous from Local History and you can select a previous version and replace the file rather than having to select the contents and paste.

This also works if you delete a file, even if you delete it within eclipse. Simply create a new file of the same name, refresh the project to get the new file, and compare to local history.

2009/09/07

Getting Started

Filed under: design, development — digitaljoel @ 8:44 pm

I’ve had a personal project on my mind for some time. I’ve been talking about it with my close friends who would be potential users. I’ve thought about it a lot. I know a ton of features that I want to put in. I’ve played with putting it on Google’s App Engine, and even started a small Eclipse project using the App Engine and GWT plugin. I read a book about GWT at safaribooksonline and started playing with it in my project and got some simple interaction working. Everything seemed to be going great.

Then, I stalled.

I’ve come up with several reasons why I’m stalled. I’m too busy with my extra curricular activities after work to sit down and work on the app. I’m not sure I want to use Google’s Big Table as my data store. App Engine is still in beta with Java support, and while I’ve been invited to participate, I’ve also been following the message boards and there’s still some issues. Of course, that’s to be expected in beta software. I’m also not sure I want to use GWT as the interface.

In the end, I realize these are not reasons. These are excuses. What I’m really missing is direction. I sat down to write one small portion of the website, focusing on functionality, and I just couldn’t do it. Why is that? I finally realized what the problem is, while I have all of these grand ideas for functionality, I don’t really have any idea of exactly what I’m going to do. Designing with Eclipse open on a .java file isn’t the way to go.

I finally decided that I need to write a specification for my application. I didn’t think I needed such a thing since I’m the only person working on the project, but I think I was wrong. Now, after being stalled for 3 months, I’m excited about my project again. I’ve only got a VERY rough and incomplete outline of the specification in place, but already it has helped me to have a much clearer vision of what it’s going to be and how the user will interact with it. I find that I’m trying to make time to work on it rather than trying to find excuses not to work on it.

I’ve read specifications at work, but never really written one. I had a bit of a hard time figuring out where to start. Fortunately for me, there are plenty of well educated people out there that have spent their own time to educate the masses. One particular web page I found interesting was http://www.mojofat.com/tutorial/index.html which has a great tutorial on writing a specification.

For my small, single developer web app it may be a little bit of overkill, but it has given me a lot of questions to consider while thinking about my app, especially the the stuff in the “Define the Application” step.

Starting to code an application without a specification is like starting a cross country trip without a map or itinerary. Yeah, you can probably do it, but chances are you’ll take a lot of side tracks, detours, and wrong turns, and it will probably take a lot more effort to get all the way through it, if you make it at all before you run out of gas. The difference is, in a cross country trip, the detours can be fun and interesting, whereas in a development project the detours are mostly just annoying and costly.

Create a free website or blog at WordPress.com.