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;
    }
 private void moveWithinGridRange(JGrid grid, int dx, int dy, boolean changeLead) {
   if (changeLead) {
     leadRow = clipToRange(leadRow + dy, 0, grid.getRowCount());
     leadColumn = clipToRange(leadColumn + dx, 0, grid.getColumnCount());
   } else {
     anchorRow = clipToRange(anchorRow + dy, 0, grid.getRowCount());
     anchorColumn = clipToRange(anchorColumn + dx, 0, grid.getColumnCount());
   }
 }
  public Dimension getMaximumSize(JComponent c) {
    int lastRow = grid.getRowCount() - 1;
    int lastCol = grid.getColumnCount() - 1;

    Rectangle cellBounds = grid.getCellBounds(lastRow, lastCol);
    int width = cellBounds.x + cellBounds.width;
    int height = cellBounds.y + cellBounds.height;

    return new Dimension(width, height);
  }
 protected void paintEditor(Graphics g) {
   Component component = grid.getEditorComponent();
   if (component == null) {
     return;
   }
   Rectangle cellBounds = grid.getCellBounds(grid.getEditingRow(), grid.getEditingColumn());
   component.setBounds(cellBounds);
   component.validate();
   component.requestFocus();
 }
 protected void paintCells(Graphics g, int rowMin, int rowMax, int colMin, int colMax) {
   for (int row = rowMin; row <= rowMax; row++) {
     for (int column = colMin; column <= colMax; column++) {
       /* Paint cell if it is atomic */
       if (!grid.isCellSpan(row, column)) {
         Rectangle cellBounds = grid.getCellBounds(row, column);
         paintCell(g, cellBounds, row, column);
       }
     }
   }
 }
 /*
  * Borders are handled by the UI since they are shared between cells
  * and therefore must overlay after the painting of the cells
  */
 private void paintBorders(Graphics g, int rowMin, int rowMax, int colMin, int colMax) {
   // Include borders from adjacent rows/columns of the clip region
   rowMin = Math.max(0, rowMin - 1);
   rowMax = Math.min(grid.getRowCount() - 1, rowMax + 1);
   colMin = Math.max(0, colMin - 1);
   colMax = Math.min(grid.getColumnCount() - 1, colMax + 1);
   for (int row = rowMin; row <= rowMax; row++) {
     for (int column = colMin; column <= colMax; column++) {
       Rectangle cellRect = grid.getCellBounds(row, column);
       paintBorder(g, cellRect, row, column);
     }
   }
 }
  protected void paintGrid(Graphics g, int rowMin, int rowMax, int colMin, int colMax) {
    if (!grid.getShowGrid()) {
      return; // do nothing
    }

    int y1 = grid.getRowPosition(rowMin);
    int y2 = grid.getRowPosition(rowMax) + grid.getRowHeight(rowMax);
    int x1 = grid.getColumnPosition(colMin);
    int x2 = grid.getColumnPosition(colMax) + grid.getColumnWidth(colMax);

    g.setColor(grid.getGridColor());

    // Draw the horizontal lines
    for (int row = rowMin; row <= rowMax; row++) {
      int rowY = grid.getRowPosition(row);
      g.drawLine(x1, rowY, x2, rowY);
    }
    g.drawLine(x1, y2, x2, y2);

    // Draw the vertical gridlines
    for (int col = colMin; col <= colMax; col++) {
      int colX = grid.getColumnPosition(col);
      g.drawLine(colX, y1, colX, y2);
    }
    g.drawLine(x2, y1, x2, y2);
  }
    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);
        }
      }
    }
 /** Paint cell at (row, column) */
 protected void paintCell(Graphics g, Rectangle cellBounds, int row, int column) {
   if (grid.getEditingRow() == row && grid.getEditingColumn() == column) {
     return;
   }
   GridCellRenderer renderer = grid.getCellRenderer(row, column);
   Component rendererComp = grid.prepareRenderer(renderer, row, column);
   CellSpan span = grid.getSpanModel().getSpanOver(row, column);
   rendererPane.paintComponent(
       g,
       rendererComp,
       grid,
       cellBounds.x,
       cellBounds.y,
       cellBounds.width,
       cellBounds.height,
       true);
 }
  public void installUI(JComponent c) {
    grid = (JGrid) c;

    rendererPane = new CellRendererPane();
    grid.add(rendererPane);

    installDefaults();
    installListeners();
    installKeyboardActions();
  }
 private void paintBorder(Graphics g, Rectangle cellRect, int row, int column) {
   if (grid.getSpanModel().isCellSpan(row, column)) {
     CellSpan span = grid.getSpanModel().getSpanOver(row, column);
     row = span.getRow();
     column = span.getColumn();
   }
   // Paint border
   javax.swing.border.Border border = grid.getStyleModel().getCellStyle(row, column).getBorder();
   Insets borderInsets = border.getBorderInsets(grid);
   Rectangle borderRect = new Rectangle(cellRect);
   int topOffset = (borderInsets.top >> 1);
   int leftOffset = (borderInsets.left >> 1);
   int bottomOffset = (borderInsets.bottom / 3) + topOffset;
   int rightOffset = (borderInsets.right / 3) + leftOffset;
   borderRect.x -= leftOffset;
   borderRect.y -= topOffset;
   borderRect.width += rightOffset;
   borderRect.height += bottomOffset;
   border.paintBorder(grid, g, borderRect.x, borderRect.y, borderRect.width, borderRect.height);
 }
  /** special paint handler for merged cell regions */
  protected void paintSpans(Graphics g, int rowMin, int rowMax, int colMin, int colMax) {
    Rectangle clip = g.getClipBounds();
    Iterator cell = grid.getSpanModel().getSpanIterator();
    while (cell.hasNext()) {
      CellSpan span = (CellSpan) cell.next();
      Rectangle cellBounds = grid.getCellBounds(span.getRow(), span.getColumn());

      // Only paint cell if visible
      if (span.getLastRow() >= rowMin
          && span.getLastColumn() >= colMin
          && span.getFirstRow() <= rowMax
          && span.getFirstColumn() <= colMax) {
        paintCell(g, cellBounds, span.getRow(), span.getColumn());
        // Paint grid line around cell
        if (grid.getShowGrid()) {
          g.setColor(grid.getGridColor());
          g.drawRect(cellBounds.x, cellBounds.y, cellBounds.width, cellBounds.height);
        }
      }
    }
  }
 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();
   }
 }
  public void paint(Graphics g, JComponent c) {
    if (grid.getRowCount() <= 0 || grid.getColumnCount() <= 0) {
      return; // nothing to paint
    }

    Rectangle clip = g.getClipBounds();
    Point minLocation = clip.getLocation();
    Point maxLocation = new Point(clip.x + clip.width - 1, clip.y + clip.height - 1);
    int rowMin = grid.rowAtPoint(minLocation);
    int rowMax = grid.rowAtPoint(maxLocation);
    // This should never happen.
    if (rowMin == -1) {
      rowMin = 0;
    }
    // If the spread does not have enough rows to fill the view we'll get -1.
    // Replace this with the index of the last row.
    if (rowMax == -1) {
      rowMax = grid.getRowCount() - 1;
    }
    int colMin = grid.columnAtPoint(minLocation);
    int colMax = grid.columnAtPoint(maxLocation);
    // This should never happen.
    if (colMin == -1) {
      colMin = 0;
    }
    // If the spread does not have enough columns to fill the view we'll get -1.
    // Replace this with the index of the last column.
    if (colMax == -1) {
      colMax = grid.getColumnCount() - 1;
    }

    // Paint cells
    paintCells(g, rowMin, rowMax, colMin, colMax);

    // Paint grid
    paintGrid(g, rowMin, rowMax, colMin, colMax);

    // Paint spans
    paintSpans(g, rowMin, rowMax, colMin, colMax);

    // Paint borders
    paintBorders(g, rowMin, rowMax, colMin, colMax);

    // Paint editor
    paintEditor(g);
  }
    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 uninstallUI(JComponent c) {
   grid.remove(rendererPane);
 }
 /** Attaches listeners to the JGrid */
 protected void installListeners() {
   mouseInputListener = createMouseInputListener();
   grid.addMouseListener(mouseInputListener);
   grid.addMouseMotionListener(mouseInputListener);
 }
 protected void installDefaults() {
   String string = UIManager.getLookAndFeel().getName();
   Color defaultGridColor = UIManager.getColor("Table.gridColor");
   Color defaultForegroundColor = UIManager.getColor("TableHeader.foreground");
   Color defaultBackgroundColor = UIManager.getColor("TableHeader.background");
   Font defaultGridFont = UIManager.getFont("Table.font");
   Border defaultGridBorder = UIManager.getBorder("TableHeader.border");
   Color defaultSelectionForegroundColor = defaultForegroundColor.brighter();
   Color defaultSelectionBackgroundColor = defaultBackgroundColor;
   Color defaultFocusForegroundColor = defaultForegroundColor.brighter();
   Color defaultFocusBackgroundColor = defaultBackgroundColor.brighter();
   if (!installedHeader) {
     UIManager.getDefaults().put("GridHeader.gridColor", defaultGridColor);
     UIManager.getDefaults().put("GridHeader.foreground", defaultForegroundColor);
     UIManager.getDefaults().put("GridHeader.background", defaultBackgroundColor);
     UIManager.getDefaults()
         .put("GridHeader.selectionForegroundColor", defaultSelectionForegroundColor);
     UIManager.getDefaults()
         .put("GridHeader.selectionBackgroundColor", defaultSelectionBackgroundColor);
     UIManager.getDefaults().put("GridHeader.focusForegroundColor", defaultFocusForegroundColor);
     UIManager.getDefaults().put("GridHeader.focusBackgroundColor", defaultFocusBackgroundColor);
     UIManager.getDefaults().put("GridHeader.border", defaultGridBorder);
     UIManager.getDefaults().put("GridHeader.font", defaultGridFont);
   }
   Color foregroundColor = gridHeader.getForeground();
   Color backgroundColor = gridHeader.getBackground();
   Font gridFont = gridHeader.getFont();
   Border gridBorder = gridHeader.getBorder();
   Color gridColor = gridHeader.getGridColor();
   Color selectionForegroundColor = gridHeader.getSelectionForegroundColor();
   Color selectionBackgroundColor = gridHeader.getSelectionBackgroundColor();
   Color focusForegroundColor = gridHeader.getFocusForegroundColor();
   Color focusBackgroundColor = gridHeader.getFocusBackgroundColor();
   if (foregroundColor == null || foregroundColor instanceof UIResource)
     gridHeader.setForeground(defaultForegroundColor);
   if (backgroundColor == null || backgroundColor instanceof UIResource)
     gridHeader.setBackground(defaultBackgroundColor);
   if (gridColor == null || gridColor instanceof UIResource)
     gridHeader.setGridColor(defaultGridColor);
   if (gridFont == null || gridFont instanceof UIResource) gridHeader.setFont(defaultGridFont);
   if (gridBorder == null || gridBorder instanceof UIResource)
     gridHeader.setBorder(defaultGridBorder);
   if (selectionForegroundColor == null || selectionForegroundColor instanceof UIResource)
     gridHeader.setSelectionForegroundColor(defaultSelectionForegroundColor);
   if (selectionBackgroundColor == null || selectionBackgroundColor instanceof UIResource)
     gridHeader.setSelectionBackgroundColor(defaultSelectionBackgroundColor);
   if (focusForegroundColor == null || focusForegroundColor instanceof UIResource)
     gridHeader.setFocusForegroundColor(defaultFocusForegroundColor);
   if (focusBackgroundColor == null || focusBackgroundColor instanceof UIResource)
     gridHeader.setFocusBackgroundColor(defaultFocusBackgroundColor);
 }
 protected void installDefaults() {
   Color defaultGridColor = UIManager.getColor("Table.gridColor");
   Color defaultForegroundColor = UIManager.getColor("Table.foreground");
   Color defaultBackgroundColor = UIManager.getColor("Table.background");
   Border defaultBorder = UIManager.getBorder("Table.scrollPaneBorder");
   Color defaultSelectionForeground = UIManager.getColor("Table.selectionForeground");
   Color defaultSelectionBackground = UIManager.getColor("Table.selectionBackground");
   Color defaultFocusCellForeground = UIManager.getColor("Table.focusCellForeground");
   Color defaultFocusCellBackground = new Color(153, 153, 204);
   Font defaultFont = UIManager.getFont("Table.font");
   Border defaultGridBorder = UIManager.getBorder("Table.border");
   InputMap inputMap = (InputMap) UIManager.get("Table.ancestorInputMap");
   if (!installed) {
     UIManager.getDefaults().put("Grid.gridColor", defaultGridColor);
     UIManager.getDefaults().put("Grid.foreground", defaultForegroundColor);
     UIManager.getDefaults().put("Grid.background", defaultBackgroundColor);
     UIManager.getDefaults().put("Grid.selectionForegroundColor", defaultSelectionForeground);
     UIManager.getDefaults().put("Grid.selectionBackgroundColor", defaultSelectionBackground);
     UIManager.getDefaults().put("Grid.focusForegroundColor", defaultFocusCellForeground);
     UIManager.getDefaults().put("Grid.focusBackgroundColor", defaultFocusCellBackground);
     UIManager.getDefaults().put("Grid.border", defaultGridBorder);
     UIManager.getDefaults().put("Grid.font", defaultFont);
     UIManager.getDefaults().put("Grid.scrollPaneBorder", defaultBorder);
     UIManager.getDefaults().put("Grid.ancestorInputMap", inputMap);
     installed = true;
   }
   Color foregroundColor = grid.getForeground();
   Color backgroundColor = grid.getBackground();
   Font font = grid.getFont();
   Border border = grid.getBorder();
   Color gridColor = grid.getGridColor();
   Color selectionForeground = grid.getSelectionForegroundColor();
   Color selectionBackground = grid.getSelectionBackgroundColor();
   Color focusForeground = grid.getFocusForegroundColor();
   Color focusBackground = grid.getFocusBackgroundColor();
   if (foregroundColor == null || foregroundColor instanceof UIResource)
     grid.setForeground(defaultForegroundColor);
   if (backgroundColor == null || backgroundColor instanceof UIResource)
     grid.setBackground(defaultBackgroundColor);
   if (font == null || font instanceof UIResource) grid.setFont(defaultFont);
   if (gridColor == null || gridColor instanceof UIResource) grid.setGridColor(defaultGridColor);
   if (border == null || border instanceof UIResource) grid.setBorder(defaultGridBorder);
   if (selectionForeground == null || selectionForeground instanceof UIResource)
     grid.setSelectionForegroundColor(defaultSelectionForeground);
   if (selectionBackground == null || selectionBackground instanceof UIResource)
     grid.setSelectionBackgroundColor(defaultSelectionBackground);
   if (focusForeground == null || focusForeground instanceof UIResource)
     grid.setFocusForegroundColor(defaultFocusCellForeground);
   if (focusBackground == null || focusBackground instanceof UIResource)
     grid.setFocusBackgroundColor(defaultFocusCellBackground);
 }
  public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
    // Check there is something to print
    if (pageIndex >= maxNumPage || grid.getRowCount() == 0 || grid.getColumnCount() == 0) {
      return Printable.NO_SUCH_PAGE;
    }

    Graphics2D g2d = (Graphics2D) g;
    g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());

    int pageWidth = (int) pageFormat.getImageableWidth();
    int pageHeight = (int) pageFormat.getImageableHeight();
    int gridWidth = grid.getColumnModel().getTotalSize();
    int gridHeight = grid.getRowModel().getTotalSize();

    // Check there is something visible to print
    if (gridWidth == 0 || gridHeight == 0) {
      return Printable.NO_SUCH_PAGE;
    }

    // Calculate page to print
    int pageColumn = pageIndex % pageCols;
    int pageRow = pageIndex / pageCols;

    // Calculate scale to make the grid fit in maxNumPage
    float xFitPageScale = (pageWidth * pageCols) / (float) gridWidth;
    float yFitPageScale = (pageHeight * pageRows) / (float) gridHeight;
    float minScale = Math.min(xFitPageScale, yFitPageScale);

    // Scale the print area
    int pageX = (int) ((pageColumn * pageWidth) / minScale);
    int pageY = (int) ((pageRow * pageHeight) / minScale);
    pageWidth = (int) (pageWidth / minScale);
    pageHeight = (int) (pageHeight / minScale);

    // Get clip region for page
    Point p = new Point(pageX, pageY);
    int firstRow = grid.rowAtPoint(p);
    int firstCol = grid.columnAtPoint(p);
    int x1 = grid.getColumnPosition(firstCol);
    int y1 = grid.getRowPosition(firstRow);
    int x2 = x1 + pageWidth;
    int y2 = y1 + pageHeight;
    p = new Point(x2, y2);
    int lastRow = grid.rowAtPoint(p);
    int lastCol = grid.columnAtPoint(p);
    if (grid.getRowPosition(lastRow) + grid.getRowHeight(lastRow) > y2) {
      y2 = grid.getRowPosition(lastRow);
    } else {
      y2 = grid.getRowPosition(lastRow) + grid.getRowHeight(lastRow);
    }
    if (grid.getColumnPosition(lastCol) + grid.getColumnWidth(lastCol) > x2) {
      x2 = grid.getColumnPosition(lastCol);
    } else {
      x2 = grid.getColumnPosition(lastCol) + grid.getColumnWidth(lastCol);
    }
    int x = x1;
    int y = y1;
    int width = x2 - x1;
    int height = y2 - y1;

    SelectionSettings selectionSettings = new SelectionSettings(grid.getSelectionModel());
    selectionSettings.clear();

    // Print the page
    g2d.scale(minScale, minScale);
    g2d.translate(-x, -y);
    g2d.setClip(x, y, width, height);
    grid.print(g2d);

    selectionSettings.restore();

    return Printable.PAGE_EXISTS;
  }