Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 299055 - Snippets showing how to bind selection of TableRidget and ComboRidget
Summary: Snippets showing how to bind selection of TableRidget and ComboRidget
Status: RESOLVED FIXED
Alias: None
Product: Riena
Classification: RT
Component: UI (show other bugs)
Version: 1.2.0   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: 2.0.0.M6   Edit
Assignee: Elias Volanakis CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-07 11:08 EST by Yang Meyer CLA
Modified: 2010-02-11 21:42 EST (History)
1 user (show)

See Also:


Attachments
SnippetTableRidgetBindingSelectionWithoutIndirection (6.60 KB, text/plain)
2010-01-07 11:10 EST, Yang Meyer CLA
no flags Details
SnippetTableRidgetBindingSelection (7.16 KB, text/plain)
2010-01-07 11:11 EST, Yang Meyer CLA
no flags Details
SnippetComboRidgetModelIndirection (3.37 KB, text/plain)
2010-01-07 11:11 EST, Yang Meyer CLA
no flags Details
Updated Snippets (patch) (21.77 KB, text/plain)
2010-01-07 13:55 EST, Elias Volanakis CLA
no flags Details
Updated SnippetTableRidgetBindingSelection (6.65 KB, text/plain)
2010-01-13 05:32 EST, Yang Meyer CLA
elias: iplog+
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Yang Meyer CLA 2010-01-07 11:08:35 EST
Three new snippets, with sketches of what is bound to what:

* SnippetTableRidgetBindingSelectionWithoutIndirection: table selection -> label text
* SnippetTableRidgetBindingSelection: table selection -> model -> label text
* SnippetComboRidgetModelIndirection: combobox value/selection -> model -> label text

IMPORTANT NOTE, concerning SnippetTableRidgetBindingSelection:
The bindings do not seem to work. The model is updated correctly when something is selected in the table, but is not correctly reflected in the label text. In the attached file I re-bind the model to the label, which of course is bogus… (marked XXX in source)
Comment 1 Yang Meyer CLA 2010-01-07 11:10:07 EST
Created attachment 155509 [details]
SnippetTableRidgetBindingSelectionWithoutIndirection
Comment 2 Yang Meyer CLA 2010-01-07 11:11:31 EST
Created attachment 155510 [details]
SnippetTableRidgetBindingSelection

Bogus but working snippet; points out the problem.
Comment 3 Yang Meyer CLA 2010-01-07 11:11:55 EST
Created attachment 155511 [details]
SnippetComboRidgetModelIndirection
Comment 4 Elias Volanakis CLA 2010-01-07 12:55:52 EST
Reg. SnippetTableRidgetBindingSelection: the rebind is necessary, because the instance of currentSelection changes. 

t1: currentSelection#instance1
t2: outputRidget.bindToModel(currentSelection);
    outputRidget.updateFromModel(); // shows instance1
t3: currentSelection#instance2; // ridget still bound to instance1
t4: outputRidget.bindToModel(currentSelection);
    outputRidget.updateFromModel(); // shows instance2
Comment 5 Elias Volanakis CLA 2010-01-07 13:53:30 EST
Hi Yang,

thanks for the snippets.

The short answer is that, for the table, you cannot bind to a subproperty of the current selection directly. What is missing is API like:

tableRidget.bindSingleSelection("english", outputRidget, "text");

If you think we need this please open a separate bugzilla so we can keep track of this request.

As a workaround you can use:

tableRidget.addSelectionListener(new ISelectionListener() {
	public void ridgetSelected(SelectionEvent event) {
		List<Object> newSelection = event.getNewSelection();
		if (!newSelection.isEmpty()) {
			MyNode node = (MyNode) newSelection.get(0);
			outputRidget.setText(node.getEnglish());
		}
	}
});

I'm attaching a patch with all snippets. Feel free to change as appropriate.

A couple of quick comments:

- I'm providing adding a snippet that shows an alternative way to achieve the results of SnippetComboRidgetModelIndirection. If there is need to store something one can also use a WritableValue instead of a field. 
- Please use SWT.NONE instead of 0 when creating new SWT controls, i.e. new Composite(shell, SWT.NONE)
- I would prefer to use to UIControlsFactory to create new widgets, so that we can demonstrate this aspect of Riena as well. 
- The preferred naming convention for the Snippets is SnippetRidgetName123. We can add a longer description the the Riena Snippets on the wiki, after the snippets are committed -- http://wiki.eclipse.org/Riena_Snippets
Comment 6 Elias Volanakis CLA 2010-01-07 13:55:08 EST
Created attachment 155530 [details]
Updated Snippets (patch)
Comment 7 Yang Meyer CLA 2010-01-08 05:43:53 EST
Elias, thanks for all the helpful comments and snippet updates.

Your work-around works, of course. But would you still call it "binding"?? (i.e. you aren't using bindSingleSelectionToModel(...) and instead set the values directly)

Let's say I don't mind outputting the whole MyNode object (toString()) constituting the current selection, bypassing the requirement for binding to a subproperty of the current selection. So I believe I should be able to bind the table selection to the field 'MyNode currentSelection', and this same field to the output ridget, like this:

	tableRidget.bindSingleSelectionToModel(this, "currentSelection");
	outputRidget.bindToModel(this, "currentSelection"); 
	tableRidget.addSelectionListener(new ISelectionListener() {
		public void ridgetSelected(SelectionEvent event) {
			outputRidget.updateFromModel();
		}
	});

However this doesn't work. The reason seems to be that - even though it's called *single* selection - the selection is of type List. I'm assuming there's a good reason for this; but what is it?
Comment 8 Elias Volanakis CLA 2010-01-13 01:26:27 EST
(In reply to comment #7)

Hi Yang,

(a) This:
>     tableRidget.bindSingleSelectionToModel(this, "currentSelection");
will bind the whole current selection from the tableRidget to setSetCurrentSelection().

If you want a (sub)property of the current selection, you would need something like: tableRidget.bindSingleSelection("english", outputRidget, "text"); which is 
not supported.

(b) ISelectionListener is the same for Single and Multiple selection, in order to simplify the API. This is why single selection is considered as a special case of multiple selection - i.e. you always get a list in both cases.

I hope this answers your questions?
Comment 9 Yang Meyer CLA 2010-01-13 05:30:21 EST
Hi Elias,

Thanks for your comments. I rebuilt my example from comment #7: strangely enough now it does work. I probably had had some leftover artifact somewhere :-/ Sorry for the trouble.

At least I now understand better, and am writing it down for future reference (might be obvious, but initially wasn't for me):

* tableRidget.bindSingleSelectionToModel(this, "currentSelection")
  binds the table's currently selected MyNode object (the row) to the 
  property currentSelection of type MyNode.

* If you use ISelectionListener to retrieve the current selection through
  selectionEvent. getNewSelection(), you will get a List<Object>, as shown 
  in comment #5.

I am attaching an improved version of SnippetTableRidgetBindingSelection that shows how to bind the label to a subproperty of the selected-row object. It's not pretty, but I think it's still better than the manual updating from comment #5.
Comment 10 Yang Meyer CLA 2010-01-13 05:32:01 EST
Created attachment 155964 [details]
Updated SnippetTableRidgetBindingSelection
Comment 11 Elias Volanakis CLA 2010-02-11 21:40:31 EST
Thanks Yang!

I've commited the table snippets as SnippetTableRidget005 + 006.

I've left out the Combo snippet because I thought it is very similar to other snippets: SnipppetComboRidget001/002, SnippetTableRidget005/006