Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Add new row header
Add new row header [message #1773640] Mon, 02 October 2017 06:56 Go to next message
cenk Mising name is currently offline cenk Mising nameFriend
Messages: 159
Registered: July 2009
Senior Member
Hi,

Is it possible Add new row headers. Is there any examples for this.
Re: Add new row header [message #1773641 is a reply to message #1773640] Mon, 02 October 2017 07:11 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
What do you consider to be a new row header? In theory it is the same as for the body region. The only thing to consider is the dimensional dependency.

It is not clear to me what you are trying to achieve. The layer concepts are the same in every region, despite the dimensional dependency.
Re: Add new row header [message #1773656 is a reply to message #1773641] Mon, 02 October 2017 12:33 Go to previous messageGo to next message
cenk Mising name is currently offline cenk Mising nameFriend
Messages: 159
Registered: July 2009
Senior Member
Hi,

I can solved my problem. I found ScrollableRowHeaderExample.java. And I modified this source and I added check box on second row header. But I can't make editable check box on row header.

Our test code is below. How can I editable check box. Thank you for your help
package multiheaderrow;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.config.IEditableRule;
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
import org.eclipse.nebula.widgets.nattable.data.IRowDataProvider;
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
import org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor;
import org.eclipse.nebula.widgets.nattable.edit.EditConfigAttributes;
import org.eclipse.nebula.widgets.nattable.edit.action.ToggleCheckBoxColumnAction;
import org.eclipse.nebula.widgets.nattable.edit.editor.CheckBoxCellEditor;
import org.eclipse.nebula.widgets.nattable.examples.AbstractNatExample;
import org.eclipse.nebula.widgets.nattable.examples.data.person.PersonService;
import org.eclipse.nebula.widgets.nattable.examples.runner.StandaloneNatExampleRunner;
import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer;
import org.eclipse.nebula.widgets.nattable.layer.AbstractLayer;
import org.eclipse.nebula.widgets.nattable.layer.CompositeLayer;
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
import org.eclipse.nebula.widgets.nattable.layer.LabelStack;
import org.eclipse.nebula.widgets.nattable.layer.cell.CellOverrideLabelAccumulator;
import org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator;
import org.eclipse.nebula.widgets.nattable.painter.IOverlayPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.decorator.PaddingDecorator;
import org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayerPainter;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.nebula.widgets.nattable.ui.NatEventData;
import org.eclipse.nebula.widgets.nattable.ui.action.IDragMode;
import org.eclipse.nebula.widgets.nattable.ui.action.IMouseAction;
import org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry;
import org.eclipse.nebula.widgets.nattable.ui.matcher.CellPainterMouseEventMatcher;
import org.eclipse.nebula.widgets.nattable.ui.matcher.MouseEventMatcher;
import org.eclipse.nebula.widgets.nattable.util.ClientAreaAdapter;
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
import org.eclipse.nebula.widgets.nattable.viewport.SliderScroller;
import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
import org.eclipse.nebula.widgets.nattable.viewport.command.RecalculateScrollBarsCommand;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Slider;

/**
 *
 */
public class ScrollableRowHeaderExample_Test extends AbstractNatExample {
	private DataLayer rowDataLayer;
    public static void main(String[] args) throws Exception {
        StandaloneNatExampleRunner.run(600, 400, new ScrollableRowHeaderExample_Test());
    }

    @Override
    public String getDescription() {
        return "This example shows a scrollable row header.";
    }

    @Override
    public Control createExampleControl(Composite parent) {
        // property names of the Person class
        String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };

        // mapping from property to label, needed for column header labels
        Map propertyToLabelMap = new HashMap<>();
        propertyToLabelMap.put("firstName", "Firstname");
        propertyToLabelMap.put("lastName", "Lastname");
        propertyToLabelMap.put("gender", "Gender");
        propertyToLabelMap.put("married", "Married");
        propertyToLabelMap.put("birthday", "Birthday");

        IColumnPropertyAccessor columnPropertyAccessor =
                new ReflectiveColumnPropertyAccessor<>(propertyNames);

        IDataProvider bodyDataProvider =
                new ListDataProvider<>(PersonService.getPersons(10), columnPropertyAccessor);
        DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
        SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
        ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);

        rowDataLayer = new DataLayer(new IRowDataProvider() {
            @Override
            public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
            	
            }

            @Override
            public int getRowCount() {
                return bodyDataProvider.getRowCount();
            }

            @Override
            public Object getDataValue(int columnIndex, int rowIndex) {
            	
            	
            	return false;
            }

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

			@Override
			public Object getRowObject(int rowIndex) {
				// TODO Auto-generated method stub
				return Integer.valueOf(rowIndex + 1);
			}

			@Override
			public int indexOfRowObject(Object rowObject) {
				// TODO Auto-generated method stub
				return 0;
			}
        });
        rowDataLayer.setDefaultColumnWidth(20);
        ViewportLayer rowViewport = new ViewportLayer(rowDataLayer);
        RowHeaderLayer scrollableRowHeaderLayer = new RowHeaderLayer(rowViewport, viewportLayer, selectionLayer);

        RowHeaderLayer rowHeaderLayer =
                new RowHeaderLayer(
                        new DefaultRowHeaderDataLayer(
                                new DefaultRowHeaderDataProvider(bodyDataProvider)),
                        viewportLayer,
                        selectionLayer);
        RowHeaderLayer rowHeaderLayer2 =
                new RowHeaderLayer(
                		rowDataLayer,
                        viewportLayer,
                        selectionLayer);
        
        ColumnOverrideLabelAccumulator cellOverrideLabelAccumulator =  new ColumnOverrideLabelAccumulator(rowHeaderLayer2);
        rowDataLayer.setConfigLabelAccumulator(cellOverrideLabelAccumulator);
        cellOverrideLabelAccumulator.registerColumnOverrides(0, "CELL_EDITOR_TYPE_CHECKBOX");
//        RowHeaderLayer rowHeaderLayer =
//        		new RowHeaderLayer(
//        				new DefaultRowHeaderDataLayer(
//        						new DefaultRowHeaderDataProvider(bodyDataProvider)),
//        				viewportLayer,
//        				selectionLayer);

        CompositeLayer compositeLayer = new CompositeLayer(2, 1);
        compositeLayer.setChildLayer(GridRegion.ROW_HEADER, rowHeaderLayer, 0, 0);
        compositeLayer.setChildLayer(GridRegion.ROW_HEADER, rowHeaderLayer2, 1, 0);

        // build the column header layer
        IDataProvider columnHeaderDataProvider =
                new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
        final DataLayer columnHeaderDataLayer =
                new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
        final AbstractLayer columnHeaderLayer =
                new ColumnHeaderLayer(columnHeaderDataLayer, viewportLayer, selectionLayer);

        // build the corner layer
        IDataProvider cornerDataProvider =
                new DefaultCornerDataProvider(columnHeaderDataProvider, rowDataLayer.getDataProvider());
        DataLayer cornerDataLayer =
                new DataLayer(cornerDataProvider);
        final ILayer cornerLayer =
                new CornerLayer(cornerDataLayer, compositeLayer, columnHeaderLayer);

        GridLayer gridLayer = new GridLayer(
                viewportLayer, columnHeaderLayer, compositeLayer, cornerLayer);

        // MULTI-VIEWPORT-CONFIGURATION

        // use a cell layer painter that is configured for left clipping
        // this ensures that the rendering works correctly for split
        // viewports
        selectionLayer.setLayerPainter(new SelectionLayerPainter(true, false));

        columnHeaderLayer.setLayerPainter(new CellLayerPainter(true, false));

        // as the CompositeLayer is setting a IClientAreaProvider for the
        // composition we need to set a special ClientAreaAdapter after the
        // creation of the CompositeLayer to support split viewports
        int leftWidth = 100;
        ClientAreaAdapter leftClientAreaAdapter =
                new ClientAreaAdapter(rowViewport.getClientAreaProvider());
        leftClientAreaAdapter.setWidth(leftWidth);
        rowViewport.setClientAreaProvider(leftClientAreaAdapter);

        // Wrap NatTable in composite so we can slap on the external horizontal
        // sliders
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout gridLayout = new GridLayout(1, false);
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        gridLayout.horizontalSpacing = 0;
        gridLayout.verticalSpacing = 0;
        composite.setLayout(gridLayout);

        NatTable natTable = new NatTable(composite, gridLayer, false);
        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.verticalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = true;
        natTable.setLayoutData(gridData);

        createSplitSliders(composite, rowViewport, rowHeaderLayer.getWidth(), viewportLayer);

        // add an IOverlayPainter to render the split viewport border
        natTable.addOverlayPainter(new IOverlayPainter() {

            @Override
            public void paintOverlay(GC gc, ILayer layer) {
                Color beforeColor = gc.getForeground();
                gc.setForeground(GUIHelper.COLOR_GRAY);
                int viewportBorderX = compositeLayer.getWidth() - 1;
                gc.drawLine(viewportBorderX, 0, viewportBorderX, layer.getHeight() - 1);
                gc.setForeground(beforeColor);
            }
        });

        // Mouse move - Show resize cursor
//        natTable.getUiBindingRegistry().registerFirstMouseMoveBinding(
//                new ClientAreaResizeMatcher(compositeLayer),
//                new VerticalResizeCursorAction());

        natTable.getUiBindingRegistry().registerFirstMouseDragMode(
                new ClientAreaResizeMatcher(compositeLayer),
                new ClientAreaResizeDragMode(compositeLayer, rowDataLayer, leftClientAreaAdapter, rowViewport, viewportLayer));

        natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
        
        natTable.addConfiguration(new AbstractRegistryConfiguration(){


			@Override
			public void configureRegistry(IConfigRegistry configRegistry) {
				
				configRegistry.registerConfigAttribute(
		                CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(),
		                DisplayMode.NORMAL, "CELL_EDITOR_TYPE_CHECKBOX");
		        configRegistry.registerConfigAttribute(
		                EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(),
		                DisplayMode.NORMAL, "CELL_EDITOR_TYPE_CHECKBOX");
		        
		        configRegistry.registerConfigAttribute(
		                EditConfigAttributes.CELL_EDITABLE_RULE,
		                IEditableRule.ALWAYS_EDITABLE, DisplayMode.EDIT,
		                "CELL_EDITOR_TYPE_CHECKBOX");
			}
			
        });
        natTable.configure();
        return natTable;
    }

    private void createSplitSliders(
            Composite natTableParent, final ViewportLayer left, int fixedHeaderWidth, final ViewportLayer right) {
        Composite sliderComposite = new Composite(natTableParent, SWT.NONE);
        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = false;
        gridData.heightHint = 17;
        sliderComposite.setLayoutData(gridData);

        GridLayout gridLayout = new GridLayout(2, false);
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        gridLayout.horizontalSpacing = 0;
        gridLayout.verticalSpacing = 0;
        sliderComposite.setLayout(gridLayout);

        // Slider Left
        // Need a composite here to set preferred size because Slider can't be
        // subclassed.
        Composite sliderLeftComposite = new Composite(sliderComposite, SWT.NONE) {
            @Override
            public Point computeSize(int wHint, int hHint, boolean changed) {
                int width = ((ClientAreaAdapter) left.getClientAreaProvider()).getWidth() + fixedHeaderWidth;
                return new Point(width, 17);
            }
        };
        sliderLeftComposite.setLayout(new FillLayout());
        gridData = new GridData();
        gridData.horizontalAlignment = GridData.BEGINNING;
        gridData.verticalAlignment = GridData.BEGINNING;
        sliderLeftComposite.setLayoutData(gridData);

        Slider sliderLeft = new Slider(sliderLeftComposite, SWT.HORIZONTAL);
        gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.verticalAlignment = GridData.FILL;
        sliderLeft.setLayoutData(gridData);

        left.setHorizontalScroller(new SliderScroller(sliderLeft));

        // Slider Right
        Slider sliderRight = new Slider(sliderComposite, SWT.HORIZONTAL);
        gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.verticalAlignment = GridData.BEGINNING;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = false;
        sliderRight.setLayoutData(gridData);

        right.setHorizontalScroller(new SliderScroller(sliderRight));
    }

    class ClientAreaResizeMatcher extends MouseEventMatcher {

        ILayer rowHeaderLayer;

        public ClientAreaResizeMatcher(ILayer rowHeaderLayer) {
            this.rowHeaderLayer = rowHeaderLayer;
        }

        @Override
        public boolean matches(NatTable natTable, MouseEvent event, LabelStack regionLabels) {
            if (regionLabels != null && regionLabels.hasLabel(GridRegion.COLUMN_HEADER)
                    && (event.x > this.rowHeaderLayer.getWidth() - 5 && event.x < this.rowHeaderLayer.getWidth() + 5)) {
                return true;
            }
            return false;
        }
    }

    class ClientAreaResizeDragMode implements IDragMode {

        ILayer baseLayer;
        ClientAreaAdapter clientAreaAdapter;
        ViewportLayer[] viewportLayer;

        int diff = 0;

        public ClientAreaResizeDragMode(ILayer resizable, ILayer baseLayer,
                ClientAreaAdapter clientAreaAdapter, ViewportLayer... viewportLayer) {
            this.baseLayer = baseLayer;
            this.clientAreaAdapter = clientAreaAdapter;
            this.viewportLayer = viewportLayer;
            this.diff = resizable.getWidth() - clientAreaAdapter.getWidth();
        }

        @Override
        public void mouseDown(NatTable natTable, MouseEvent event) {}

        @Override
        public void mouseMove(NatTable natTable, MouseEvent event) {
            // TODO overlay support
        }

        @Override
        public void mouseUp(NatTable natTable, MouseEvent event) {
            int newWidth = event.x - this.diff;
            if (newWidth < 0) {
                newWidth = 1;
            } else if (newWidth > this.baseLayer.getWidth()) {
                newWidth = this.baseLayer.getWidth();
            }
            this.clientAreaAdapter.setWidth(newWidth);
            for (ViewportLayer vp : this.viewportLayer) {
                vp.invalidateHorizontalStructure();

                vp.doCommand(new RecalculateScrollBarsCommand());
            }
            natTable.redraw();

            natTable.getParent().layout(true, true);
        }

    }
}



Re: Add new row header [message #1773660 is a reply to message #1773656] Mon, 02 October 2017 14:02 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
You need to enable editing in the row header region aswell.
Re: Add new row header [message #1773663 is a reply to message #1773660] Mon, 02 October 2017 14:22 Go to previous messageGo to next message
cenk Mising name is currently offline cenk Mising nameFriend
Messages: 159
Registered: July 2009
Senior Member
Thank you. but How can be do this.?

Is below code enough?

configRegistry.registerConfigAttribute(
		                EditConfigAttributes.CELL_EDITABLE_RULE,
		                IEditableRule.ALWAYS_EDITABLE, DisplayMode.EDIT,
		                "CELL_EDITOR_TYPE_CHECKBOX");

Re: Add new row header [message #1773677 is a reply to message #1773663] Mon, 02 October 2017 17:00 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
No, you also need to register an editor and the necessary bindings for the row header. Have a look at the DefaultEditBindings to get an idea.
Re: Add new row header [message #1773685 is a reply to message #1773677] Mon, 02 October 2017 21:33 Go to previous messageGo to next message
cenk Mising name is currently offline cenk Mising nameFriend
Messages: 159
Registered: July 2009
Senior Member
Hi
I can add checkbox and can be checked and unchecked. But checkbox image is not updated correctly. May you run example code.(If it is possible) You can see problem.

The code is below. Thank you for your response

package multiheaderrow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.config.IEditableRule;
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
import org.eclipse.nebula.widgets.nattable.data.IRowDataProvider;
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
import org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor;
import org.eclipse.nebula.widgets.nattable.data.convert.DefaultBooleanDisplayConverter;
import org.eclipse.nebula.widgets.nattable.edit.EditConfigAttributes;
import org.eclipse.nebula.widgets.nattable.edit.action.MouseEditAction;
import org.eclipse.nebula.widgets.nattable.edit.action.ToggleCheckBoxColumnAction;
import org.eclipse.nebula.widgets.nattable.edit.config.DefaultEditBindings;
import org.eclipse.nebula.widgets.nattable.edit.editor.CheckBoxCellEditor;
import org.eclipse.nebula.widgets.nattable.examples.AbstractNatExample;
import org.eclipse.nebula.widgets.nattable.examples.data.person.PersonService;
import org.eclipse.nebula.widgets.nattable.examples.runner.StandaloneNatExampleRunner;
import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider;
import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer;
import org.eclipse.nebula.widgets.nattable.layer.AbstractLayer;
import org.eclipse.nebula.widgets.nattable.layer.CompositeLayer;
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
import org.eclipse.nebula.widgets.nattable.layer.LabelStack;
import org.eclipse.nebula.widgets.nattable.layer.cell.CellOverrideLabelAccumulator;
import org.eclipse.nebula.widgets.nattable.layer.cell.ColumnOverrideLabelAccumulator;
import org.eclipse.nebula.widgets.nattable.painter.IOverlayPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.decorator.PaddingDecorator;
import org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayerPainter;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.nebula.widgets.nattable.ui.NatEventData;
import org.eclipse.nebula.widgets.nattable.ui.action.IDragMode;
import org.eclipse.nebula.widgets.nattable.ui.action.IMouseAction;
import org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry;
import org.eclipse.nebula.widgets.nattable.ui.matcher.CellPainterMouseEventMatcher;
import org.eclipse.nebula.widgets.nattable.ui.matcher.MouseEventMatcher;
import org.eclipse.nebula.widgets.nattable.util.ClientAreaAdapter;
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
import org.eclipse.nebula.widgets.nattable.viewport.SliderScroller;
import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
import org.eclipse.nebula.widgets.nattable.viewport.command.RecalculateScrollBarsCommand;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Slider;

/**
 *
 */
public class ScrollableRowHeaderExample_Test extends AbstractNatExample {
	private DataLayer rowDataLayer;
    public static void main(String[] args) throws Exception {
        StandaloneNatExampleRunner.run(600, 400, new ScrollableRowHeaderExample_Test());
    }

    @Override
    public String getDescription() {
        return "This example shows a scrollable row header.";
    }

    @Override
    public Control createExampleControl(Composite parent) {
        // property names of the Person class
        String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" };

        // mapping from property to label, needed for column header labels
        Map propertyToLabelMap = new HashMap<>();
        propertyToLabelMap.put("firstName", "Firstname");
        propertyToLabelMap.put("lastName", "Lastname");
        propertyToLabelMap.put("gender", "Gender");
        propertyToLabelMap.put("married", "Married");
        propertyToLabelMap.put("birthday", "Birthday");

        IColumnPropertyAccessor columnPropertyAccessor =
                new ReflectiveColumnPropertyAccessor<>(propertyNames);

        IDataProvider bodyDataProvider =
                new ListDataProvider<>(PersonService.getPersons(1), columnPropertyAccessor);
        DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
        SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
        ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);

        rowDataLayer = new DataLayer(new IRowDataProvider() {
        	
        	Boolean checked=new Boolean(false);
            @Override
            public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
            	System.out.println("set data "+newValue);
            	checked = (Boolean) newValue;
            	
            	
            }

            @Override
            public int getRowCount() {
                return bodyDataProvider.getRowCount();
            }

            @Override
            public Object getDataValue(int columnIndex, int rowIndex) {
            	System.out.println("getDataValue "+checked);
            	return checked;
            }

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

			@Override
			public Object getRowObject(int rowIndex) {
				// TODO Auto-generated method stub
				return null;//;Integer.valueOf(rowIndex + 1);
			}

			@Override
			public int indexOfRowObject(Object rowObject) {
				// TODO Auto-generated method stub
				return 0;
			}
        });
        rowDataLayer.setDefaultColumnWidth(20);
        ViewportLayer rowViewport = new ViewportLayer(rowDataLayer);
        RowHeaderLayer scrollableRowHeaderLayer = new RowHeaderLayer(rowViewport, viewportLayer, selectionLayer);

        RowHeaderLayer rowHeaderLayer =
                new RowHeaderLayer(
                        new DefaultRowHeaderDataLayer(
                                new DefaultRowHeaderDataProvider(bodyDataProvider)),
                        viewportLayer,
                        selectionLayer);
        RowHeaderLayer rowHeaderLayer2 =
                new RowHeaderLayer(
                		rowDataLayer,
                        viewportLayer,
                        selectionLayer);
        
        ColumnOverrideLabelAccumulator cellOverrideLabelAccumulator =  new ColumnOverrideLabelAccumulator(rowHeaderLayer2);
        rowDataLayer.setConfigLabelAccumulator(cellOverrideLabelAccumulator);
        cellOverrideLabelAccumulator.registerColumnOverrides(0, "CELL_EDITOR_TYPE_CHECKBOX");
        

        CompositeLayer compositeLayer = new CompositeLayer(2, 1);
        compositeLayer.setChildLayer(GridRegion.ROW_HEADER, rowHeaderLayer, 0, 0);
        compositeLayer.setChildLayer(GridRegion.ROW_HEADER, rowHeaderLayer2, 1, 0);

        // build the column header layer
        IDataProvider columnHeaderDataProvider =
                new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
        final DataLayer columnHeaderDataLayer =
                new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
        final AbstractLayer columnHeaderLayer =
                new ColumnHeaderLayer(columnHeaderDataLayer, viewportLayer, selectionLayer);

        // build the corner layer
        IDataProvider cornerDataProvider =
                new DefaultCornerDataProvider(columnHeaderDataProvider, rowDataLayer.getDataProvider());
        DataLayer cornerDataLayer =
                new DataLayer(cornerDataProvider);
        final ILayer cornerLayer =
                new CornerLayer(cornerDataLayer, compositeLayer, columnHeaderLayer);

        GridLayer gridLayer = new GridLayer(
                viewportLayer, columnHeaderLayer, compositeLayer, cornerLayer);

        // MULTI-VIEWPORT-CONFIGURATION

        // use a cell layer painter that is configured for left clipping
        // this ensures that the rendering works correctly for split
        // viewports
        selectionLayer.setLayerPainter(new SelectionLayerPainter(true, false));

        columnHeaderLayer.setLayerPainter(new CellLayerPainter(true, false));

        // as the CompositeLayer is setting a IClientAreaProvider for the
        // composition we need to set a special ClientAreaAdapter after the
        // creation of the CompositeLayer to support split viewports
        int leftWidth = 100;
        ClientAreaAdapter leftClientAreaAdapter =
                new ClientAreaAdapter(rowViewport.getClientAreaProvider());
        leftClientAreaAdapter.setWidth(leftWidth);
        rowViewport.setClientAreaProvider(leftClientAreaAdapter);

        // Wrap NatTable in composite so we can slap on the external horizontal
        // sliders
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout gridLayout = new GridLayout(1, false);
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        gridLayout.horizontalSpacing = 0;
        gridLayout.verticalSpacing = 0;
        composite.setLayout(gridLayout);

        NatTable natTable = new NatTable(composite, gridLayer, false);
        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.verticalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = true;
        natTable.setLayoutData(gridData);


        // add an IOverlayPainter to render the split viewport border
        natTable.addOverlayPainter(new IOverlayPainter() {

            @Override
            public void paintOverlay(GC gc, ILayer layer) {
                Color beforeColor = gc.getForeground();
                gc.setForeground(GUIHelper.COLOR_GRAY);
                int viewportBorderX = compositeLayer.getWidth() - 1;
                gc.drawLine(viewportBorderX, 0, viewportBorderX, layer.getHeight() - 1);
                gc.setForeground(beforeColor);
            }
        });


        natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
        
        natTable.addConfiguration(new AbstractRegistryConfiguration(){


			private CheckBoxPainter checkBoxPainter;

			@Override
			public void configureRegistry(IConfigRegistry configRegistry) {
				checkBoxPainter = new CheckBoxPainter();
				
				configRegistry.registerConfigAttribute(
		                EditConfigAttributes.CELL_EDITABLE_RULE,
		                IEditableRule.ALWAYS_EDITABLE, DisplayMode.NORMAL,
		                "CELL_EDITOR_TYPE_CHECKBOX");
				
				 configRegistry.registerConfigAttribute(
			                EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(),
			                DisplayMode.EDIT, "CELL_EDITOR_TYPE_CHECKBOX");
				
				configRegistry.registerConfigAttribute(
		                CellConfigAttributes.CELL_PAINTER, checkBoxPainter,
		                DisplayMode.NORMAL, "CELL_EDITOR_TYPE_CHECKBOX");
				
		       
		        configRegistry.registerConfigAttribute(
		                CellConfigAttributes.DISPLAY_CONVERTER,
		                new DefaultBooleanDisplayConverter(),
		                DisplayMode.NORMAL,
		                "CELL_EDITOR_TYPE_CHECKBOX");
		        
		        
			}
			
			@Override
			public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
				uiBindingRegistry.registerFirstSingleClickBinding(
		                new CellPainterMouseEventMatcher(GridRegion.ROW_HEADER,
		                        MouseEventMatcher.LEFT_BUTTON, checkBoxPainter),
		                new MouseEditAction());
				super.configureUiBindings(uiBindingRegistry);
			}
        });
        natTable.configure();
        return natTable;
    }


    class ClientAreaResizeMatcher extends MouseEventMatcher {

        ILayer rowHeaderLayer;

        public ClientAreaResizeMatcher(ILayer rowHeaderLayer) {
            this.rowHeaderLayer = rowHeaderLayer;
        }

        @Override
        public boolean matches(NatTable natTable, MouseEvent event, LabelStack regionLabels) {
            if (regionLabels != null && regionLabels.hasLabel(GridRegion.COLUMN_HEADER)
                    && (event.x > this.rowHeaderLayer.getWidth() - 5 && event.x < this.rowHeaderLayer.getWidth() + 5)) {
                return true;
            }
            return false;
        }
    }

}
Re: Add new row header [message #1773693 is a reply to message #1773685] Tue, 03 October 2017 06:19 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Sorry but no. I think you are using the wrong approach for your composition. Much too complicated as you don't need multiple viewports. And I don't have the time to check unnecessary complicated code.
Previous Topic:GroupBySummary Update Parent Summary value
Next Topic:SizeConfig doesd not invalidate cache when DPI converter is set
Goto Forum:
  


Current Time: Thu Apr 25 15:31:39 GMT 2024

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

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

Back to the top