Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » how to use ListDataProvider class...(package org.eclipse.nebula.widgets.nattable.data)
icon5.gif  how to use ListDataProvider class... [message #991710] Thu, 20 December 2012 02:54 Go to next message
lau alex is currently offline lau alexFriend
Messages: 3
Registered: June 2012
Junior Member
hello:

i want to use MouseListener (mouseDoubleClick) get the current object.

object o = ListDataProvider.getRowObject(int rowIndex);

how to write in the mouselistener, or other implementation method。

please help.... Embarrassed Embarrassed Embarrassed Embarrassed Embarrassed Embarrassed

example :

package com.eoilio.mrpii.ui.currency;

/*******************************************************************************
 * Copyright (c) 2012 Original authors 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:
 *     Original authors and others - initial API and implementation
 ******************************************************************************/

import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
import org.eclipse.nebula.widgets.nattable.layer.cell.CellOverrideLabelAccumulator;
import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.nebula.widgets.nattable.style.Style;
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.springframework.context.ApplicationContext;

import ca.odell.glazedlists.GlazedLists;

import com.eoilio.mrpii.dao.AppContext;
import com.eoilio.mrpii.dao.Currencycodedata;
import com.eoilio.mrpii.dao.CurrencycodedataDAO;
import com.eoilio.mrpii.ui.widget.nattable.AbstractNatExample;
import com.eoilio.mrpii.ui.widget.nattable.extension.builder.NatTableBuilder;
import com.eoilio.mrpii.ui.widget.nattable.extension.builder.model.TableColumn;
import com.eoilio.mrpii.ui.widget.nattable.extension.builder.model.TableModel;

public class CurrencyBuilder extends AbstractNatExample implements MouseListener {
	private NatTableBuilder<Currencycodedata> builder;
	private NatTable natTable;

	public Control createExampleControl(Composite parent) {
		ApplicationContext appCon = AppContext.CreateApplicationContext();
		CurrencycodedataDAO dao = (CurrencycodedataDAO) appCon
				.getBean("CurrencycodedataDAO");
		TableColumn[] columns = new TableColumn[] {
				new TableColumn(0, "code", "编码").setWidth(60).setCategory("A"),
				new TableColumn(1, "name", "币别").setWidth(60).setCategory("A"),
				new TableColumn(2, "exchange", "汇率").setWidth(80).setCategory(
						"A"),
				new TableColumn(3, "commutedDes", "折算方式").setWidth(200)
						.setCategory("A") };
		TableModel table = new TableModel(columns);

		builder = new NatTableBuilder<Currencycodedata>(parent, table,
				GlazedLists.eventList(dao.findAll()),
				Currencycodedata.rowIdAccessor);

		// Setup all the layer stacks
		natTable = builder.setupLayerStacks();
		
		natTable.addMouseListener(this);
		
		// Since the build() method has not been invoked yet you can
		// tweak layer configuration as you want
		customize();
		return builder.build();
	}

	private void customize() {
		Style style = new Style();
		style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR,
				GUIHelper.COLOR_RED);

		ListDataProvider<Currencycodedata> bodyDataProvider = builder
				.getBodyLayerStack().getDataProvider();
		CellOverrideLabelAccumulator<Currencycodedata> myAccumulator = new CellOverrideLabelAccumulator<Currencycodedata>(
				bodyDataProvider);
		myAccumulator.registerOverride("AAA", 2, "myLabel");

		builder.addCellLabelsToBody(myAccumulator);
		
		IConfigRegistry configRegistry = natTable.getConfigRegistry();
		configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,
				style, DisplayMode.NORMAL, "myLabel");
	}

	@Override
	public void mouseDoubleClick(MouseEvent event) {
		// TODO Auto-generated method stub
		System.out.println(event.toString());
	}

	@Override
	public void mouseDown(MouseEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseUp(MouseEvent event) {
		// TODO Auto-generated method stub
		
	}

}
Re: how to use ListDataProvider class... [message #991956 is a reply to message #991710] Thu, 20 December 2012 14:49 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Hi,

there are several ways to achieve this.

1. Use tight coupling, which might be working for your example.
public void mouseDoubleClick(MouseEvent e) {
	//get the row position for the click in the NatTable
	int rowPos = natTable.getRowPositionByY(e.y);
	//transform the NatTable row position to the row position of the body layer stack
	int bodyRowPos = LayerUtil.convertRowPosition(natTable, rowPos, gridLayer.getBodyLayer());
	Currencycodedata ccd = bodyDataProvider.getRowObject(bodyRowPos);
	//Do something with ccd
}


2. You might want to use the selection provider mechanism
// Provides rows where any cell in the row is selected
ISelectionProvider selectionProvider = new RowSelectionProvider<Person>(gridLayer.getBodyLayer().getSelectionLayer(), bodyDataProvider, false);
		
selectionProvider.addSelectionChangedListener(new ISelectionChangedListener() {
			
	public void selectionChanged(SelectionChangedEvent event) {
		System.out.println("Selection changed:");
				
		IStructuredSelection selection = (IStructuredSelection) event.getSelection();
		@SuppressWarnings("rawtypes")
		Iterator it = selection.iterator();
		while (it.hasNext()) {
			System.out.println("  " + it.next());
		}
	}
			
});


The second code example is out of the Get_and_set_selected_objects examples of the NatTable examples.

Hope that helps,
Dirk
Re: how to use ListDataProvider class... [message #1065493 is a reply to message #991956] Wed, 26 June 2013 06:10 Go to previous message
lau alex is currently offline lau alexFriend
Messages: 3
Registered: June 2012
Junior Member
thanks for your achieve.
you give me a better way to achieve this.

I use selection provider have something wrong
Previous Topic:Making columns sortable
Next Topic:about ListDataProvider.getRowObject(int rowIndex) Method of use
Goto Forum:
  


Current Time: Sat Apr 20 00:13:59 GMT 2024

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

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

Back to the top