better binary data display

This commit is contained in:
Honfika
2013-12-28 13:36:22 +01:00
parent 2ed6001cee
commit 8ddc8c206c
2 changed files with 68 additions and 12 deletions

View File

@@ -20,19 +20,25 @@ import com.jpexs.decompiler.flash.gui.abc.LineMarkedEditorPane;
import com.jpexs.helpers.Helper;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public final class BinaryPanel extends JPanel implements ActionListener {
public final class BinaryPanel extends JPanel implements ActionListener, ComponentListener {
public LineMarkedEditorPane hexEditor = new LineMarkedEditorPane();
private byte[] data;
public BinaryPanel() {
super(new BorderLayout());
setOpaque(true);
setBackground(View.DEFAULT_BACKGROUND_COLOR);
add(hexEditor, BorderLayout.CENTER);
// todo: set textArea size, to hide horizontal scrollbar
add(new JScrollPane(hexEditor), BorderLayout.CENTER);
JPanel bottomPanel = new JPanel(new BorderLayout());
JPanel buttonsPanel = new JPanel(new FlowLayout());
@@ -45,9 +51,30 @@ public final class BinaryPanel extends JPanel implements ActionListener {
}
public void setBinaryData(byte[] data) {
this.data = data;
setBackground(View.swfBackgroundColor);
int widthInChars = getWidth() / 7 -3; // -3: scrollbar
int blockCount = widthInChars / 34;
hexEditor.setEditable(false);
hexEditor.setText(Helper.byteArrayToHex(data, 32));
hexEditor.setText(Helper.byteArrayToHex(data, blockCount * 8));
hexEditor.setFont(new Font("Monospaced", Font.PLAIN, hexEditor.getFont().getSize()));
//hexEditor.setContentType("text/plain"); //throws exception. why?
}
@Override
public void componentResized(ComponentEvent e) {
setBinaryData(data);
}
@Override
public void componentMoved(ComponentEvent ce) {
}
@Override
public void componentShown(ComponentEvent ce) {
}
@Override
public void componentHidden(ComponentEvent ce) {
}
}

View File

@@ -570,10 +570,10 @@ public class Helper {
public static GraphTextWriter byteArrayToHexWithHeader(GraphTextWriter writer, byte[] data) {
writer.appendNoHilight("#hexdata").newLine().newLine();
return byteArrayToHex(writer, data, 8, 8);
return byteArrayToHex(writer, data, 8, 8, false);
}
public static GraphTextWriter byteArrayToHex(GraphTextWriter writer, byte[] data, int bytesPerRow, int groupSize) {
public static GraphTextWriter byteArrayToHex(GraphTextWriter writer, byte[] data, int bytesPerRow, int groupSize, boolean addChars) {
/* // hex data from decompiled actions
Scanner scanner = new Scanner(srcWithHex);
@@ -586,15 +586,44 @@ public class Helper {
}
}*/
for (int i = 0; i < data.length; i++) {
if (i > 0) {
if (i % bytesPerRow == 0) {
writer.newLine();
} else if (i % groupSize == 0) {
int rowCount = data.length / bytesPerRow;
if (data.length % bytesPerRow > 0) {
rowCount++;
}
for (int row = 0; row < rowCount; row++) {
if (row > 0) {
writer.newLine();
}
for (int i = 0; i < bytesPerRow; i++) {
int idx = row * bytesPerRow + i;
if (data.length == idx) {
break;
}
if (i> 0 && i % groupSize == 0) {
writer.appendNoHilight(" ");
}
writer.appendNoHilight(String.format("%02x ", data[idx]));
}
if (addChars) {
writer.appendNoHilight(" ");
for (int i = 0; i < bytesPerRow; i++) {
int idx = row * bytesPerRow + i;
if (data.length == idx) {
break;
}
if (i> 0 && i % groupSize == 0) {
writer.appendNoHilight(" ");
}
byte ch = data[idx];
if (ch >=0 && ch < 32) {
ch = '.';
}
writer.appendNoHilight((char) ch + "");
}
}
writer.appendNoHilight(String.format("%02x ", data[i]));
}
writer.newLine();
@@ -603,7 +632,7 @@ public class Helper {
public static String byteArrayToHex(byte[] data, int bytesPerRow) {
HilightedTextWriter writer = new HilightedTextWriter(false);
byteArrayToHex(writer, data, bytesPerRow, 8);
byteArrayToHex(writer, data, bytesPerRow, 8, true);
return writer.toString();
}