/* 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 int getColumnLeft(int index) { if (index < m_Model.getFixedColumnCount()) { int x = 0; for (int i = 0; i < index; i++) { x += m_Model.getColumnWidth(i); } return x; } if (index < m_LeftColumn) return -1; int x = getFixedWidth(); for (int i = m_LeftColumn; i < index; i++) { x += m_Model.getColumnWidth(i); } return x; }
protected void onMouseUp(MouseEvent e) { // if (e.button == 1) { if (m_Model == null) return; setCapture(false); m_Capture = false; if (m_ResizeColumnIndex != -1) { fireColumnResize(m_ResizeColumnIndex, m_Model.getColumnWidth(m_ResizeColumnIndex)); m_ResizeColumnIndex = -1; redraw(); } if (m_ResizeRowIndex != -1) { m_ResizeRowIndex = -1; m_Model.setRowHeight(m_NewRowSize); m_LineRestore = null; fireRowResize(m_NewRowSize); redraw(); } if (m_ClickColumnIndex != -1) { int col = m_ClickColumnIndex; int row = m_ClickRowIndex; m_ClickColumnIndex = -1; m_ClickRowIndex = -1; if (m_CellEditor == null) { drawCell(new GC(this), col, row); } } } }
/** * 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; }
/** * 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); }
protected void onMouseMove(MouseEvent e) { if (m_Model == null) return; // show resize cursor? if ((m_ResizeColumnIndex != -1) || (getColumnForResize(e.x, e.y) >= 0)) setCursor(new Cursor(m_Display, SWT.CURSOR_SIZEWE)); else if ((m_ResizeRowIndex != -1) || (getRowForResize(e.x, e.y) >= 0)) setCursor(new Cursor(m_Display, SWT.CURSOR_SIZENS)); else setCursor(null); if (e.button == 1) { // extend selection? if (m_ClickColumnIndex != -1 && m_MultiSelectMode) { int row = calcRowNum(e.y); int col = calcColumnNum(e.x); if (row >= m_Model.getFixedRowCount() && col >= m_Model.getFixedColumnCount()) { m_ClickColumnIndex = col; m_ClickRowIndex = row; focusCell(col, row, (e.stateMask | SWT.SHIFT)); } } } // column resize? if (m_ResizeColumnIndex != -1) { Rectangle rect = getClientArea(); int oldSize = m_Model.getColumnWidth(m_ResizeColumnIndex); if (e.x > rect.x + rect.width - 1) e.x = rect.x + rect.width - 1; int newSize = e.x - m_ResizeColumnLeft; if (newSize < 5) newSize = 5; int leftX = getColumnLeft(m_ResizeColumnIndex); int rightX = getColumnRight(m_ResizeColumnIndex); m_Model.setColumnWidth(m_ResizeColumnIndex, newSize); newSize = m_Model.getColumnWidth(m_ResizeColumnIndex); GC gc = new GC(this); gc.copyArea(rightX, 0, rect.width - rightX, rect.height, leftX + newSize, 0); drawCol(gc, m_ResizeColumnIndex); if (newSize < oldSize) { int delta = oldSize - newSize; redraw(rect.width - delta, 0, delta, rect.height, false); } gc.dispose(); } // row resize? if (m_ResizeRowIndex != -1) { Rectangle rect = getClientArea(); GC gc = new GC(this); // calculate new size if (e.y > rect.y + rect.height - 1) e.y = rect.y + rect.height - 1; m_NewRowSize = e.y - m_ResizeRowTop; if (m_NewRowSize < m_Model.getRowHeightMinimum()) m_NewRowSize = m_Model.getRowHeightMinimum(); // restore old line area if (m_LineRestore != null) { gc.drawImage(m_LineRestore, m_LineX, m_LineY); } // safe old picture and draw line gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK)); int lineEnd = getColumnRight(m_LeftColumn + m_ColumnsVisible - 1); m_LineRestore = new Image(m_Display, lineEnd, 1); m_LineX = rect.x + 1; m_LineY = m_ResizeRowTop + m_NewRowSize - 1; gc.copyArea(m_LineRestore, m_LineX, m_LineY); gc.drawLine(m_LineX, m_LineY, rect.x + lineEnd, m_LineY); gc.dispose(); } }
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 getColumnRight(int index) { if (index < 0) return 0; return getColumnLeft(index) + m_Model.getColumnWidth(index); }
protected int getFixedWidth() { int width = 0; for (int i = 0; i < m_Model.getFixedColumnCount(); i++) width += m_Model.getColumnWidth(i); return width; }