Wicket Retrieving A CheckGroup's model when clicking a checkbox - java

I have a form with a checkbox column.
ArrayList<MyObject> myObjectsList = // Load from database ...
ArrayList<MyObject> selectedObjects = new ArrayList<MyObject>();
Form form = new Form("my-form");
CheckGroup<MyObject> myObjectGroup = new CheckGroup<MyObject>("object-check-group", new AbstractReadOnlyModel() {
#Override
public Object getObject() {
return selectedObjects;
}
});
WebMarkupContainer container = new WebMarkupContainer("table-container");
DataView dataView = new DataView("table-body", new ListDataProvider<MyObject>(myObjectsList)) {
public void populateItem(final Item item) {
final MyObject myObject = (MyObject) item.getModelObject();
item.add(new Check("check-box", item.getModel())); // Checkbox for each item
// And a couple more columns
}
};
container.add(dataView);
group.add(container);
form.add(group);
add(form);
Now what I would want is to update a form whenever the number of selected objects change :
item.add(new Check("check-box", item.getModel()).add(new AjaxEventBehavior("onclick") {
#Override
protected void onEvent(AjaxRequestTarget target) {
selectedObjects = (ArrayList<MyGroup>) myObjectGroup.getModelObject(); // this does not work sadly :(
// Update form elements
}
}));
Is there an easy way to achieve this with Wicket??

Add a AjaxFormChoiceComponentUpdatingBehavior to your checkgroup. The ajax will trigger everytime a check in your group is clicked.
myObjectGroup.add(new AjaxFormChoiceComponentUpdatingBehavior()
{
#Override
protected void onUpdate(AjaxRequestTarget target)
{
//update form elements
}
});

Related

Is there a way to change the contents of a ComboBoxTableCell depending on the row and column that is being edited?

I am using a JavaFX tableview that has 5 columns (Data Type of Column): Task Id (int), Task Name (String), Task Description (String), Status (String) and Remark (String). Now, I am loading the data from a database that has the same columns and what I'm trying to do is when the user clicks on the task status column in the software the column cell changes to a Combobox (String) but the list of statuses that are displayed in the Combobox should be different depending on the selected item that the user is trying to edit (aka the status of the task)
I have tried creating a new cellFactory in the OnEdit of the column and I have also tried to override the update item method and using a boolean variable I would set whether to set the graphic to a Combobox or not
myStatusColumn.setOnEditStart(new EventHandler<TableColumn.CellEditEvent<Task, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<Task, String> event) {
try {
dataBeingEdited = true;
Task task = myTasksTable.getSelectionModel().getSelectedItem();
statuses = Login.dbHandler.getStatuses(task.getTaskTypeID());
myStatusColumn.setCellFactory(new Callback<TableColumn<Task, String>, TableCell<Task, String>>() {
#Override
public TableCell<Task, String> call(TableColumn<Task, String> param) {
return new TableCell<Task,String>(){
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(empty)
setGraphic(null);
else
{
if(dataBeingEdited)
{
ComboBox<String> comboBox = new ComboBox<>(statuses);
setGraphic(comboBox);
}
}
}
};
}
});
} catch (SQLException e) {
e.printStackTrace();
}
}
});
I expect the output to be a Combobox when I double click on the status column but I am not getting a Combobox to appear and when I get
so far I have not found a way to directly use the ComboBoxTableCell class but what I did is redesigned my own using the CellFactory. What I did was before returning the Cell created in the CellFactory I set an onMouseClickedListner that will check for a double click and when the user double clicks I would get the selected item from the table and set the graphic to be a Combobox with values that depend on the row selected and the column clicked
and then I set an onKeyPressedListener that will change the item selected and then refresh the table and update the database
myStatusColumn.setCellFactory(new Callback<TableColumn<Task, String>, TableCell<Task, String>>() {
#Override
public TableCell<Task, String> call(TableColumn<Task, String> param) {
ComboBox<String> box = new ComboBox<>();
TableCell<Task, String> cell = new TableCell<Task, String>() {
#Override
protected void updateItem(String item, boolean empty) {
if (empty)
setGraphic(null);
else {
setEditable(false);
setText(item);
}
}
};
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getButton().equals(MouseButton.PRIMARY))
{
Task task = myTasksTable.getSelectionModel().getSelectedItem();
if(task!=null)
box.setItems(FXCollections.observableArrayList(task.getStatuses()));
cell.setEditable(true);
}
if(event.getClickCount()==2 && cell.isEditable() ) {
box.getSelectionModel().select(0);
cell.setText(null);
cell.setGraphic(box);
}
}
});
cell.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if(event.getCode().equals(KeyCode.ENTER))
{
try {
TaskLog taskLog = (TaskLog) myTasksTable.getSelectionModel().getSelectedItem();
if(taskLog != null) {
taskLog.setStatues(box.getSelectionModel().getSelectedItem());
taskLog.setStatuesID(Login.dbHandler.getStatusID(taskLog.getStatues()));
System.out.println(taskLog.getStatues());
Login.dbHandler.addNewTaskLog(taskLog);
cell.setEditable(false);
myTasksTable.refresh();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
return cell;
}
});

how filter list that add some filter and remove some in Realm java query

I have list show some stuff and every stuff has some tag.
I want filter stuffs by all tags clicked and change filter by some tag deselect.
Below code show my implements methods that written for do it. But just filter list buy one tag!!! How can filter all tag selected ?
private OrderedRealmCollection<StuffPOJO> list;
#Override
public void onFiltersSelected(ArrayList<TagPojo> filters) {
}
#Override
public void onNothingSelected() {
UpdateStuffList(list.where().findAllAsync());
}
#Override
public void onFilterSelected(TagPojo item) {
///just filter list buy last tag selected !!!
/// how can filter all tag selected?
UpdateStuffList(list.where()
.equalTo("tagList.id", item.getId())
.findAllAsync());
}
#Override
public void onFilterDeselected(TagPojo item) {
}
#Override
public void UpdateTagList(OrderedRealmCollection<TagPojo> list) {
tagAdapter = new Adapter(list);
}
#Override
public void UpdateStuffList(OrderedRealmCollection<StuffPOJO> list) {
this.list = list;
stuffAdapter.updateData(this.list);
}
You need to store all the filters somewhere:
private List<TagPojo> filters = new ArrayList<>();
#Override
public void onFilterSelected(TagPojo item) {
filters.add(item);
String[] filterIds = filters.stream().map(f -> f.getId()).toArray(size -> new String[size]);
UpdateStuffList(list.where()
.in("tagList.id", filterIds )
.findAllAsync());
}
#Override
public void onFilterDeselected(TagPojo item) {
// need to have a proper TagPojo.equals() for this
filters.remove(item);
}

How to set Root checkbox selected when every child is selected(GWT)

I am building GWT app where I have Tree and TreeItems with CheckBoxes. I have one root CheckBox called allCheckBox and his child elements rootCheckBox(this checkBoxes also have theirs children but that is not matter for this). I want that, when user opens dialog with checkBoxes, this checkBox is selected if every childCheckBox is selected. I have done that when tihs root checkBox is selected that child checkBoxes also are selected.
This is my piece of code:
enter cod GWT_DOMAIN_SERVICE.findAll(new AsyncCallback<List<GwtDomain>>() {
#Override
public void onFailure(Throwable caught) {
exitMessage = MSGS.dialogAddPermissionErrorDomains(caught.getLocalizedMessage());
exitStatus = false;
hide();
}
#Override
public void onSuccess(List<GwtDomain> result) {
for (final GwtDomain gwtDomain : result) {
GWT_DOMAIN_SERVICE.findActionsByDomainName(gwtDomain.name(), new AsyncCallback<List<GwtAction>>() {
#Override
public void onFailure(Throwable caught) {
exitMessage = MSGS.dialogAddPermissionErrorActions(caught.getLocalizedMessage());
exitStatus = false;
hide();
}
#Override
public void onSuccess(List<GwtAction> result) {
checkedItems = new GwtCheckedItems();
checkedItems.setName(gwtDomain);
rootCheckBox = new CheckBox();
rootCheckBox.setBoxLabel(gwtDomain.toString());
listCheckBoxes.add(rootCheckBox);
rootTreeItem = new TreeItem(rootCheckBox);
childCheckBoxMapList = new HashMap<GwtAction, CheckBox>();
checkedItems.setMap(childCheckBoxMapList);
for (GwtAccessPermission gwtAccessPermission : checkedPermissionsList) {
if (gwtAccessPermission.getPermissionDomain().toString().equals(checkedItems.getName().toString())) {
if (gwtAccessPermission.getPermissionAction().toString().equals(GwtAction.ALL.toString())) {
rootCheckBox.setValue(true);
}
}
}
if (listOfNewClass.size() == checkedPermissionsList.size()) {
allCheckBox.setValue(true);
}
for (final GwtAction gwtAction : result) {
final CheckBox childTreeItemCheckox = new CheckBox();
treeItem = new TreeItem(childTreeItemCheckox);
childTreeItemCheckox.setBoxLabel(gwtAction.toString());
rootTreeItem.addItem(treeItem);
childListOfNewClass.add(gwtAction);
allTreeItem.addItem(rootTreeItem);
childCheckBoxMapList.put(gwtAction, childTreeItemCheckox);
for (GwtAccessPermission gwtAccessPermission : checkedPermissionsList) {
if (gwtAccessPermission.getPermissionDomain().toString().equals(gwtDomain.toString())) {
if (gwtAccessPermission.getPermissionAction().toString().equals(gwtAction.toString())) {
childTreeItemCheckox.setValue(true);
}
}
}
}
listOfNewClass.put(checkedItems, rootCheckBox);
}
});
}
allCheckBox.addListener(Events.OnClick, new Listener<BaseEvent>() {
#Override
public void handleEvent(BaseEvent be) {
if (allCheckBox.getValue()) {
for (CheckBox checkBox : listCheckBoxes) {
if (!checkBox.getValue()) {
checkBox.setValue(true);
}
}
} else {
for (CheckBox checkBox : listCheckBoxes) {
checkBox.setValue(false);
}
}
}
});
How to set that when all rootCheckBoxes are checked then allCheckBox become also checked?
EDIT: This checkedPermissionsList is List of rootCheckBox which are checked.
Well the listener would have to iterate the child boxes and see if they are all selected, and set the parent box accordingly
Listener<BaseEvent> listener = new Listener<>() {
public void handleEvent(BaseEvent be) {
boolean allSet = listCheckBoxes.stream().allMatch(CheckBox::getValue);
allCheckBox.setValue(allSet); // this will also unselect if appropriate
}
}
It's the same listener for all boxes, so add it to each
listCheckBoxes.forEach(box -> box.addListener(Event.OnClick, listener));
In the pre-Java 8 version:
Listener<BaseEvent> listener = new Listener<>() {
public void handleEvent(BaseEvent be) {
boolean allSet = true;
for (CheckBox child : listCheckBoxes) {
if (!child.getValue()) {
allSet = false; // found a non-checked box
break;
}
}
allCheckBox.setValue(allSet); // this will also unselect if appropriate
}
// and set the listener to the children with
for (CheckBox box : listCheckBoxes) {
box.addListener(Event.Clicked, listener);
}

How to delete a JavaFx TableView Row

I have a JavaFx TableView with each Row having a column with a delete button which when clicked should delete the TableRow, as well as the corresponding entries in the H2 database via Hibernate.
So far I'm not getting anything. Nothing happens on button click. Not even if I manually assign the item Primary Key like so:
NewBeautifulKiwi toDelete = (NewBeautifulKiwi) session.get(NewBeautifulKiwi.class, 97);
Please help me make this work; the button click to delete the TableRow it belongs to as well as the Database items populating that particular TableRow. So far nothing happens at all on ButtonClick.
Thank you in advance.
Ps.
The buttons also get printed where the columns are empty. It would also help if you helped me solve this and only have Buttons on Rows with data
The Class Extract:
public class HomeController implements Initializable {
#FXML
public static TableView<NewBeautifulKiwi> KIWI_TABLE;
#FXML
private TableColumn<NewBeautifulKiwi, Object> KiwiAction;
// Initializes the controller class.
#Override
public void initialize(URL url, ResourceBundle rb) {
KiwiAction.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, Object>("KiwiAction"));
KiwiAction.setCellFactory(new Callback<TableColumn<NewBeautifulKiwi, Object>, TableCell<NewBeautifulKiwi, Object>>() {
#Override
public TableCell<NewBeautifulKiwi, Object> call(TableColumn<NewBeautifulKiwi, Object> param) {
final Button button;
Image image = new Image(getClass().getResourceAsStream("/MediaTools/Error.png"));
final ImageView imageView = new ImageView();
imageView.setFitHeight(16);
imageView.setFitWidth(16);
imageView.setImage(image);
button = new Button("", imageView);
final TableCell<NewBeautifulKiwi, Object> cell = new TableCell<NewBeautifulKiwi, Object>() {
#Override
public void updateItem(Object item, boolean empty) {
if (item != null) {
super.updateItem(item, empty);
final VBox vbox = new VBox(0);
button.setAlignment(Pos.CENTER);
button.maxWidth(32);
button.getStyleClass().add("deleteButton");
final TableCell<NewBeautifulKiwi, Object> c = this;
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
TableRow tableRow = c.getTableRow();
NewBeautifulKiwi item = (NewBeautifulKiwi) tableRow.getTableView().getItems().get(tableRow.getIndex());
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
NewBeautifulKiwi toDelete = (NewBeautifulKiwi) session.get(NewBeautifulKiwi.class, item);
session.delete(toDelete);
session.getTransaction().commit();
session.flush();
session.close();
System.out.println("Deleted");
}
});
vbox.getChildren().add(button);
setGraphic(vbox);
}
}
};
cell.setGraphic(button);
return cell;
}
});
});
Kiwi.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, String>("Kiwi"));
}
I have created a SSCCE to help with deletion of row data with a button. Please have a look at the following code :
TableViewDeleteSample

Add clickHandler for buttoncell added in Cell List

I have created a cellList :
I want to add a clickhandler when user clicks on button "Send"
Please Help. FieldUpdater should work if user clicks on "Send" button.
Here is the code :
final String imageHtml =AbstractImagePrototype.create(images.contact()).getHTML();
// first make a list of HasCell type - MyClass is the type of object being displayed in the CellList (could be String for simple labels)
List<HasCell<contactinfo, ?>> hasCells = new ArrayList<HasCell<contactinfo, ?>>();
hasCells.add(new HasCell<contactinfo, String>()
{
public ButtonCell cell = new ButtonCell();
public Cell<String> getCell()
{
return cell;
}
#Override
public String getValue(contactinfo object)
{
return "Send";
}
#Override
public FieldUpdater<contactinfo, String> getFieldUpdater() {
FieldUpdater< contactinfo, String > updater= new FieldUpdater<contactinfo, String>() {
#Override
public void update(int index, contactinfo object, String value) {
Window.alert("You clicked "+object.getName());
}
};
return updater;
}
}
);
// now construct the actual composite cell using the list (hasCells)
Cell<contactinfo> myClassCell = new CompositeCell<contactinfo>(hasCells)
{
#Override
public void render(Context context, contactinfo value, SafeHtmlBuilder sb)
{
sb.appendHtmlConstant("<table><tbody><tr>");
super.render(context, value, sb);
sb.appendHtmlConstant("</tr></tbody></table>");
}
#Override
protected Element getContainerElement(Element parent)
{
// Return the first TR element in the table.
return parent.getFirstChildElement().getFirstChildElement();
}
#Override
protected <X> void render(Context context, contactinfo contactinfo, SafeHtmlBuilder sb, HasCell<contactinfo, X> hasCell)
{
this renders each of the cells inside the composite cell in a new table cell
// Value can be null, so do a null check..
if (contactinfo == null) {
return;
}
sb.appendHtmlConstant("<table>");
// Add the contact image.
sb.appendHtmlConstant("<tr><td rowspan='3'>");
sb.appendHtmlConstant(imageHtml);
sb.appendHtmlConstant("</td>");
// Add the name and address.
sb.appendHtmlConstant("<td style='font-size:95%;'>");
if(contactinfo.getName()!=null)
sb.appendEscaped(contactinfo.getName());
sb.appendHtmlConstant("</td></tr><tr><td>");
if(contactinfo.getAddress()!=null)
sb.appendEscaped(contactinfo.getRemarks());
sb.appendHtmlConstant("</td>");
Cell<X> cell = hasCell.getCell();
sb.appendHtmlConstant("<td>");
cell.render(context, hasCell.getValue(contactinfo), sb);
sb.appendHtmlConstant("</td></tr></table>");
}
};
// then make the actual cellList, passing the composite cell
cellList =new CellList<contactinfo>(myClassCell,KEY_PROVIDER);
// Add a selection model so we m select cells.
singleselectionModel = new SingleSelectionModel<contactinfo>(
KEY_PROVIDER);
cellList.setSelectionModel(singleselectionModel);
singleselectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
}
});
Also, I do not see in code any piece that handles event. Did you read through http://www.gwtproject.org/doc/latest/DevGuideUiCustomCells.html#cell-onBrowserEvent
Have you tried the code sample provided by GWT - http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellSampler . Browse the "Source Code" !!!
If you have not read already then you should start here # DevGuideUiCustomCells

Categories