/* gibt die Nummer einer Modellspalte zuruck */ protected int getColumnForResize(int x, int y) { if (m_Model == null) return -1; if ((y <= 0) || (y >= m_Model.getFirstRowHeight() + (m_Model.getFixedRowCount() - 1) * m_Model.getRowHeight())) return -1; if (x < getFixedWidth() + 3) { for (int i = 0; i < m_Model.getFixedColumnCount(); i++) if (Math.abs(x - getColumnRight(i)) < 3) { if (m_Model.isColumnResizable(i)) return i; return -1; } } for (int i = m_LeftColumn; i < m_Model.getColumnCount(); i++) { int left = getColumnLeft(i); int right = left + m_Model.getColumnWidth(i); if (Math.abs(x - right) < 3) { if (m_Model.isColumnResizable(i)) return i; return -1; } if ((x >= left + 3) && (x <= right - 3)) break; } return -1; }
protected void onKeyDown(KeyEvent e) { boolean focusChanged = false; int newFocusRow = m_FocusRow; int newFocusCol = m_FocusCol; if (m_Model == null) return; if ((e.character == ' ') || (e.character == '\r')) { openEditorInFocus(); return; } else if (e.keyCode == SWT.HOME) { newFocusCol = m_Model.getFixedColumnCount(); if (newFocusRow == -1) newFocusRow = m_Model.getFixedRowCount(); focusChanged = true; } else if (e.keyCode == SWT.END) { newFocusCol = m_Model.getColumnCount() - 1; if (newFocusRow == -1) newFocusRow = m_Model.getFixedRowCount(); focusChanged = true; } else if (e.keyCode == SWT.ARROW_LEFT) { if (!m_RowSelectionMode) { if (newFocusCol > m_Model.getFixedColumnCount()) newFocusCol--; } focusChanged = true; } else if (e.keyCode == SWT.ARROW_RIGHT) { if (!m_RowSelectionMode) { if (newFocusCol == -1) { newFocusCol = m_Model.getFixedColumnCount(); newFocusRow = m_Model.getFixedRowCount(); } else if (newFocusCol < m_Model.getColumnCount() - 1) newFocusCol++; } focusChanged = true; } else if (e.keyCode == SWT.ARROW_DOWN) { if (newFocusRow == -1) { newFocusRow = m_Model.getFixedRowCount(); newFocusCol = m_Model.getFixedColumnCount(); } else if (newFocusRow < m_Model.getRowCount() - 1) newFocusRow++; focusChanged = true; } else if (e.keyCode == SWT.ARROW_UP) { if (newFocusRow > m_Model.getFixedRowCount()) newFocusRow--; focusChanged = true; } else if (e.keyCode == SWT.PAGE_DOWN) { newFocusRow += m_RowsVisible - 1; if (newFocusRow >= m_Model.getRowCount()) newFocusRow = m_Model.getRowCount() - 1; if (newFocusCol == -1) newFocusCol = m_Model.getFixedColumnCount(); focusChanged = true; } else if (e.keyCode == SWT.PAGE_UP) { newFocusRow -= m_RowsVisible - 1; if (newFocusRow < m_Model.getFixedRowCount()) newFocusRow = m_Model.getFixedRowCount(); if (newFocusCol == -1) newFocusCol = m_Model.getFixedColumnCount(); focusChanged = true; } if (focusChanged) { focusCell(newFocusCol, newFocusRow, e.stateMask); if (!isCellFullyVisible(m_FocusCol, m_FocusRow)) scrollToFocus(); } }
/** * Selects the given cell. If scroll is true, it scrolls to show this cell if neccessary. In Row * Selection Mode, the given row is selected and a scroll to the given column is done. Does * nothing if the cell does not exist. * * @param col * @param row * @param scroll */ public void setSelection(int col, int row, boolean scroll) { if (col < m_Model.getColumnCount() && col >= m_Model.getFixedColumnCount() && row < m_Model.getRowCount() && row >= m_Model.getFixedRowCount()) { focusCell(col, row, 0); if (scroll) { scrollToFocus(); } } }
protected void onPaint(PaintEvent event) { Rectangle rect = getClientArea(); GC gc = event.gc; doCalculations(); if (m_Model != null) { drawBottomSpace(gc); drawCells( gc, gc.getClipping(), 0, m_Model.getFixedColumnCount(), 0, m_Model.getFixedRowCount()); drawCells( gc, gc.getClipping(), m_LeftColumn, m_Model.getColumnCount(), 0, m_Model.getFixedRowCount()); drawCells( gc, gc.getClipping(), 0, m_Model.getFixedColumnCount(), m_TopRow, m_TopRow + m_RowsVisible); drawCells( gc, gc.getClipping(), m_LeftColumn, m_Model.getColumnCount(), m_TopRow, m_TopRow + m_RowsVisible); } else { gc.fillRectangle(rect); } }
/** * Returns the area that is occupied by the given cell * * @param col * @param row * @return Rectangle */ public Rectangle getCellRect(int col, int row) { int m_HeaderHeight = m_Model.getFirstRowHeight(); if ((col < 0) || (col >= m_Model.getColumnCount())) return new Rectangle(-1, -1, 0, 0); int x = getColumnLeft(col) + 1; int y; if (row == 0) y = 0; else if (row < m_Model.getFixedRowCount()) y = m_HeaderHeight + ((row - 1) * m_Model.getRowHeight()); else y = m_HeaderHeight + (m_Model.getFixedRowCount() - 1 + row - m_TopRow) * m_Model.getRowHeight(); int width = m_Model.getColumnWidth(col) - 1; int height = m_Model.getRowHeight() - 1; if (row == 0) height = m_Model.getFirstRowHeight() - 1; return new Rectangle(x, y, width, height); }
/** * Returns the number of the column that is present at position x or -1, if out of area. * * @param y * @return int */ public int calcColumnNum(int x) { if (m_Model == null) return -1; int col = 0; int z = 0; for (int i = 0; i < m_Model.getFixedColumnCount(); i++) { if ((x >= z) && (x <= z + m_Model.getColumnWidth(i))) { return i; } z += m_Model.getColumnWidth(i); } col = -1; z = getFixedWidth(); for (int i = m_LeftColumn; i < m_Model.getColumnCount(); i++) { if ((x >= z) && (x <= z + m_Model.getColumnWidth(i))) { col = i; break; } z += m_Model.getColumnWidth(i); } return col; }
/** * Resizes the given column to its optimal width. * * <p>Is also called if user doubleclicks in the resize area of a resizable column. * * <p>The optimal width is determined by asking the CellRenderers for the visible cells of the * column for the optimal with and taking the minimum of the results. Note that the optimal width * is only determined for the visible area of the table because otherwise this could take very * long time. * * @param column The column to resize * @return int The optimal with that was determined or -1, if column out of range. */ public int resizeColumnOptimal(int column) { if (column >= 0 && column < m_Model.getColumnCount()) { int optWidth = 5; for (int i = 0; i < m_Model.getFixedRowCount(); i++) { int width = m_Model .getCellRenderer(column, i) .getOptimalWidth(m_GC, column, i, m_Model.getContentAt(column, i), true); if (width > optWidth) optWidth = width; } for (int i = m_TopRow; i < m_TopRow + m_RowsVisible; i++) { int width = m_Model .getCellRenderer(column, i) .getOptimalWidth(m_GC, column, i, m_Model.getContentAt(column, i), true); if (width > optWidth) optWidth = width; } m_Model.setColumnWidth(column, optWidth); redraw(); return optWidth; } return -1; }
/* * Focusses the given Cell. Assumes that the given cell is in the viewable * area. Does all neccessary redraws. */ protected void focusCell(int col, int row, int stateMask) { GC gc = new GC(this); // close cell editor if active if (m_CellEditor != null) m_CellEditor.close(true); /* * Special rule: in row selection mode the selection if a fixed cell in * a non-fixed row is allowed and handled as a selection of a non-fixed * cell. */ if (row >= m_Model.getFixedRowCount() && (col >= m_Model.getFixedColumnCount() || m_RowSelectionMode)) { if ((stateMask & SWT.CTRL) == 0 && (stateMask & SWT.SHIFT) == 0) { // case: no modifier key boolean redrawAll = (m_Selection.size() > 1); int oldFocusRow = m_FocusRow; int oldFocusCol = m_FocusCol; clearSelectionWithoutRedraw(); addToSelection(col, row); m_FocusRow = row; m_FocusCol = col; if (redrawAll) redraw(); else if (m_RowSelectionMode) { if (isRowVisible(oldFocusRow)) drawRow(gc, oldFocusRow); if (isRowVisible(m_FocusRow)) drawRow(gc, m_FocusRow); } else { if (isCellVisible(oldFocusCol, oldFocusRow)) drawCell(gc, oldFocusCol, oldFocusRow); if (isCellVisible(m_FocusCol, m_FocusRow)) drawCell(gc, m_FocusCol, m_FocusRow); } } else if ((stateMask & SWT.CTRL) != 0) { // case: CTRL key pressed if (toggleSelection(col, row)) { m_FocusCol = col; m_FocusRow = row; } if (m_RowSelectionMode) { drawRow(gc, row); } else { drawCell(gc, col, row); } } else if ((stateMask & SWT.SHIFT) != 0) { // case: SHIFT key pressed if (m_RowSelectionMode) { if (row < m_FocusRow) { // backword selection while (row != m_FocusRow) { addToSelection(0, --m_FocusRow); } } else { // foreward selection while (row != m_FocusRow) { addToSelection(0, ++m_FocusRow); } } } else // cell selection mode { if (row < m_FocusRow || (row == m_FocusRow && col < m_FocusCol)) { // backword selection while (row != m_FocusRow || col != m_FocusCol) { m_FocusCol--; if (m_FocusCol < m_Model.getFixedColumnCount()) { m_FocusCol = m_Model.getColumnCount(); m_FocusRow--; } addToSelection(m_FocusCol, m_FocusRow); } } else { // foreward selection while (row != m_FocusRow || col != m_FocusCol) { m_FocusCol++; if (m_FocusCol == m_Model.getColumnCount()) { m_FocusCol = m_Model.getFixedColumnCount(); m_FocusRow++; } addToSelection(m_FocusCol, m_FocusRow); } } } redraw(); } // notify non-fixed cell listeners fireCellSelection(col, row, stateMask); } else { // a fixed cell was focused drawCell(gc, col, row); // notify fixed cell listeners fireFixedCellSelection(col, row, stateMask); } gc.dispose(); }
protected void drawRow(GC gc, int row) { drawCells(gc, getClientArea(), 0, m_Model.getFixedColumnCount(), row, row + 1); drawCells(gc, getClientArea(), m_LeftColumn, m_Model.getColumnCount(), row, row + 1); }
protected void doCalculations() { if (m_Model == null) { ScrollBar sb = getHorizontalBar(); if (sb != null) { sb.setMinimum(0); sb.setMaximum(1); sb.setPageIncrement(1); sb.setThumb(1); sb.setSelection(1); } sb = getVerticalBar(); if (sb != null) { sb.setMinimum(0); sb.setMaximum(1); sb.setPageIncrement(1); sb.setThumb(1); sb.setSelection(1); } return; } int m_HeaderHeight = m_Model.getFirstRowHeight(); int m_RowHeight = m_Model.getRowHeight(); Rectangle rect = getClientArea(); if (m_LeftColumn < m_Model.getFixedColumnCount()) { m_LeftColumn = m_Model.getFixedColumnCount(); } if (m_TopRow < m_Model.getFixedRowCount()) { m_TopRow = m_Model.getFixedRowCount(); } int fixedWidth = getFixedWidth(); int fixedHeight = m_HeaderHeight + (m_Model.getFixedRowCount() - 1) * m_Model.getRowHeight(); m_ColumnsVisible = 0; m_ColumnsFullyVisible = 0; if (m_Model.getColumnCount() > m_Model.getFixedColumnCount()) { int runningWidth = getColumnLeft(m_LeftColumn); for (int col = m_LeftColumn; col < m_Model.getColumnCount(); col++) { if (runningWidth < rect.width + rect.x) m_ColumnsVisible++; runningWidth += m_Model.getColumnWidth(col); if (runningWidth < rect.width + rect.x) m_ColumnsFullyVisible++; else break; } } ScrollBar sb = getHorizontalBar(); if (sb != null) { if (m_Model.getColumnCount() <= m_Model.getFixedColumnCount()) { sb.setMinimum(0); sb.setMaximum(1); sb.setPageIncrement(1); sb.setThumb(1); sb.setSelection(1); } else { sb.setMinimum(m_Model.getFixedColumnCount()); sb.setMaximum(m_Model.getColumnCount()); sb.setIncrement(1); sb.setPageIncrement(2); sb.setThumb(m_ColumnsFullyVisible); sb.setSelection(m_LeftColumn); } } m_RowsFullyVisible = Math.max(0, (rect.height - fixedHeight) / m_RowHeight); m_RowsFullyVisible = Math.min(m_RowsFullyVisible, m_Model.getRowCount() - m_Model.getFixedRowCount()); m_RowsFullyVisible = Math.max(0, m_RowsFullyVisible); m_RowsVisible = m_RowsFullyVisible + 1; if (m_TopRow + m_RowsFullyVisible > m_Model.getRowCount()) { m_TopRow = Math.max(m_Model.getFixedRowCount(), m_Model.getRowCount() - m_RowsFullyVisible); } if (m_TopRow + m_RowsFullyVisible >= m_Model.getRowCount()) { m_RowsVisible--; } sb = getVerticalBar(); if (sb != null) { if (m_Model.getRowCount() <= m_Model.getFixedRowCount()) { sb.setMinimum(0); sb.setMaximum(1); sb.setPageIncrement(1); sb.setThumb(1); sb.setSelection(1); } else { sb.setMinimum(m_Model.getFixedRowCount()); sb.setMaximum(m_Model.getRowCount()); sb.setPageIncrement(m_RowsVisible); sb.setIncrement(1); sb.setThumb(m_RowsFullyVisible); sb.setSelection(m_TopRow); } } }
protected int getLastColumnRight() { return getColumnRight(m_Model.getColumnCount() - 1); }