Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XWT » TableViewer selection binding with converter and validation
TableViewer selection binding with converter and validation [message #1015538] Fri, 01 March 2013 07:51 Go to next message
Jürgen Weinberger is currently offline Jürgen WeinbergerFriend
Messages: 36
Registered: August 2012
Member
Hello!

I have some trouble with a TableViewer and the selection binding. Hopefully somebody can help or explain the problem to me.

I have a simple TitleAreaDialog with a TableViewer, where the User may selected one or more things and the standard ok and cancel button. The multiselection binding works fine. But now i have to enable/disable the ok button depending on the selection in the TableViewer (Nothing selected disabled else enabled).
Pretty simple but the problem is that the validation rule is never checked and it seems that the converter is also just called when the number of selections increases.

I use the xwt databindings with validations, and converters very often but only with other widgets and had no problems. Is there a limitation with the selection binding? How could i realise something like this (without adding a selectionlistener in java code).

Here is my sample code (modified the xwt samples) maybe someone knows more about this issue.

Thanks in advance and best regards
Weinma

<Composite xmlns="http://www.eclipse.org/xwt/presentation"
    xmlns:x="http://www.eclipse.org/xwt"
    xmlns:j="clr-namespace:test"
    DataContext="{StaticResource myCompany}">

	<Composite.layout>
		<GridLayout numColumns="2"/>
	</Composite.layout>

	<Composite.Resources>
		<j:Company x:Key="myCompany">
			<j:Company.employees>
				<x:Array Type="j:Employee">
					<j:Employee Name="Thomas" Age="32"/>
					<j:Employee Name="Jin" Age="27"/>
				</x:Array>
			</j:Company.employees>
		</j:Company>
	</Composite.Resources>

	<TableViewer x:style="MULTI | FULL_SELECTION | BORDER" Name="TableViewer" input="{Binding Path=employees}">
		<TableViewer.multiSelection>
			<Binding path="selectedEmployees" updateSourceTrigger="PropertyChanged">
				<Binding.validationRules>
					<j:TestValidationRule/>
				</Binding.validationRules>
				
				<Binding.converter>
					<j:TestConverter/>
				</Binding.converter>		
			</Binding>
		</TableViewer.multiSelection>
		
		<TableViewer.columns>
			<TableViewerColumn width="150" text="Name" bindingPath="name"/>
			<TableViewerColumn width="150" text="Age" bindingPath="age"/>
		</TableViewer.columns>
		
		<TableViewer.control.layoutData>
			<GridData horizontalAlignment="FILL"
         		grabExcessHorizontalSpace="true"/>
		</TableViewer.control.layoutData>
		
		<TableViewer.table headervisible="true" linesvisible="true"/>
	</TableViewer>

</Composite> 



package test;

import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.e4.xwt.IValidationRule;

public class TestValidationRule implements IValidationRule {

	public TestValidationRule() {
		System.out.println("Created ValidationRule");
	}
	
	public IStatus validate(Object value) {
		System.out.println("Validation");
		return ValidationStatus.ok();
	}

	public Direction getBindingMode() {
		return Direction.Both;
	}

	public Phase getPhase() {
		return Phase.AfterConvert;
	}

	public IStatus validateBack(Object value) {
		System.out.println("Validation back");
		return ValidationStatus.ok();
	}

}


package test;

import org.eclipse.e4.xwt.IValueConverter;

public class TestConverter implements IValueConverter{

	public TestConverter() {
		System.out.println("Created TestConverter");
	}
	
	public Object getFromType() {
		return Object.class;
	}

	public Object getToType() {
		return Employee.class;
	}

	public Object convert(Object fromObject) {
		System.out.println("Converting");
		return fromObject;
	}

	public Object convertBack(Object value) {
		System.out.println("Converting back");
		return value;
	}

}


package test;
/*******************************************************************************
 * Copyright (c) 2006, 2010 Soyatec (http://www.soyatec.com) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Soyatec - initial API and implementation
 *******************************************************************************/

public class Company {
	protected Employee[] employees;
	protected Employee[] selectedEmployees;

	public Company() {
	}

	public Employee[] getEmployees() {
		return employees;
	}

	public void setEmployees(Employee[] employees) {
		this.employees = employees;
	}

	public Employee[] getSelectedEmployees() {
		System.out.println("Get selected employees "+getSelectedEmployeesSize());
		return selectedEmployees;
	}

	private int getSelectedEmployeesSize() {
		int size = 0;
		if(selectedEmployees != null) {
			size = selectedEmployees.length;
		}
		return size;
	}

	public void setSelectedEmployees(Employee[] selectedEmployees) {
		this.selectedEmployees = selectedEmployees;
		System.out.println("Set selected employees "+getSelectedEmployeesSize());
	}
	
	
}
 


Re: TableViewer selection binding with converter and validation [message #1015555 is a reply to message #1015538] Fri, 01 March 2013 08:56 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 756
Registered: July 2009
Senior Member
You could try the selectionEvent on TableViewer.table:

<TableViewer.table selectionEvent="yourHandlerMethod"/>


Then, check in your handler method if there is a selection or not and activate/deactivate the button...
Re: TableViewer selection binding with converter and validation [message #1015584 is a reply to message #1015555] Fri, 01 March 2013 09:42 Go to previous messageGo to next message
Jürgen Weinberger is currently offline Jürgen WeinbergerFriend
Messages: 36
Registered: August 2012
Member
Hi!
Thanks for the fast reply!
As i mentioned before i would prefer not to use the SelectionListener / Handler and thought this should all be possible by the validation in the binding.

The thing is that i am playing around with xwt to check out if we could use it in our Application and enviroment. Till now it seems quite usefull (and it would definitely be great if there wasn't such a lack of documentation and tools. Still hoping that this will improve in the future). It just sometimes seems to me that xwt hasn't a clear line. One time you solve it this way one another. But i also still have the feeling that i am not using it correct Smile

Maybe another idea why the validation is not called ?

best regards weinma
Re: TableViewer selection binding with converter and validation [message #1015624 is a reply to message #1015584] Fri, 01 March 2013 12:42 Go to previous message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 756
Registered: July 2009
Senior Member
Jürgen Weinberger wrote on Fri, 01 March 2013 10:42

As i mentioned before i would prefer not to use the SelectionListener / Handler and thought this should all be possible by the validation in the binding.


The validator should just validate. You want it to disable/enable a button?

If you make the button available to the xwt form, you could use xwt to enable/disable it:

<Composite.parent.yourOkButtonAsProperty enabled="{Binding elementName=TableViewer, path=singleSelection}" />

Jürgen Weinberger wrote on Fri, 01 March 2013 10:42

Maybe another idea why the validation is not called ?


Can you check if the validator is called with single selection ("selection" instead of "multiSelection")? If yes, that may be a bug.
Previous Topic:XWT Update Site
Next Topic:xwt with Sash and FormLayout
Goto Forum:
  


Current Time: Fri Nov 28 00:50:43 GMT 2014

Powered by FUDForum. Page generated in 0.01784 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software