forked from Konloch/bytecode-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJHexEditor.java
More file actions
257 lines (213 loc) · 7.9 KB
/
JHexEditor.java
File metadata and controls
257 lines (213 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.jhe.hexed;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Created by IntelliJ IDEA. User: laullon Date: 08-abr-2003 Time: 13:21:09
*/
public class JHexEditor extends JPanel implements FocusListener,
AdjustmentListener, MouseWheelListener {
private static final long serialVersionUID = 2289328616534802372L;
byte[] buff;
public int cursor;
protected static Font font = new Font("Monospaced", 0, 12);
protected int border = 2;
public boolean DEBUG = false;
private JPanel panel;
private JScrollBar sb;
private int inicio = 0;
private int lineas = 10;
public JHexEditor(byte[] buff) {
super();
this.buff = buff;
this.addMouseWheelListener(this);
sb = new JScrollBar(JScrollBar.VERTICAL);
sb.addAdjustmentListener(this);
sb.setMinimum(0);
sb.setMaximum(buff.length / getLineas());
JPanel p1, p2, p3;
// centro
p1 = new JPanel(new BorderLayout(1, 1));
p1.add(new JHexEditorHEX(this), BorderLayout.CENTER);
p1.add(new Columnas(), BorderLayout.NORTH);
// izq.
p2 = new JPanel(new BorderLayout(1, 1));
p2.add(new Filas(), BorderLayout.CENTER);
p2.add(new Caja(), BorderLayout.NORTH);
// der
p3 = new JPanel(new BorderLayout(1, 1));
p3.add(sb, BorderLayout.EAST);
p3.add(new JHexEditorASCII(this), BorderLayout.CENTER);
p3.add(new Caja(), BorderLayout.NORTH);
panel = new JPanel();
panel.setLayout(new BorderLayout(1, 1));
panel.add(p1, BorderLayout.CENTER);
panel.add(p2, BorderLayout.WEST);
panel.add(p3, BorderLayout.EAST);
this.setLayout(new BorderLayout(1, 1));
this.add(panel, BorderLayout.CENTER);
}
public void paint(Graphics g) {
FontMetrics fn = getFontMetrics(font);
Rectangle rec = this.getBounds();
lineas = (rec.height / fn.getHeight()) - 1;
int n = (buff.length / 16) - 1;
if (lineas > n) {
lineas = n;
inicio = 0;
}
sb.setValues(getInicio(), +getLineas(), 0, buff.length / 16);
sb.setValueIsAdjusting(true);
super.paint(g);
}
protected void actualizaCursor() {
int n = (cursor / 16);
System.out.print("- " + inicio + "<" + n + "<" + (lineas + inicio)
+ "(" + lineas + ")");
if (n < inicio)
inicio = n;
else if (n >= inicio + lineas)
inicio = n - (lineas - 1);
System.out.println(" - " + inicio + "<" + n + "<" + (lineas + inicio)
+ "(" + lineas + ")");
repaint();
}
protected int getInicio() {
return inicio;
}
protected int getLineas() {
return lineas;
}
protected void fondo(Graphics g, int x, int y, int s) {
FontMetrics fn = getFontMetrics(font);
g.fillRect(((fn.stringWidth(" ") + 1) * x) + border,
(fn.getHeight() * y) + border, ((fn.stringWidth(" ") + 1) * s),
fn.getHeight() + 1);
}
protected void cuadro(Graphics g, int x, int y, int s) {
FontMetrics fn = getFontMetrics(font);
g.drawRect(((fn.stringWidth(" ") + 1) * x) + border,
(fn.getHeight() * y) + border, ((fn.stringWidth(" ") + 1) * s),
fn.getHeight() + 1);
}
protected void printString(Graphics g, String s, int x, int y) {
FontMetrics fn = getFontMetrics(font);
g.drawString(s, ((fn.stringWidth(" ") + 1) * x) + border,
((fn.getHeight() * (y + 1)) - fn.getMaxDescent()) + border);
}
public void focusGained(FocusEvent e) {
this.repaint();
}
public void focusLost(FocusEvent e) {
this.repaint();
}
public void adjustmentValueChanged(AdjustmentEvent e) {
inicio = e.getValue();
if (inicio < 0)
inicio = 0;
repaint();
}
public void mouseWheelMoved(MouseWheelEvent e) {
inicio += (e.getUnitsToScroll());
if ((inicio + lineas) >= buff.length / 16)
inicio = (buff.length / 16) - lineas;
if (inicio < 0)
inicio = 0;
repaint();
}
public void keyPressed(KeyEvent e) {
/*
* switch(e.getKeyCode()) { case 33: // rep if(cursor>=(16*lineas))
* cursor-=(16*lineas); actualizaCursor(); break; case 34: // fin
* if(cursor<(buff.length-(16*lineas))) cursor+=(16*lineas);
* actualizaCursor(); break; case 35: // fin cursor=buff.length-1;
* actualizaCursor(); break; case 36: // ini cursor=0;
* actualizaCursor(); break; case 37: // <-- if(cursor!=0) cursor--;
* actualizaCursor(); break; case 38: // <-- if(cursor>15) cursor-=16;
* actualizaCursor(); break; case 39: // --> if(cursor!=(buff.length-1))
* cursor++; actualizaCursor(); break; case 40: // -->
* if(cursor<(buff.length-16)) cursor+=16; actualizaCursor(); break; }
*/
}
private class Columnas extends JPanel {
private static final long serialVersionUID = -1734199617526339842L;
public Columnas() {
this.setLayout(new BorderLayout(1, 1));
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public Dimension getMinimumSize() {
Dimension d = new Dimension();
FontMetrics fn = getFontMetrics(font);
int h = fn.getHeight();
int nl = 1;
d.setSize(((fn.stringWidth(" ") + 1) * +((16 * 3) - 1))
+ (border * 2) + 1, h * nl + (border * 2) + 1);
return d;
}
public void paint(Graphics g) {
Dimension d = getMinimumSize();
g.setColor(Color.white);
g.fillRect(0, 0, d.width, d.height);
g.setColor(Color.black);
g.setFont(font);
for (int n = 0; n < 16; n++) {
if (n == (cursor % 16))
cuadro(g, n * 3, 0, 2);
String s = "00" + Integer.toHexString(n);
s = s.substring(s.length() - 2);
printString(g, s, n * 3, 0);
}
}
}
private class Caja extends JPanel {
private static final long serialVersionUID = -6124062720565016834L;
public Dimension getPreferredSize() {
return getMinimumSize();
}
public Dimension getMinimumSize() {
Dimension d = new Dimension();
FontMetrics fn = getFontMetrics(font);
int h = fn.getHeight();
d.setSize((fn.stringWidth(" ") + 1) + (border * 2) + 1, h
+ (border * 2) + 1);
return d;
}
}
private class Filas extends JPanel {
private static final long serialVersionUID = 8797347523486018051L;
public Filas() {
this.setLayout(new BorderLayout(1, 1));
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public Dimension getMinimumSize() {
Dimension d = new Dimension();
FontMetrics fn = getFontMetrics(font);
int h = fn.getHeight();
int nl = getLineas();
d.setSize((fn.stringWidth(" ") + 1) * (8) + (border * 2) + 1, h
* nl + (border * 2) + 1);
return d;
}
public void paint(Graphics g) {
Dimension d = getMinimumSize();
g.setColor(Color.white);
g.fillRect(0, 0, d.width, d.height);
g.setColor(Color.black);
g.setFont(font);
int ini = getInicio();
int fin = ini + getLineas();
int y = 0;
for (int n = ini; n < fin; n++) {
if (n == (cursor / 16))
cuadro(g, 0, y, 8);
String s = "0000000000000" + Integer.toHexString(n);
s = s.substring(s.length() - 8);
printString(g, s, 0, y++);
}
}
}
}