@Override protected void paintComponent(Graphics g) { measures.ensureComputed(g); Rectangle clip = g.getClipBounds(); if (isOpaque()) { g.setColor(getBackground()); g.fillRect(clip.x, clip.y, clip.width, clip.height); } long addr0 = model.getFirstOffset(); long addr1 = model.getLastOffset(); long xaddr0 = measures.toAddress(0, clip.y); if (xaddr0 == addr0) xaddr0 = measures.getBaseAddress(model); long xaddr1 = measures.toAddress(getWidth(), clip.y + clip.height) + 1; highlighter.paint(g, xaddr0, xaddr1); g.setColor(getForeground()); Font baseFont = g.getFont(); FontMetrics baseFm = g.getFontMetrics(baseFont); Font labelFont = baseFont.deriveFont(Font.ITALIC); FontMetrics labelFm = g.getFontMetrics(labelFont); int cols = measures.getColumnCount(); int baseX = measures.getBaseX(); int baseY = measures.toY(xaddr0) + baseFm.getAscent() + baseFm.getLeading() / 2; int dy = measures.getCellHeight(); int labelWidth = measures.getLabelWidth(); int labelChars = measures.getLabelChars(); int cellWidth = measures.getCellWidth(); int cellChars = measures.getCellChars(); for (long a = xaddr0; a < xaddr1; a += cols, baseY += dy) { String label = toHex(a, labelChars); g.setFont(labelFont); g.drawString( label, baseX - labelWidth + (labelWidth - labelFm.stringWidth(label)) / 2, baseY); g.setFont(baseFont); long b = a; for (int j = 0; j < cols; j++, b++) { if (b >= addr0 && b <= addr1) { String val = toHex(model.get(b), cellChars); int x = measures.toX(b) + (cellWidth - baseFm.stringWidth(val)) / 2; g.drawString(val, x, baseY); } } } caret.paintForeground(g, xaddr0, xaddr1); }
public long toAddress(int x, int y) { HexModel model = hex.getModel(); if (model == null) return Integer.MIN_VALUE; long addr0 = model.getFirstOffset(); long addr1 = model.getLastOffset(); long base = getBaseAddress(model) + ((long) y / cellHeight) * cols; int offs = (x - baseX) / (cellWidth + (spacerWidth + 2) / 4); if (offs < 0) offs = 0; if (offs >= cols) offs = cols - 1; long ret = base + offs; if (ret > addr1) ret = addr1; if (ret < addr0) ret = addr0; return ret; }
public void selectAll() { caret.setDot(model.getLastOffset(), false); caret.setDot(0, true); }
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(); }