public MatchWindow(final OpenTrader session, final Tradable tradable, final List<Match> matches) {
super(false, true);
this.session = session;
setAnimationEnabled(true);
DockPanel panel = new DockPanel();
panel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
final HTML header = new HTML();
final boolean ask = (tradable instanceof Ask);
String txt = (matches.isEmpty() ? "No" : ""+matches.size()) + " matching "+ (ask ? "Bid" : "Ask") + " for "
+ toString(tradable) + "<br>";
header.setHTML(txt);
header.addStyleName("table-caption");
Button close = new Button(matches.isEmpty() ? "OK" : "Cancel");
close.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
hide();
}
});
FlexTable body = new FlexTable();
final RadioButton[] buttons = new RadioButton[matches.size()];
if (!matches.isEmpty()) {
for (int i = 0; i < matches.size(); i++) {
Match match = matches.get(i);
Tradable t2 = ask ? match.getBid() : match.getAsk();
Trader cpty = ask ? match.getBid().getBuyer() : match.getAsk().getSeller();
buttons[i] = new RadioButton("matches");
buttons[i].setValue(i == 0);
body.setWidget(i, 0, buttons[i]);
body.setWidget(i, 1, FormatUtil.formatPrice(t2.getPrice()));
body.setWidget(i, 2, FormatUtil.formatVolume(t2.getVolume()));
body.setText(i, 3, " by " + cpty.getName());
}
Button act = new Button(ask ? "Sell" : "Buy");
act.setFocus(true);
body.setWidget(matches.size()+1, 1, act);
act.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
for (int i = 0; i < buttons.length; i++) {
if (buttons[i].getValue()) {
Match match = matches.get(i);
Tradable t = ask ? match.getAsk() : match.getBid();
session.getService().trade(match, new TradeCallback(t));
hide(true);
}
}
}
});
body.setWidget(matches.size()+1, 2, close);
} else {
body.setWidget(0,0, new HTML("<p>Open a new browser page and login with a different Trader name<br>"
+ "to create a matching " + (ask ? "Bid" : "Ask") + "<p>"));
close.setFocus(true);
body.setWidget(1, 0, close);
body.getFlexCellFormatter().setAlignment(1,0,
HasHorizontalAlignment.ALIGN_CENTER,
HasVerticalAlignment.ALIGN_MIDDLE);
}
panel.add(header, DockPanel.NORTH);
panel.add(body, DockPanel.CENTER);
setWidget(panel);
}