private static void selectionRegionChanged( JGrid grid, SelectionRegion selectionRegion, boolean add) { if (!add) { grid.getSelectionModel().clearSelection(); } List<Cell> selectedCells = new ArrayList<Cell>(); for (int i = selectionRegion.getFirstRow(); i <= selectionRegion.getLastRow(); i++) { for (int j = selectionRegion.getFirstColumn(); j <= selectionRegion.getLastColumn(); j++) { boolean isSpan = grid.getSpanModel().isCellSpan(i, j); // if (!isSpan) { Cell cell = new Cell(i, j); // System.out.println("cell=" + cell); selectedCells.add(cell); // } else { // CellSpan cellSpan = grid.getSpanModel().getSpanOver(i, j); // if ((cellSpan.getRow() == i) && (cellSpan.getGroupColumn() == j)) // { // Cell cell = new Cell(i, j); // System.out.println("cell=" + cell); // selectedCells.add(cell); // } // } } } grid.getSelectionModel().addSelectionCells(selectedCells); }
private void adjustFocusAndSelection(MouseEvent event) { if (shouldIgnore(event)) { return; } Point point = event.getPoint(); int row = grid.rowAtPoint(point); int column = grid.columnAtPoint(point); // The autoscroller can generate drag events outside range. if ((column == -1) || (row == -1)) { System.err.println("Out of bounds"); return; } if (grid.editCellAt(row, column, event)) { setDispatchComponent(event); repostEvent(event); } else { grid.requestFocus(); } // System.out.println("^^^^^ "+selectionRegion); if (selectionRegion == null) { selectionRegion = new SelectionRegion(); selectionRegion.setFirstRow(row); selectionRegion.setFirstColumn(column); selectionRegion.setLastRow(row); selectionRegion.setLastColumn(column); } else { // for shift pressed if (row > selectionRegion.getFirstRow()) { selectionRegion.setLastRow(row); } if (row < selectionRegion.getFirstRow()) { selectionRegion.setLastRow(selectionRegion.getFirstRow()); selectionRegion.setFirstRow(row); } if (row == selectionRegion.getFirstRow()) { selectionRegion.setLastRow(row); } if (column > selectionRegion.getFirstColumn()) { selectionRegion.setLastColumn(column); } if (column < selectionRegion.getFirstColumn()) { selectionRegion.setLastColumn(selectionRegion.getFirstColumn()); selectionRegion.setFirstColumn(column); } if (column == selectionRegion.getFirstColumn()) { selectionRegion.setLastColumn(column); } } // System.out.println(">>>> " + selectionRegion); Iterator<CellSpan> it = grid.getSpanModel().getSpanIterator(); while (it.hasNext()) { CellSpan span = it.next(); if (span.getLastRow() >= selectionRegion.getFirstRow() && span.getFirstRow() <= selectionRegion.getLastRow() && span.getLastColumn() >= selectionRegion.getFirstColumn() && span.getFirstColumn() <= selectionRegion.getLastColumn()) { selectionRegion.setFirstRow(Math.min(selectionRegion.getFirstRow(), span.getFirstRow())); selectionRegion.setLastRow(Math.max(selectionRegion.getLastRow(), span.getLastRow())); selectionRegion.setFirstColumn( Math.min(selectionRegion.getFirstColumn(), span.getFirstColumn())); selectionRegion.setLastColumn( Math.max(selectionRegion.getLastColumn(), span.getLastColumn())); } } GridCellEditor editor = grid.getCurrentCellEditor(); if ((editor == null) || editor.shouldSelectCell(event)) { // Update selection model setValueIsAdjusting(true); if (event.isControlDown()) { if (grid.getSelectionModel().isSelected(row, column)) { Cell cell = new Cell(row, column); boolean isSpan = grid.getSpanModel().isCellSpan(row, column); if (isSpan) { // System.out.println("--isSpan"); CellSpan cellSpan = grid.getSpanModel().getSpanOver(row, column); for (int i = cellSpan.getFirstRow(); i <= cellSpan.getLastRow(); i++) { for (int j = cellSpan.getFirstColumn(); j <= cellSpan.getLastColumn(); j++) { grid.getSelectionModel().removeSelectionCell(new Cell(i, j)); } } } else { grid.getSelectionModel().removeSelectionCell(cell); } } else { selectionRegionChanged(grid, selectionRegion, true); } } else { selectionRegionChanged(grid, selectionRegion, false); } } }