private TextArea ticker;
public Calculator() {
DockPanel dockPanel = new DockPanel();
Grid controls = new Grid(5, 2);
Grid numbersP = new Grid(4, 3);
// initialize the 1-9 buttons
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
numbersP.setWidget(row, col, new NumberButton(this, row
* 3 + col + 1));
}
}
numbersP.setWidget(3, 0, new NumberButton(this, 0));
numbersP.setWidget(3, 1, new NumberButton(this, "."));
numbersP.setWidget(3, 2, new ControlButton(this,
new ControlAction(this, "+/-") {
@Override
public boolean isMultiArg() {
return false;
}
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return -1 * current;
}
}));
controls.setWidget(0, 0, new ControlButton(this,
new ControlAction(this, "/") {
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return previous / current;
}
}));
controls.setWidget(0, 1, new ControlButton(this,
new ControlAction(this, "sqrt") {
@Override
public boolean isMultiArg() {
return false;
}
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return Math.sqrt(current);
}
}));
controls.setWidget(1, 0, new ControlButton(this,
new ControlAction(this, "*") {
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return previous * current;
}
}));
controls.setWidget(1, 1, new ControlButton(this,
new ControlAction(this, "%") {
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return previous % current;
}
}));
controls.setWidget(2, 0, new ControlButton(this,
new ControlAction(this, "-") {
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return previous - current;
}
}));
controls.setWidget(2, 1, new ControlButton(this,
new ControlAction(this, "1/x") {
@Override
public boolean isMultiArg() {
return false;
}
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return 1 / current;
}
}));
controls.setWidget(3, 0, new ControlButton(this,
new ControlAction(this, "+") {
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return previous + current;
}
}));
controls.setWidget(3, 1, new ControlButton(this,
new ControlAction(this, "=") {
@Override
public boolean isMultiArg() {
return false;
}
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
if (lastAction == null) {
return current;
}
return lastAction.performAction(null, previous,
current);
}
}));
controls.setWidget(4, 0, new ControlButton(this,
new ControlAction(this, "bksp") {
@Override
public boolean isMultiArg() {
return false;
}
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
String cStr = current + "";
if (cStr.endsWith(".0")) {
cStr = cStr.substring(0, cStr.length() - 3);
} else {
cStr = cStr.substring(0, cStr.length() - 1);
}
if (cStr.equals("")) {
cStr = "0";
}
return Double.parseDouble(cStr);
}
}));
controls.setWidget(4, 1, new ControlButton(this,
new ControlAction(this, "clear") {
@Override
public boolean isMultiArg() {
return false;
}
@Override
public double performAction(ControlAction lastAction,
double previous, double current) {
return 0;
}
}));
dockPanel.add(numbersP, DockPanel.CENTER);
dockPanel.add(controls, DockPanel.EAST);
inputBox = new TextBox();
inputBox.addStyleName("ResultBox");
dockPanel.add(inputBox, DockPanel.NORTH);
ticker = new TextArea();
ticker.setSize("7em", "140px");
MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
oracle.add("Jill");
oracle.add("Jeff");
oracle.add("James");
oracle.add("Jennifer");
SuggestBox box = new SuggestBox(oracle);
box.addEventHandler(new SuggestionHandler() {
public void onSuggestionSelected(SuggestionEvent suggE) {
String selected = suggE.getSelectedSuggestion()
.getReplacementString();
// do something with selected suggestion
}
});
dockPanel.add(box, DockPanel.SOUTH);
HorizontalPanel mainP = new HorizontalPanel();
mainP.add(dockPanel);
mainP.add(ticker);