Search in sources :

Example 26 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class RenameColumnIntegrationTest method shouldRenameColumnHeader.

@Test
public void shouldRenameColumnHeader() {
    String originalColumnHeader = this.natTableFixture.getDataValueByPosition(2, 0).toString();
    assertEquals("Column 2", originalColumnHeader);
    this.natTableFixture.doCommand(new RenameColumnHeaderCommand(this.natTableFixture, 2, TEST_COLUMN_NAME));
    String renamedColumnHeader = this.natTableFixture.getDataValueByPosition(2, 0).toString();
    assertEquals(TEST_COLUMN_NAME, renamedColumnHeader);
    assertEquals(1, this.listener.getEventsCount());
    RenameColumnHeaderEvent event = (RenameColumnHeaderEvent) this.listener.getReceivedEvent(RenameColumnHeaderEvent.class);
    assertEquals(new Range(2, 3), event.getColumnPositionRanges().iterator().next());
}
Also used : RenameColumnHeaderEvent(org.eclipse.nebula.widgets.nattable.columnRename.event.RenameColumnHeaderEvent) RenameColumnHeaderCommand(org.eclipse.nebula.widgets.nattable.columnRename.RenameColumnHeaderCommand) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) Test(org.junit.Test)

Example 27 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class RenameColumnIntegrationTest method shouldRenameColumnHeaderForReorderedColumnProgrammatically.

@Test
public void shouldRenameColumnHeaderForReorderedColumnProgrammatically() {
    String originalColumnHeader = this.natTableFixture.getDataValueByPosition(2, 0).toString();
    assertEquals("Column 2", originalColumnHeader);
    this.natTableFixture.doCommand(new ColumnReorderCommand(this.natTableFixture, 1, 5));
    originalColumnHeader = this.natTableFixture.getDataValueByPosition(2, 0).toString();
    assertEquals("Column 3", originalColumnHeader);
    this.grid.getColumnHeaderLayer().renameColumnIndex(2, TEST_COLUMN_NAME);
    String renamedColumnHeader = this.natTableFixture.getDataValueByPosition(2, 0).toString();
    assertEquals(TEST_COLUMN_NAME, renamedColumnHeader);
    assertEquals(2, this.listener.getEventsCount());
    RenameColumnHeaderEvent event = (RenameColumnHeaderEvent) this.listener.getReceivedEvent(RenameColumnHeaderEvent.class);
    assertEquals(new Range(2, 3), event.getColumnPositionRanges().iterator().next());
}
Also used : RenameColumnHeaderEvent(org.eclipse.nebula.widgets.nattable.columnRename.event.RenameColumnHeaderEvent) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) ColumnReorderCommand(org.eclipse.nebula.widgets.nattable.reorder.command.ColumnReorderCommand) Test(org.junit.Test)

Example 28 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class NatTable method handleLayerEvent.

// Events /////////////////////////////////////////////////////////////////
@Override
public void handleLayerEvent(ILayerEvent event) {
    List<ILayerListener> currentListeners;
    this.eventListenerLock.readLock().lock();
    try {
        currentListeners = this.listeners;
    } finally {
        this.eventListenerLock.readLock().unlock();
    }
    for (ILayerListener layerListener : currentListeners) {
        layerListener.handleLayerEvent(event);
    }
    if (event instanceof CellVisualUpdateEvent) {
        CellVisualUpdateEvent update = (CellVisualUpdateEvent) event;
        repaintCell(update.getColumnPosition(), update.getRowPosition());
        return;
    }
    if (event instanceof ColumnVisualUpdateEvent) {
        ColumnVisualUpdateEvent update = (ColumnVisualUpdateEvent) event;
        // if more than one column has changed repaint the whole table
        Collection<Range> ranges = update.getColumnPositionRanges();
        if (ranges.size() == 1) {
            Range range = ranges.iterator().next();
            if (range.end - range.start == 1) {
                repaintColumn(range.start);
                return;
            }
        }
    }
    if (event instanceof RowVisualUpdateEvent) {
        RowVisualUpdateEvent update = (RowVisualUpdateEvent) event;
        // if more than one row has changed repaint the whole table
        Collection<Range> ranges = update.getRowPositionRanges();
        if (ranges.size() == 1) {
            Range range = ranges.iterator().next();
            if (range.end - range.start == 1) {
                repaintRow(range.start);
                return;
            }
        }
    }
    if (event instanceof ISelectionEvent) {
        if (event instanceof CellSelectionEvent || event instanceof RowSelectionEvent) {
            Event e = new Event();
            e.widget = this;
            try {
                notifyListeners(SWT.Selection, e);
            } catch (RuntimeException re) {
                // $NON-NLS-1$
                log.error("Error on SWT selection processing", re);
            }
        }
        // in case of selections we redraw immediately
        // this is because with Bug 440037 it was reported that
        // NatTable is too lazy in handling selections which
        // was caused by the EventConflaterChain that only performs
        // updates every 100ms to avoid flickering when handling too
        // many refresh operations in a short period
        redraw();
    } else if (event instanceof IVisualChangeEvent) {
        this.conflaterChain.addEvent(event);
    }
    if (event instanceof CellEditorCreatedEvent) {
        CellEditorCreatedEvent editorEvent = (CellEditorCreatedEvent) event;
        this.activeCellEditor = editorEvent.getEditor();
        Control editorControl = this.activeCellEditor.getEditorControl();
        if (editorControl != null && !editorControl.isDisposed()) {
            editorControl.addDisposeListener(new DisposeListener() {

                @Override
                public void widgetDisposed(DisposeEvent e) {
                    NatTable.this.activeCellEditor = null;
                    ActiveCellEditorRegistry.unregisterActiveCellEditor();
                }
            });
        } else {
            this.activeCellEditor = null;
            ActiveCellEditorRegistry.unregisterActiveCellEditor();
        }
        ActiveCellEditorRegistry.registerActiveCellEditor(this.activeCellEditor);
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) IVisualChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.IVisualChangeEvent) CellVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.CellVisualUpdateEvent) RowVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.RowVisualUpdateEvent) ILayerListener(org.eclipse.nebula.widgets.nattable.layer.ILayerListener) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) DisposeEvent(org.eclipse.swt.events.DisposeEvent) CellEditorCreatedEvent(org.eclipse.nebula.widgets.nattable.edit.CellEditorCreatedEvent) Control(org.eclipse.swt.widgets.Control) ColumnVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.ColumnVisualUpdateEvent) CellSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) FocusEvent(org.eclipse.swt.events.FocusEvent) PaintEvent(org.eclipse.swt.events.PaintEvent) CellVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.CellVisualUpdateEvent) CellEditorCreatedEvent(org.eclipse.nebula.widgets.nattable.edit.CellEditorCreatedEvent) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) CellSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent) ColumnVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.ColumnVisualUpdateEvent) IVisualChangeEvent(org.eclipse.nebula.widgets.nattable.layer.event.IVisualChangeEvent) Event(org.eclipse.swt.widgets.Event) RowVisualUpdateEvent(org.eclipse.nebula.widgets.nattable.layer.event.RowVisualUpdateEvent) RowSelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent) ISelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ISelectionEvent) ILayerEvent(org.eclipse.nebula.widgets.nattable.layer.event.ILayerEvent) ISelectionEvent(org.eclipse.nebula.widgets.nattable.selection.event.ISelectionEvent)

Example 29 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class RenameColumnHelper method handleStructuralChanges.

/**
 * Handle the given collection of {@link StructuralDiff} objects to update
 * the indexes of the renamed column labels.
 *
 * @param columnDiffs
 *            The {@link StructuralDiff}s to handle
 * @since 1.4
 */
public void handleStructuralChanges(Collection<StructuralDiff> columnDiffs) {
    // the number of all deleted columns that don't have a corresponding
    // index anymore (last column cases)
    List<Integer> toRemove = new ArrayList<Integer>();
    for (StructuralDiff columnDiff : columnDiffs) {
        if (columnDiff.getDiffType() != null && columnDiff.getDiffType().equals(DiffTypeEnum.DELETE)) {
            Range beforePositionRange = columnDiff.getBeforePositionRange();
            for (int i = beforePositionRange.start; i < beforePositionRange.end; i++) {
                int index = i;
                if (index >= 0)
                    toRemove.add(index);
            }
        }
    }
    // remove the column indexes that are deleted
    for (Integer r : toRemove) {
        this.renamedColumnsLabelsByIndex.remove(r);
    }
    // modify column indexes regarding the deleted columns
    List<Integer> indices = new ArrayList<Integer>(this.renamedColumnsLabelsByIndex.keySet());
    Collections.sort(indices);
    Map<Integer, String> modified = new TreeMap<Integer, String>();
    for (Integer column : indices) {
        // check number of removed indexes that are lower than the current
        // one
        int deletedBefore = 0;
        for (Integer removed : toRemove) {
            if (removed < column) {
                deletedBefore++;
            }
        }
        int modColumn = column - deletedBefore;
        if (modColumn >= 0)
            modified.put(modColumn, this.renamedColumnsLabelsByIndex.get(column));
    }
    this.renamedColumnsLabelsByIndex.clear();
    this.renamedColumnsLabelsByIndex.putAll(modified);
    for (StructuralDiff columnDiff : columnDiffs) {
        if (columnDiff.getDiffType() != null && columnDiff.getDiffType().equals(DiffTypeEnum.ADD)) {
            indices = new ArrayList<Integer>(this.renamedColumnsLabelsByIndex.keySet());
            Collections.sort(indices);
            Range beforePositionRange = columnDiff.getBeforePositionRange();
            Range afterPositionRange = columnDiff.getAfterPositionRange();
            Map<Integer, String> modifiedColumns = new TreeMap<Integer, String>();
            int beforeIndex = this.columnHeaderLayer.getColumnIndexByPosition(beforePositionRange.start);
            for (Integer column : indices) {
                if (column >= beforeIndex) {
                    modifiedColumns.put(column + (afterPositionRange.end - afterPositionRange.start), this.renamedColumnsLabelsByIndex.get(column));
                } else {
                    modifiedColumns.put(column, this.renamedColumnsLabelsByIndex.get(column));
                }
            }
            this.renamedColumnsLabelsByIndex.clear();
            this.renamedColumnsLabelsByIndex.putAll(modifiedColumns);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) StructuralDiff(org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range) TreeMap(java.util.TreeMap)

Example 30 with Range

use of org.eclipse.nebula.widgets.nattable.coordinate.Range in project nebula.widgets.nattable by eclipse.

the class CopyDataCommandHandler method assembleCopiedDataStructure.

/**
 * Collects and assembles the selected data that should be copied to the
 * clipboard.
 *
 * @return A two dimensional array containing the selected cells to copy to
 *         the clipboard. The first level of this array represent the row
 *         positions of the cells, while the second level contains the cells
 *         itself based on the column position.
 */
protected ILayerCell[][] assembleCopiedDataStructure() {
    final Set<Range> selectedRows = this.selectionLayer.getSelectedRowPositions();
    final ILayerCell[][] copiedCells = assembleColumnHeaders();
    // cleanup the row positions to copy
    // this is needed because taking only the Range.start into account leads
    // to overriding values in the array instead of adding if there are
    // multiple Ranges returned
    List<Integer> selectedRowPositions = new ArrayList<Integer>();
    for (Range range : selectedRows) {
        for (int rowPosition = range.start; rowPosition < range.end; rowPosition++) {
            selectedRowPositions.add(rowPosition);
        }
    }
    // ensure the correct order as a Set is not ordered at all and we want
    // to paste the values in the same order we copied them.
    Collections.sort(selectedRowPositions);
    final int rowOffset = this.columnHeaderDataLayer != null ? this.columnHeaderDataLayer.getRowCount() : 0;
    for (int i = 0; i < selectedRowPositions.size(); i++) {
        Integer rowPos = selectedRowPositions.get(i);
        copiedCells[i + rowOffset] = assembleBody(rowPos);
    }
    return copiedCells;
}
Also used : ArrayList(java.util.ArrayList) Range(org.eclipse.nebula.widgets.nattable.coordinate.Range)

Aggregations

Range (org.eclipse.nebula.widgets.nattable.coordinate.Range)115 Test (org.junit.Test)54 ArrayList (java.util.ArrayList)40 StructuralDiff (org.eclipse.nebula.widgets.nattable.layer.event.StructuralDiff)23 HashSet (java.util.HashSet)12 Rectangle (org.eclipse.swt.graphics.Rectangle)12 RowDeleteEvent (org.eclipse.nebula.widgets.nattable.layer.event.RowDeleteEvent)9 RowSelectionEvent (org.eclipse.nebula.widgets.nattable.selection.event.RowSelectionEvent)9 NatTableFixture (org.eclipse.nebula.widgets.nattable.test.fixture.NatTableFixture)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)6 RenameColumnHeaderCommand (org.eclipse.nebula.widgets.nattable.columnRename.RenameColumnHeaderCommand)6 HideRowPositionsEvent (org.eclipse.nebula.widgets.nattable.hideshow.event.HideRowPositionsEvent)6 ColumnResizeEvent (org.eclipse.nebula.widgets.nattable.resize.event.ColumnResizeEvent)6 ShowRowPositionsEvent (org.eclipse.nebula.widgets.nattable.hideshow.event.ShowRowPositionsEvent)5 ColumnReorderEvent (org.eclipse.nebula.widgets.nattable.reorder.event.ColumnReorderEvent)5 SelectRowsCommand (org.eclipse.nebula.widgets.nattable.selection.command.SelectRowsCommand)5 Point (org.eclipse.swt.graphics.Point)5 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)4