示例#1
0
  private void computeCellSize(Graphics g) {
    HexModel model = hex.getModel();

    // compute number of characters in headers and cells
    if (model == null) {
      headerChars = 4;
      cellChars = 2;
    } else {
      int logSize = 0;
      long addrEnd = model.getLastOffset();
      while (addrEnd > (1L << logSize)) {
        logSize++;
      }
      headerChars = (logSize + 3) / 4;
      cellChars = (model.getValueWidth() + 3) / 4;
    }

    // compute character sizes
    FontMetrics fm = g == null ? null : g.getFontMetrics(hex.getFont());
    int charWidth;
    int spaceWidth;
    int lineHeight;
    if (fm == null) {
      charWidth = 8;
      spaceWidth = 6;
      Typeface font = hex.getFont();
      if (font == null) {
        lineHeight = 16;
      } else {
        lineHeight = font.getSize();
      }
    } else {
      guessed = false;
      charWidth = 0;
      for (int i = 0; i < 16; i++) {
        int width = fm.stringWidth(Integer.toHexString(i));
        if (width > charWidth) charWidth = width;
      }
      spaceWidth = fm.stringWidth(" ");
      lineHeight = fm.getHeight();
    }

    // update header and cell dimensions
    headerWidth = headerChars * charWidth + spaceWidth;
    spacerWidth = spaceWidth;
    cellWidth = cellChars * charWidth + spaceWidth;
    cellHeight = lineHeight;

    // compute preferred size
    int width = headerWidth + cols * cellWidth + (cols / 4) * spacerWidth;
    long height;
    if (model == null) {
      height = 16 * cellHeight;
    } else {
      long addr0 = getBaseAddress(model);
      long addr1 = model.getLastOffset();
      long rows = (int) (((addr1 - addr0 + 1) + cols - 1) / cols);
      height = rows * cellHeight;
      if (height > Integer.MAX_VALUE) height = Integer.MAX_VALUE;
    }

    // update preferred size
    Size pref = hex.getPreferredSize();
    if (pref.width != width || pref.height != height) {
      pref.width = width;
      Size.create(width, (int) height);
      hex.setPreferredSize(pref);
      hex.revalidate();
    }

    widthChanged();
  }