Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created January 6, 2016 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branflake2267/65ea27e16868c546ea7e to your computer and use it in GitHub Desktop.
Save branflake2267/65ea27e16868c546ea7e to your computer and use it in GitHub Desktop.
Example of how two combos could interact
import java.util.ArrayList;
import java.util.List;
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.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
import com.sencha.gxt.core.client.ValueProvider;
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.data.shared.StringLabelProvider;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.container.Viewport;
import com.sencha.gxt.widget.core.client.event.CollapseEvent;
import com.sencha.gxt.widget.core.client.event.CollapseEvent.CollapseHandler;
import com.sencha.gxt.widget.core.client.event.StartEditEvent;
import com.sencha.gxt.widget.core.client.event.StartEditEvent.StartEditHandler;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.form.TextField;
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.Grid.GridCell;
import com.sencha.gxt.widget.core.client.grid.GridView;
import com.sencha.gxt.widget.core.client.grid.editing.GridInlineEditing;
public class GridInlineEditingTwoCombosTest2 implements EntryPoint {
@Override
public void onModuleLoad() {
VerticalLayoutContainer vlc = new VerticalLayoutContainer();
vlc.add(createGrid(), new VerticalLayoutData(1, 1));
Viewport vp = new Viewport();
vp.add(vlc);
RootPanel.get().add(vp);
}
private static final PlaceProperties properties = GWT.create(PlaceProperties.class);
protected Grid<Plant> grid;
private FramedPanel panel;
private ListStore<Plant> store;
private ListStore<String> comboStore1;
private ListStore<String> comboStore2;
public Widget createGrid() {
if (panel == null) {
ColumnConfig<Plant, String> nameCol = new ColumnConfig<Plant, String>(properties.name(), 220, "Name");
ColumnConfig<Plant, String> lightCol1 = new ColumnConfig<Plant, String>(properties.light(), 130, "Combo1");
ColumnConfig<Plant, String> lightCol2 = new ColumnConfig<Plant, String>(properties.light(), 130, "Combo2");
ColumnConfig<Plant, String> indorCol = new ColumnConfig<Plant, String>(properties.indoor(), 55, "Indoor");
List<ColumnConfig<Plant, ?>> columns = new ArrayList<ColumnConfig<Plant, ?>>();
columns.add(nameCol);
columns.add(lightCol1);
columns.add(lightCol2);
columns.add(indorCol);
ColumnModel<Plant> columnModel = new ColumnModel<Plant>(columns);
store = new ListStore<Plant>(properties.key());
store.setAutoCommit(false);
store.addAll(getPlants());
GridView<Plant> gridView = new GridView<Plant>();
grid = new Grid<Plant>(store, columnModel, gridView);
grid.getView().setAutoExpandColumn(nameCol);
comboStore1 = new ListStore<String>(new ModelKeyProvider<String>() {
@Override
public String getKey(String item) {
return item.toString();
}
});
comboStore1.add("Mostly Shady");
comboStore1.add("Shade");
comboStore1.add("Mostly Sunny");
comboStore1.add("Shady");
comboStore2 = new ListStore<String>(new ModelKeyProvider<String>() {
@Override
public String getKey(String item) {
return item.toString();
}
});
final ComboBox<String> combo1 = new ComboBox<String>(comboStore1, new StringLabelProvider<String>());
combo1.setTriggerAction(TriggerAction.ALL);
combo1.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
// ~~~~ this could be used to observe when the blur happens from the combo
// ~~~~ instead of this, the onCollapse could be used to observe if the user did something sooner
String value1 = combo1.getCurrentValue();
System.out.println("combo1 " + value1);
}
});
combo1.addCollapseHandler(new CollapseHandler() {
@Override
public void onCollapse(CollapseEvent event) {
// ~~~~ this could be used to observe when the combo collapses.
System.out.println("collapse");
String value1 = combo1.getCurrentValue();
System.out.println("combo1 " + value1);
// ~~~~ TODO change the value options in combo2
}
});
final ComboBox<String> combo2 = new ComboBox<String>(comboStore2, new StringLabelProvider<String>());
combo2.setTriggerAction(TriggerAction.ALL);
combo2.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
String value2 = combo2.getCurrentValue();
System.out.println("combo2 " + value2);
}
});
// editing
final GridInlineEditing<Plant> editing = new GridInlineEditing<Plant>(grid);
editing.addEditor(nameCol, new TextField());
editing.addEditor(lightCol1, combo1);
editing.addEditor(lightCol2, combo2);
editing.addStartEditHandler(new StartEditHandler<Plant>() {
@Override
public void onStartEdit(StartEditEvent<Plant> event) {
// ~~~~~ this could be used to set a reference to which cell, row and model are being edited.
GridCell cell = event.getEditCell();
int col = cell.getCol();
int row = cell.getRow();
Plant model = store.get(cell.getRow());
}
});
}
panel = new FramedPanel();
panel.setHeadingText("Editable Grid LayoutExampleWithChart");
panel.setPixelSize(600, 400);
panel.setWidget(grid);
return panel;
}
private void changeCombo2Values(String value1) {
comboStore2.clear();
if (value1.equals("Shade")) {
comboStore2.add("true");
comboStore2.add("maybe");
comboStore2.add("could be");
} else {
comboStore2.add("true");
comboStore2.add("false");
}
}
private static int AUTO_ID = 0;
public class Plant {
private int id;
private String name;
private String light;
private String indoor;
public Plant() {
id = AUTO_ID++;
}
public Plant(String name, String light, String indoor) {
this();
setName(name);
setLight(light);
setIndoor(indoor);
}
public int getId() {
return id;
}
public String getLight() {
return light;
}
public String getName() {
return name;
}
public String isIndoor() {
return indoor;
}
public void setId(int id) {
this.id = id;
}
public void setIndoor(String indoor) {
this.indoor = indoor;
}
public void setLight(String light) {
this.light = light;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name != null ? name : super.toString();
}
}
public List<Plant> getPlants() {
List<Plant> plants = new ArrayList<Plant>();
plants.add(new Plant("Bloodroot", "Mostly Shady", "true"));
plants.add(new Plant("Columbine", "Shade", "true"));
plants.add(new Plant("Marsh Marigold", "Mostly Sunny", "false"));
plants.add(new Plant("Cowslip", "Mostly Shady", "true"));
plants.add(new Plant("Dutchman's-Breeches", "Mostly Shady", "false"));
plants.add(new Plant("Ginger, Wild", "Mostly Shady", "true"));
return plants;
}
interface PlaceProperties extends PropertyAccess<Plant> {
@Path("id")
ModelKeyProvider<Plant> key();
ValueProvider<Plant, String> name();
ValueProvider<Plant, String> light();
ValueProvider<Plant, String> indoor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment