[DataBinding] Binding elements in different views [message #645091] |
Wed, 15 December 2010 14:58 |
|
Hi all!!
I'm developing a RAP application in which I need a master-detail scenario between a table or list and a form (of Control elements).
I have yet used the examples in the snippets page (http://wiki.eclipse.org/JFace_Data_Binding/Snippets) or the RAP project from Eclipse CVS repository org.eclipse.rap.demo.databinding.
I can create a master-detail which reside in a single view:
// Creating table viewer and relativa columns
viewer = new TableViewer(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
createColumns(viewer);
// Setting content provider
ObservableListContentProvider viewerContent = new ObservableListContentProvider();
viewer.setContentProvider(viewerContent);
// Setting label provider
String[] attrPerson = new String[] { "name", "address", "city" };
IObservableMap[] attributeMaps = BeansObservables.observeMaps( viewerContent.getKnownElements(), Person.class, attrPerson);
omlProvider = new ObservableMapLabelProvider(attributeMaps);
viewer.setLabelProvider(omlProvider);
// In "list" I have my list of data to populate the table
input = new WritableList(list, Person.class);
viewer.setInput(input);
// Let's binding
// Making the table observable
IObservableValue selectedPerson = ViewersObservables.observeSingleSelection(viewer);
Realm realm = SWTObservables.getRealm(parent.getDisplay());
DataBindingContext bindingContext = new DataBindingContext(realm);
// Binding the form elements with the selected element of the table
// 1st element; other one are set in a similar way
IObservableValue modelObservableValue = BeansObservables.observeDetailValue(selectedPerson, "name", String.class);
ISWTObservableValue targetObservableValue = SWTObservables.observeText(name, SWT.Modify);
Binding binding = bindingContext.bindValue(targetObservableValue, modelObservableValue);
This code works because I have all the needed elements in the same view. If, for example, I have the table in a view and the form in another one, how can I make the data of the selection from the table available to the form view?
My problem is in the step of the creation of the "selectedPerson" object on which the UI elements of the form are based. The method ViewersObservables#observeSingleSelection works woth teh viewer object which in my case is in a different view.
Thank you in advanced who can help.
Gabriele
|
|
|
Re: [DataBinding] Binding elements in different views [message #645962 is a reply to message #645091] |
Tue, 21 December 2010 10:47 |
|
Maybe this isn't a common use case, or I haven't exposed so well my problem.
I make a brief summary: I want to do a master-detail scenario in different views.
I resolved my problem using the "Selection service" framework:
// Made my tableviewer as selection provider
getSite().setSelectionProvider(tableviewer);
....
// Add a selection listener in the "detail" view
getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(...).
...
// Management of the selection
private ISelectionListener mylistener = new ISelectionListener() {
public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
if (sourcepart != MyView.this &&
selection instanceof IStructuredSelection) {
doSomething(((IStructuredSelection) selection).toList());
}
}
};
When I select an element from my tableviewer, I have a selection object which is a Person object (in mycase); so I have the model observable object in the binding process.
So I can use the previous code where the "selectedPerson" is the selection object
/* "selection" has generic type Object; casting it to IStructuredSelection, I can have the first element of the selction, i.e. the selected object
*/
IStructuredSelection sselection = (IStructuredSelection) selection;
/* After checking the type of the selected object, I can use it as model observable part of the following binding
*/
selectedPerson = (Person) sselction.getFirstElement();
Then it is possible to bind each UI element of the detail view with the model represented by the selected object.
|
|
|
|
Re: [DataBinding] Binding elements in different views [message #646294 is a reply to message #646279] |
Thu, 23 December 2010 08:30 |
|
Thanks for your reply Matthew
To select an element from the table another way which I used is:
IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
final ISelectionService selectionService = window.getSelectionService();
selectionService.addSelectionListener(...)
I think that your suggestion make the same tasks as the part I wrote the previous time (i.e. obtain the first element of hte selection):
Quote: |
/* "selection" has generic type Object; casting it to IStructuredSelection, I can have the first element of the selction, i.e. the selected object
*/
IStructuredSelection sselection = (IStructuredSelection) selection;
/* After checking the type of the selected object, I can use it as model observable part of the following binding
*/
selectedPerson = (Person) sselction.getFirstElement();
|
I will try your solution, and I will let you know.
thanks again
Gabriele
|
|
|
Powered by
FUDForum. Page generated in 0.02792 seconds