public void actionPerformed(ActionEvent e) { JGrid grid = (JGrid) e.getSource(); SelectionModel sm = grid.getSelectionModel(); anchorRow = sm.getAnchorRow(); leadRow = sm.getLeadRow(); anchorColumn = sm.getAnchorColumn(); leadColumn = sm.getLeadColumn(); int oldAnchorRow = anchorRow; int oldAnchorColumn = anchorColumn; if (grid.isEditing() && !grid.getCurrentCellEditor().stopCellEditing()) { return; } if (!inSelection) { moveWithinGridRange(grid, dx, dy, extend); if (!extend) { grid.changeSelection(anchorRow, anchorColumn, false, extend); } else { grid.changeSelection(leadRow, leadColumn, false, extend); } } else { if (moveWithinSelectedRange(grid, dx, dy, false)) { grid.changeSelection(anchorRow, anchorColumn, true, true); } else { grid.changeSelection(anchorRow, anchorColumn, false, false); } } }
private boolean moveWithinSelectedRange(JGrid grid, int dx, int dy, boolean ignoreCarry) { SelectionModel sm = grid.getSelectionModel(); int newAnchorRow = anchorRow + dy; int newAnchorColumn = anchorColumn + dx; int rowSgn; int colSgn; int rowCount = selectionSpan(sm, SwingConstants.VERTICAL); int columnCount = selectionSpan(sm, SwingConstants.HORIZONTAL); boolean canStayInSelection = (rowCount * columnCount > 1); if (canStayInSelection) { rowSgn = compare(newAnchorRow, sm, SwingConstants.VERTICAL); colSgn = compare(newAnchorColumn, sm, SwingConstants.HORIZONTAL); } else { // If there is only one selected cell, there is no point // in trying to stay within the selected area. Move outside // the selection, wrapping at the table boundaries. rowCount = grid.getRowCount(); columnCount = grid.getColumnCount(); rowSgn = compare(newAnchorRow, 0, rowCount); colSgn = compare(newAnchorColumn, 0, columnCount); } anchorRow = newAnchorRow - rowCount * rowSgn; anchorColumn = newAnchorColumn - columnCount * colSgn; if (!ignoreCarry) { return moveWithinSelectedRange(grid, rowSgn, colSgn, true); } return canStayInSelection; }
public void actionPerformed(ActionEvent e) { JGrid grid = (JGrid) e.getSource(); if (toLimit) { if (vertically) { int rowCount = grid.getRowCount(); this.dx = 0; this.dy = forwards ? rowCount : -rowCount; } else { int colCount = grid.getColumnCount(); this.dx = forwards ? colCount : -colCount; this.dy = 0; } } else { if (!(grid.getParent().getParent() instanceof JScrollPane)) { return; } Dimension delta = grid.getParent().getSize(); SelectionModel sm = grid.getSelectionModel(); int start = 0; if (vertically) { start = (extend) ? sm.getLeadRow() : sm.getAnchorRow(); } else { start = (extend) ? sm.getLeadColumn() : sm.getAnchorColumn(); } if (vertically) { Rectangle r = grid.getCellBounds(start, 0); r.y += forwards ? delta.height : -delta.height; this.dx = 0; int newRow = grid.rowAtPoint(r.getLocation()); if (newRow == -1 && forwards) { newRow = grid.getRowCount(); } this.dy = newRow - start; } else { Rectangle r = grid.getCellBounds(0, start); r.x += forwards ? delta.width : -delta.width; int newColumn = grid.columnAtPoint(r.getLocation()); if (newColumn == -1 && forwards) { newColumn = grid.getColumnCount(); } this.dx = newColumn - start; this.dy = 0; } } super.actionPerformed(e); }
public void actionPerformed(ActionEvent e) { JGrid grid = (JGrid) e.getSource(); if (!grid.hasFocus()) { CellEditor cellEditor = grid.getCurrentCellEditor(); if (cellEditor != null && !cellEditor.stopCellEditing()) { return; } grid.requestFocus(); } SelectionModel selectionModel = grid.getSelectionModel(); int anchorRow = selectionModel.getAnchorRow(); int anchorColumn = selectionModel.getAnchorColumn(); grid.editCellAt(anchorRow, anchorColumn, null); Component editorComp = grid.getEditorComponent(); if (editorComp != null) { editorComp.requestFocus(); } }