Created
December 24, 2013 18:03
-
-
Save branflake2267/8116353 to your computer and use it in GitHub Desktop.
GXT 2 dialog with grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.sencha.gxt.test.client.gridindialog; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import java.util.List; | |
| import com.extjs.gxt.ui.client.GXT; | |
| import com.extjs.gxt.ui.client.Style.HorizontalAlignment; | |
| import com.extjs.gxt.ui.client.data.BaseModel; | |
| import com.extjs.gxt.ui.client.event.ButtonEvent; | |
| import com.extjs.gxt.ui.client.event.Events; | |
| import com.extjs.gxt.ui.client.event.FieldEvent; | |
| import com.extjs.gxt.ui.client.event.Listener; | |
| import com.extjs.gxt.ui.client.event.SelectionListener; | |
| import com.extjs.gxt.ui.client.store.ListStore; | |
| import com.extjs.gxt.ui.client.widget.Dialog; | |
| import com.extjs.gxt.ui.client.widget.button.Button; | |
| import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction; | |
| import com.extjs.gxt.ui.client.widget.form.SimpleComboBox; | |
| import com.extjs.gxt.ui.client.widget.grid.CellSelectionModel; | |
| import com.extjs.gxt.ui.client.widget.grid.ColumnConfig; | |
| import com.extjs.gxt.ui.client.widget.grid.ColumnData; | |
| import com.extjs.gxt.ui.client.widget.grid.ColumnModel; | |
| import com.extjs.gxt.ui.client.widget.grid.Grid; | |
| import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer; | |
| import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel; | |
| import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem; | |
| import com.extjs.gxt.ui.client.widget.toolbar.ToolBar; | |
| import com.google.gwt.i18n.client.DateTimeFormat; | |
| import com.google.gwt.i18n.client.NumberFormat; | |
| import com.google.gwt.user.client.ui.RootPanel; | |
| import com.google.gwt.user.client.ui.Widget; | |
| public class GXT2_GridInDialog { | |
| private ColumnModel cm; | |
| // instantiate this class, or reconfigure this differently | |
| public GXT2_GridInDialog() { | |
| Button button = new Button("Open"); | |
| button.addSelectionListener(new SelectionListener<ButtonEvent>() { | |
| @Override | |
| public void componentSelected(ButtonEvent ce) { | |
| open(); | |
| } | |
| }); | |
| RootPanel.get().add(button); | |
| } | |
| protected void open() { | |
| Dialog dialog = new Dialog(); | |
| dialog.setPixelSize(600, 600); | |
| dialog.add(createGrid()); | |
| dialog.show(); | |
| } | |
| public Widget createGrid() { | |
| final NumberFormat currency = NumberFormat.getCurrencyFormat(); | |
| final NumberFormat number = NumberFormat.getFormat("0.00"); | |
| GridCellRenderer<Stock> change = new GridCellRenderer<Stock>() { | |
| public String render(Stock model, String property, ColumnData config, int rowIndex, int colIndex, | |
| ListStore<Stock> store, Grid<Stock> grid) { | |
| double val = (Double) model.get(property); | |
| String style = val < 0 ? "red" : GXT.isHighContrastMode ? "#00ff5a" : "green"; | |
| String v = number.format(val); | |
| return "<span qtitle='" + cm.getColumnById(property).getHeaderHtml() + "' qtip='" + v | |
| + "' style='font-weight: bold;color:" + style + "'>" + v + "</span>"; | |
| } | |
| }; | |
| GridCellRenderer<Stock> gridNumber = new GridCellRenderer<Stock>() { | |
| public String render(Stock model, String property, ColumnData config, int rowIndex, int colIndex, | |
| ListStore<Stock> store, Grid<Stock> grid) { | |
| Number value = model.<Number> get(property); | |
| return value == null ? null : currency.format(model.<Number> get(property)); | |
| } | |
| }; | |
| List<ColumnConfig> configs = new ArrayList<ColumnConfig>(); | |
| ColumnConfig column = new ColumnConfig(); | |
| column.setId("name"); | |
| column.setHeaderHtml("Company"); | |
| column.setWidth(200); | |
| column.setRowHeader(true); | |
| configs.add(column); | |
| column = new ColumnConfig(); | |
| column.setId("symbol"); | |
| column.setHeaderHtml("Symbol"); | |
| column.setWidth(100); | |
| configs.add(column); | |
| column = new ColumnConfig(); | |
| column.setId("last"); | |
| column.setHeaderHtml("Last"); | |
| column.setAlignment(HorizontalAlignment.RIGHT); | |
| column.setWidth(75); | |
| column.setRenderer(gridNumber); | |
| configs.add(column); | |
| column = new ColumnConfig("change", "Change", 100); | |
| column.setAlignment(HorizontalAlignment.RIGHT); | |
| column.setRenderer(change); | |
| configs.add(column); | |
| column = new ColumnConfig("date", "Last Updated", 100); | |
| column.setAlignment(HorizontalAlignment.RIGHT); | |
| column.setDateTimeFormat(DateTimeFormat.getFormat("MM/dd/yyyy")); | |
| configs.add(column); | |
| ListStore<Stock> store = new ListStore<Stock>(); | |
| store.add(getStocks()); | |
| cm = new ColumnModel(configs); | |
| final Grid<Stock> grid = new Grid<Stock>(store, cm); | |
| grid.setStyleAttribute("borderTop", "none"); | |
| grid.setAutoExpandColumn("name"); | |
| grid.setBorders(false); | |
| grid.setStripeRows(true); | |
| grid.setColumnLines(true); | |
| grid.setColumnReordering(true); | |
| grid.setPixelSize(600, 600); | |
| ToolBar toolBar = new ToolBar(); | |
| toolBar.getAriaSupport().setLabel("Grid Options"); | |
| toolBar.add(new LabelToolItem("Selection Mode: ")); | |
| final SimpleComboBox<String> type = new SimpleComboBox<String>(); | |
| type.getAriaSupport().setLabelledBy(toolBar.getItem(0).getId()); | |
| type.setTriggerAction(TriggerAction.ALL); | |
| type.setEditable(false); | |
| type.setFireChangeEventOnSetValue(true); | |
| type.setWidth(100); | |
| type.add("Row"); | |
| type.add("Cell"); | |
| type.setSimpleValue("Row"); | |
| type.addListener(Events.Change, new Listener<FieldEvent>() { | |
| public void handleEvent(FieldEvent be) { | |
| boolean cell = type.getSimpleValue().equals("Cell"); | |
| grid.getSelectionModel().deselectAll(); | |
| if (cell) { | |
| grid.setSelectionModel(new CellSelectionModel<Stock>()); | |
| } else { | |
| grid.setSelectionModel(new GridSelectionModel<Stock>()); | |
| } | |
| } | |
| }); | |
| toolBar.add(type); | |
| return grid; | |
| } | |
| public List<Stock> getStocks() { | |
| List<Stock> stocks = new ArrayList<Stock>(); | |
| stocks.add(new Stock("Apple Inc.", "AAPL", 125.64, 123.43)); | |
| stocks.add(new Stock("Cisco Systems, Inc.", "CSCO", 25.84, 26.3)); | |
| stocks.add(new Stock("Google Inc.", "GOOG", 516.2, 512.6)); | |
| stocks.add(new Stock("Intel Corporation", "INTC", 21.36, 21.53)); | |
| stocks.add(new Stock("Level 3 Communications, Inc.", "LVLT", 5.55, 5.54)); | |
| stocks.add(new Stock("Microsoft Corporation", "MSFT", 29.56, 29.72)); | |
| stocks.add(new Stock("Nokia Corporation (ADR)", "NOK", 27.83, 27.93)); | |
| stocks.add(new Stock("Oracle Corporation", "ORCL", 18.73, 18.98)); | |
| stocks.add(new Stock("Starbucks Corporation", "SBUX", 27.33, 27.36)); | |
| stocks.add(new Stock("Yahoo! Inc.", "YHOO", 26.97, 27.29)); | |
| stocks.add(new Stock("Applied Materials, Inc.", "AMAT", 18.4, 18.66)); | |
| stocks.add(new Stock("Comcast Corporation", "CMCSA", 25.9, 26.4)); | |
| stocks.add(new Stock("Sirius Satellite", "SIRI", 2.77, 2.74)); | |
| return stocks; | |
| } | |
| public class Stock extends BaseModel { | |
| public Stock() { | |
| } | |
| public Stock(String name, String symbol, double open, double last) { | |
| set("name", name); | |
| set("symbol", symbol); | |
| set("open", open); | |
| set("last", last); | |
| set("date", new Date()); | |
| set("change", last - open); | |
| } | |
| public Stock(String name, double open, double change, double pctChange, Date date, String industry) { | |
| set("name", name); | |
| set("open", open); | |
| set("change", change); | |
| set("percentChange", pctChange); | |
| set("date", date); | |
| set("industry", industry); | |
| } | |
| public String getIndustry() { | |
| return get("industry"); | |
| } | |
| public void setIndustry(String industry) { | |
| set("industry", industry); | |
| } | |
| public Date getLastTrans() { | |
| return (Date) get("date"); | |
| } | |
| public String getName() { | |
| return (String) get("name"); | |
| } | |
| public String getSymbol() { | |
| return (String) get("symbol"); | |
| } | |
| public double getOpen() { | |
| Double open = (Double) get("open"); | |
| return open.doubleValue(); | |
| } | |
| public double getLast() { | |
| Double open = (Double) get("last"); | |
| return open.doubleValue(); | |
| } | |
| public double getChange() { | |
| return getLast() - getOpen(); | |
| } | |
| public double getPercentChange() { | |
| return getChange() / getOpen(); | |
| } | |
| public String toString() { | |
| return getName(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment