Checkpoint - basic tree example working. Rename previous example to 'stocks'. git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7595 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/bikeshed/src/com/google/gwt/cells/client/ButtonCell.java b/bikeshed/src/com/google/gwt/cells/client/ButtonCell.java index 717fd4c..9aa06cd 100644 --- a/bikeshed/src/com/google/gwt/cells/client/ButtonCell.java +++ b/bikeshed/src/com/google/gwt/cells/client/ButtonCell.java
@@ -25,13 +25,13 @@ @Override public void onBrowserEvent(Element parent, String value, NativeEvent event, - Mutator<String, String> mutator) { - if (mutator == null) { + ValueUpdater<String> valueUpdater) { + if (valueUpdater == null) { return; } if ("mouseup".equals(event.getType())) { - mutator.mutate(value, null); + valueUpdater.update(value); } }
diff --git a/bikeshed/src/com/google/gwt/cells/client/Cell.java b/bikeshed/src/com/google/gwt/cells/client/Cell.java index 401a622..27f84bb 100644 --- a/bikeshed/src/com/google/gwt/cells/client/Cell.java +++ b/bikeshed/src/com/google/gwt/cells/client/Cell.java
@@ -29,10 +29,10 @@ * @param parent * @param value * @param event - * @param mutator + * @param valueUpdater a {@link ValueUpdater}, or null */ public void onBrowserEvent(Element parent, C value, NativeEvent event, - Mutator<C, C> mutator) { + ValueUpdater<C> valueUpdater) { } // TODO: render needs a way of assuming text by default, but allowing HTML.
diff --git a/bikeshed/src/com/google/gwt/cells/client/CheckboxCell.java b/bikeshed/src/com/google/gwt/cells/client/CheckboxCell.java index 9800eec..ee711f8 100644 --- a/bikeshed/src/com/google/gwt/cells/client/CheckboxCell.java +++ b/bikeshed/src/com/google/gwt/cells/client/CheckboxCell.java
@@ -26,14 +26,14 @@ @Override public void onBrowserEvent(Element parent, Boolean value, NativeEvent event, - Mutator<Boolean, Boolean> mutator) { - if (mutator == null) { + ValueUpdater<Boolean> valueUpdater) { + if (valueUpdater == null) { return; } if ("change".equals(event.getType())) { InputElement input = parent.getFirstChild().cast(); - mutator.mutate(value, input.isChecked()); + valueUpdater.update(input.isChecked()); } }
diff --git a/bikeshed/src/com/google/gwt/cells/client/Mutator.java b/bikeshed/src/com/google/gwt/cells/client/Mutator.java deleted file mode 100644 index 6b20b5e..0000000 --- a/bikeshed/src/com/google/gwt/cells/client/Mutator.java +++ /dev/null
@@ -1,26 +0,0 @@ -/* - * Copyright 2010 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.google.gwt.cells.client; - -/** - * A mutator can be added to a Cell to mutate data. - * - * @param <T> the data type that will be modified - * @param <C> the data type of the cell - */ -public interface Mutator<T, C> { - void mutate(T object, C after); -}
diff --git a/bikeshed/src/com/google/gwt/cells/client/TextInputCell.java b/bikeshed/src/com/google/gwt/cells/client/TextInputCell.java index bafc0ea..96834a6 100644 --- a/bikeshed/src/com/google/gwt/cells/client/TextInputCell.java +++ b/bikeshed/src/com/google/gwt/cells/client/TextInputCell.java
@@ -26,14 +26,14 @@ @Override public void onBrowserEvent(Element parent, String value, NativeEvent event, - Mutator<String, String> mutator) { - if (mutator == null) { + ValueUpdater<String> valueUpdater) { + if (valueUpdater == null) { return; } if ("change".equals(event.getType())) { InputElement input = parent.getFirstChild().cast(); - mutator.mutate(value, input.getValue()); + valueUpdater.update(input.getValue()); } }
diff --git a/bikeshed/src/com/google/gwt/list/client/Column.java b/bikeshed/src/com/google/gwt/list/client/Column.java index e2aec50..c19a7df 100644 --- a/bikeshed/src/com/google/gwt/list/client/Column.java +++ b/bikeshed/src/com/google/gwt/list/client/Column.java
@@ -16,7 +16,8 @@ package com.google.gwt.list.client; import com.google.gwt.cells.client.Cell; -import com.google.gwt.cells.client.Mutator; +import com.google.gwt.cells.client.ValueUpdater; +import com.google.gwt.cells.client.FieldUpdater; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.NativeEvent; @@ -28,16 +29,17 @@ */ public abstract class Column<T, C> { private final Cell<C> cell; - private Mutator<T, C> mutator; + private FieldUpdater<T, C> fieldUpdater; public Column(Cell<C> cell) { this.cell = cell; } public void onBrowserEvent(Element elem, final T object, NativeEvent event) { - cell.onBrowserEvent(elem, getValue(object), event, new Mutator<C, C>() { - public void mutate(C unused, C after) { - mutator.mutate(object, after); + cell.onBrowserEvent(elem, getValue(object), event, + fieldUpdater == null ? null : new ValueUpdater<C>() { + public void update(C value) { + fieldUpdater.update(object, value); } }); } @@ -46,8 +48,8 @@ cell.render(getValue(object), sb); } - public void setMutator(Mutator<T, C> mutator) { - this.mutator = mutator; + public void setFieldUpdater(FieldUpdater<T, C> fieldUpdater) { + this.fieldUpdater = fieldUpdater; } protected Cell<C> getCell() { @@ -55,5 +57,4 @@ } protected abstract C getValue(T object); - } \ No newline at end of file
diff --git a/bikeshed/src/com/google/gwt/list/client/PagingTableListView.java b/bikeshed/src/com/google/gwt/list/client/PagingTableListView.java index a4d1811..88ac483 100644 --- a/bikeshed/src/com/google/gwt/list/client/PagingTableListView.java +++ b/bikeshed/src/com/google/gwt/list/client/PagingTableListView.java
@@ -16,7 +16,7 @@ package com.google.gwt.list.client; import com.google.gwt.cells.client.ButtonCell; -import com.google.gwt.cells.client.Mutator; +import com.google.gwt.cells.client.ValueUpdater; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.EventTarget; @@ -125,15 +125,15 @@ } else if (row == pageSize) { if (col == 0) { prevButton.onBrowserEvent(elem, null, event, - new Mutator<String, String>() { - public void mutate(String object, String after) { + new ValueUpdater<String>() { + public void update(String value) { previousPage(); } }); } else if (col == 2) { nextButton.onBrowserEvent(elem, null, event, - new Mutator<String, String>() { - public void mutate(String object, String after) { + new ValueUpdater<String>() { + public void update(String value) { nextPage(); } });
diff --git a/bikeshed/src/com/google/gwt/list/client/SimpleCellList.java b/bikeshed/src/com/google/gwt/list/client/SimpleCellList.java index b9ba606..d283d6a 100644 --- a/bikeshed/src/com/google/gwt/list/client/SimpleCellList.java +++ b/bikeshed/src/com/google/gwt/list/client/SimpleCellList.java
@@ -16,7 +16,7 @@ package com.google.gwt.list.client; import com.google.gwt.cells.client.Cell; -import com.google.gwt.cells.client.Mutator; +import com.google.gwt.cells.client.ValueUpdater; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.list.shared.ListEvent; @@ -41,7 +41,7 @@ private final ArrayList<T> data = new ArrayList<T>(); private final Element tmpElem; private ListRegistration reg; - private Mutator<T, T> mutator; + private ValueUpdater<T> valueUpdater; public SimpleCellList(ListModel<T> model, Cell<T> cell) { this.cell = cell; @@ -93,12 +93,12 @@ } if (idxString.length() > 0) { int idx = Integer.parseInt(idxString); - cell.onBrowserEvent(target, data.get(idx), event, mutator); + cell.onBrowserEvent(target, data.get(idx), event, valueUpdater); } } - public void setMutator(Mutator<T, T> mutator) { - this.mutator = mutator; + public void setValueUpdater(ValueUpdater<T> valueUpdater) { + this.valueUpdater = valueUpdater; } private void gc(int size) {
diff --git a/bikeshed/src/com/google/gwt/list/shared/ListEvent.java b/bikeshed/src/com/google/gwt/list/shared/ListEvent.java index 1718e52..0986dc4 100644 --- a/bikeshed/src/com/google/gwt/list/shared/ListEvent.java +++ b/bikeshed/src/com/google/gwt/list/shared/ListEvent.java
@@ -43,8 +43,8 @@ return TYPE; } - private final int start; private final int length; + private final int start; private final List<T> values; /** @@ -54,7 +54,7 @@ * @param length the length of the data * @param values the new values */ - protected ListEvent(int start, int length, List<T> values) { + public ListEvent(int start, int length, List<T> values) { this.start = start; this.length = length; this.values = values;
diff --git a/bikeshed/src/com/google/gwt/list/shared/SizeChangeEvent.java b/bikeshed/src/com/google/gwt/list/shared/SizeChangeEvent.java index 7565f1c..73b4bf6 100644 --- a/bikeshed/src/com/google/gwt/list/shared/SizeChangeEvent.java +++ b/bikeshed/src/com/google/gwt/list/shared/SizeChangeEvent.java
@@ -39,8 +39,8 @@ return TYPE; } - private final int size; private final boolean exact; + private final int size; /** * Creates a {@link SizeChangeEvent}. @@ -48,7 +48,7 @@ * @param size the total size of the list * @param exact true if this is an exact size */ - protected SizeChangeEvent(int size, boolean exact) { + public SizeChangeEvent(int size, boolean exact) { this.size = size; this.exact = exact; }
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/DataBackedWidgets.gwt.xml b/bikeshed/src/com/google/gwt/sample/stocks/Stocks.gwt.xml similarity index 89% rename from bikeshed/src/com/google/gwt/sample/datawidgets/DataBackedWidgets.gwt.xml rename to bikeshed/src/com/google/gwt/sample/stocks/Stocks.gwt.xml index 69b4052..6788e6a 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/DataBackedWidgets.gwt.xml +++ b/bikeshed/src/com/google/gwt/sample/stocks/Stocks.gwt.xml
@@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 0.0.999//EN" "http://google-web-toolkit.googlecode.com/svn/tags/0.0.999/distro-source/core/src/gwt-module.dtd"> -<module rename-to='databackedwidgets'> +<module rename-to='stocks'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name='com.google.gwt.user.User'/> <inherits name='com.google.gwt.list.List'/> @@ -15,7 +15,7 @@ <!-- Other module inherits --> <!-- Specify the app entry point class. --> - <entry-point class='com.google.gwt.sample.datawidgets.client.DataBackedWidgets'/> + <entry-point class='com.google.gwt.sample.stocks.client.Stocks'/> <!-- Specify the paths for translatable code --> <source path='client'/>
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/client/BuySellPopup.java b/bikeshed/src/com/google/gwt/sample/stocks/client/BuySellPopup.java similarity index 94% rename from bikeshed/src/com/google/gwt/sample/datawidgets/client/BuySellPopup.java rename to bikeshed/src/com/google/gwt/sample/stocks/client/BuySellPopup.java index 317ae2f..feeb8c1 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/client/BuySellPopup.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/client/BuySellPopup.java
@@ -13,15 +13,15 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.client; +package com.google.gwt.sample.stocks.client; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.i18n.client.NumberFormat; -import com.google.gwt.sample.datawidgets.shared.StockQuote; -import com.google.gwt.sample.datawidgets.shared.Transaction; +import com.google.gwt.sample.stocks.shared.StockQuote; +import com.google.gwt.sample.stocks.shared.Transaction; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DeferredCommand; import com.google.gwt.user.client.Window; @@ -126,9 +126,9 @@ * * @param cash the available cash */ - public void setAvailableCash(double cash) { + public void setAvailableCash(int cash) { // TODO: Bind the available cash field. - layout.setText(5, 1, NumberFormat.getCurrencyFormat("USD").format(cash)); + layout.setText(5, 1, NumberFormat.getCurrencyFormat("USD").format(cash / 100.0)); } /**
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/client/StockQuoteCell.java b/bikeshed/src/com/google/gwt/sample/stocks/client/StockQuoteCell.java similarity index 89% rename from bikeshed/src/com/google/gwt/sample/datawidgets/client/StockQuoteCell.java rename to bikeshed/src/com/google/gwt/sample/stocks/client/StockQuoteCell.java index 2b9e983..38c8eb4 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/client/StockQuoteCell.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/client/StockQuoteCell.java
@@ -13,10 +13,10 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.client; +package com.google.gwt.sample.stocks.client; import com.google.gwt.cells.client.Cell; -import com.google.gwt.sample.datawidgets.shared.StockQuote; +import com.google.gwt.sample.stocks.shared.StockQuote; /** * A cell that represents a {@link StockQuote}.
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/client/StockService.java b/bikeshed/src/com/google/gwt/sample/stocks/client/StockService.java similarity index 81% rename from bikeshed/src/com/google/gwt/sample/datawidgets/client/StockService.java rename to bikeshed/src/com/google/gwt/sample/stocks/client/StockService.java index 46d3a3d..23299e9 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/client/StockService.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/client/StockService.java
@@ -1,23 +1,23 @@ /* * Copyright 2010 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.client; +package com.google.gwt.sample.stocks.client; -import com.google.gwt.sample.datawidgets.shared.StockRequest; -import com.google.gwt.sample.datawidgets.shared.StockResponse; -import com.google.gwt.sample.datawidgets.shared.Transaction; +import com.google.gwt.sample.stocks.shared.StockRequest; +import com.google.gwt.sample.stocks.shared.StockResponse; +import com.google.gwt.sample.stocks.shared.Transaction; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/client/StockServiceAsync.java b/bikeshed/src/com/google/gwt/sample/stocks/client/StockServiceAsync.java similarity index 81% rename from bikeshed/src/com/google/gwt/sample/datawidgets/client/StockServiceAsync.java rename to bikeshed/src/com/google/gwt/sample/stocks/client/StockServiceAsync.java index fca11c4..0eda48f 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/client/StockServiceAsync.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/client/StockServiceAsync.java
@@ -13,11 +13,11 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.client; +package com.google.gwt.sample.stocks.client; -import com.google.gwt.sample.datawidgets.shared.StockRequest; -import com.google.gwt.sample.datawidgets.shared.StockResponse; -import com.google.gwt.sample.datawidgets.shared.Transaction; +import com.google.gwt.sample.stocks.shared.StockRequest; +import com.google.gwt.sample.stocks.shared.StockResponse; +import com.google.gwt.sample.stocks.shared.Transaction; import com.google.gwt.user.client.rpc.AsyncCallback; /**
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/client/DataBackedWidgets.java b/bikeshed/src/com/google/gwt/sample/stocks/client/Stocks.java similarity index 80% rename from bikeshed/src/com/google/gwt/sample/datawidgets/client/DataBackedWidgets.java rename to bikeshed/src/com/google/gwt/sample/stocks/client/Stocks.java index a1e15c9..2a9f608 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/client/DataBackedWidgets.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/client/Stocks.java
@@ -1,24 +1,24 @@ /* * Copyright 2010 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.client; +package com.google.gwt.sample.stocks.client; import com.google.gwt.cells.client.ButtonCell; import com.google.gwt.cells.client.CheckboxCell; import com.google.gwt.cells.client.CurrencyCell; -import com.google.gwt.cells.client.Mutator; +import com.google.gwt.cells.client.FieldUpdater; import com.google.gwt.cells.client.TextCell; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; @@ -30,13 +30,14 @@ import com.google.gwt.list.client.Column; import com.google.gwt.list.client.PagingTableListView; import com.google.gwt.list.shared.AsyncListModel; +import com.google.gwt.list.shared.ListListModel; import com.google.gwt.list.shared.Range; import com.google.gwt.list.shared.AsyncListModel.DataSource; -import com.google.gwt.sample.datawidgets.shared.StockQuote; -import com.google.gwt.sample.datawidgets.shared.StockQuoteList; -import com.google.gwt.sample.datawidgets.shared.StockRequest; -import com.google.gwt.sample.datawidgets.shared.StockResponse; -import com.google.gwt.sample.datawidgets.shared.Transaction; +import com.google.gwt.sample.stocks.shared.StockQuote; +import com.google.gwt.sample.stocks.shared.StockQuoteList; +import com.google.gwt.sample.stocks.shared.StockRequest; +import com.google.gwt.sample.stocks.shared.StockResponse; +import com.google.gwt.sample.stocks.shared.Transaction; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; @@ -47,16 +48,33 @@ import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; +import java.util.List; + /** * Entry point classes define <code>onModuleLoad()</code>. */ -public class DataBackedWidgets implements EntryPoint { +public class Stocks implements EntryPoint { /** * The delay between updates in milliseconds. */ private static final int UPDATE_DELAY = 5000; + private Column<StockQuote, String> buyColumn = new Column<StockQuote, String>( + new ButtonCell()) { + @Override + protected String getValue(StockQuote object) { + return "Buy"; + } + }; + + /** + * The popup used to purchase stock. + */ + private BuySellPopup buySellPopup = new BuySellPopup(); + + private final Label cashLabel = new Label(); + /** * The {@link StockService} used to retrieve data. */ @@ -72,6 +90,9 @@ } }; + private AsyncListModel<StockQuote> favoritesListModel; + + private PagingTableListView<StockQuote> favoritesTable; private Column<StockQuote, String> nameColumn = new Column<StockQuote, String>( new TextCell()) { @Override @@ -88,14 +109,12 @@ } }; - private Column<StockQuote, String> buyColumn = new Column<StockQuote, String>( - new ButtonCell()) { - @Override - protected String getValue(StockQuote object) { - return "Buy"; - } - }; + private final TextBox queryField = new TextBox(); + + private PagingTableListView<StockQuote> resultsTable; + private AsyncListModel<StockQuote> searchListModel; + private Column<StockQuote, String> sellColumn = new Column<StockQuote, String>( new ButtonCell()) { @Override @@ -104,17 +123,6 @@ } }; - private final TextBox queryField = new TextBox(); - private final Label cashLabel = new Label(); - - private Column<StockQuote, String> tickerColumn = new Column<StockQuote, String>( - new TextCell()) { - @Override - protected String getValue(StockQuote object) { - return object.getTicker(); - } - }; - private Column<StockQuote, String> sharesColumn = new Column<StockQuote, String>( new TextCell()) { @Override @@ -123,11 +131,37 @@ } }; - private AsyncListModel<StockQuote> favoritesListModel; + private Column<Transaction, String> subtotalColumn = new Column<Transaction, String>( + new TextCell()) { + @Override + protected String getValue(Transaction object) { + int price = object.getActualPrice() * object.getQuantity(); + return (object.isBuy() ? " (" : " ") + getFormattedPrice(price) + + (object.isBuy() ? ")" : ""); + } + }; - private PagingTableListView<StockQuote> resultsTable; + private Column<StockQuote, String> tickerColumn = new Column<StockQuote, String>( + new TextCell()) { + @Override + protected String getValue(StockQuote object) { + return object.getTicker(); + } + }; - private AsyncListModel<StockQuote> searchListModel; + private Column<Transaction, String> transactionColumn = new Column<Transaction, String>( + new TextCell()) { + @Override + protected String getValue(Transaction object) { + return object.toString(); + } + }; + + private ListListModel<Transaction> transactionListModel; + + private List<Transaction> transactions; + + private PagingTableListView<Transaction> transactionTable; /** * The timer used to update the stock quotes. @@ -139,13 +173,6 @@ } }; - private PagingTableListView<StockQuote> favoritesTable; - - /** - * The popup used to purchase stock. - */ - private BuySellPopup buySellPopup = new BuySellPopup(); - /** * This is the entry point method. */ @@ -175,6 +202,9 @@ update(); } }); + + transactionListModel = new ListListModel<Transaction>(); + transactions = transactionListModel.getList(); // Create the results table. resultsTable = new PagingTableListView<StockQuote>(searchListModel, 10); @@ -190,22 +220,26 @@ favoritesTable.addColumn(sharesColumn); favoritesTable.addColumn(buyColumn); favoritesTable.addColumn(sellColumn); + + transactionTable = new PagingTableListView<Transaction>(transactionListModel, 10); + transactionTable.addColumn(transactionColumn); + transactionTable.addColumn(subtotalColumn); - favoriteColumn.setMutator(new Mutator<StockQuote, Boolean>() { - public void mutate(StockQuote object, Boolean after) { - setFavorite(object.getTicker(), after); + favoriteColumn.setFieldUpdater(new FieldUpdater<StockQuote, Boolean>() { + public void update(StockQuote object, Boolean value) { + setFavorite(object.getTicker(), value); } }); - buyColumn.setMutator(new Mutator<StockQuote, String>() { - public void mutate(StockQuote object, String after) { + buyColumn.setFieldUpdater(new FieldUpdater<StockQuote, String>() { + public void update(StockQuote object, String value) { buySellPopup.setStockQuote(object, true); buySellPopup.center(); } }); - sellColumn.setMutator(new Mutator<StockQuote, String>() { - public void mutate(StockQuote object, String after) { + sellColumn.setFieldUpdater(new FieldUpdater<StockQuote, String>() { + public void update(StockQuote object, String value) { buySellPopup.setStockQuote(object, false); buySellPopup.center(); } @@ -221,6 +255,7 @@ } public void onSuccess(Transaction result) { + transactions.add(0, result); update(); } }); @@ -237,6 +272,8 @@ RootPanel.get().add(resultsTable); RootPanel.get().add(new HTML("<hr>")); RootPanel.get().add(favoritesTable); + RootPanel.get().add(new HTML("<hr>")); + RootPanel.get().add(transactionTable); // Add a handler to send the name to the server queryField.addKeyUpHandler(new KeyUpHandler() { @@ -250,7 +287,7 @@ /** * Set or unset a ticker symbol as a 'favorite'. - * + * * @param ticker the ticker symbol * @param favorite if true, make the stock a favorite */ @@ -278,9 +315,13 @@ } } + private String getFormattedPrice(int price) { + return NumberFormat.getCurrencyFormat("USD").format((double) price / 100.0); + } + /** * Process the {@link StockResponse} from the server. - * + * * @param response the stock response */ private void processStockResponse(StockResponse response) { @@ -297,8 +338,8 @@ favorites.size(), favorites); // Update available cash. - double cash = response.getCash() / 100.0; - cashLabel.setText(NumberFormat.getCurrencyFormat("USD").format(cash)); + int cash = response.getCash(); + cashLabel.setText(getFormattedPrice(cash)); buySellPopup.setAvailableCash(cash); // Restart the update timer.
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/server/NasdaqStocks.java b/bikeshed/src/com/google/gwt/sample/stocks/server/NasdaqStocks.java similarity index 99% rename from bikeshed/src/com/google/gwt/sample/datawidgets/server/NasdaqStocks.java rename to bikeshed/src/com/google/gwt/sample/stocks/server/NasdaqStocks.java index 62649a7..b1671f6 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/server/NasdaqStocks.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/server/NasdaqStocks.java
@@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.server; +package com.google.gwt.sample.stocks.server; /** * The list of all Nasdaq stocks (note: this is a snapshot plus @@ -1063,7 +1063,6 @@ "GBCI", "Glacier Bancorp, Inc.", "GBNK", "Guaranty Bancorp", "GCBC", "Greene County Bancorp, Inc.", - "GCFB", "Granite City Food & Brewery Ltd.", "GCOM", "Globecomm Systems Inc.", "GENC", "Gencor Industries Inc.", "GENE", "Genetic Technologies Ltd",
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/server/PlayerStatus.java b/bikeshed/src/com/google/gwt/sample/stocks/server/PlayerStatus.java similarity index 98% rename from bikeshed/src/com/google/gwt/sample/datawidgets/server/PlayerStatus.java rename to bikeshed/src/com/google/gwt/sample/stocks/server/PlayerStatus.java index 41da1e2..1893f56 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/server/PlayerStatus.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/server/PlayerStatus.java
@@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.server; +package com.google.gwt.sample.stocks.server; import java.util.HashMap; import java.util.TreeSet;
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/server/StockServiceImpl.java b/bikeshed/src/com/google/gwt/sample/stocks/server/StockServiceImpl.java similarity index 93% rename from bikeshed/src/com/google/gwt/sample/datawidgets/server/StockServiceImpl.java rename to bikeshed/src/com/google/gwt/sample/stocks/server/StockServiceImpl.java index 810cec1..b268e5b 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/server/StockServiceImpl.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/server/StockServiceImpl.java
@@ -13,19 +13,19 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.server; +package com.google.gwt.sample.stocks.server; import com.google.appengine.api.users.User; import com.google.appengine.api.users.UserService; import com.google.appengine.api.users.UserServiceFactory; import com.google.gwt.list.shared.Range; import com.google.gwt.list.shared.AbstractListModel.DefaultRange; -import com.google.gwt.sample.datawidgets.client.StockService; -import com.google.gwt.sample.datawidgets.shared.StockQuote; -import com.google.gwt.sample.datawidgets.shared.StockQuoteList; -import com.google.gwt.sample.datawidgets.shared.StockRequest; -import com.google.gwt.sample.datawidgets.shared.StockResponse; -import com.google.gwt.sample.datawidgets.shared.Transaction; +import com.google.gwt.sample.stocks.client.StockService; +import com.google.gwt.sample.stocks.shared.StockQuote; +import com.google.gwt.sample.stocks.shared.StockQuoteList; +import com.google.gwt.sample.stocks.shared.StockRequest; +import com.google.gwt.sample.stocks.shared.StockResponse; +import com.google.gwt.sample.stocks.shared.Transaction; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import java.io.IOException; @@ -127,14 +127,14 @@ // Perform the transaction with the user. int quantity = transaction.getQuantity(); + int price = quote.getPrice(); if (transaction.isBuy()) { - ensurePlayer().buy(ticker, quantity, quote.getPrice()); + ensurePlayer().buy(ticker, quantity, price); } else { - ensurePlayer().sell(ticker, quantity, quote.getPrice()); + ensurePlayer().sell(ticker, quantity, price); } - // - return new Transaction(true, ticker, quantity); + return new Transaction(true, ticker, quantity, price); } /**
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockQuote.java b/bikeshed/src/com/google/gwt/sample/stocks/shared/StockQuote.java similarity index 97% rename from bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockQuote.java rename to bikeshed/src/com/google/gwt/sample/stocks/shared/StockQuote.java index 851655b..678c752 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockQuote.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/shared/StockQuote.java
@@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.shared; +package com.google.gwt.sample.stocks.shared; import java.io.Serializable;
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockQuoteList.java b/bikeshed/src/com/google/gwt/sample/stocks/shared/StockQuoteList.java similarity index 94% rename from bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockQuoteList.java rename to bikeshed/src/com/google/gwt/sample/stocks/shared/StockQuoteList.java index c1684a4..fff7c51 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockQuoteList.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/shared/StockQuoteList.java
@@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.shared; +package com.google.gwt.sample.stocks.shared; import java.util.ArrayList;
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockRequest.java b/bikeshed/src/com/google/gwt/sample/stocks/shared/StockRequest.java similarity index 95% rename from bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockRequest.java rename to bikeshed/src/com/google/gwt/sample/stocks/shared/StockRequest.java index e895339..c76ca2c 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockRequest.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/shared/StockRequest.java
@@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.shared; +package com.google.gwt.sample.stocks.shared; import com.google.gwt.list.shared.Range;
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockResponse.java b/bikeshed/src/com/google/gwt/sample/stocks/shared/StockResponse.java similarity index 96% rename from bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockResponse.java rename to bikeshed/src/com/google/gwt/sample/stocks/shared/StockResponse.java index fa560b0..83091f0 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/StockResponse.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/shared/StockResponse.java
@@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.shared; +package com.google.gwt.sample.stocks.shared; import java.io.Serializable;
diff --git a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/Transaction.java b/bikeshed/src/com/google/gwt/sample/stocks/shared/Transaction.java similarity index 67% rename from bikeshed/src/com/google/gwt/sample/datawidgets/shared/Transaction.java rename to bikeshed/src/com/google/gwt/sample/stocks/shared/Transaction.java index 01567a3..10b7df8 100644 --- a/bikeshed/src/com/google/gwt/sample/datawidgets/shared/Transaction.java +++ b/bikeshed/src/com/google/gwt/sample/stocks/shared/Transaction.java
@@ -13,7 +13,9 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.google.gwt.sample.datawidgets.shared; +package com.google.gwt.sample.stocks.shared; + +import com.google.gwt.i18n.client.NumberFormat; import java.io.Serializable; @@ -22,22 +24,34 @@ */ public class Transaction implements Serializable { + private int actualPrice; // in pennies + /** * True if a buy transaction, false if a sell transaction. */ private boolean isBuy; - private String ticker; private int quantity; + private String ticker; public Transaction(boolean isBuy, String ticker, int quantity) { + this(isBuy, ticker, quantity, -1); + } + + public Transaction(boolean isBuy, String ticker, int quantity, + int actualPrice) { super(); this.isBuy = isBuy; this.ticker = ticker; this.quantity = quantity; + this.actualPrice = actualPrice; + } + + Transaction() { } - Transaction() { + public int getActualPrice() { + return actualPrice; } public int getQuantity() { @@ -55,6 +69,11 @@ @Override public String toString() { String op = isBuy ? "Bought" : "Sold"; - return op + " " + quantity + " shares of " + ticker; + if (actualPrice == -1) { + return op + " " + quantity + " shares of " + ticker; + } else { + return op + " " + quantity + " shares of " + ticker + " at " + + NumberFormat.getCurrencyFormat("USD").format(actualPrice / 100.0); + } } }
diff --git a/bikeshed/src/com/google/gwt/sample/tree/client/Tree.java b/bikeshed/src/com/google/gwt/sample/tree/client/Tree.java new file mode 100644 index 0000000..df03061 --- /dev/null +++ b/bikeshed/src/com/google/gwt/sample/tree/client/Tree.java
@@ -0,0 +1,60 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.sample.tree.client; + +import com.google.gwt.core.client.EntryPoint; +import com.google.gwt.list.shared.ListEvent; +import com.google.gwt.list.shared.ListHandler; +import com.google.gwt.list.shared.SizeChangeEvent; +import com.google.gwt.sample.tree.shared.TreeNode; +import com.google.gwt.user.client.ui.RootPanel; + +import java.util.ArrayList; +import java.util.List; + +/** + * A demo of the asynchronous Tree model. + */ +public class Tree implements EntryPoint { + + static class FakeTreeNode extends TreeNode<String> { + static int gensym = 1; + private static final int FANOUT = 5; + + FakeTreeNode(int value) { + this.nodeData = "" + value; + } + + @Override + protected void onRangeChanged(int start, int length) { + // TODO: use start, length + for (ListHandler<TreeNode<String>> handler : handlers) { + handler.onSizeChanged(new SizeChangeEvent(FANOUT, true)); + List<TreeNode<String>> values = new ArrayList<TreeNode<String>>(FANOUT); + for (int i = 0; i < FANOUT; i++) { + values.add(new FakeTreeNode(gensym++)); + } + handler.onDataChanged(new ListEvent<TreeNode<String>>(0, FANOUT, values)); + } + } + } + + public void onModuleLoad() { + TreeNode<String> root = new FakeTreeNode(0); + TreeView<String> treeView = new TreeView<String>(root); + RootPanel.get().add(treeView); + } +}
diff --git a/bikeshed/src/com/google/gwt/sample/tree/client/TreeView.java b/bikeshed/src/com/google/gwt/sample/tree/client/TreeView.java new file mode 100644 index 0000000..2a5bbf2 --- /dev/null +++ b/bikeshed/src/com/google/gwt/sample/tree/client/TreeView.java
@@ -0,0 +1,96 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.sample.tree.client; + +import com.google.gwt.event.logical.shared.CloseEvent; +import com.google.gwt.event.logical.shared.CloseHandler; +import com.google.gwt.event.logical.shared.OpenEvent; +import com.google.gwt.event.logical.shared.OpenHandler; +import com.google.gwt.list.shared.ListEvent; +import com.google.gwt.list.shared.ListHandler; +import com.google.gwt.list.shared.ListRegistration; +import com.google.gwt.list.shared.SizeChangeEvent; +import com.google.gwt.sample.tree.shared.TreeNode; +import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.Tree; +import com.google.gwt.user.client.ui.TreeItem; + +import java.util.HashMap; + +/** + * A tree view. + * + * @param <T> the data type of each tree node. + */ +public class TreeView<T> extends Composite { + + private HashMap<TreeItem, TreeNode<T>> nodeMap = + new HashMap<TreeItem, TreeNode<T>>(); + private Tree tree; + + public TreeView(TreeNode<T> root) { + tree = new Tree(); + tree.setAnimationEnabled(true); + + TreeItem rootItem = new TreeItem("root"); + nodeMap.put(rootItem, root); + rootItem.addItem(""); + tree.addItem(rootItem); + + addHandler(rootItem).setRangeOfInterest(0, 10); + + tree.addOpenHandler(new OpenHandler<TreeItem>() { + public void onOpen(OpenEvent<TreeItem> event) { + TreeItem treeItem = event.getTarget(); + addHandler(treeItem).setRangeOfInterest(0, 10); + } + }); + tree.addCloseHandler(new CloseHandler<TreeItem>() { + public void onClose(CloseEvent<TreeItem> event) { + // TODO - remove list handler + } + }); + initWidget(tree); + } + + protected TreeNode<T> getNode(TreeItem treeItem) { + return nodeMap.get(treeItem); + } + + private ListRegistration addHandler(final TreeItem item) { + TreeNode<T> node = getNode(item); + return node.addListHandler(new ListHandler<TreeNode<T>>() { + public void onDataChanged(ListEvent<TreeNode<T>> event) { + int idx = event.getStart(); + for (TreeNode<T> node : event.getValues()) { + TreeItem treeItem = item.getChild(idx++); + nodeMap.put(treeItem, node); + treeItem.setText(node.getNodeData().toString()); + } + } + + public void onSizeChanged(SizeChangeEvent event) { + int size = event.getSize(); + item.removeItems(); + for (int i = 0; i < size; i++) { + TreeItem child = new TreeItem(""); + item.addItem(child); + child.addItem(""); + } + } + }); + } +}
diff --git a/bikeshed/src/com/google/gwt/sample/tree/shared/TreeNode.java b/bikeshed/src/com/google/gwt/sample/tree/shared/TreeNode.java new file mode 100644 index 0000000..d191155 --- /dev/null +++ b/bikeshed/src/com/google/gwt/sample/tree/shared/TreeNode.java
@@ -0,0 +1,56 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.sample.tree.shared; + +import com.google.gwt.list.shared.ListHandler; +import com.google.gwt.list.shared.ListRegistration; +import com.google.gwt.list.shared.SizeChangeEvent; + +import java.util.ArrayList; +import java.util.List; + +/** + * A node in a Tree. + * + * @param <T> the data type contained in the node. + */ +public abstract class TreeNode<T> { + + protected List<ListHandler<TreeNode<T>>> handlers = + new ArrayList<ListHandler<TreeNode<T>>>(); + protected T nodeData; + + public ListRegistration addListHandler(final ListHandler<TreeNode<T>> handler) { + handler.onSizeChanged(new SizeChangeEvent(5, true)); // TODO - unhack + handlers.add(handler); + + return new ListRegistration() { + public void removeHandler() { + handlers.remove(handler); + } + + public void setRangeOfInterest(int start, int length) { + onRangeChanged(start, length); + } + }; + } + + public T getNodeData() { + return nodeData; + } + + protected abstract void onRangeChanged(int start, int length); +}
diff --git a/bikeshed/war/DataBackedWidgets.css b/bikeshed/war/DataBackedWidgets.css deleted file mode 100644 index 7aca7ac..0000000 --- a/bikeshed/war/DataBackedWidgets.css +++ /dev/null
@@ -1,34 +0,0 @@ -/** Add css rules here for your application. */ - - -/** Example rules used by the template application (remove for your app) */ -h1 { - font-size: 2em; - font-weight: bold; - color: #777777; - margin: 40px 0px 70px; - text-align: center; -} - -.sendButton { - display: block; - font-size: 16pt; -} - -/** Most GWT widgets already have a style name defined */ -.gwt-DialogBox { - width: 400px; -} - -.dialogVPanel { - margin: 5px; -} - -.serverResponseLabelError { - color: red; -} - -/** Set ids using widget.getElement().setId("idOfElement") */ -#closeButton { - margin: 15px 6px 6px; -}
diff --git a/bikeshed/war/DataBackedWidgets.html b/bikeshed/war/DataBackedWidgets.html deleted file mode 100644 index 7401053..0000000 --- a/bikeshed/war/DataBackedWidgets.html +++ /dev/null
@@ -1,32 +0,0 @@ -<!doctype html> -<html> - <head> - <meta http-equiv="content-type" content="text/html; charset=UTF-8"> - <link type="text/css" rel="stylesheet" href="DataBackedWidgets.css"> - <title>Data Backed Widgets Sample</title> - <script type="text/javascript" language="javascript" src="databackedwidgets/databackedwidgets.nocache.js"></script> - </head> - - <body> - <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe> - <noscript> - <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif"> - Your web browser must have JavaScript enabled - in order for this application to display correctly. - </div> - </noscript> - - <h1>Data Backed Widgets Sample</h1> - <table align="center"> - <tr> - <td colspan="2" style="font-weight:bold;">Enter a search query:</td> - </tr> - <tr> - <td id="queryFieldContainer"></td> - </tr> - <tr> - <td colspan="2" style="color:red;" id="errorLabelContainer"></td> - </tr> - </table> - </body> -</html>
diff --git a/bikeshed/war/WEB-INF/web.xml b/bikeshed/war/WEB-INF/web.xml index 73c9336..b41b49d 100644 --- a/bikeshed/war/WEB-INF/web.xml +++ b/bikeshed/war/WEB-INF/web.xml
@@ -8,17 +8,18 @@ <!-- Servlets --> <servlet> <servlet-name>stockServlet</servlet-name> - <servlet-class>com.google.gwt.sample.datawidgets.server.StockServiceImpl</servlet-class> + <servlet-class>com.google.gwt.sample.stocks.server.StockServiceImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>stockServlet</servlet-name> - <url-pattern>/databackedwidgets/stock</url-pattern> + <url-pattern>/stocks/stock</url-pattern> </servlet-mapping> <!-- Default page to serve --> <welcome-file-list> - <welcome-file>DataBackedWidgets.html</welcome-file> + <welcome-file>Stocks.html</welcome-file> + <welcome-file>Tree.html</welcome-file> </welcome-file-list> <!-- Require login. -->