Created
October 3, 2014 20:58
-
-
Save branflake2267/83701cbb2699059840e1 to your computer and use it in GitHub Desktop.
LockedGrid
This file contains 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.Collections; | |
import java.util.Date; | |
import java.util.List; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.editor.client.Editor.Path; | |
import com.google.gwt.safehtml.shared.SafeHtmlUtils; | |
import com.google.gwt.user.client.ui.RootPanel; | |
import com.google.gwt.user.client.ui.Widget; | |
import com.sencha.gxt.core.client.Style.ScrollDirection; | |
import com.sencha.gxt.core.client.ValueProvider; | |
import com.sencha.gxt.core.client.dom.XDOM; | |
import com.sencha.gxt.core.client.util.DateWrapper; | |
import com.sencha.gxt.core.client.util.Margins; | |
import com.sencha.gxt.core.client.util.Size; | |
import com.sencha.gxt.data.shared.LabelProvider; | |
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.container.HorizontalLayoutContainer; | |
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer.HorizontalLayoutData; | |
import com.sencha.gxt.widget.core.client.container.Viewport; | |
import com.sencha.gxt.widget.core.client.event.BodyScrollEvent; | |
import com.sencha.gxt.widget.core.client.event.BodyScrollEvent.BodyScrollHandler; | |
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.GridView; | |
public class GridExampleWithFreezing { | |
private static final StockProperties props = GWT.create(StockProperties.class); | |
private HorizontalLayoutContainer gridWrapper; | |
public GridExampleWithFreezing() { | |
Widget grid = asWidget(); | |
Viewport viewport = new Viewport(); | |
viewport.add(grid); | |
RootPanel.get().add(viewport); | |
} | |
public Widget asWidget() { | |
if (gridWrapper == null) { | |
ColumnConfig<Stock, String> nameCol = new ColumnConfig<Stock, String>(props.name(), 50, SafeHtmlUtils.fromTrustedString("<b>Company</b>")); | |
ColumnConfig<Stock, String> symbolCol = new ColumnConfig<Stock, String>(props.symbol(), 100, "Symbol"); | |
ColumnConfig<Stock, Double> lastCol = new ColumnConfig<Stock, Double>(props.last(), 75, "Last"); | |
ColumnConfig<Stock, Double> changeCol = new ColumnConfig<Stock, Double>(props.change(), 100, "Change"); | |
ColumnConfig<Stock, Date> lastTransCol = new ColumnConfig<Stock, Date>(props.lastTrans(), 100, "Last Updated"); | |
List<ColumnConfig<Stock, ?>> l = new ArrayList<ColumnConfig<Stock, ?>>(); | |
//Remove name from main set of columns | |
// l.add(nameCol); | |
l.add(symbolCol); | |
l.add(lastCol); | |
l.add(changeCol); | |
l.add(lastTransCol); | |
//create two column models, one for the locked section | |
ColumnModel<Stock> lockedCm = new ColumnModel<Stock>(Collections.<ColumnConfig<Stock, ?>>singletonList(nameCol)); | |
ColumnModel<Stock> cm = new ColumnModel<Stock>(l); | |
ListStore<Stock> store = new ListStore<Stock>(props.key()); | |
store.addAll(getStocks()); | |
//locked grid | |
final Grid<Stock> lockedGrid = new Grid<Stock>(store, lockedCm) { | |
@Override | |
protected Size adjustSize(Size size) { | |
//this is a tricky part - convince the grid to draw just slightly too wide | |
//and so push the scrollbar out of sight | |
return new Size(size.getWidth() + XDOM.getScrollBarWidth() - 1, size.getHeight()); | |
} | |
}; | |
lockedGrid.setView(new GridView<Stock>(){{ | |
this.scrollOffset=0; | |
}}); | |
//require columns to always fit, preventing scrollbar | |
lockedGrid.getView().setForceFit(true); | |
//main grid, with horiz scrollbar | |
final Grid<Stock> grid = new Grid<Stock>(store, cm); | |
//don't want this feature, want to encourage horizontal scrollbars | |
// grid.getView().setAutoExpandColumn(nameCol); | |
grid.getView().setStripeRows(true); | |
grid.getView().setColumnLines(true); | |
grid.setBorders(false); | |
grid.setColumnReordering(true); | |
grid.setStateful(true); | |
grid.setStateId("gridExample"); | |
//link scrolling | |
lockedGrid.addBodyScrollHandler(new BodyScrollHandler() { | |
@Override | |
public void onBodyScroll(BodyScrollEvent event) { | |
grid.getView().getScroller().scrollTo(ScrollDirection.TOP, event.getScrollTop()); | |
} | |
}); | |
grid.addBodyScrollHandler(new BodyScrollHandler() { | |
@Override | |
public void onBodyScroll(BodyScrollEvent event) { | |
lockedGrid.getView().getScroller().scrollTo(ScrollDirection.TOP, event.getScrollTop()); | |
} | |
}); | |
//add locked column, only 300px wide (in this example, use layouts to change how this works | |
HorizontalLayoutData lockedColumnLayoutData = new HorizontalLayoutData(300, 1.0); | |
//this is optional - without this, you get a little offset issue at the very bottom of the non-locked grid | |
lockedColumnLayoutData.setMargins(new Margins(0, 0, XDOM.getScrollBarWidth(), 0)); | |
gridWrapper = new HorizontalLayoutContainer(); | |
gridWrapper.add(lockedGrid, lockedColumnLayoutData); | |
//add non-locked section, taking up all remaining width | |
gridWrapper.add(grid, new HorizontalLayoutData(1.0, 1.0)); | |
} | |
return gridWrapper; | |
} | |
private static Date randomDate() { | |
DateWrapper w = new DateWrapper(); | |
int r = (int) (Math.random() * 10) * 10; | |
w = w.addDays(-r); | |
return w.asDate(); | |
} | |
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())); | |
stocks.add(new Stock("Starbucks Corporation", "SBUX", 27.33, 27.36, randomDate())); | |
stocks.add(new Stock("Yahoo! Inc.", "YHOO", 26.97, 27.29, randomDate())); | |
stocks.add(new Stock("Applied Materials, Inc.", "AMAT", 18.4, 18.66, randomDate())); | |
stocks.add(new Stock("Comcast Corporation", "CMCSA", 25.9, 26.4, randomDate())); | |
stocks.add(new Stock("Sirius Satellite", "SIRI", 2.77, 2.74, randomDate())); | |
stocks.add(new Stock("Tellabs, Inc.", "TLAB", 10.64, 10.75, randomDate())); | |
stocks.add(new Stock("eBay Inc.", "EBAY", 30.43, 31.21, randomDate())); | |
stocks.add(new Stock("Broadcom Corporation", "BRCM", 30.88, 30.48, randomDate())); | |
stocks.add(new Stock("CMGI Inc.", "CMGI", 2.14, 2.13, randomDate())); | |
stocks.add(new Stock("Amgen, Inc.", "AMGN", 56.22, 57.02, randomDate())); | |
stocks.add(new Stock("Limelight Networks", "LLNW", 23, 22.11, randomDate())); | |
stocks.add(new Stock("Amazon.com, Inc.", "AMZN", 72.47, 72.23, randomDate())); | |
stocks.add(new Stock("E TRADE Financial Corporation", "ETFC", 24.32, 24.58, randomDate())); | |
stocks.add(new Stock("AVANIR Pharmaceuticals", "AVNR", 3.7, 3.52, randomDate())); | |
stocks.add(new Stock("Gemstar-TV Guide, Inc.", "GMST", 4.41, 4.55, randomDate())); | |
stocks.add(new Stock("Akamai Technologies, Inc.", "AKAM", 43.08, 45.32, randomDate())); | |
stocks.add(new Stock("Motorola, Inc.", "MOT", 17.74, 17.69, randomDate())); | |
stocks.add(new Stock("Advanced Micro Devices, Inc.", "AMD", 13.77, 13.98, randomDate())); | |
stocks.add(new Stock("General Electric Company", "GE", 36.8, 36.91, randomDate())); | |
stocks.add(new Stock("Texas Instruments Incorporated", "TXN", 35.02, 35.7, randomDate())); | |
stocks.add(new Stock("Qwest Communications", "Q", 9.9, 10.03, randomDate())); | |
stocks.add(new Stock("Tyco International Ltd.", "TYC", 33.48, 33.26, randomDate())); | |
stocks.add(new Stock("Pfizer Inc.", "PFE", 26.21, 26.19, randomDate())); | |
stocks.add(new Stock("Time Warner Inc.", "TWX", 20.3, 20.45, randomDate())); | |
stocks.add(new Stock("Sprint Nextel Corporation", "S", 21.85, 21.76, randomDate())); | |
stocks.add(new Stock("Bank of America Corporation", "BAC", 49.92, 49.73, randomDate())); | |
stocks.add(new Stock("Taiwan Semiconductor", "TSM", 10.4, 10.52, randomDate())); | |
stocks.add(new Stock("AT&T Inc.", "T", 39.7, 39.66, randomDate())); | |
stocks.add(new Stock("United States Steel Corporation", "X", 115.81, 114.62, randomDate())); | |
stocks.add(new Stock("Exxon Mobil Corporation", "XOM", 81.77, 81.86, randomDate())); | |
stocks.add(new Stock("Valero Energy Corporation", "VLO", 72.46, 72.6, randomDate())); | |
stocks.add(new Stock("Micron Technology, Inc.", "MU", 12.02, 12.27, randomDate())); | |
stocks.add(new Stock("Verizon Communications Inc.", "VZ", 42.5, 42.61, randomDate())); | |
stocks.add(new Stock("Avaya Inc.", "AV", 16.96, 16.96, randomDate())); | |
stocks.add(new Stock("The Home Depot, Inc.", "HD", 37.66, 37.79, randomDate())); | |
stocks.add(new Stock("First Data Corporation", "FDC", 32.7, 32.65, randomDate())); | |
return stocks; | |
} | |
private static int COUNTER = 0; | |
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, 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 interface StockProperties extends PropertyAccess<Stock> { | |
@Path("id") | |
ModelKeyProvider<Stock> key(); | |
@Path("name") | |
LabelProvider<Stock> nameLabel(); | |
ValueProvider<Stock, String> name(); | |
ValueProvider<Stock, String> symbol(); | |
ValueProvider<Stock, Double> open(); | |
ValueProvider<Stock, Double> last(); | |
ValueProvider<Stock, Double> change(); | |
ValueProvider<Stock, Date> lastTrans(); | |
ValueProvider<Stock, Boolean> split(); | |
ValueProvider<Stock, String> industry(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment