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.

1 comment:

Unknown said...

so will you be posting code or releasing code at some point?