How can I keep an extra empty row in a SWT table?

813 views Asked by At

How can I keep an extra empty row in a SWT table without adding dummy value in the model? I want to show an empty row always for the purpose of painting a rectangle around the last row? Any clue?

Scenario: The table size is going to be fixed. If I have 5 items then I want the sixth row as empty where I can draw. If I have 100 rows then I want the empty row at the 101st position and the table should scroll on some event and show that painted rectangle.

Hope to see an answer soon.

1

There are 1 answers

0
Amit Kumar On BEST ANSWER

Finally I am able to add an empty row in the table without adding a dummy value in the content provider. Here is what I did: I extended the JFace TableViewer class and overrided the refresh(), refresh(Object element) and inputChanged(Object input, Object oldInput) method. Basically in all these three methods I first remove the empty Item if there is any and then let the original jface method call happen and then I again add the new empty table item.

Below is the code which worked for me.

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;



/**
 * @author amitkumar
 */
public class ExtraEmptyRowTableViewer extends TableViewer {

    boolean addExtraRow = false;

    public ExtraEmptyRowTableViewer(Composite parent) {
        super(parent);
        IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (workbenchWindow != null
                && workbenchWindow.getActivePage() != null
                && workbenchWindow.getActivePage().getActiveEditor() != null
                && workbenchWindow.getActivePage().getActiveEditor().getClass().getName().equals(
                        "org.eclipse.compare.internal.CompareEditor")) {
            addExtraRow = true;
        }
    }

    public ExtraEmptyRowTableViewer(Composite composite, int style) {
        super(composite, style);
        IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (workbenchWindow != null
                && workbenchWindow.getActivePage() != null
                && workbenchWindow.getActivePage().getActiveEditor() != null
                && workbenchWindow.getActivePage().getActiveEditor().getClass().getName().equals(
                        "org.eclipse.compare.internal.CompareEditor")) {
            addExtraRow = true;
        }
    }

    @Override
    public void refresh(Object element) {
        if (!addExtraRow) {
            super.refresh(element);
        } else {
            removeEmptyRow();
            super.refresh(element);
        }
    }

    @Override
    protected void inputChanged(Object input, Object oldInput) {
        if (!addExtraRow) {
            super.inputChanged(input, oldInput);
        } else {
            removeEmptyRow();
            super.inputChanged(input, oldInput);
            @SuppressWarnings("unused")
            TableItem tableItem = new TableItem(getTable(), SWT.NO_BACKGROUND | SWT.NO_FOCUS);
        }

    }

    public void removeEmptyRow() {
        try {
            for (TableItem tableItem : getTable().getItems()) {
                if (tableItem == null || tableItem.getText() == null
                        || "".equals(tableItem.getText())) {
                    tableItem.dispose();
                }
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void refresh() {
        if (!addExtraRow) {
            super.refresh();
        } else {
            removeEmptyRow();
            super.refresh();
            @SuppressWarnings("unused")
            TableItem tableItem = new TableItem(getTable(), SWT.NO_BACKGROUND | SWT.NO_FOCUS);
        }
    }

}

Thanks... Amit Kumar