Disable an option(s) in a dropdown Unity - unity3d

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.

Related

Swap the type of link depending on model object

I'm at complete loss how to proceed further:
I have panel with a DropDownChoice and a submit button next to it. Depending on the selected value of the DropDownChoice (Obtained upon the firing of a OnChangeAjaxBehavior attached to it, the submit button needs to either replace the whole panel with a different one, OR become an ExternalLink.
Currently, the code looks like that:
public class ReportSelectionPanel extends Panel {
protected OptionItem selectedOption ;
public ReportSelectionPanel(String id) {
super(id);
IModel<List<OptionItem>> choices = new AbstractReadOnlyModel() {
// Create a list of options to be displayed in the DropDownChoice
} ;
final IModel<OptionItem> optionModel =
new PropertyModel<OptionItem>(this,"selectedOption") ;
final DropDownChoice<OptionItem> options =
new DropDownChoice("selectChoice",optionModel,choices) ;
// I don't know what the button should be... Plain Button? A Link?
final Component button = ???
options.add( new OnChangeAjaxBehavior() {
protected void onUpdate(AjaxRequestTarget target) {
if ( selectedOption.getChild() == null ) {
// button becomes an ExternalLink.
// A new window will popup once button is clicked
} else {
// button becomes a Something, and upon clicking,
// this ReportSelectionPanel instance gets replaced by
// an new Panel instance, the type of which is
// selectedOption.getChild()
}
} ) ;
I'm really not quite sure what the commented lines should become to achieve the result. Any suggestions?
Thanks!
Eric
IMHO it's nicer to keep just one button and just react differently depending on the selected option:
final Component button = new AjaxButton("button") {
public void onClick(AjaxRequestTarget target) {
if (selectedOption.getChild() == null) {
PopupSettings popup = new PopupSettings();
popup.setTarget("'" + externalUrl + "'");
target.appendJavascript(popup.getPopupJavaScript());
} else {
ReportSelectionPanel.this.replaceWith(new ReportResultPanel("..."));
}
}
};
// not needed if options and button are inside a form
// options.add( new OnChangeAjaxBehavior() { } ) ;

What is the right usage for the SingleSelectionModel?

we would like to link from a CellTable to a property editor page. We use the SingleSelectionModel to get notified, when a user clicks on an item.
It is initialized like this:
private final SingleSelectionModel<Device> selectionModel = new SingleSelectionModel<Device>();
We then assign the selection change handler:
selectionModel.addSelectionChangeHandler(this);
Our selection change handler looks like this:
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Log.debug("DevicesPresenter: SelectionChangeEvent caught.");
Device selectedDevice = selectionModel.getSelectedObject();
if (selectedDevice != null) {
selectionModel.clear();
if (selectionModel.getSelectedObject() != null){
Log.debug("DevicesPresenter: selected item is " + selectionModel.getSelectedObject());
}
else{
Log.debug("DevicesPresenter: selected item is null");
}
deviceEditorDialog.setCurrentDevice(selectedDevice.getUuid());
// get the container data for this device
clientModelProvider.fetchContainersForDevice(selectedDevice.getUuid());
PlaceRequest request = new PlaceRequest.Builder()
.nameToken(NameTokens.deviceInfo)
.with("uuid", selectedDevice.getUuid())
.build();
Log.debug("Navigating to " + request.toString());
placeManager.revealPlace(request);
}
}
Now there are two issues: There always seem to be two SelectionChangeEvents at once and i really cannot see why. The other thing is: How is the right way do handle selection of items and the related clearing of the selection model? Do we do that the right way?
Thanks!
If you only want to get notified of "clicks" without keeping the "clicked" item selected, use a NoSelectionModel instead; no need to clear the selection model as soon as something is selected.
As for your other issue with being called twice, double-check that you haven't added your selection handler twice (if you can unit-test your DevicesPresenter, introspect the handlers inside the selection model for example)
In your line selectionModel.addSelectionChangeHandler(this); what does this refer?
Here my code how I use SingleSelectionModel
public class MyClass{
private final SingleSelectionModel<CountryDto> selectionModel = new SingleSelectionModel<CountryDto>();
...
public MyClass(){
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
CountryDto selected = selectionModel
.getSelectedObject();
if (selected != null) {
Window.alert("Selected country "+selected.getTitle());
}
}
});
}
}

How to hide tab pages in a tab control

I am learner in c# and I have a small doubt.
In my windows form page, I have a tabControl contains 3 tabPages.I also have a comboBox outside the tabControl with 3 items listing the names of tab pages.
What i want to do is select the name of the first tabPage from the comboBox, and only that tabPage should be displayed by hiding the other tabPages.
Visibility property is not applicable to tabPages. So how can i do this?
Thanks in advance.
According to Hiding and Showing Tabpages in a Tabcontrol by Debasmit Samal:
Visiblity property has not been implemented on the TabControl, and
there is no Insert method also. Some workaround on this
private void HideTabPage(TabPage tp)
{
if (tabControl1.TabPages.Contains(tp))
tabControl1.TabPages.Remove(tp);
}
private void ShowTabPage(TabPage tp)
{
ShowTabPage(tp, tabControl1.TabPages.Count);
}
private void ShowTabPage(TabPage tp , int index)
{
if (tabControl1.TabPages.Contains(tp)) return;
InsertTabPage(tp, index);
}
private void InsertTabPage(TabPage tabpage, int index)
{
if (index < 0 || index > tabControl1.TabCount)
throw new ArgumentException("Index out of Range.");
tabControl1.TabPages.Add(tabpage);
if (index < tabControl1.TabCount - 1)
do
{
SwapTabPages(tabpage, (tabControl1.TabPages[tabControl1.TabPages.IndexOf(tabpage) - 1]));
}
while (tabControl1.TabPages.IndexOf(tabpage) != index);
tabControl1.SelectedTab = tabpage;
}
private void SwapTabPages(TabPage tp1, TabPage tp2)
{
if (tabControl1.TabPages.Contains(tp1) == false || tabControl1.TabPages.Contains(tp2) == false)
throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");
int Index1 = tabControl1.TabPages.IndexOf(tp1);
int Index2 = tabControl1.TabPages.IndexOf(tp2);
tabControl1.TabPages[Index1] = tp2;
tabControl1.TabPages[Index2] = tp1;
//Uncomment the following section to overcome bugs in the Compact Framework
//tabControl1.SelectedIndex = tabControl1.SelectedIndex;
//string tp1Text, tp2Text;
//tp1Text = tp1.Text;
//tp2Text = tp2.Text;
//tp1.Text=tp2Text;
//tp2.Text=tp1Text;
}

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));
}