FileDocCategorySizeDatePackage
JHexEditor.javaAPI Docmp4parser 1.0-RC-179638Wed Dec 19 20:10:21 GMT 2012com.coremedia.iso.gui.hex

JHexEditor

public class JHexEditor extends JPanel implements AdjustmentListener, MouseWheelListener, FocusListener
Combines a hex view and an ASCII view to a fully fledged hex editor.

Fields Summary
ByteBuffer
buff
public int
cursor
protected static Font
font
protected int
border
public boolean
DEBUG
private JScrollBar
scrollBar
private int
inicio
private int
numberOfVisibleLines
Constructors Summary
public JHexEditor(ByteBuffer buff)


       
        super();
        this.buff = buff;

        this.addMouseWheelListener(this);

        scrollBar = new JScrollBar(JScrollBar.VERTICAL);
        scrollBar.addAdjustmentListener(this);
        scrollBar.setMinimum(0);
        scrollBar.setMaximum((int) (buff.limit() / getNumberOfVisibleLines()));

        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(scrollBar, BorderLayout.EAST);
        p3.add(new JHexEditorASCII(this), BorderLayout.CENTER);
        p3.add(new Caja(), BorderLayout.NORTH);

        JPanel 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);
    
Methods Summary
protected voidactualizaCursor()

        int n = (cursor / 16);
        if ((buff.limit() % 16) > 0) {
            n++;
        }
        if (n < inicio) {
            inicio = n;
        } else if (n >= inicio + numberOfVisibleLines) {
            inicio = n - numberOfVisibleLines + 2;
        }
        repaint();
    
public voidadjustmentValueChanged(java.awt.event.AdjustmentEvent e)

        inicio = e.getValue();
        if (inicio < 0) inicio = 0;
        repaint();
    
protected voidcuadro(java.awt.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 voidfilledCursor(java.awt.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);
    
public voidfocusGained(java.awt.event.FocusEvent e)

        this.repaint();
    
public voidfocusLost(java.awt.event.FocusEvent e)

        this.repaint();
    
protected intgetInicio()

        return inicio;
    
protected intgetNumberOfVisibleLines()

        if (DEBUG) {
            System.err.println("numberOfVisibleLines: " + numberOfVisibleLines);
        }
        return numberOfVisibleLines;
    
public voidkeyPressed(java.awt.event.KeyEvent e)

        switch (e.getKeyCode()) {
            case 33:    // rep
                if (cursor >= (16 * numberOfVisibleLines)) {
                    cursor -= (16 * numberOfVisibleLines);
                }
                actualizaCursor();
                break;
            case 34:    // fin
                if (cursor < (buff.limit() - (16 * numberOfVisibleLines))) {
                    cursor += (16 * numberOfVisibleLines);
                }
                actualizaCursor();
                break;
            case 35:    // fin
                cursor = (int) (buff.limit() - 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.limit() - 1)) cursor++;
                actualizaCursor();
                break;
            case 40:    // -->
                if (cursor < (buff.limit() - 16)) cursor += 16;
                actualizaCursor();
                break;

        }
    
public voidmouseWheelMoved(java.awt.event.MouseWheelEvent e)

        inicio += (e.getUnitsToScroll());
        int n = (int) (buff.limit() / 16);
        n += 4;
        if ((inicio + numberOfVisibleLines) >= n) {
            inicio = n - numberOfVisibleLines;
        }
        if (inicio < 0) {
            inicio = 0;
        }
        repaint();
    
public voidpaint(java.awt.Graphics g)

        FontMetrics fn = getFontMetrics(font);
        Rectangle rec = this.getBounds();
        numberOfVisibleLines = (rec.height / fn.getHeight()) - 2;
        int n = (int) (buff.limit() / 16);
        if ((buff.limit() % 16) > 0) {
            n++;
        }
        if (numberOfVisibleLines > n) {
            numberOfVisibleLines = n;
            inicio = 0;
        }

        scrollBar.setValues(getInicio(), getNumberOfVisibleLines(), 0, n);
        scrollBar.setValueIsAdjusting(true);
        super.paint(g);
    
protected voidprintString(java.awt.Graphics g, java.lang.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);