examplegui.java
来自「java的swt图形程序」· Java 代码 · 共 2,141 行 · 第 1/5 页
JAVA
2,141 行
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();
}
}
protected void onMouseDoubleClick(MouseEvent e) {
if (m_Model == null)
return;
if (e.button == 1) {
if (e.y < m_Model.getFirstRowHeight()
+ ((m_Model.getFixedRowCount() - 1) * m_Model
.getRowHeight())) {
// double click in header area
int columnIndex = getColumnForResize(e.x, e.y);
resizeColumnOptimal(columnIndex);
return;
} else
openEditorInFocus();
}
}
/**
* Resizes the given column to its optimal width.
*
* Is also called if user doubleclicks in the resize area of a resizable
* column.
*
* 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;
}
/**
* This method activated the cell editor on the current focus cell, if the
* table model allows cell editing for this cell.
*/
public void openEditorInFocus() {
m_CellEditor = m_Model.getCellEditor(m_FocusCol, m_FocusRow);
if (m_CellEditor != null) {
Rectangle r = getCellRect(m_FocusCol, m_FocusRow);
m_CellEditor.open(this, m_FocusCol, m_FocusRow, r);
}
}
/*
* Tries to open KTableCellEditor on the given cell. If the cell exists, the
* model is asked for an editor and if there is one, the table scrolls the
* cell into the view and openes the editor on the cell. @param col @param
* row
*
* public void tryToOpenEditorAt(int col, int row) { if (col >= 0 && col <
* m_Model.getColumnCount() && row >= 0 && row < m_Model.getRowCount()) {
* m_CellEditor = m_Model.getCellEditor(col, row); if (m_CellEditor != null) {
* m_FocusCol = col; m_FocusRow = row; scrollToFocus(); Rectangle r =
* getCellRect(col, row); m_CellEditor.open(this, m_FocusCol, m_FocusRow,
* r); } } }
*/
protected void scrollToFocus() {
boolean change = false;
// vertical scroll allowed?
if (getVerticalBar() != null) {
if (m_FocusRow < m_TopRow) {
m_TopRow = m_FocusRow;
change = true;
}
if (m_FocusRow >= m_TopRow + m_RowsFullyVisible) {
m_TopRow = m_FocusRow - m_RowsFullyVisible + 1;
change = true;
}
}
// horizontal scroll allowed?
if (getHorizontalBar() != null) {
if (m_FocusCol < m_LeftColumn) {
m_LeftColumn = m_FocusCol;
change = true;
}
if (m_FocusCol >= m_LeftColumn + m_ColumnsFullyVisible) {
int oldLeftCol = m_LeftColumn;
Rectangle rect = getClientArea();
while (m_LeftColumn < m_FocusCol
&& getColumnRight(m_FocusCol) > rect.width + rect.x) {
m_LeftColumn++;
}
change = (oldLeftCol != m_LeftColumn);
}
}
if (change)
redraw();
}
protected void fireCellSelection(int col, int row, int statemask) {
for (int i = 0; i < cellSelectionListeners.size(); i++) {
((KTableCellSelectionListener) cellSelectionListeners.get(i))
.cellSelected(col, row, statemask);
}
}
protected void fireFixedCellSelection(int col, int row, int statemask) {
for (int i = 0; i < cellSelectionListeners.size(); i++) {
((KTableCellSelectionListener) cellSelectionListeners.get(i))
.fixedCellSelected(col, row, statemask);
}
}
protected void fireColumnResize(int col, int newSize) {
for (int i = 0; i < cellResizeListeners.size(); i++) {
((KTableCellResizeListener) cellResizeListeners.get(i))
.columnResized(col, newSize);
}
}
protected void fireRowResize(int newSize) {
for (int i = 0; i < cellResizeListeners.size(); i++) {
((KTableCellResizeListener) cellResizeListeners.get(i))
.rowResized(newSize);
}
}
/**
* Adds a listener that is notified when a cell is selected.
*
* This can happen either by a click on the cell or by arrow keys. Note that
* the listener is not called for each cell that the user selects in one
* action using Shift+Click. To get all these cells use the listener and
* getCellSelecion() or getRowSelection().
*
* @param listener
*/
public void addCellSelectionListener(KTableCellSelectionListener listener) {
cellSelectionListeners.add(listener);
}
/**
* Adds a listener that is notified when a cell is resized. This happens
* when the mouse button is released after a resizing.
*
* @param listener
*/
public void addCellResizeListener(KTableCellResizeListener listener) {
cellResizeListeners.add(listener);
}
/**
* Removes the listener if present. Returns true, if found and removed from
* the list of listeners.
*/
public boolean removeCellSelectionListener(
KTableCellSelectionListener listener) {
return cellSelectionListeners.remove(listener);
}
/**
* Removes the listener if present. Returns true, if found and removed from
* the list of listeners.
*/
public boolean removeCellResizeListener(KTableCellResizeListener listener) {
return cellResizeListeners.remove(listener);
}
// ////////////////////////////////////////////////////////////////////////////
// SELECTION
// ////////////////////////////////////////////////////////////////////////////
/**
* Sets the "Row Selection Mode". The current selection is cleared when this
* method is called.
*
* @param rowSelectMode
* In the "Row Selection Mode", the table always selects a
* complete row. Otherwise, each individual cell can be selected.
*
* This mode can be combined with the "Multi Selection Mode".
*
*/
public void setRowSelectionMode(boolean rowSelectMode) {
m_RowSelectionMode = rowSelectMode;
clearSelection();
}
/**
* Sets the "Multi Selection Mode". The current selection is cleared when
* this method is called.
*
* @param multiSelectMode
* In the "Multi Select Mode", more than one cell or row can be
* selected. The user can achieve this by shift-click and
* ctrl-click. The selected cells/rows can be scattored ofer the
* complete table. If you pass false, only a single cell or row
* can be selected.
*
* This mode can be combined with the "Row Selection Mode".
*/
public void setMultiSelectionMode(boolean multiSelectMode) {
m_MultiSelectMode = multiSelectMode;
clearSelection();
}
/**
* Returns true if in "Row Selection Mode".
*
* @see setSelectionMode
* @return boolean
*/
public boolean isRowSelectMode() {
return m_RowSelectionMode;
}
/**
* Returns true if in "Multi Selection Mode".
*
* @see setSelectionMode
* @return boolean
*/
public boolean isMultiSelectMode() {
return m_MultiSelectMode;
}
protected void clearSelectionWithoutRedraw() {
m_Selection.clear();
}
/**
* Clears the current selection (in all selection modes).
*/
public void clearSelection() {
/*
* if (m_MultiSelectMode) { if (m_Selection.size() < m_RowsFullyVisible *
* m_ColumnsVisible) { if (m_RowSelectionMode) { for (Iterator iter =
* m_Selection.values().iterator(); iter.hasNext();) { int row =
* ((Integer) iter.next()).intValue(); if (row >= m_TopRow && row <
* m_TopRow+m_RowsFullyVisible) { } } } else { for (Iterator iter =
* m_Selection.values().iterator(); iter.hasNext();) { Point element =
* (Point) iter.next(); } } } }
*/
clearSelectionWithoutRedraw();
m_FocusCol = -1;
m_FocusRow = -1;
if (m_MultiSelectMode)
redraw();
}
/*
* works in both modes: Cell and Row Selection. Has no redraw functionality!
*
* Returns true, if added to selection.
*/
protected boolean toggleSelection(int col, int row) {
if (m_MultiSelectMode) {
Object o;
if (m_RowSelectionMode) {
o = new Integer(row);
} else {
o = new Point(col, row);
}
if (m_Selection.get(o) != null) {
m_Selection.remove(o);
return false;
} else {
m_Selection.put(o, o);
return true;
}
}
return false;
}
/*
* works in both modes: Cell and Row Selection. Has no redraw functionality!
*/
protected void addToSelection(int col, int row) {
if (m_MultiSelectMode) {
if (m_RowSelectionMode) {
Integer o = new Integer(row);
m_Selection.put(o, o);
} else {
Point o = new Point(col, row);
m_Selection.put(o, o);
}
}
// System.out.println(m_Selection.size()+" "+col+"/"+row);
}
/**
* 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();
}
}
}
/**
* Returns true, if the given cell is selected. Works also in Row Selection
* Mode.
*
* @param col
* @param row
* @return boolean
*/
public boolean isCellSelected(int col, int row) {
if (!m_MultiSelectMode) {
if (m_RowSelectionMode)
return (row == m_FocusRow);
return (col == m_FocusCol && row == m_FocusRow);
}
if (m_RowSelectionMode)
return (m_Selection.get(new Integer(row)) != null);
else
return (m_Selection.get(new Point(col, row)) != null);
}
/**
* Returns true, if the given row is selected. Returns always false if not
* in Row Selection Mode!
*
* @param row
* @return boolean
*/
public boolean isRowSelected(int row) {
return (m_Selection.get(new Integer(row)) != null);
}
/**
* Returns an array of the selected row numbers. Returns null if not in Row
* Selection Mode. Returns an array with one or none element if not in Multi
* Selection Mode.
*
* @return int[]
*/
public int[] getRowSelection() {
if (!m_RowSelectionMode)
return null;
if (!m_MultiSelectMode) {
if (m_FocusRow < 0)
return new int[0];
int[] tmp = new int[1];
tmp[0] = m_FocusRow;
return tmp;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?