Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Last active January 26, 2020 05:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branflake2267/1c4fca8998a8d6e71d388237f3d1b6c0 to your computer and use it in GitHub Desktop.
Save branflake2267/1c4fca8998a8d6e71d388237f3d1b6c0 to your computer and use it in GitHub Desktop.
GXT 4 with TextButtonCell. The TextButton cell can be disabled, disabling clicks and with disabled style.
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
public interface CompositeCellResources extends ClientBundle {
public CompositeCellResources INSTANCE = GWT.create(CompositeCellResources.class);
public interface GridStyles extends CssResource {
String button();
}
@Source("GridStyles.gss")
public GridStyles styles();
}
.button td > div {
line-height: 10px;
}
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.CompositeCell;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor.Path;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.RootPanel;
import com.sencha.gxt.cell.core.client.form.TextInputCell;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.core.client.resources.ThemeStyles;
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.button.TextButton;
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.GridViewConfig;
public class GridWithCompositeCellDisableButton implements EntryPoint {
private static final StockProperties props = GWT.create(StockProperties.class);
private Grid<Stock> grid;
@Override
public void onModuleLoad() {
// ~~~ resources
CompositeCellResources.INSTANCE.styles().ensureInjected();
TextButton button = new TextButton();
button.disable();
Viewport viewport = new Viewport();
viewport.add(createGrid());
RootPanel.get().add(viewport);
}
public Grid<Stock> createGrid() {
ListStore<Stock> store = new ListStore<Stock>(props.key());
// Combine more than one cell for the column
List<HasCell<Stock, ?>> cells = new ArrayList<HasCell<Stock, ?>>();
cells.add(new TextButtonHasCell(store));
CompositeCell<Stock> compositeCell = new CompositeCell<Stock>(cells) {
// override the default layout
@Override
protected <X> void render(Cell.Context context, Stock value, SafeHtmlBuilder sb, HasCell<Stock, X> hasCell) {
// Override individual cell layout
Cell<X> cell = hasCell.getCell();
// set the width of the text input cell
if (cell instanceof TextInputCell) {
ColumnConfig<Stock, Object> cc = grid.getColumnModel().getColumn(context.getColumn());
TextInputCell tic = (TextInputCell) cell;
tic.setWidth(cc.getWidth() - 20);
}
sb.appendHtmlConstant("<span>");
cell.render(context, hasCell.getValue(value), sb);
sb.appendHtmlConstant("</span>");
}
};
ColumnConfig<Stock, String> nameCol = new ColumnConfig<Stock, String>(props.name(), 50, "Company");
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");
// Use the model as the value provider and convert it in the HasCell implementation
ColumnConfig<Stock, Stock> lastTransCol = new ColumnConfig<Stock, Stock>(new IdentityValueProvider<Stock>("Last"));
lastTransCol.setHeader("Last");
lastTransCol.setWidth(250);
lastTransCol.setCell(compositeCell);
//nameCol.setCellPadding(false);
List<ColumnConfig<Stock, ?>> columns = new ArrayList<ColumnConfig<Stock, ?>>();
columns.add(nameCol);
columns.add(symbolCol);
columns.add(lastCol);
columns.add(changeCol);
columns.add(lastTransCol);
ColumnModel<Stock> columnModel = new ColumnModel<Stock>(columns);
store.addAll(getStocks());
grid = new Grid<Stock>(store, columnModel);
grid.getView().setAutoExpandColumn(nameCol);
grid.getView().setViewConfig(new GridViewConfig<Stock>() {
@Override
public String getRowStyle(Stock model, int rowIndex) {
return null;
}
@Override
public String getColStyle(Stock model, ValueProvider<? super Stock, ?> valueProvider, int rowIndex, int colIndex) {
// ~~~~ disabled style
String styles = "";
// disabled style for column
if (rowIndex == 0 && colIndex == 4) {
styles = ThemeStyles.get().style().disabled();
}
// ~~~ add style for button text
if (colIndex == 4) {
styles += " " + CompositeCellResources.INSTANCE.styles().button();
}
return styles;
}
});
return grid;
}
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();
}
}
import java.util.Date;
public class Stock {
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);
private static int COUNTER = 0;
private String filter;
public Stock() {
this.id = Integer.valueOf(COUNTER++);
}
public Stock(String name, double open, double change, double pctChange, Date date, String industry) {
this();
this.change = change;
this.date = date;
this.industry = industry;
this.name = name;
this.open = open;
}
public Stock(String name, String symbol, double open, double last, Date date) {
this();
this.date = date;
this.symbol = symbol;
this.name = name;
this.open = open;
this.last = last;
this.change = last - open;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Double getChange() {
return change;
}
public void setChange(Double change) {
this.change = change;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getIndustry() {
return industry;
}
public void setIndustry(String industry) {
this.industry = industry;
}
public Double getLast() {
return last;
}
public void setLast(Double last) {
this.last = last;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getOpen() {
return open;
}
public void setOpen(Double open) {
this.open = open;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public boolean isSplit() {
return split;
}
public void setSplit(boolean split) {
this.split = split;
}
public double getPercentageChange() {
return getChange() / getOpen();
}
private String getType() {
double r = Math.random();
if (r <= .25) {
return "Auto";
} else if (r <= .5) {
return "Media";
} else if (r <= .75) {
return "Medica;";
}
return "Tech";
}
@Override
public String toString() {
String s = "";
s += "name=" + name + " ";
s += "name=" + name + " ";
return s;
}
public String getFilter() {
return filter;
}
public void setFilter(String filter) {
this.filter = filter;
}
}
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.sencha.gxt.cell.core.client.TextButtonCell;
import com.sencha.gxt.data.shared.ListStore;
/**
* The Text Button Cell
*/
public class TextButtonHasCell implements HasCell<Stock, String> {
private TextButtonCell textInputCell;
private int rowIndex;
public TextButtonHasCell(final ListStore<Stock> store) {
textInputCell = new TextButtonCell() {
@Override
public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event,
ValueUpdater<String> valueUpdater) {
Stock m = store.get(context.getIndex());
// ~~~~ skip events for a column
if (rowIndex == 0) { // ignore events for cell
// skip the event for this model
} else {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
}
}
};
}
@Override
public Cell<String> getCell() {
return textInputCell;
}
@Override
public FieldUpdater<Stock, String> getFieldUpdater() {
return new FieldUpdater<Stock, String>() {
@Override
public void update(int index, Stock object, String value) {
// nothing to update
}
};
}
@Override
public String getValue(Stock object) {
// TODO Same value for every button
return "Change";
}
}
@branflake2267
Copy link
Author

The first button cell is disabled.

screen shot 2017-11-03 at 5 05 17 pm

@branflake2267
Copy link
Author

screen shot 2017-11-06 at 5 51 06 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment