Created
December 18, 2013 23:51
-
-
Save branflake2267/8031904 to your computer and use it in GitHub Desktop.
GXT_GroupingGrid.java
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
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import com.google.gwt.cell.client.DateCell; | |
import com.google.gwt.cell.client.TextCell; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.editor.client.Editor.Path; | |
import com.google.gwt.i18n.client.DateTimeFormat; | |
import com.google.gwt.user.client.ui.RootPanel; | |
import com.google.gwt.user.client.ui.Widget; | |
import com.sencha.gxt.core.client.ValueProvider; | |
import com.sencha.gxt.core.client.util.DateWrapper; | |
import com.sencha.gxt.data.shared.ListStore; | |
import com.sencha.gxt.data.shared.ModelKeyProvider; | |
import com.sencha.gxt.data.shared.PropertyAccess; | |
import com.sencha.gxt.widget.core.client.ContentPanel; | |
import com.sencha.gxt.widget.core.client.container.Viewport; | |
import com.sencha.gxt.widget.core.client.grid.ColumnConfig; | |
import com.sencha.gxt.widget.core.client.grid.ColumnModel; | |
import com.sencha.gxt.widget.core.client.grid.Grid; | |
import com.sencha.gxt.widget.core.client.grid.GroupingView; | |
public class GXT_GroupingGrid { | |
public int COUNTER = 0; | |
private ContentPanel panel; | |
public GXT_GroupingGrid() { | |
Viewport viewport = new Viewport(); | |
RootPanel.get().add(viewport); | |
viewport.add(asWidget()); | |
} | |
public Widget asWidget() { | |
if (panel == null) { | |
StockProperties properties = GWT.create(StockProperties.class); | |
ColumnConfig<Stock, String> name = new ColumnConfig<Stock, String>(properties.name(), 60); | |
ColumnConfig<Stock, Double> change = new ColumnConfig<Stock, Double>(properties.change(), 20); | |
ColumnConfig<Stock, String> industry = new ColumnConfig<Stock, String>(properties.industry(), 20); | |
ColumnConfig<Stock, Date> lastUpdated = new ColumnConfig<Stock, Date>(properties.lastTrans(), 20); | |
name.setHeader("Name"); | |
change.setHeader("Change"); | |
industry.setHeader("Industry"); | |
lastUpdated.setHeader("Last Updated"); | |
name.setCell(new TextCell()); | |
lastUpdated.setCell(new DateCell(DateTimeFormat.getFormat("MM/dd/yyyy"))); | |
List<ColumnConfig<Stock, ?>> columns = new ArrayList<ColumnConfig<Stock, ?>>(); | |
columns.add(name); | |
columns.add(change); | |
columns.add(industry); | |
columns.add(lastUpdated); | |
ColumnModel<Stock> cm = new ColumnModel<Stock>(columns); | |
final GroupingView<Stock> view = new GroupingView<Stock>(); | |
view.setShowGroupedColumn(false); | |
view.setForceFit(true); | |
ListStore<Stock> store = new ListStore<Stock>(properties.key()); | |
store.addAll(getStocks()); | |
Grid<Stock> grid = new Grid<Stock>(store, cm); | |
grid.setView(view); | |
view.groupBy(industry); | |
panel = new ContentPanel(); | |
panel.setHeadingHtml("Grouping Example"); | |
panel.setSize("700", "450"); | |
panel.add(grid); | |
panel.addStyleName("margin-10"); | |
panel.setCollapsible(true); | |
} | |
return panel; | |
} | |
public class Stock implements Serializable { | |
private Integer id; | |
private Double change; | |
private Date date = new Date(); | |
private String industry = getType(); | |
private Double last; | |
private String name; | |
private Double open; | |
private String symbol; | |
private boolean split = Boolean.valueOf(Math.random() > .5); | |
public Stock() { | |
this.id = Integer.valueOf(COUNTER++); | |
} | |
public Stock(String name, double open, double change, double pctChange, Date date, String industry) { | |
this(); | |
this.name = name; | |
this.open = open; | |
this.change = change; | |
this.date = date; | |
this.industry = industry; | |
} | |
public Stock(String name, String symbol, double open, double last, Date date) { | |
this(); | |
this.name = name; | |
this.symbol = symbol; | |
this.change = last - open; | |
this.open = open; | |
this.last = last; | |
this.date = date; | |
} | |
public Double getChange() { | |
return change; | |
} | |
public Integer getId() { | |
return id; | |
} | |
public String getIndustry() { | |
return industry; | |
} | |
public Double getLast() { | |
return last; | |
} | |
public Date getLastTrans() { | |
return date; | |
} | |
public String getName() { | |
return name; | |
} | |
public Double getOpen() { | |
return open; | |
} | |
/** | |
* Read-only property, based on other values | |
* | |
* @return the percent change | |
*/ | |
public double getPercentChange() { | |
return getChange() / getOpen(); | |
} | |
public String getSymbol() { | |
return symbol; | |
} | |
public boolean isSplit() { | |
return split; | |
} | |
public void setChange(Double change) { | |
this.change = change; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public void setIndustry(String industry) { | |
this.industry = industry; | |
} | |
public void setLast(Double last) { | |
this.last = last; | |
} | |
public void setLastTrans(Date date) { | |
this.date = date; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setOpen(Double open) { | |
this.open = open; | |
} | |
public void setSplit(boolean split) { | |
this.split = split; | |
} | |
public void setSymbol(String symbol) { | |
this.symbol = symbol; | |
} | |
public String toString() { | |
return getName(); | |
} | |
private String getType() { | |
double r = Math.random(); | |
if (r <= .25) { | |
return "Auto"; | |
} else if (r > .25 && r <= .50) { | |
return "Media"; | |
} else if (r > .5 && r <= .75) { | |
return "Medical"; | |
} else { | |
return "Tech"; | |
} | |
} | |
} | |
public List<Stock> getStocks() { | |
List<Stock> stocks = new ArrayList<Stock>(); | |
stocks.add(new Stock("Apple Inc.", "AAPL", 125.64, 123.43, randomDate())); | |
stocks.add(new Stock("Cisco Systems, Inc.", "CSCO", 25.84, 26.3, randomDate())); | |
stocks.add(new Stock("Google Inc.", "GOOG", 516.2, 512.6, randomDate())); | |
stocks.add(new Stock("Intel Corporation", "INTC", 21.36, 21.53, randomDate())); | |
stocks.add(new Stock("Level 3 Communications, Inc.", "LVLT", 5.55, 5.54, randomDate())); | |
stocks.add(new Stock("Microsoft Corporation", "MSFT", 29.56, 29.72, randomDate())); | |
stocks.add(new Stock("Nokia Corporation (ADR)", "NOK", 27.83, 27.93, randomDate())); | |
stocks.add(new Stock("Oracle Corporation", "ORCL", 18.73, 18.98, randomDate())); | |
return stocks; | |
} | |
private Date randomDate() { | |
DateWrapper w = new DateWrapper(); | |
int r = (int) (Math.random() * 10) * 10; | |
w = w.addDays(-r); | |
return w.asDate(); | |
} | |
public interface StockProperties extends PropertyAccess<Stock> { | |
@Path("symbol") | |
ModelKeyProvider<Stock> key(); | |
ValueProvider<Stock, String> name(); | |
ValueProvider<Stock, String> symbol(); | |
ValueProvider<Stock, Double> last(); | |
ValueProvider<Stock, Double> change(); | |
ValueProvider<Stock, Date> lastTrans(); | |
ValueProvider<Stock, String> industry(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment