Is there any way to select/deselect the child tree components of checkbox in swt depending upon the parent selection/deselection respectively? - swt

When I select top treeitem check box then all subtreeitems checkboxes gets selected and if I deselct the top treeitem then all subtreeitems checkboxes gets deselected. But after deselecting all subtreeitems, I want to select any the subtreeitem checkbox manually How can I implement??
parentTree= new Tree(mainComp, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);`
TreeItem[] treeItems = dataValidationTree.getItems();
for (TreeItem item : treeItems)
{
TreeItem[] subTreeItems = item.getItems();
for (TreeItem subItem : subTreeItems)
{
boolean checked = item.getChecked();
checkSubTree(item, checked);
}
}
private void checkSubTree(TreeItem item,boolean checked) {
item.setChecked(checked);
for(TreeItem subItems : item.getItems())
{
checkSubTree(subItems,checked);
}
}

If you are trying to use a selection listener to check all of the subtree you would do it like this:
Tree checkTree = new Tree(shell, SWT.CHECK ...
...
checkTree.addListener(SWT.Selection, event ->
{
TreeItem item = (TreeItem)event.item;
for (TreeItem subItems : item.getItems())
checkSubTree(subItems, item.getChecked());
});
...
private static void checkSubTree(TreeItem item, boolean checked)
{
item.setChecked(checked);
for (TreeItem subItems : item.getItems())
checkSubTree(subItems, checked);
}
This only looks at the sub-tree which was selected and will allow individual items to be check/unchecked normally.

Related

How to get current row (element) on TreeEditor Button click event

TreeViewerColumn colEdit= new TreeViewerColumn (viewer, column);
colEdit.setLabelProvider(new ColumnLabelProvider(){
#Override
public void update(ViewerCell cell) {
TreeItem item = (TreeItem) cell.getItem();
Button btnEdit= new Button((Composite) cell.getViewerRow().getControl(),SWT.NONE);
btnEdit.setText("Edit");
TreeEditor editor = new TreeEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(btnEdit, item, cell.getColumnIndex());
editor.layout();
btnEdit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
// How to get current row element.
TreeItem[] selection = treeViewer.getTree().getSelection(); // (Selection is empty, because click on button and selection not happened)
}
});
}
});
treeViewer.getTree().getSelection(); return empty because Tree Editor with button control added and while button click row selection not happening.
cell.getElement() returning null in handler method.
btnEdit.setData can help me but Is there any way to get current row (element) on which button click event it clicked ??

Disable an option(s) in a dropdown Unity

I need to disable 1(or 2) dropdown option from a dropdown menu in Unity.
The dropdown menu should not be repopulated.
There should not be any deletion/deactivated options from the dropdown menu
Anyone have any idea how to do this. ?
Similar to this answer but a slightly different approach.
The other answer uses a hardcoded toggle.name == "Item 1: Option B" to compare the buttons. I'd rather use a central and index-based system:
Put this component on the DropDown
[RequireComponent(typeof(Dropdown))]
[DisallowMultipleComponent]
public class DropDownController : MonoBehaviour, IPointerClickHandler
{
[Tooltip("Indexes that should be ignored. Indexes are 0 based.")]
public List<int> indexesToDisable = new List<int>();
private Dropdown _dropdown;
private void Awake()
{
_dropdown = GetComponent<Dropdown>();
}
public void OnPointerClick(PointerEventData eventData)
{
var dropDownList = GetComponentInChildren<Canvas>();
if (!dropDownList) return;
// If the dropdown was opened find the options toggles
var toogles = dropDownList.GetComponentsInChildren<Toggle>(true);
// the first item will always be a template item from the dropdown we have to ignore
// so we start at one and all options indexes have to be 1 based
for (var i = 1; i < toogles.Length; i++)
{
// disable buttons if their 0-based index is in indexesToDisable
// the first item will always be a template item from the dropdown
// so in order to still have 0 based indexes for the options here we use i-1
toogles[i].interactable = !indexesToDisable.Contains(i - 1);
}
}
// Anytime change a value by index
public void EnableOption(int index, bool enable)
{
if (index < 1 || index > _dropdown.options.Count)
{
Debug.LogWarning("Index out of range -> ignored!", this);
return;
}
if (enable)
{
// remove index from disabled list
if (indexesToDisable.Contains(index)) indexesToDisable.Remove(index);
}
else
{
// add index to disabled list
if (!indexesToDisable.Contains(index)) indexesToDisable.Add(index);
}
var dropDownList = GetComponentInChildren<Canvas>();
// If this returns null than the Dropdown was closed
if (!dropDownList) return;
// If the dropdown was opened find the options toggles
var toogles = dropDownList.GetComponentsInChildren<Toggle>(true);
toogles[index].interactable = enable;
}
// Anytime change a value by string label
public void EnableOption(string label, bool enable)
{
var index = _dropdown.options.FindIndex(o => string.Equals(o.text, label));
// We need a 1-based index
EnableOption(index + 1, enable);
}
}
Configure in the Inspector
or via scripts e.g.
dropDownReference.GetComponent<DropDownController>().EnableOption("Option B", false);
You can achieve this using the Toggle component from DropDown->Template->ViewPort->Content->Item.
This script is creating items every time DropDown menu is selected. You can just access Toogle and disable interactable field like this:
void Start ()
{
//This script should be attached to Item
Toggle toggle = gameObject.GetComponent<Toggle>();
Debug.Log(toggle);
if (toggle != null && toggle.name == "Item 1: Option B")
{
toggle.interactable = false;
}
}
You can also see a DropDown list is created every time arrow on DrowDown is clicked and destroyed when menu is closed.

dynamically select a checkbox for siblings treenode in Smart GWT

I have a selectable Tree with checkbox appearance. I need to select all sibling TreeNode on selection of a specific TreeNode.
I could get all the sibling tree nodes, but I don't know what is the attribute name of TreeNode to make that checkbox selected.
Can anybody help me giving some way to select those nodes.
compareGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
#Override
public void onSelectionChanged(SelectionEvent event) {
TreeNode node = (TreeNode) event.getSelectedRecord();
TreeNode parent = tree.getParent(node);//tree is Tree object
treeGrid.selectRecord(parent);
TreeNode[] nodes = tree.getAllNodes(parent);
for(int i=0; i< nodes.length; i++){
if(!nodes[i].getAttributeAsBoolean("isSelected"))
treeGrid.selectRecord(nodes[i]);
}
}
}
});
You can use any of the following:
treeGrid.selectAllRecords();
treeGrid.selectRecord(record);
treeGrid.selectRecords(records);
The first method will select all the TreeNodes of the tree.
The 2nd one will select only one specified TreeNodes of the tree.
And the 3rd one will select multiple specified TreeNodes of the tree.
There are multiple overloaded methods for the last 2 methods, which allows you to specify Nodes in terms of, TreeNode(s) itself, or index of the TreeNode(s).
Here's a solution quite close (without checkboxes) to what you need.
employeeTreeGrid.addNodeClickHandler(new NodeClickHandler() {
public void onNodeClick(NodeClickEvent event) {
if (event.getNode() != null) {
TreeNode node = event.getNode();
TreeNode parent = employeeTree.getParent(node);
if (employeeTreeGrid.isSelected(node)) {
List<TreeNode> nodesToSelect = new ArrayList<TreeNode>();
// omit parent (root) if on first level
if (!"1".equals(node.getAttribute("ReportsTo"))) {
nodesToSelect.add(parent);
}
TreeNode[] siblings = employeeTree.getChildren(parent);
nodesToSelect.addAll(Arrays.asList(siblings));
RecordList recordList = employeeTreeGrid.getOriginalRecordList();
for (TreeNode treeNode : nodesToSelect) {
Record record = recordList.find("EmployeeId", treeNode.getAttribute("EmployeeId"));
if (record != null) {
employeeTreeGrid.selectRecord(record);
}
}
}
}
}
});
Have to use the RecordList and first find required records in order to use ListGrid.selectRecord() methods.
Using SelectionAppearance.CHECKBOX and SelectionChangedHandler can be tricky as programmatic selections are going to trigger further selection events.
This is based on Checkbox tree sample with below changes.
// employeeTreeGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
// employeeTreeGrid.setShowSelectedStyle(false);
employeeTreeGrid.setShowPartialSelection(false);
// employeeTreeGrid.setCascadeSelection(true);
employeeTreeGrid.setSelectionType(SelectionStyle.SIMPLE);
To get the value of selected checkbox from tree grid in smart gwt I have following solution ListGridRecord[] arrRec = event.getSelection(); sample code is below.
employeeTreeGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
employeeTreeGrid.setSelectionType(SelectionStyle.SIMPLE);
employeeTreeGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
#Override
public void onSelectionChanged(SelectionEvent event)
//selectedCounties Set to add selected checkbox or deslected checkbox names/title
if (selectedCounties == null || selectedCounties.size() == 0)
selectedCounties = new TreeSet<String>();
selectedCounties.clear();
ListGridRecord[] arrRec = event.getSelection();
for (ListGridRecord listGridRecord : arrRec) {
selectedCounties.add(listGridRecord.getAttribute("Name"));
}
// You can do iteration over it if needed
selectedCounties.remove("All Counties");
Iterator<String> it = selectedCounties.iterator();
while (it.hasNext()) {
if (it.next().contains("Zone")) {
it.remove();
}
}
}
});

GWT:how to get selected radio button's value

I am create dynamic number of radio buttons in my GWT
public void createTestList(ArrayList<Test> result){
for(int i =0 ; i<result.size();i++){
int id = result.get(i).getTestId();
RadioButton rd = new RadioButton("group", result.get(i).getTestType());
verticalPanel.add(rd);
}
where Test is my Entity class ..
I am getting 4 different types of radio buttons in my view , Now if i select any one of the radio button, first I need to get the id of the selected Radio button , how can this be possible ?
secondly How will i check that which one of the multiple radio button is selected ?
Thanks
You need to check public java.lang.Boolean getValue() on each radio button whether it is checked or not.
it is possible to add click handler and update selected radio button variable:
choiceItemKind = new VerticalPanel();
ArrayList<String> kinds = new ArrayList<String>();
kinds.add(...);
kinds.add(...);
choiceItemKind.clear();
ClickHandler choiceClickHandler = new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
addItemKindSelectedLabel = ((RadioButton) event.getSource()).getText();
}
};
for (String label : kinds)
{
RadioButton radioButton = new RadioButton("kind", label);
//radioButton.setTitle("Tooltyp");
if (label.equals(addItemKindSelectedLabel))
radioButton.setValue(true);
radioButton.addClickHandler(choiceClickHandler);
choiceItemKind.add(radioButton);
}
...
addItemKindSelectedLabel = "";
...
if (!addItemKindSelectedLabel.isEmpty())
...;
upd: set selected radiobutton without rebuild:
for (int i = 0; i < choiceItemKind.getWidgetCount(); i++)
{
RadioButton radioButton = (RadioButton) choiceItemKind.getWidget(i);
radioButton.setValue(radioButton.getText().equals(addItemKindSelectedLabel));
}

Java SWT: how to delete the selected row in a SWT table

I have implemented one SWT table having a button widget in one column. On click of a button I am deleting the entire row. But I don't understand how to refresh/redraw/update the table.
Table processListTable;
TableItem tableItem;
Image deleteImage = Activator.getImageDescriptor("icons/trash.gif").createImage();
private void addRowInTable() {
tableItem = new TableItem(processListTable, SWT.FILL);
tableItem.setText(0, "value 1");
tableItem.setText(1, "value 2");
TableEditor editor = new TableEditor(processListTable);
final Button deleteButton = new Button(processListTable, SWT.PUSH | SWT.FILL);
deleteButton.pack();
editor.minimumWidth = deleteButtonButton.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(deleteButtonButton, tableItem, 2);
deleteButtonButton.setImage(deleteImage);
deleteButtonButton.addListener(SWT.Selection, new SelectionListener(tableItem, checkButton));
}
class SelectionListener implements Listener {
TableItem item;
Button deleteButton;
public SelectionListener(TableItem item, Button deleteButton) {
this.item = item;
this.deleteButton = deleteButton;
}
public void handleEvent(Event event) {
this.deleteButton.dispose();
this.item.dispose();
}
}
Check SWT snippet remove selected items from Table.
Just call table.remove(int rowIdx); instead of item.dispose();
public void handleEvent(Event event) {
this.deleteButton.dispose();
this.trash.dispose();
this.item .dispose();
Table table = viewer.getTable();
table.getColumn(2).pack();
table.getColumn(2).setWidth(100);
}
This is the solution for refresh the SWT table.
Use the JFace TableViewer with a model class, delete the object from the model and refresh the TableViewer.