|
|
Re: Use IDisplayConverter for copy to clipboard [message #1431385 is a reply to message #1430646] |
Thu, 25 September 2014 16:13 |
Timm Baumeister 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 #1446414 is a reply to message #1431462] |
Thu, 16 October 2014 20:26 |
Timm Baumeister 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 ();
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.03729 seconds