Here is the code for the class:
- Code: Select all
package org.tbrc.gwt.person.client;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
public class PersonsList extends Composite {
private VerticalPanel verticalPanel;
private Label nResultLabel;
protected ScrollPanel scrollPanel;
protected Grid grid;
protected NodeList persons;
protected PersonPanel personPanel;
public void setPersonPanel(PersonPanel p) {
this.personPanel = p;
}
public PersonsList() {
verticalPanel = new VerticalPanel();
initWidget(verticalPanel);
verticalPanel.setVisible(false);
verticalPanel.setBorderWidth(2);
final HorizontalPanel horizontalPanel = new HorizontalPanel();
verticalPanel.add(horizontalPanel);
nResultLabel = new Label("n results");
horizontalPanel.add(nResultLabel);
nResultLabel.setVisible(false);
scrollPanel = new ScrollPanel();
scrollPanel.setSize("400px", "400px"); //$NON-NLS-1$ //$NON-NLS-2$
verticalPanel.add(scrollPanel);
grid = new Grid();
grid.resize(1, 1);
scrollPanel.setWidget(grid);
}
public void populateList(NodeList persons) {
this.persons = persons;
int numResults = persons.getLength();
nResultLabel.setText(String.valueOf(numResults) + " results");
nResultLabel.setVisible(true);
grid.resize(numResults, 1);
for (int i = 0; i < numResults; i++) {
Element person = (Element) persons.item(i);
NodeList names = person.getElementsByTagName("name"); //$NON-NLS-1$
Node pn = names.item(0);
String primaryName = pn.getChildNodes().item(0).toString();
primaryName = primaryName.replaceAll("'", "\'"); //$NON-NLS-1$ //$NON-NLS-2$
String circa = ""; //$NON-NLS-1$
NodeList events = person.getElementsByTagName("event"); //$NON-NLS-1$
for (int j = 0; j < events.getLength(); j++) {
Element event = (Element) events.item(j);
circa = event.getAttribute("circa"); //$NON-NLS-1$
if (circa != null) break;
circa = ""; //$NON-NLS-1$
}
Hyperlink hyperlink = new Hyperlink(circa + " - " + primaryName, String.valueOf(i)); //$NON-NLS-1$
grid.setWidget(i, 0, hyperlink);
}
verticalPanel.setVisible(true);
History.addHistoryListener(new PersonsHistoryListener());
}
public class PersonsHistoryListener implements HistoryListener {
public void onHistoryChanged(String token) {
int which = Integer.parseInt(token);
personPanel.byRidCallback.onSuccess(persons.item(which).toString());
}
}
}