Document Hex Editor better

This commit is contained in:
Nico Mexis 2021-07-21 23:31:05 +02:00
parent fa85fd7479
commit b72d47bef9
No known key found for this signature in database
GPG key ID: 27D6E17CE092AB78
3 changed files with 84 additions and 91 deletions

View file

@ -23,22 +23,22 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
protected int textLength = 16; protected int textLength = 16;
protected int lastWidth; protected int lastWidth;
byte[] buff; byte[] buf;
public int cursor; public int cursor;
public Font font; public Font font;
protected int border = 2; protected int border = 2;
public boolean DEBUG = false; public boolean DEBUG = false;
private final JScrollBar sb; private final JScrollBar sb;
private int inicio = 0; private int begin = 0;
private int lineas = 10; private int lines = 10;
private JHexEditorHEX hex; private final JHexEditorHEX hex;
private JHexEditorASCII ascii; private final JHexEditorASCII ascii;
public JHexEditor(byte[] buff) public JHexEditor(byte[] buff)
{ {
super(); super();
this.buff = buff; this.buf = buff;
this.font = new Font(Font.MONOSPACED, Font.PLAIN, BytecodeViewer.viewer.getFontSize()); this.font = new Font(Font.MONOSPACED, Font.PLAIN, BytecodeViewer.viewer.getFontSize());
checkSize(); checkSize();
@ -48,26 +48,25 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
sb = new JScrollBar(JScrollBar.VERTICAL); sb = new JScrollBar(JScrollBar.VERTICAL);
sb.addAdjustmentListener(this); sb.addAdjustmentListener(this);
sb.setMinimum(0); sb.setMinimum(0);
sb.setMaximum(buff.length / getLineas()); sb.setMaximum(buff.length / getLines());
JPanel p1, p2, p3; JPanel p1, p2, p3;
// centro // HEX Editor
hex = new JHexEditorHEX(this); hex = new JHexEditorHEX(this);
p1 = new JPanel(new BorderLayout(1, 1)); p1 = new JPanel(new BorderLayout(1, 1));
p1.add(hex, BorderLayout.CENTER); p1.add(hex, BorderLayout.CENTER);
p1.add(new Columnas(), BorderLayout.NORTH); p1.add(new Column(), BorderLayout.NORTH);
// izq.
p2 = new JPanel(new BorderLayout(1, 1)); p2 = new JPanel(new BorderLayout(1, 1));
p2.add(new Filas(), BorderLayout.CENTER); p2.add(new Row(), BorderLayout.CENTER);
p2.add(new Caja(), BorderLayout.NORTH); p2.add(new Cell(), BorderLayout.NORTH);
// der // ASCII Editor
ascii = new JHexEditorASCII(this); ascii = new JHexEditorASCII(this);
p3 = new JPanel(new BorderLayout(1, 1)); p3 = new JPanel(new BorderLayout(1, 1));
p3.add(sb, BorderLayout.EAST); p3.add(sb, BorderLayout.EAST);
p3.add(ascii, BorderLayout.CENTER); p3.add(ascii, BorderLayout.CENTER);
p3.add(new Caja(), BorderLayout.NORTH); p3.add(new Cell(), BorderLayout.NORTH);
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(1, 1)); panel.setLayout(new BorderLayout(1, 1));
@ -90,14 +89,14 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
FontMetrics fn = getFontMetrics(font); FontMetrics fn = getFontMetrics(font);
Rectangle rec = this.getBounds(); Rectangle rec = this.getBounds();
lineas = (rec.height / fn.getHeight()) - 1; lines = (rec.height / fn.getHeight()) - 1;
int n = (buff.length / textLength) - 1; int n = (buf.length / textLength) - 1;
if (lineas > n) { if (lines > n) {
lineas = n; lines = n;
inicio = 0; begin = 0;
} }
sb.setValues(getInicio(), +getLineas(), 0, buff.length / textLength); sb.setValues(getBegin(), +getLines(), 0, buf.length / textLength);
sb.setValueIsAdjusting(true); sb.setValueIsAdjusting(true);
super.paint(g); super.paint(g);
} }
@ -139,22 +138,22 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
}); });
} }
protected int getInicio() { protected int getBegin() {
return inicio; return begin;
} }
protected int getLineas() { protected int getLines() {
return lineas; return lines;
} }
protected void fondo(Graphics g, int x, int y, int s) { protected void background(Graphics g, int x, int y, int s) {
FontMetrics fn = getFontMetrics(font); FontMetrics fn = getFontMetrics(font);
g.fillRect(((fn.stringWidth(" ") + 1) * x) + border, g.fillRect(((fn.stringWidth(" ") + 1) * x) + border,
(fn.getHeight() * y) + border, ((fn.stringWidth(" ") + 1) * s), (fn.getHeight() * y) + border, ((fn.stringWidth(" ") + 1) * s),
fn.getHeight() + 1); fn.getHeight() + 1);
} }
protected void cuadro(Graphics g, int x, int y, int s) { protected void border(Graphics g, int x, int y, int s) {
FontMetrics fn = getFontMetrics(font); FontMetrics fn = getFontMetrics(font);
g.drawRect(((fn.stringWidth(" ") + 1) * x) + border, g.drawRect(((fn.stringWidth(" ") + 1) * x) + border,
(fn.getHeight() * y) + border, ((fn.stringWidth(" ") + 1) * s), (fn.getHeight() * y) + border, ((fn.stringWidth(" ") + 1) * s),
@ -179,41 +178,39 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
@Override @Override
public void adjustmentValueChanged(AdjustmentEvent e) { public void adjustmentValueChanged(AdjustmentEvent e) {
inicio = e.getValue(); begin = e.getValue();
if (inicio < 0) if (begin < 0)
inicio = 0; begin = 0;
repaint(); repaint();
} }
@Override @Override
public void mouseWheelMoved(MouseWheelEvent e) { public void mouseWheelMoved(MouseWheelEvent e) {
inicio += (e.getUnitsToScroll()); begin += (e.getUnitsToScroll());
if ((inicio + lineas) >= buff.length / textLength) if ((begin + lines) >= buf.length / textLength)
inicio = (buff.length / textLength) - lineas; begin = (buf.length / textLength) - lines;
if (inicio < 0) if (begin < 0)
inicio = 0; begin = 0;
repaint(); repaint();
} }
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
/* /* switch(e.getKeyCode()) { case 33: // rep if(cursor>=(16*lines))
* switch(e.getKeyCode()) { case 33: // rep if(cursor>=(16*lineas)) cursor-=(16*lines); refreshCursor(); break; case 34: // fin
* cursor-=(16*lineas); actualizaCursor(); break; case 34: // fin if(cursor<(buff.length-(16*lines))) cursor+=(16*lines);
* if(cursor<(buff.length-(16*lineas))) cursor+=(16*lineas); refreshCursor(); break; case 35: // fin cursor=buff.length-1;
* actualizaCursor(); break; case 35: // fin cursor=buff.length-1; refreshCursor(); break; case 36: // ini cursor=0;
* actualizaCursor(); break; case 36: // ini cursor=0; refreshCursor(); break; case 37: // <-- if(cursor!=0) cursor--;
* actualizaCursor(); break; case 37: // <-- if(cursor!=0) cursor--; refreshCursor(); break; case 38: // <-- if(cursor>15) cursor-=16;
* actualizaCursor(); break; case 38: // <-- if(cursor>15) cursor-=16; refreshCursor(); break; case 39: // --> if(cursor!=(buff.length-1))
* actualizaCursor(); break; case 39: // --> if(cursor!=(buff.length-1)) cursor++; refreshCursor(); break; case 40: // -->
* cursor++; actualizaCursor(); break; case 40: // --> if(cursor<(buff.length-16)) cursor+=16; refreshCursor(); break; } */
* if(cursor<(buff.length-16)) cursor+=16; actualizaCursor(); break; }
*/
} }
private class Columnas extends JPanel { private class Column extends JPanel {
private static final long serialVersionUID = -1734199617526339842L; private static final long serialVersionUID = -1734199617526339842L;
public Columnas() { public Column() {
this.setLayout(new BorderLayout(1, 1)); this.setLayout(new BorderLayout(1, 1));
} }
@ -228,7 +225,7 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
FontMetrics fn = getFontMetrics(font); FontMetrics fn = getFontMetrics(font);
int h = fn.getHeight(); int h = fn.getHeight();
int nl = 1; int nl = 1;
d.setSize(((fn.stringWidth(" ") + 1) * +((textLength * 3) - 1)) d.setSize(((fn.stringWidth(" ") + 1) * ((textLength * 3) - 1))
+ (border * 2) + 1, h * nl + (border * 2) + 1); + (border * 2) + 1, h * nl + (border * 2) + 1);
return d; return d;
} }
@ -243,7 +240,7 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
for (int n = 0; n < textLength; n++) { for (int n = 0; n < textLength; n++) {
if (n == (cursor % textLength)) if (n == (cursor % textLength))
cuadro(g, n * 3, 0, 2); border(g, n * 3, 0, 2);
String s = "00" + Integer.toHexString(n); String s = "00" + Integer.toHexString(n);
s = s.substring(s.length() - 2); s = s.substring(s.length() - 2);
printString(g, s, n * 3, 0); printString(g, s, n * 3, 0);
@ -251,7 +248,7 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
} }
} }
private class Caja extends JPanel { private class Cell extends JPanel {
private static final long serialVersionUID = -6124062720565016834L; private static final long serialVersionUID = -6124062720565016834L;
@Override @Override
@ -271,10 +268,10 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
} }
private class Filas extends JPanel { private class Row extends JPanel {
private static final long serialVersionUID = 8797347523486018051L; private static final long serialVersionUID = 8797347523486018051L;
public Filas() { public Row() {
this.setLayout(new BorderLayout(1, 1)); this.setLayout(new BorderLayout(1, 1));
} }
@ -288,7 +285,7 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
Dimension d = new Dimension(); Dimension d = new Dimension();
FontMetrics fn = getFontMetrics(font); FontMetrics fn = getFontMetrics(font);
int h = fn.getHeight(); int h = fn.getHeight();
int nl = getLineas(); int nl = getLines();
d.setSize((fn.stringWidth(" ") + 1) * (8) + (border * 2) + 1, h d.setSize((fn.stringWidth(" ") + 1) * (8) + (border * 2) + 1, h
* nl + (border * 2) + 1); * nl + (border * 2) + 1);
return d; return d;
@ -303,13 +300,13 @@ public class JHexEditor extends JPanel implements FocusListener, AdjustmentListe
g.setColor(Configuration.lafTheme.isDark() ? Color.white : Color.black); g.setColor(Configuration.lafTheme.isDark() ? Color.white : Color.black);
g.setFont(font); g.setFont(font);
int ini = getInicio(); int ini = getBegin();
int fin = ini + getLineas(); int fin = ini + getLines();
int y = 0; int y = 0;
for (int n = ini; n < fin; n++) for (int n = ini; n < fin; n++)
{ {
if (n == (cursor / textLength)) if (n == (cursor / textLength))
cuadro(g, 0, y, 8); border(g, 0, y, 8);
String s = "0000000000000" + Integer.toHexString(n); String s = "0000000000000" + Integer.toHexString(n);
s = s.substring(s.length() - 8); s = s.substring(s.length() - 8);
printString(g, s, 0, y++); printString(g, s, 0, y++);

View file

@ -42,7 +42,7 @@ public class JHexEditorASCII extends JComponent implements MouseListener, KeyLis
FontMetrics fn = getFontMetrics(he.font); FontMetrics fn = getFontMetrics(he.font);
int w = fn.stringWidth(" "); int w = fn.stringWidth(" ");
int h = fn.getHeight(); int h = fn.getHeight();
int nl = he.getLineas(); int nl = he.getLines();
int len = he.textLength + 1; int len = he.textLength + 1;
int width = (len * w) + (he.border * 2) + 5; int width = (len * w) + (he.border * 2) + 5;
@ -64,7 +64,7 @@ public class JHexEditorASCII extends JComponent implements MouseListener, KeyLis
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
debug("paint(" + g + ")"); debug("paint(" + g + ")");
debug("cursor=" + he.cursor + " buff.length=" + he.buff.length); debug("cursor=" + he.cursor + " buff.length=" + he.buf.length);
Dimension d = getMinimumSize(); Dimension d = getMinimumSize();
g.setColor(Configuration.lafTheme.isDark() ? Color.darkGray : Color.white); g.setColor(Configuration.lafTheme.isDark() ? Color.darkGray : Color.white);
g.fillRect(0, 0, d.width, d.height); g.fillRect(0, 0, d.width, d.height);
@ -72,21 +72,21 @@ public class JHexEditorASCII extends JComponent implements MouseListener, KeyLis
g.setFont(he.font); g.setFont(he.font);
// datos ascii // ASCII data
int ini = he.getInicio() * he.textLength; int start = he.getBegin() * he.textLength;
int fin = ini + (he.getLineas() * he.textLength); int stop = start + (he.getLines() * he.textLength);
if (fin > he.buff.length) if (stop > he.buf.length)
fin = he.buff.length; stop = he.buf.length;
int x = 0; int x = 0;
int y = 0; int y = 0;
for (int n = ini; n < fin; n++) { for (int n = start; n < stop; n++) {
if (n == he.cursor) { if (n == he.cursor) {
g.setColor(Color.blue); g.setColor(Color.blue);
if (hasFocus()) if (hasFocus())
he.fondo(g, x, y, 1); he.background(g, x, y, 1);
else else
he.cuadro(g, x, y, 1); he.border(g, x, y, 1);
if (hasFocus()) if (hasFocus())
g.setColor(Configuration.lafTheme.isDark() ? Color.black : Color.white); g.setColor(Configuration.lafTheme.isDark() ? Color.black : Color.white);
else else
@ -95,8 +95,8 @@ public class JHexEditorASCII extends JComponent implements MouseListener, KeyLis
g.setColor(Configuration.lafTheme.isDark() ? Color.white : Color.black); g.setColor(Configuration.lafTheme.isDark() ? Color.white : Color.black);
} }
String s = String.valueOf((char) (he.buff[n] & 0xFF));//"" + new Character((char) he.buff[n]); String s = String.valueOf((char) (he.buf[n] & 0xFF));//"" + new Character((char) he.buff[n]);
if ((he.buff[n] < 20) || (he.buff[n] > 126)) if ((he.buf[n] < 20) || (he.buf[n] > 126))
s = ".";//"" + (char) 16; s = ".";//"" + (char) 16;
he.printString(g, s, (x++), y); he.printString(g, s, (x++), y);
if (x == he.textLength) { if (x == he.textLength) {
@ -112,20 +112,18 @@ public class JHexEditorASCII extends JComponent implements MouseListener, KeyLis
System.out.println("JHexEditorASCII ==> " + s); System.out.println("JHexEditorASCII ==> " + s);
} }
// calcular la posicion del raton public int calculateMousePosition(int x, int y) {
public int calcularPosicionRaton(int x, int y) {
FontMetrics fn = getFontMetrics(he.font); FontMetrics fn = getFontMetrics(he.font);
x = x / (fn.stringWidth(" ") + 1); x = x / (fn.stringWidth(" ") + 1);
y = y / fn.getHeight(); y = y / fn.getHeight();
debug("x=" + x + " ,y=" + y); debug("x=" + x + " ,y=" + y);
return x + ((y + he.getInicio()) * he.textLength); return x + ((y + he.getBegin()) * he.textLength);
} }
// mouselistener
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
debug("mouseClicked(" + e + ")"); debug("mouseClicked(" + e + ")");
he.cursor = calcularPosicionRaton(e.getX(), e.getY()); he.cursor = calculateMousePosition(e.getX(), e.getY());
this.requestFocus(); this.requestFocus();
he.repaint(); he.repaint();
} }

View file

@ -40,7 +40,7 @@ public class JHexEditorHEX extends JComponent implements MouseListener, KeyListe
public void paint(Graphics g) public void paint(Graphics g)
{ {
debug("paint(" + g + ")"); debug("paint(" + g + ")");
debug("cursor=" + he.cursor + " buff.length=" + he.buff.length); debug("cursor=" + he.cursor + " buff.length=" + he.buf.length);
if(!Configuration.lafTheme.isDark()) if(!Configuration.lafTheme.isDark())
{ {
@ -56,25 +56,25 @@ public class JHexEditorHEX extends JComponent implements MouseListener, KeyListe
g.setFont(he.font); g.setFont(he.font);
int ini = he.getInicio() * he.textLength; int start = he.getBegin() * he.textLength;
int fin = ini + (he.getLineas() * he.textLength); int stop = start + (he.getLines() * he.textLength);
if (fin > he.buff.length) if (stop > he.buf.length)
fin = he.buff.length; stop = he.buf.length;
// datos hex // HEX data
int x = 0; int x = 0;
int y = 0; int y = 0;
for (int n = ini; n < fin; n++) { for (int n = start; n < stop; n++) {
if (n == he.cursor) { if (n == he.cursor) {
if (hasFocus()) { if (hasFocus()) {
g.setColor(Color.black); g.setColor(Color.black);
he.fondo(g, (x * 3), y, 2); he.background(g, (x * 3), y, 2);
g.setColor(Color.blue); g.setColor(Color.blue);
int cursor = 0; int cursor = 0;
he.fondo(g, (x * 3) + cursor, y, 1); he.background(g, (x * 3) + cursor, y, 1);
} else { } else {
g.setColor(Color.blue); g.setColor(Color.blue);
he.cuadro(g, (x * 3), y, 2); he.border(g, (x * 3), y, 2);
} }
if (hasFocus()) if (hasFocus())
@ -85,7 +85,7 @@ public class JHexEditorHEX extends JComponent implements MouseListener, KeyListe
g.setColor(Configuration.lafTheme.isDark() ? Color.white : Color.black); g.setColor(Configuration.lafTheme.isDark() ? Color.white : Color.black);
} }
String s = ("0" + Integer.toHexString(he.buff[n])); String s = ("0" + Integer.toHexString(he.buf[n]));
s = s.substring(s.length() - 2); s = s.substring(s.length() - 2);
he.printString(g, s, ((x++) * 3), y); he.printString(g, s, ((x++) * 3), y);
if (x == he.textLength) { if (x == he.textLength) {
@ -100,20 +100,18 @@ public class JHexEditorHEX extends JComponent implements MouseListener, KeyListe
System.out.println("JHexEditorHEX ==> " + s); System.out.println("JHexEditorHEX ==> " + s);
} }
// calcular la posicion del raton public int calculateMousePosition(int x, int y) {
public int calcularPosicionRaton(int x, int y) {
FontMetrics fn = getFontMetrics(he.font); FontMetrics fn = getFontMetrics(he.font);
x = x / ((fn.stringWidth(" ") + 1) * 3); x = x / ((fn.stringWidth(" ") + 1) * 3);
y = y / fn.getHeight(); y = y / fn.getHeight();
debug("x=" + x + " ,y=" + y); debug("x=" + x + " ,y=" + y);
return x + ((y + he.getInicio()) * he.textLength); return x + ((y + he.getBegin()) * he.textLength);
} }
// mouselistener
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
debug("mouseClicked(" + e + ")"); debug("mouseClicked(" + e + ")");
he.cursor = calcularPosicionRaton(e.getX(), e.getY()); he.cursor = calculateMousePosition(e.getX(), e.getY());
this.requestFocus(); this.requestFocus();
he.repaint(); he.repaint();
} }
@ -149,7 +147,7 @@ public class JHexEditorHEX extends JComponent implements MouseListener, KeyListe
* he.buff[he.cursor]=(byte)Integer.parseInt(new String(str),16); * he.buff[he.cursor]=(byte)Integer.parseInt(new String(str),16);
* *
* if(cursor!=1) cursor=1; else if(he.cursor!=(he.buff.length-1)){ * if(cursor!=1) cursor=1; else if(he.cursor!=(he.buff.length-1)){
* he.cursor++; cursor=0;} he.actualizaCursor(); } * he.cursor++; cursor=0;} he.refreshCursor(); }
*/ */
} }