Last active
August 29, 2015 14:00
-
-
Save branflake2267/11091554 to your computer and use it in GitHub Desktop.
Testing disable inline editing
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.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import com.google.gwt.cell.client.DateCell; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.core.client.Scheduler; | |
import com.google.gwt.core.client.Scheduler.ScheduledCommand; | |
import com.google.gwt.editor.client.Editor.Path; | |
import com.google.gwt.i18n.client.DateTimeFormat; | |
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; | |
import com.google.gwt.user.client.ui.RootPanel; | |
import com.google.gwt.user.client.ui.Widget; | |
import com.sencha.gxt.cell.core.client.form.CheckBoxCell; | |
import com.sencha.gxt.core.client.IdentityValueProvider; | |
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.widget.core.client.FramedPanel; | |
import com.sencha.gxt.widget.core.client.button.TextButton; | |
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack; | |
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.CompleteEditEvent; | |
import com.sencha.gxt.widget.core.client.event.CompleteEditEvent.CompleteEditHandler; | |
import com.sencha.gxt.widget.core.client.event.SelectEvent; | |
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler; | |
import com.sencha.gxt.widget.core.client.form.CheckBox; | |
import com.sencha.gxt.widget.core.client.form.DateField; | |
import com.sencha.gxt.widget.core.client.form.DateTimePropertyEditor; | |
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.GridEditing; | |
import com.sencha.gxt.widget.core.client.grid.editing.GridInlineEditing; | |
public class GridInlineEditingTest { | |
public GridInlineEditingTest() { | |
VerticalLayoutContainer vlc = new VerticalLayoutContainer(); | |
vlc.add(createGrid(), new VerticalLayoutData(1, 1)); | |
Viewport vp = new Viewport(); | |
vp.add(vlc); | |
RootPanel.get().add(vp); | |
} | |
interface PlaceProperties extends PropertyAccess<Plant> { | |
ValueProvider<Plant, Date> available(); | |
@Path("id") | |
ModelKeyProvider<Plant> key(); | |
ValueProvider<Plant, String> name(); | |
ValueProvider<Plant, Boolean> indoor(); | |
} | |
private static final PlaceProperties properties = GWT.create(PlaceProperties.class); | |
protected Grid<Plant> grid; | |
private FramedPanel panel; | |
private ListStore<Plant> store; | |
private DateField dateField; | |
private CheckBox checkField; | |
public Widget createGrid() { | |
if (panel == null) { | |
ColumnConfig<Plant, Plant> nameCol = new ColumnConfig<Plant, Plant>(new IdentityValueProvider<Plant>("Name"), | |
220, "Name"); | |
ColumnConfig<Plant, Date> dateCol = new ColumnConfig<Plant, Date>(properties.available(), 95, "Date"); | |
ColumnConfig<Plant, Boolean> indorCol = new ColumnConfig<Plant, Boolean>(properties.indoor(), 55, "Indoor"); | |
// display formatting | |
DateCell dateCell = new DateCell(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT)); | |
dateCol.setCell(dateCell); | |
List<ColumnConfig<Plant, ?>> l = new ArrayList<ColumnConfig<Plant, ?>>(); | |
l.add(nameCol); | |
l.add(dateCol); | |
l.add(indorCol); | |
ColumnModel<Plant> columns = new ColumnModel<Plant>(l); | |
store = new ListStore<Plant>(properties.key()); | |
store.setAutoCommit(false); | |
store.addAll(getPlants()); | |
GridView<Plant> gridView = new GridView<Plant>(); | |
grid = new Grid<Plant>(store, columns, gridView); | |
grid.getView().setAutoExpandColumn(nameCol); | |
// EDITING// | |
final GridEditing<Plant> editing = new GridInlineEditing<Plant>(grid) { | |
@Override | |
public void startEditing(GridCell cell) { | |
super.startEditing(cell); | |
int row = cell.getRow(); | |
int col = cell.getCol(); | |
System.out.println("got here. row " + row + ", col " + col); | |
Plant model = store.get(row); | |
boolean enableDate = model.getIndoor(); | |
if (enableDate) { | |
dateField.enable(); | |
} else { | |
dateField.disable(); | |
} | |
} | |
}; | |
store.setAutoCommit(true); | |
dateField = new DateField(new DateTimePropertyEditor(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT))); | |
dateField.setClearValueOnParseError(false); | |
editing.addEditor(dateCol, dateField); | |
CheckBoxCell checkBoxCell = new CheckBoxCell(); | |
checkField = new CheckBox(checkBoxCell); | |
editing.addEditor(indorCol, checkField); | |
editing.addCompleteEditHandler(new CompleteEditHandler<Plant>() { | |
// not firing for checkbox: | |
@Override | |
public void onCompleteEdit(CompleteEditEvent<Plant> event) { | |
GridCell cell = event.getEditCell(); | |
int row = cell.getRow(); | |
int col = cell.getCol(); | |
System.out.println("got here. row " + row + ", col " + col); | |
Scheduler.get().scheduleFinally(new ScheduledCommand() { | |
@Override | |
public void execute() { | |
if (checkField != null) { | |
Boolean enableDate = checkField.getValue(); | |
if (enableDate != null && enableDate) { | |
dateField.enable(); | |
} else { | |
dateField.disable(); | |
} | |
System.out.println("next row plant= enableDate=" + enableDate); | |
} | |
} | |
}); | |
} | |
}); | |
panel = new FramedPanel(); | |
panel.setHeadingText("Editable Grid Example"); | |
panel.setPixelSize(600, 400); | |
panel.addStyleName("margin-10"); | |
VerticalLayoutContainer con = new VerticalLayoutContainer(); | |
con.setBorders(true); | |
con.add(grid, new VerticalLayoutData(1, 1)); | |
panel.setWidget(con); | |
panel.setButtonAlign(BoxLayoutPack.CENTER); | |
panel.addButton(new TextButton("Reset", new SelectHandler() { | |
@Override | |
public void onSelect(SelectEvent event) { | |
store.rejectChanges(); | |
} | |
})); | |
panel.addButton(new TextButton("Save", new SelectHandler() { | |
@Override | |
public void onSelect(SelectEvent event) { | |
store.commitChanges(); | |
} | |
})); | |
} | |
return panel; | |
} | |
private static int AUTO_ID = 0; | |
public class Plant { | |
private DateTimeFormat df = DateTimeFormat.getFormat("MM/dd/y"); | |
private int id; | |
private String name; | |
private String light; | |
private double price; | |
private Date available; | |
private boolean indoor; | |
private String color; | |
private int difficulty; | |
private double progress; | |
private boolean enableDate = true; | |
public Plant() { | |
id = AUTO_ID++; | |
difficulty = (int) (Math.random() * 100); | |
progress = Math.random(); | |
} | |
public Plant(String name, String light, double price, String available, boolean indoor) { | |
this(); | |
setName(name); | |
setLight(light); | |
setPrice(price); | |
setAvailable(df.parse(available)); | |
setIndoor(indoor); | |
} | |
public int getId() { | |
return id; | |
} | |
public double getProgress() { | |
return progress; | |
} | |
public String getColor() { | |
return color; | |
} | |
public int getDifficulty() { | |
return difficulty; | |
} | |
public Date getAvailable() { | |
return available; | |
} | |
public String getLight() { | |
return light; | |
} | |
public String getName() { | |
return name; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public boolean isIndoor() { | |
return indoor; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public void setProgress(double progress) { | |
this.progress = progress; | |
} | |
public void setAvailable(Date available) { | |
this.available = available; | |
} | |
public void setDifficulty(int difficulty) { | |
this.difficulty = difficulty; | |
} | |
public void setColor(String color) { | |
this.color = color; | |
} | |
public void setIndoor(boolean indoor) { | |
this.indoor = indoor; | |
} | |
public boolean getIndoor() { | |
return indoor; | |
} | |
public void setLight(String light) { | |
this.light = light; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
@Override | |
public String toString() { | |
return name != null ? name : super.toString(); | |
} | |
public boolean isEnableDate() { | |
return enableDate; | |
} | |
public void setEnableDate(boolean enableDate) { | |
this.enableDate = enableDate; | |
} | |
} | |
public List<Plant> getPlants() { | |
List<Plant> plants = new ArrayList<Plant>(); | |
plants.add(new Plant("Bloodroot", "Mostly Shady", 2.44, "03/15/2006", true)); | |
plants.add(new Plant("Columbine", "Shade", 9.37, "03/15/2006", true)); | |
plants.add(new Plant("Marsh Marigold", "Mostly Sunny", 6.81, "05/17/2006", false)); | |
plants.add(new Plant("Cowslip", "Mostly Shady", 9.90, "03/06/2006", true)); | |
plants.add(new Plant("Dutchman's-Breeches", "Mostly Shady", 6.44, "01/20/2006", true)); | |
plants.add(new Plant("Ginger, Wild", "Mostly Shady", 9.03, "04/18/2006", true)); | |
return plants; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment