Unique ContextMenu for each TreeItem - treeview

I can set ContextMenu for a TreeView through the custom cellFactory, but the problem is that this menu is applied to all items in the tree. Is there are an way to bind different contextMenus to different items?

By design, the custom TreeCell you have defined will be reused multiple times while rendering the tree view item values and it is good thing. In updateItem method, you can change the content of the "current" contextMenu according to the item passed as parameter to this updateItem method.

Related

Performing action when NSRuleEditor criterion is selected

How can I determine which criterion is being selected in the NSPopUpButton menu of an NSRuleEditor? Inside ruleEditorRowsDidChange(_:)
I could track the criteria for each row and process the changes manually, but it seems like there should be an easier way.
I tried returning an NSMenuItem instead of a String for the criterion's value in the rule editor's delegate; then setting the target and action properties on the menu item. That allows me to have a method called when the menu item (criterion) is selected in the rule editor. It also means I know which criterion triggered the action method because each menu item's target is the criterion itself. However, setting the target and action properties on the menu item overrides the private target and action values that the rule editor sets on the menu item. This means that after selecting a criterion in the rule editor, the target/action that I set fires, but the selection change is not reflected in the rule editor.
My current solution is to return an NSPopUpButton as the criterion's displayValue (since it inherits from NSView) and manage the button's items and target/action manually. This works well but feels like it breaks the design of the rule editor a bit by having a single criterion managing multiple values itself instead of relying on the rule editor's delegate).
Am I going about this the wrong way? Is there a simpler method to run criterion-specific code when a criterion is selected within a rule editor?
Thanks!
Set the action of the menu item and inside the action call - (void)setCriteria:(NSArray *)criteria andDisplayValues:(NSArray *)values forRowAtIndex:(NSInteger)rowIndex of the rule editor to refresh the row. You can get the row with - (NSInteger)rowForDisplayValue:(id)displayValue.

Using NSArrayController to bind model objects to an inspector view, how to catch special cases like empty and multiple selections?

I have inspector view and because it contains a lot of UI I want to explore cocoa bindings to connect it to the model. The inspector view will update depending on the currently selected object in an outline view and the inspector view displays controls to alter properties (like colour, name, shape etc.) that can be changed.
This works well when only one row in the outline view is selected because the view has a one-to-one relationship with the model object. However, there are two special cases that I want to be able to catch:
Empty selection
If no items in the outline view are selected I want to display a special "No items selected view" placeholder view.
Multiple selection
If multiple items are selected I can either display a "Multiple items selected view" placeholder view or I can decide to let the changes apply globally to all selected model objects.
My question is how can these special cases be caught when using bindings? Moreover, how can I do behaviour "A" when there is an empty selection and behaviour "B" when there is a multiple section. This sort of thing is trivial when using target/action because you can just code the logic into the action method.
Bindings seem great when propagating changes to values but I'm unsure how to go about implementing bindings that require different decisions based on current selection state.

GWT Watermarked (AKA placeholder) ListBox

I want to have in my gwt application a ListBox with a watermark.
I already created TextBox and a DateBox with a watermark by extending those classes and the property placeholder of the DOM.
However I could not such property for the ListBox. There isn't any, right? (how can I tell? except for trying.)
Assuming that there is no such property I would like to implement such a class by extending ListBox.
I am not sure how such a ListBox would behave.
could you please help me define the behaviour of such ListBox or maybe you know a site that uses one so I could play with it? Or just point me to a code example.
Thanks
I would think it would be a non editable list item as the placeholder. You could have the setPlaceholder set the text on the list item. The placeholder list item would only be visible when the list was empty. You couldn't select the list item, and all methods to access list items would never see it.

UserControl instead of DataTemplates - is there a way for big collections?

There are two ways. May be both are stupid...
I have to display some collections of items.
First one.
I use DataTemplate for ListBoxItem.
Just set itemSource = myCollection;
That's all. Simple scheme.
Second one.
Each item in my collection has property view. It's a UserControl. That define how item renders.
Create DataTemplate with ContentPresenter only.
Binding Content property to a view.
Just set itemSource = myCollection;
That's all. More complex. But works too.
Has second one right to live? My doubt is that I have to create instance of UserControl for every item in my collection?
Is not it too expensive for collection with over than 500 items?
Thanks.
I don't believe there is much difference, with the DataTemplate approach the framework will create an instance of the DataTemplate for each item in the collection. In the second approach an instance of the user control will be created for each item, there may be a few more controls but only a few per item.
One reason the second approach could be preferable is that you can have logic around which content is bound. This could mean different user controls for each item in the list. Caliburn Micro lets you use this approach very naturally.

How to bind test data to a SketchFlow ComboBox?

I haven't been able to successfully bind some test data to a SketchFlow ComboBox. I added a sample data source and created a collection with some basic string values. There are a number of tutorials on how to bind to a ListBox in SketchFlow and that works fine, just not finding anything for a ComboBox.
One thing that caught me out a few times is that you need to have your data in "list mode" before you drag it on to the control (combo box or list box).
It seems to work as I expected, so perhaps I'm not understanding your issue correctly, but I'll give it a shot! I created a sample DataSource with a couple of strings in it, added a ComboBox to the layout root and then just dragged the Collection from the DataSource and dropped it on the ComboBox.
Keep in mind that if you drag the DataSource itself, you'll only be setting the DataContext property of the ComboBox and that's not enough to get your items to display. You also need the ItemsSource bound to the Collection of the DataSource and an ItemTemplate or a DisplayMemberPath to tell the ComboBox how to display your items.
Additionally, if you use the drag and drop method of binding, it will use the ItemTemplate approach for you, which may or may not be what you want as it will generally create a StackPanel and display all fields from the row in your Collection per item in the ComboBox. You can remove the ItemTemplate and set DisplayMemberPath to whichever field you wish to have displayed from your data source's collection.

Resources