Search in sources :

Example 11 with NatTable

use of org.eclipse.nebula.widgets.nattable.NatTable in project nebula.widgets.nattable by eclipse.

the class ViewportLayerDisposalTest method testPostEventToDisposedLayer.

/**
 * Tests that an {@link IStructuralChangeEvent} will be safely handled by a
 * table's {@link ViewportLayer} after the table has been disposed (see: bug
 * <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=447942">Bug
 * 447942</a>).
 *
 * @throws InterruptedException
 *             on multi-threading issues
 */
@Test
public void testPostEventToDisposedLayer() throws InterruptedException {
    final DataLayer dataLayer = new TestDataLayer();
    final ViewportLayer viewportLayer = new ViewportLayer(dataLayer);
    final NatTable table = new NatTable(this.shell, viewportLayer);
    // Setting the table's size will cause the viewport layer to connect
    // itself to the table's scrollbars.
    table.setSize(500, 500);
    // Change a value in the data layer on a background thread.
    final Thread backgroundUpdater = new Thread(new Runnable() {

        @Override
        public void run() {
            // Calling "setDataValue" on a background thread will cause the
            // actual update to be executed asnchronously on the ui thread.
            dataLayer.setDataValue(4, 49, "VALUE");
        }
    }, "LayerDisposalTest.backgroundUpdater");
    backgroundUpdater.start();
    backgroundUpdater.join();
    // Disposing the table will also dispose all of its layers.
    table.dispose();
    // to the (disposed) viewport layer.
    while (this.shell.getDisplay().readAndDispatch()) {
    }
    // Test that the background thread has successfully updated the table's
    // data layer.
    Assert.assertEquals("The table's data layer should have been updated.", "VALUE", dataLayer.getDataValue(4, 49));
}
Also used : DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) Test(org.junit.Test)

Example 12 with NatTable

use of org.eclipse.nebula.widgets.nattable.NatTable in project nebula.widgets.nattable by eclipse.

the class NatExporter method exportMultipleNatTables.

/**
 * Export multiple NatTable instances to one file by using the given
 * ILayerExporter.
 *
 * @param exporter
 *            The ILayerExporter to use for exporting.
 * @param natTablesMap
 *            The NatTable instances to export. They keys in the map will be
 *            used as sheet titles while the values are the instances to
 *            export.
 * @param exportOnSameSheet
 *            Flag to configure whether multiple NatTable instances should
 *            be exported on the same sheet or not.
 * @param sheetName
 *            The sheet name that should be used in case of exporting
 *            multiple NatTables on a single sheet.
 * @since 1.5
 */
public void exportMultipleNatTables(final ILayerExporter exporter, final Map<String, NatTable> natTablesMap, final boolean exportOnSameSheet, final String sheetName) {
    final OutputStream outputStream = getOutputStream(exporter);
    if (outputStream == null) {
        return;
    }
    Runnable exportRunnable = new Runnable() {

        @Override
        public void run() {
            try {
                exporter.exportBegin(outputStream);
                if (exportOnSameSheet) {
                    exporter.exportLayerBegin(outputStream, sheetName);
                }
                for (String name : natTablesMap.keySet()) {
                    NatTable natTable = natTablesMap.get(name);
                    exportLayer(exporter, outputStream, name, natTable, natTable.getConfigRegistry(), !exportOnSameSheet);
                }
                if (exportOnSameSheet) {
                    exporter.exportLayerEnd(outputStream, sheetName);
                }
                exporter.exportEnd(outputStream);
                NatExporter.this.exportSucceeded = true;
            } catch (Exception e) {
                NatExporter.this.exportSucceeded = false;
                handleExportException(e);
            } finally {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    // $NON-NLS-1$
                    LOG.error("Failed to close the output stream", e);
                }
            }
            openExport(exporter);
        }
    };
    if (this.shell != null) {
        // Run with the SWT display so that the progress bar can paint
        if (this.runAsynchronously) {
            this.shell.getDisplay().asyncExec(exportRunnable);
        } else {
            this.shell.getDisplay().syncExec(exportRunnable);
        }
    } else {
        exportRunnable.run();
    }
}
Also used : OutputStream(java.io.OutputStream) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) IOException(java.io.IOException) IOException(java.io.IOException)

Example 13 with NatTable

use of org.eclipse.nebula.widgets.nattable.NatTable in project nebula.widgets.nattable by eclipse.

the class ColumnGroupMenuItemProviders method removeColumnGroupMenuItemProvider.

public static IMenuItemProvider removeColumnGroupMenuItemProvider(final String menuLabel) {
    return new IMenuItemProvider() {

        @Override
        public void addMenuItem(final NatTable natTable, final Menu popupMenu) {
            MenuItem columnStyleEditor = new MenuItem(popupMenu, SWT.PUSH);
            columnStyleEditor.setText(menuLabel);
            columnStyleEditor.setEnabled(true);
            columnStyleEditor.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    NatEventData natEventData = MenuItemProviders.getNatEventData(e);
                    int columnPosition = natEventData.getColumnPosition();
                    int columnIndex = natEventData.getNatTable().getColumnIndexByPosition(columnPosition);
                    natTable.doCommand(new RemoveColumnGroupCommand(columnIndex));
                }
            });
        }
    };
}
Also used : RemoveColumnGroupCommand(org.eclipse.nebula.widgets.nattable.group.command.RemoveColumnGroupCommand) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) IMenuItemProvider(org.eclipse.nebula.widgets.nattable.ui.menu.IMenuItemProvider) SelectionEvent(org.eclipse.swt.events.SelectionEvent) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) MenuItem(org.eclipse.swt.widgets.MenuItem) NatEventData(org.eclipse.nebula.widgets.nattable.ui.NatEventData) Menu(org.eclipse.swt.widgets.Menu)

Example 14 with NatTable

use of org.eclipse.nebula.widgets.nattable.NatTable in project nebula.widgets.nattable by eclipse.

the class ViewportLayer method doCommand.

@Override
public boolean doCommand(ILayerCommand command) {
    if (command instanceof ClientAreaResizeCommand && command.convertToTargetLayer(this)) {
        if (this.processingClientAreaResizeCommand) {
            return false;
        }
        this.processingClientAreaResizeCommand = true;
        // on client area resize we reset the keep in viewport row position
        this.keepInViewportRowPosition = -1;
        ClientAreaResizeCommand clientAreaResizeCommand = (ClientAreaResizeCommand) command;
        // remember the difference from client area to body region area
        // needed because the scrollbar will be removed and therefore the
        // client area will become bigger
        Scrollable scrollable = clientAreaResizeCommand.getScrollable();
        Rectangle clientArea = scrollable.getClientArea();
        Rectangle calcArea = clientAreaResizeCommand.getCalcArea();
        int widthDiff = clientArea.width - calcArea.width;
        int heightDiff = clientArea.height - calcArea.height;
        boolean initialClientAreaResize = false;
        if (this.hBarListener == null && this.horizontalScrollbarEnabled) {
            initialClientAreaResize = true;
            ScrollBar hBar = scrollable.getHorizontalBar();
            if (hBar != null) {
                if (this.horizontalScroller != null) {
                    hBar.setEnabled(false);
                    hBar.setVisible(false);
                } else {
                    this.horizontalScroller = new ScrollBarScroller(hBar);
                }
                this.hBarListener = new HorizontalScrollBarHandler(this, this.horizontalScroller);
                if (scrollable instanceof NatTable) {
                    this.hBarListener.setTable((NatTable) scrollable);
                }
            }
        }
        if (this.vBarListener == null && this.verticalScrollbarEnabled) {
            initialClientAreaResize = true;
            ScrollBar vBar = scrollable.getVerticalBar();
            if (vBar != null) {
                if (this.verticalScroller != null) {
                    vBar.setEnabled(false);
                    vBar.setVisible(false);
                } else {
                    this.verticalScroller = new ScrollBarScroller(vBar);
                }
                this.vBarListener = new VerticalScrollBarHandler(this, this.verticalScroller);
                if (scrollable instanceof NatTable) {
                    this.vBarListener.setTable((NatTable) scrollable);
                }
            }
        }
        if (initialClientAreaResize) {
            handleGridResize();
            // after handling the scrollbars recalculate the area to use for
            // percentage calculation
            Rectangle possibleArea = scrollable.getClientArea();
            possibleArea.width = possibleArea.width - widthDiff;
            possibleArea.height = possibleArea.height - heightDiff;
            clientAreaResizeCommand.setCalcArea(possibleArea);
        }
        // we don't return true here because the ClientAreaResizeCommand
        // needs to be handled by the DataLayer in case percentage sizing is
        // enabled. if we would return true, the DataLayer wouldn't be able
        // to calculate the column/row sizes regarding the client area
        boolean result = super.doCommand(command);
        if (!initialClientAreaResize) {
            handleGridResize();
        }
        // we need to first give underlying layers the chance to process the
        // command and afterwards set the processing flag to false
        // this way we avoid processing the resize multiple times because of
        // re-calculation in conjunction with scrollbar visibility state
        // changes
        this.processingClientAreaResizeCommand = false;
        return result;
    } else if (command instanceof TurnViewportOffCommand) {
        this.savedOrigin = this.origin;
        this.viewportOff = true;
        return true;
    } else if (command instanceof TurnViewportOnCommand) {
        this.viewportOff = false;
        this.origin = this.savedOrigin;
        // only necessary in case of split viewports and auto resizing, but
        // shouldn't hurt in other cases
        recalculateScrollBars();
        return true;
    } else if (command instanceof PrintEntireGridCommand) {
        moveCellPositionIntoViewport(0, 0);
    }
    return super.doCommand(command);
}
Also used : ClientAreaResizeCommand(org.eclipse.nebula.widgets.nattable.grid.command.ClientAreaResizeCommand) Rectangle(org.eclipse.swt.graphics.Rectangle) Scrollable(org.eclipse.swt.widgets.Scrollable) PrintEntireGridCommand(org.eclipse.nebula.widgets.nattable.print.command.PrintEntireGridCommand) TurnViewportOnCommand(org.eclipse.nebula.widgets.nattable.print.command.TurnViewportOnCommand) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) TurnViewportOffCommand(org.eclipse.nebula.widgets.nattable.print.command.TurnViewportOffCommand) ScrollBar(org.eclipse.swt.widgets.ScrollBar)

Example 15 with NatTable

use of org.eclipse.nebula.widgets.nattable.NatTable in project nebula.widgets.nattable by eclipse.

the class MenuItemProviders method hideRowMenuItemProvider.

/**
 * Will create and return the {@link IMenuItemProvider} that adds the action
 * for executing the {@link RowHideCommand} to a popup menu. This command is
 * intended to hide the current selected row immediately.
 * <p>
 * The {@link MenuItem} will be shown with the given menu label.
 *
 * @param menuLabel
 *            The text that will be showed for the generated
 *            {@link MenuItem}
 * @return The {@link IMenuItemProvider} for the {@link MenuItem} that
 *         executes the {@link RowHideCommand}.
 */
public static IMenuItemProvider hideRowMenuItemProvider(final String menuLabel) {
    return new IMenuItemProvider() {

        @Override
        public void addMenuItem(final NatTable natTable, final Menu popupMenu) {
            MenuItem menuItem = new MenuItem(popupMenu, SWT.PUSH);
            menuItem.setText(Messages.getLocalizedMessage(menuLabel));
            // $NON-NLS-1$
            menuItem.setImage(GUIHelper.getImage("hide_row"));
            menuItem.setEnabled(true);
            menuItem.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent event) {
                    int rowPosition = getNatEventData(event).getRowPosition();
                    natTable.doCommand(new RowHideCommand(natTable, rowPosition));
                }
            });
        }
    };
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) MenuItem(org.eclipse.swt.widgets.MenuItem) Menu(org.eclipse.swt.widgets.Menu) RowHideCommand(org.eclipse.nebula.widgets.nattable.hideshow.command.RowHideCommand)

Aggregations

NatTable (org.eclipse.nebula.widgets.nattable.NatTable)221 DataLayer (org.eclipse.nebula.widgets.nattable.layer.DataLayer)150 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)113 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)100 ILayer (org.eclipse.nebula.widgets.nattable.layer.ILayer)90 HashMap (java.util.HashMap)88 ColumnHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer)88 DefaultColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)79 GridLayer (org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer)75 DefaultCornerDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider)74 RowHeaderLayer (org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer)73 SelectionLayer (org.eclipse.nebula.widgets.nattable.selection.SelectionLayer)73 DefaultRowHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider)72 CornerLayer (org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer)72 DefaultRowHeaderDataLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer)72 ViewportLayer (org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer)71 SelectionEvent (org.eclipse.swt.events.SelectionEvent)67 DefaultColumnHeaderDataLayer (org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer)66 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)66 GridLayout (org.eclipse.swt.layout.GridLayout)64