Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Use IDisplayConverter for copy to clipboard
Use IDisplayConverter for copy to clipboard [message #1430640] Wed, 24 September 2014 15:48 Go to next message
Timm Baumeister is currently offline Timm BaumeisterFriend
Messages: 14
Registered: July 2012
Junior Member
Hi,

I am registering an IDisplayConverter with
configRegistry.registerConfigAttribute(
CellConfigAttributes.DISPLAY_CONVERTER,displayConverter,DisplayMode.NORMAL,GridRegion.BODY.toString());


The values of the target representation are correctly shown in the ui but if the user copies out of the table to the clipboard with ctrl+c, the canonical representation is shown. Is there an easy way to configure the copying to use the target representation as well?

Re: Use IDisplayConverter for copy to clipboard [message #1430646 is a reply to message #1430640] Wed, 24 September 2014 15:59 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2526
Registered: July 2012
Senior Member
IIRC you need to configure the CopyDataCommandHandler accordingly.

		CopyDataCommandHandler copyHandler = new CopyDataCommandHandler(selectionLayer, columnHeaderDataLayer, rowHeaderDataLayer);
		copyHandler.setCopyFormattedText(true);
		gridLayer.registerCommandHandler(copyHandler);


This is shown in the CopyExample of the NatTable Examples Application
Re: Use IDisplayConverter for copy to clipboard [message #1431385 is a reply to message #1430646] Thu, 25 September 2014 16:13 Go to previous messageGo to next message
Timm Baumeister is currently offline Timm BaumeisterFriend
Messages: 14
Registered: July 2012
Junior Member
For some reasons this does not work for me. CopyDataCommandHandler will use a CopyFormattedTextToClipboardSerializer if configured with setCopyFormattedText(true). Now CopyFormattedTextToClipboardSerializer.getTextForCell calls CellDisplayConversionUtils.convertDataType in which the DisplayConverter is looked up via
configRegistry.getConfigAttribute(
				                CellConfigAttributes.DISPLAY_CONVERTER,
				                cell.getDisplayMode(),
				                cell.getConfigLabels().getLabels())

This however returns a DefaultDisplayConverter which is different from what I registered with the config registry, not sure why this is the case.

A workaround is overriding the getTextForCell method and provide the DisplayConverter directly:

CopyDataCommandHandler copyHandler = new CopyDataCommandHandler(selectionLayer, null,null) {
	@Override
	public boolean doCommand(final CopyDataToClipboardCommand c) {
		final ILayerCell[][] data = assembleCopiedDataStructure();
		final CopyDataToClipboardSerializer serializer = new CopyFormattedTextToClipboardSerializer(data, c) {
			@Override
			protected String getTextForCell(ILayerCell cell) {
				Object data = displayConverter.canonicalToDisplayValue(cell, configRegistry,null);
				return data==null ? "" : data.toString();
			}
		};

		serializer.serialize();
		return true;				
	}
};
gridLayer.registerCommandHandler(copyHandler);


I am using 1.0.0.201305291732 though, maybe the behavior has changed in a more recent version.
Re: Use IDisplayConverter for copy to clipboard [message #1431462 is a reply to message #1431385] Thu, 25 September 2014 18:25 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2526
Registered: July 2012
Senior Member
Well if you are using 1.0.0 it might be a bug that has already been fixed. I remember some issue with the CopyDataCommandHandler. But I can't remember as it is more than a year old IIRC.

The other question is, are you sure that your IDisplayConverter is registered correctly? Where and when do you register it?
Re: Use IDisplayConverter for copy to clipboard [message #1446414 is a reply to message #1431462] Thu, 16 October 2014 20:26 Go to previous messageGo to next message
Timm Baumeister is currently offline Timm BaumeisterFriend
Messages: 14
Registered: July 2012
Junior Member
I just tested 1.1.1 and I still have this problem.

I am not sure if I am doing anything wrong with the DisplayConverter registration. A small test case is shown below:

import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataCommandHandler;
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
import org.eclipse.nebula.widgets.nattable.data.convert.IDisplayConverter;
import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer;
import org.eclipse.nebula.widgets.nattable.hideshow.ColumnHideShowLayer;
import org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform;
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.nebula.widgets.nattable.reorder.ColumnReorderLayer;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;

public class NatTableTest {

	static class ColumnPropertyAccessor implements IColumnPropertyAccessor<Integer> {

		@Override
		public int getColumnCount() {
			return 2;
		}

		@Override
		public Object getDataValue(Integer row, int col) {
			return row+"/"+col;
		}
		public Object getDataValueFormatted(Integer row, int col,Object o) {
			return "<"+row+"/"+col+">";

		}
		public void setDataValue(Integer arg0, int arg1, Object arg2) {
		}

		@Override
		public int getColumnIndex(String arg0) {
			return Integer.parseInt(arg0);
		}

		@Override
		public String getColumnProperty(int col) {
			return ""+col;
		}
	}		
	static public class BodyLayerStack extends AbstractLayerTransform {

		private SelectionLayer selectionLayer;

		public BodyLayerStack(IDataProvider dataProvider) {
			DataLayer bodyDataLayer = new DataLayer(dataProvider);
			ColumnReorderLayer columnReorderLayer = 
					new ColumnReorderLayer(bodyDataLayer);
			ColumnHideShowLayer columnHideShowLayer = 
					new ColumnHideShowLayer(columnReorderLayer);
			selectionLayer = new SelectionLayer(columnHideShowLayer);
			ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
			setUnderlyingLayer(viewportLayer);
		}

		public SelectionLayer getSelectionLayer() {
			return selectionLayer;
		}
	}	
	static class EmptyDataProvider implements IDataProvider {
		@Override
		public int getColumnCount() {
			return 0;
		}
		@Override
		public Object getDataValue(int arg0, int arg1) {
			return null;
		}
		@Override
		public int getRowCount() {
			return 0;
		}
		@Override
		public void setDataValue(int arg0, int arg1, Object arg2) {
		}
	};

	public static void main(String[] args) {
		Display display = new Display ();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout(1, false));
		EventList<Integer> list = new BasicEventList<>();
		for (int i=0;i<2;i++)
			list.add(i);
		final ColumnPropertyAccessor columnPropertyAccessor = new ColumnPropertyAccessor();
		ListDataProvider<Integer> bodyDataProvider = new ListDataProvider<>(list, columnPropertyAccessor);		
		final DefaultGridLayer gridLayer = new DefaultGridLayer(bodyDataProvider,new EmptyDataProvider(),new EmptyDataProvider());
		IDisplayConverter displayConverter = new IDisplayConverter() {
			@Override
			public Object canonicalToDisplayValue(Object arg0) {
				return arg0;
			}
			@Override
			public Object canonicalToDisplayValue(ILayerCell arg0,
					IConfigRegistry arg1, Object arg2) {
				int rowIx = arg0.getRowIndex();
				return columnPropertyAccessor.getDataValueFormatted(rowIx,arg0.getColumnIndex(),arg2);
			}
			@Override
			public Object displayToCanonicalValue(Object arg0) {
				return arg0;
			}
			@Override
			public Object displayToCanonicalValue(ILayerCell arg0,
					IConfigRegistry arg1, Object arg2) {
				return arg2;
			}
		};
		ConfigRegistry configRegistry = new ConfigRegistry();
		configRegistry.registerConfigAttribute(
				CellConfigAttributes.DISPLAY_CONVERTER,displayConverter,DisplayMode.NORMAL,GridRegion.BODY.toString());
		NatTable nt  = new NatTable(shell,gridLayer,false);
		final DefaultNatTableStyleConfiguration conf  = new DefaultNatTableStyleConfiguration();
		conf.configureLayer(gridLayer);
		nt.addConfiguration(conf);
		CopyDataCommandHandler copyHandler = new CopyDataCommandHandler(gridLayer.getBodyLayer().getSelectionLayer(), null,null); 
		copyHandler.setCopyFormattedText(true);
		gridLayer.registerCommandHandler(copyHandler);

		nt.setConfigRegistry(configRegistry);
		nt.configure();
		nt.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		shell.pack();
		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}
}

Re: Use IDisplayConverter for copy to clipboard [message #1446816 is a reply to message #1446414] Fri, 17 October 2014 10:57 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2526
Registered: July 2012
Senior Member
It seems the issue is the way you are registering the IDisplayConverter. Why are you creating a ConfigRegistry and adding it manually? You should use an AbstractRegistryConfiguration and register that to a layer below the GridLayer. IIRC the way you are doing it, the IDisplayConverter is overriden while configuring.
Previous Topic:Sample NAT table project
Next Topic:CornerLayer LayerPainter - row count
Goto Forum:
  


Current Time: Sat Nov 25 11:37:29 GMT 2017

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

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