@Override public void controlResized(ControlEvent e) { int restWidth = 0; for (CodeViewerColumn col : CodeViewerColumn.values()) { if (col.equals(column)) continue; restWidth += grid.getColumn(col.ordinal()).getWidth(); } if (grid.getVerticalBar().isVisible()) { restWidth += grid.getVerticalBar().getSize().x; } int totalWidth = grid.getBounds().width; grid.getColumn(column.ordinal()).setWidth(totalWidth - restWidth - 4); }
public void testTableColumnsWithNullHeaderEntry() { final ITableRidget ridget = getRidget(); final Grid control = getWidget(); control.getColumn(0).setText("foo"); // $NON-NLS-1$ control.getColumn(1).setText("bar"); // $NON-NLS-1$ final String[] properties1 = new String[] {"firstname", "lastname"}; // $NON-NLS-1$ //$NON-NLS-2$ final String[] headers = new String[] {"First Name", null}; // $NON-NLS-1$ ridget.bindToModel(manager, "persons", Person.class, properties1, headers); // $NON-NLS-1$ assertEquals("First Name", control.getColumn(0).getText()); // $NON-NLS-1$ assertEquals("", control.getColumn(1).getText()); // $NON-NLS-1$ }
/** As per Bug 295305 */ public void testSetColumnWidths() { final ITableRidget ridget = createRidget(); final Grid control = new Grid(getShell(), SWT.FULL_SELECTION | SWT.SINGLE); ridget.setUIControl(control); try { ridget.setColumnWidths(new Object[] {null}); fail(); } catch (final RuntimeException rex) { assertTrue(rex.getMessage().contains("null")); // $NON-NLS-1$ } try { ridget.setColumnWidths(new Object[] {new Object()}); fail(); } catch (final RuntimeException rex) { assertTrue(rex.getMessage().contains("Object")); // $NON-NLS-1$ } ridget.setColumnWidths( new Object[] {new ColumnPixelData(20), new ColumnPixelData(40), new ColumnPixelData(60)}); final String[] columns3 = { Person.PROPERTY_FIRSTNAME, Person.PROPERTY_LASTNAME, Person.PROPERTY_BIRTHDAY }; ridget.bindToModel(manager, "persons", Person.class, columns3, null); // $NON-NLS-1$ final int[] expected = {20, 40, 60}; for (int i = 0; i < 3; i++) { final int actual = control.getColumn(i).getWidth(); final String msg = String.format("col #%d, exp:%d, act:%d", i, expected[i], actual); // $NON-NLS-1$ assertEquals(msg, expected[i], actual); } }
public void testSetMoveableColumns() { final ITableRidget ridget = getRidget(); final Grid table = getWidget(); assertFalse(ridget.hasMoveableColumns()); assertFalse(table.getColumn(0).getMoveable()); assertFalse(table.getColumn(1).getMoveable()); ridget.setMoveableColumns(true); assertTrue(ridget.hasMoveableColumns()); assertTrue(table.getColumn(0).getMoveable()); assertTrue(table.getColumn(1).getMoveable()); ridget.setMoveableColumns(false); assertFalse(ridget.hasMoveableColumns()); assertFalse(table.getColumn(0).getMoveable()); assertFalse(table.getColumn(1).getMoveable()); }
public void testTableColumnsWithNullHeader() { final ITableRidget ridget = getRidget(); final Grid control = getWidget(); control.setHeaderVisible(true); control.getColumn(0).setText("foo"); // $NON-NLS-1$ control.getColumn(1).setText("bar"); // $NON-NLS-1$ final String[] properties1 = new String[] {"firstname", "lastname"}; // $NON-NLS-1$ //$NON-NLS-2$ // null should hide the headers ridget.bindToModel(manager, "persons", Person.class, properties1, null); // $NON-NLS-1$ assertFalse(control.getHeaderVisible()); }
/** * Returns the 0 based index of the column at {@code pt}. The code can handle re-ordered columns. * The index refers to the original ordering (as used by SWT API). * * <p>Will return -1 if no column could be computed -- this is the case when all columns are * resized to have width 0. */ private int findColumn(final Grid grid, final Point pt) { int width = 0; final int[] colOrder = grid.getColumnOrder(); // compute the current column ordering final GridColumn[] columns = new GridColumn[colOrder.length]; for (int i = 0; i < colOrder.length; i++) { final int idx = colOrder[i]; columns[i] = grid.getColumn(idx); } // find the column under Point pt\ for (final GridColumn col : columns) { final int colWidth = col.getWidth(); if (width < pt.x && pt.x < width + colWidth) { return grid.indexOf(col); } width += colWidth; } return -1; }
@Override protected final void applyComparator(final Map<Integer, Comparator<?>> comparatorMap) { if (getTableViewer() != null) { Comparator<?> compi = null; if (getSortedColumn() != -1) { final Integer key = Integer.valueOf(getSortedColumn()); compi = comparatorMap.get(key); } final Grid control = getUIControl(); clearSortIndicator(); if (compi != null) { final GridColumn column = control.getColumn(getSortedColumn()); final int direction = getSortDirection(); column.setSort(direction); final SortableComparator sortableComparator = new SortableComparator(this, compi); getTableViewer().setComparator(new TableComparator(sortableComparator)); } else { getTableViewer().setComparator(null); } } }
/** As per Bug 295305 */ public void testPreserveColumnWidths() { final int[] widths = {50, 100, 150}; final ITableRidget ridget = createRidget(); final Grid control = new Grid(getShell(), SWT.FULL_SELECTION | SWT.SINGLE); for (final int width : widths) { final GridColumn column = new GridColumn(control, SWT.NONE); column.setWidth(width); } ridget.setUIControl(control); final String[] columns3 = { Person.PROPERTY_FIRSTNAME, Person.PROPERTY_LASTNAME, Person.PROPERTY_BIRTHDAY }; ridget.bindToModel(manager, "persons", Person.class, columns3, null); // $NON-NLS-1$ ridget.updateFromModel(); for (int i = 0; i < 3; i++) { final int actual = control.getColumn(i).getWidth(); final String msg = String.format("col #%d, exp:%d, act:%d", i, widths[i], actual); // $NON-NLS-1$ assertEquals(msg, widths[i], actual); } }
public void testColumnHeaderChangesSortability() { final ITableRidget ridget = getRidget(); final Grid table = getWidget(); ridget.setColumnSortable(0, true); ridget.setComparator(0, new TypedComparator<String>()); ridget.setColumnSortable(1, true); ridget.setComparator(1, new TypedComparator<String>()); ridget.setSortedColumn(-1); assertEquals(-1, ridget.getSortedColumn()); assertFalse(ridget.isSortedAscending()); final Event e = new Event(); e.type = SWT.Selection; e.widget = table.getColumn(0); e.widget.notifyListeners(SWT.Selection, e); assertEquals(0, ridget.getSortedColumn()); assertTrue(ridget.isSortedAscending()); e.widget.notifyListeners(SWT.Selection, e); assertEquals(0, ridget.getSortedColumn()); assertFalse(ridget.isSortedAscending()); e.widget.notifyListeners(SWT.Selection, e); assertEquals(-1, ridget.getSortedColumn()); assertFalse(ridget.isSortedAscending()); e.widget.notifyListeners(SWT.Selection, e); assertEquals(0, ridget.getSortedColumn()); assertTrue(ridget.isSortedAscending()); }
@Override public void testAddClickListener() { final ITableRidget ridget = getRidget(); ridget.updateFromModel(); final Grid control = getWidget(); final RowData rowData = new RowData(); rowData.width = 300; rowData.height = 300; control.setLayoutData(rowData); control.getColumn(0).setWidth(100); control.getColumn(1).setWidth(100); control.getShell().pack(); control.getParent().update(); try { ridget.addClickListener(null); fail(); } catch (final RuntimeException npe) { ok(); } final FTClickListener listener1 = new FTClickListener(); ridget.addClickListener(listener1); final FTClickListener listener2 = new FTClickListener(); ridget.addClickListener(listener2); ridget.addClickListener(listener2); final Event mdEvent = new Event(); mdEvent.widget = control; mdEvent.type = SWT.MouseDown; mdEvent.button = 2; final GridItem row2 = control.getItem(2); mdEvent.item = row2; mdEvent.x = row2.getBounds(0).x + 1; mdEvent.y = row2.getBounds(0).y + 1; control.notifyListeners(SWT.MouseDown, mdEvent); assertEquals(0, listener1.getCount()); assertEquals(0, listener2.getCount()); mdEvent.type = SWT.MouseUp; control.notifyListeners(SWT.MouseUp, mdEvent); assertEquals(1, listener1.getCount()); assertEquals(1, listener2.getCount()); final ClickEvent event = listener2.getEvent(); assertEquals(ridget, event.getSource()); assertEquals(2, event.getButton()); assertEquals(0, event.getColumnIndex()); assertEquals(row2.getData(), event.getRow()); ridget.removeClickListener(listener1); mdEvent.type = SWT.MouseDown; control.notifyListeners(SWT.MouseDown, mdEvent); mdEvent.type = SWT.MouseUp; control.notifyListeners(SWT.MouseUp, mdEvent); assertEquals(1, listener1.getCount()); }