Friday, May 2, 2008

Swing preview panel evolution

By following and evolving ideas from the last post, I extended preview pane builder class I had done before. I added ability to update UI controls without dealing with them at all, that's right updating JTextField controls without even knowing about them. Now I don't need to define variables, create them and update, I even don't have to mess with date formatting and all that stuff. By following DRY I have it placed in RemisPreviewPanelBuilder and will try to reuse that every time I need it. That will keep my code easy to read, understand, evolve in the feature and UI of my application will be consistent across all application.

The most obvious use case for this functionality is to have a list of items and show detailed view of selected one in preview pane. Same idea is used in all mail and a lot of other applications. Screenshot from my application is shown bellow in figure 1.



The code which produces view, you see above and shows how to use it is below in list 1.


import java.awt.Component;
import lt.gloria.ptcs.rent.structure.Client;
import lt.gloria.swing.builder.RemisPreviewPanelBuilder;

/**
* CustomerPersonPreviewPanel class is an UI class used to build, show and
* update preview panel and update UI controls in it when new customer
* object is passed.
*
* @author Remigijus Bauzys
*/
public class CustomerPersonPreviewPanel {

private static String ID = "Id";
private static String PERSON_NAME = "Asmens vardas";
private static String PERSON_CODE = "Asmens kodas";
private static String REGISTRATION_DATE = "Registracijos data";
private static String CITIZENSHIP = "PilietybÄ—";

private Component pane;
private RemisPreviewPanelBuilder builder;
private Client client;

public void setSelected(Client client) {
this.client = client;
modelToView();
}

public Client getSelected() {
return this.client;
}

public Component getPane() {

if (pane == null) {
builder = new RemisPreviewPanelBuilder();

builder.addRow(ID, "");
builder.addRow(PERSON_NAME, "");
builder.addRow(PERSON_CODE, "");
builder.addRow(REGISTRATION_DATE, "");
builder.addRow(CITIZENSHIP, "");

pane = builder.getPanel();
}
return pane;
}

private void modelToView() {

builder.setValue(ID, client.getId());
builder.setValue(PERSON_NAME, client.getPerson().getTitle());
builder.setValue(PERSON_CODE, client.getPerson().getIdnumber());
builder.setValue(REGISTRATION_DATE, client.getPerson().getRegistrationDate());
builder.setValue(CITIZENSHIP, client.getPerson().getCitizenship());
}
}


Just wondering, what else can be added or improved without adding a lot of complexity.


Next day I hope, I will add samples for AddPanelBuilder and EditPanelBuilder.

Thursday, May 1, 2008

Again on Blog with Java and swing

Hi I'm on blog again, hard to remember, but I guess it's fourth or fifth time I'm starting blogging. Will see how long it will last.

Just because I'm passioned Java developer all blog will be around Java goodness.

At the present time I'm working on swing application for one of my customers. For this project I'm using my preferred libraries jgoodies-looks, jgoodies-forms and jgoodies-validation. It's hard to find something better.

One thing what I like most is a com.jgoodies.forms.builder.PanelBuilder class from jgoodies-forms library. In order to save time and in fashion of DRY I extended PanelBuilder class and got what I needed. Code is small simple, easy to understand and perfect visual result. Nothing extreme but very effective.

public Component getPane() {
initField();

RemisPreviewPanelBuilder builder = new RemisPreviewPanelBuilder();

builder.addRow("Id", id);
builder.addRow("Company title", title);
builder.addRow("Company code", regCode);
builder.addRow("VAT code", vatCode);
builder.addRow("Country", country);

return builder.getPanel();
}


and result is



Of course there is more interesting Swing UI builders such as RemisDeleteViewPanel and RemisEditViewPanel.