001: /*
002: * HexPanel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Color;
016: import java.awt.Dimension;
017: import java.awt.Font;
018: import java.awt.FontMetrics;
019: import javax.swing.JPanel;
020: import javax.swing.JTable;
021: import javax.swing.event.TableModelListener;
022: import javax.swing.table.DefaultTableCellRenderer;
023: import javax.swing.table.JTableHeader;
024: import javax.swing.table.TableColumn;
025: import javax.swing.table.TableColumnModel;
026: import javax.swing.table.TableModel;
027: import workbench.gui.WbSwingUtilities;
028: import workbench.util.NumberStringCache;
029:
030: /**
031: * @author support@sql-workbench.net
032: */
033: public class HexPanel extends JPanel {
034:
035: private JTable dataTable;
036: private LineNumberTable lines;
037:
038: public HexPanel() {
039: setBorder(WbSwingUtilities.EMPTY_BORDER);
040: dataTable = new JTable();
041: dataTable.setAutoCreateColumnsFromModel(true);
042: dataTable.setCellSelectionEnabled(false);
043: dataTable.setShowGrid(false);
044: dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
045: Font dataFont = new Font("Monospaced", 0, 12);
046: dataTable.setFont(dataFont);
047: setLayout(new BorderLayout());
048: lines = new LineNumberTable(dataTable);
049: WbScrollPane scroll = new WbScrollPane(dataTable);
050: scroll.setRowHeaderView(lines);
051: JTableHeader header = dataTable.getTableHeader();
052: DefaultTableCellRenderer rend = new DefaultTableCellRenderer();
053: //header.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
054: rend.setBackground(new Color(238, 240, 238));
055: header.setDefaultRenderer(rend);
056: this .add(header, BorderLayout.NORTH);
057: this .add(scroll, BorderLayout.CENTER);
058: }
059:
060: public HexPanel(byte[] buffer) {
061: this ();
062: setData(buffer);
063: }
064:
065: public void setData(byte[] buffer) {
066: ByteBufferTableModel model = new ByteBufferTableModel(buffer);
067: dataTable.setModel(model);
068: TableColumnModel tmod = dataTable.getColumnModel();
069: int cols = tmod.getColumnCount();
070: Font dataFont = dataTable.getFont();
071: FontMetrics fm = dataTable.getFontMetrics(dataFont);
072: int width = fm.stringWidth("000");
073: for (int i = 0; i < cols - 1; i++) {
074: TableColumn col = tmod.getColumn(i);
075: col.setPreferredWidth(width);
076: col.setMinWidth(width);
077: col.setMaxWidth(width);
078: }
079:
080: width = fm.stringWidth("MMMMMMMMMMMMMMMMM");
081: TableColumn col = tmod.getColumn(cols - 1);
082: col.setPreferredWidth(width);
083: col.setMinWidth(width);
084: col.setMaxWidth(width);
085:
086: String rowCount = NumberStringCache.getNumberString(model
087: .getRowCount());
088: width = fm.stringWidth(rowCount);
089: lines.setPreferredScrollableViewportSize(new Dimension(
090: width + 5, 32768));
091: }
092:
093: }
094:
095: class ByteBufferTableModel implements TableModel {
096: private byte[] data;
097: private int rowCount;
098: private int columns = 16;
099: private String[] labels;
100:
101: public ByteBufferTableModel(byte[] buffer) {
102: data = buffer;
103: rowCount = (buffer.length / columns) + 1;
104: labels = new String[columns + 1];
105: for (int i = 0; i < columns; i++) {
106: labels[i] = NumberStringCache.getHexString(i);
107: }
108: labels[columns] = "0123456789abcdef";
109: }
110:
111: public int getRowCount() {
112: return rowCount;
113: }
114:
115: public int getColumnCount() {
116: return columns + 1;
117: }
118:
119: public String getColumnName(int columnIndex) {
120: return labels[columnIndex];
121: }
122:
123: public Class getColumnClass(int columnIndex) {
124: return String.class;
125: }
126:
127: public boolean isCellEditable(int rowIndex, int columnIndex) {
128: return false;
129: }
130:
131: public Object getValueAt(int rowIndex, int columnIndex) {
132: if (columnIndex == columns) {
133: int rowStart = rowIndex * columns;
134: int rowEnd = Math.min(rowIndex * columns + (columns - 1),
135: data.length);
136: StringBuilder result = new StringBuilder(16);
137: for (int i = rowStart; i < rowEnd; i++) {
138: int c = (data[i] < 0 ? 256 + data[i] : data[i]);
139: if (c > 31 && c < 128 || c > 128 && c < 255) {
140: result.append((char) c);
141: } else {
142: result.append('.');
143: }
144:
145: }
146: return result.toString();
147: }
148: int offset = rowIndex * columns + columnIndex;
149: if (offset >= data.length)
150: return "";
151: int c = (data[offset] < 0 ? 256 + data[offset] : data[offset]);
152: return NumberStringCache.getHexString(c);
153:
154: // if (c < 16) return "0" + Integer.toHexString(c);
155: // else return Integer.toHexString(c);
156: }
157:
158: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
159: }
160:
161: public void addTableModelListener(TableModelListener l) {
162: }
163:
164: public void removeTableModelListener(TableModelListener l) {
165: }
166:
167: }
|