Wednesday, August 19, 2009

Simple Swing App - REVISED

Sorry for the absence (did anyone notice?), but I had a struggle with proceeding with the Swing app I had initially proposed. It seemed a bit too simplistic in regards to learning some certain platform API's. Believe me, I did not want to backpedal on my earlier post - I even thought I could just edit that post since no one has probably even read any of these yet. But, in the interest of future posts, I realized I must change the app that will be ported. So, introducing the new Simplistic Swing App:


It is just a very simple contact editor. Once again, it makes use of the Model View Controller framework I had mentioned previously. It also contains the same status area (JStatusPanel) at the bottom of the app with the same clock component.

There are also a few Open Source libraries being used. The date selection component (for the Birth Date field) is the JXDatePicker from the SwingX project. The JStatusPanel was modified to make use of an EventBus for handling messages. Finally, validation is added to some of the input fields using a Simple Validation API. Along with these, I have my own library of classes that assist in writing apps: MVC Framework, image manipulation and UI tools. Again, we want to see how all these affect the porting to the NetBeans Platform.

With that said, here are some functional specs for the app displayed above:

  • Set of contacts to be viewed is read from a contacts service (basically just a class that adheres to an interface).
  • Contacts can be selected for individual viewing and editing.
  • Changes to the contact can be saved.
  • Uncommited contact values can be reset to their previously saved values.
  • A contact can be deleted - deletions only happen after a confirmation.
We will actually expand on these features as we port it to the NetBeans Platform. For example, we will add the capability to add a new contact.

The next series of posts will talk about the code (application code as well as framework classes being used). I think it is important to have at least a strong familiarity with the application before porting.

Before we get to the code, here are the files that make up this application:


Most of these files are very simple. The main classes with any substance are: MyContactsEditor.java, MyContactsModel.java and MyUser.java. The contacts data is provided by either the MyPeanutsService.java or the MyBradyService.java. Both adhere to the ContactService interface:



import java.util.List;

/**
*
* @author borak
*/
public interface ContactService<T> {

public List<T> getContacts();
}
Very simple. The contacts data is currently hard-coded in the service providers. For example, here is the code for MyBradyService:


public class MyBradyService implements ContactService<MyUser> {

public List<MyUser> getContacts() {
List<MyUser> contacts = new ArrayList<MyUser>();

SimpleDateFormat fmt = new SimpleDateFormat("MMMMM dd, yyyy");

try {
contacts.add(new MyUser("Greg", "Brady", "242-12-3987", fmt.parse("July 04, 1962")));
contacts.add(new MyUser("Peter", "Brady", "242-12-3976", fmt.parse("October 31, 1966")));
contacts.add(new MyUser("Bobby", "Brady", "242-23-9876", fmt.parse("March 12, 1970")));
contacts.add(new MyUser("Marsha", "Brady", "242-36-1012", fmt.parse("April 18, 1963")));
contacts.add(new MyUser("Jan", "Brady", "423-67-1012", fmt.parse("August 20, 1965")));
contacts.add(new MyUser("Cindy", "Brady", "223-67-1012", fmt.parse("February 29, 1972")));
contacts.add(new MyUser("Tiger", "Brady", "111-11-1111", fmt.parse("December 25, 1976")));
} catch (ParseException ex) {
Logger.getLogger(MyBradyService.class.getName()).log(Level.SEVERE, null, ex);
}

return contacts;
}
}
Next time, we will get into the MyUser.java and MyContactsModel.java. This will then lead into the Model View Controller framework and how it works. Until next time....

Ugh! Sorry for all that line wrapping! You really need to view the code with the "View Source" button.

1 comment:

  1. Changed the template to accommodate more code. I liked the previous template, but really felt that I needed to change things so that I had more room. I probably could have modified the previous template, but as I mentioned before - I really hate web development!

    ReplyDelete