Search in sources :

Example 1 with CaretPosition

use of org.exbin.deltahex.CaretPosition in project deltahex-java by exbin.

the class CodeAreaOperationCommandHandler method moveCaret.

@Override
public void moveCaret(int positionX, int positionY, boolean selecting) {
    CaretPosition caretPosition = ((CaretCapable) codeArea.getWorker()).mousePositionToClosestCaretPosition(positionX, positionY);
    if (caretPosition != null) {
        ((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(caretPosition);
        updateSelection(selecting, caretPosition);
        notifyCaretMoved();
        undoSequenceBreak();
        codeArea.repaint();
    }
}
Also used : CaretPosition(org.exbin.deltahex.CaretPosition) CaretCapable(org.exbin.deltahex.capability.CaretCapable)

Example 2 with CaretPosition

use of org.exbin.deltahex.CaretPosition in project deltahex-java by exbin.

the class DefaultCodeAreaCommandHandler method moveCaret.

@Override
public void moveCaret(int positionX, int positionY, boolean selecting) {
    CaretPosition caretPosition = ((CaretCapable) codeArea.getWorker()).mousePositionToClosestCaretPosition(positionX, positionY);
    if (caretPosition != null) {
        ((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(caretPosition);
        updateSelection(selecting, caretPosition);
        notifyCaretMoved();
        undoSequenceBreak();
        codeArea.repaint();
    }
}
Also used : CaretPosition(org.exbin.deltahex.CaretPosition) CaretCapable(org.exbin.deltahex.capability.CaretCapable)

Example 3 with CaretPosition

use of org.exbin.deltahex.CaretPosition in project deltahex-java by exbin.

the class DefaultCodeAreaCommandHandler method setCodeValue.

private void setCodeValue(int value) {
    CaretPosition caretPosition = ((CaretCapable) codeArea.getWorker()).getCaret().getCaretPosition();
    long dataPosition = caretPosition.getDataPosition();
    int codeOffset = caretPosition.getCodeOffset();
    BinaryData data = codeArea.getData();
    CodeType codeType = getCodeType();
    byte byteValue = data.getByte(dataPosition);
    byte outputValue = CodeAreaUtils.setCodeValue(byteValue, value, codeOffset, codeType);
    ((EditableBinaryData) data).setByte(dataPosition, outputValue);
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) CaretPosition(org.exbin.deltahex.CaretPosition) CodeType(org.exbin.deltahex.CodeType) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) BinaryData(org.exbin.utils.binary_data.BinaryData)

Example 4 with CaretPosition

use of org.exbin.deltahex.CaretPosition in project deltahex-java by exbin.

the class DefaultCodeAreaCommandHandler method keyTyped.

@Override
public void keyTyped(@Nonnull KeyEvent keyEvent) {
    char keyValue = keyEvent.getKeyChar();
    // TODO Add support for high unicode codes
    if (keyValue == KeyEvent.CHAR_UNDEFINED) {
        return;
    }
    if (!((EditationModeCapable) codeArea.getWorker()).isEditable()) {
        return;
    }
    DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
    CaretPosition caretPosition = caret.getCaretPosition();
    if (caretPosition.getSection() == BasicCodeAreaSection.CODE_MATRIX.getSection()) {
        long dataPosition = caretPosition.getDataPosition();
        int codeOffset = caretPosition.getCodeOffset();
        CodeType codeType = getCodeType();
        boolean validKey = CodeAreaUtils.isValidCodeKeyValue(keyValue, codeOffset, codeType);
        if (validKey) {
            if (codeArea.hasSelection()) {
                deleteSelection();
            }
            int value;
            if (keyValue >= '0' && keyValue <= '9') {
                value = keyValue - '0';
            } else {
                value = Character.toLowerCase(keyValue) - 'a' + 10;
            }
            BinaryData data = codeArea.getData();
            if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
                if (dataPosition == codeArea.getDataSize()) {
                    ((EditableBinaryData) data).insert(dataPosition, 1);
                }
                setCodeValue(value);
            } else {
                if (codeOffset > 0) {
                    byte byteRest = data.getByte(dataPosition);
                    switch(codeType) {
                        case BINARY:
                            {
                                byteRest = (byte) (byteRest & (0xff >> codeOffset));
                                break;
                            }
                        case DECIMAL:
                            {
                                byteRest = (byte) (byteRest % (codeOffset == 1 ? 100 : 10));
                                break;
                            }
                        case OCTAL:
                            {
                                byteRest = (byte) (byteRest % (codeOffset == 1 ? 64 : 8));
                                break;
                            }
                        case HEXADECIMAL:
                            {
                                byteRest = (byte) (byteRest & 0xf);
                                break;
                            }
                        default:
                            throw new IllegalStateException("Unexpected code type " + codeType.name());
                    }
                    if (byteRest > 0) {
                        ((EditableBinaryData) data).insert(dataPosition + 1, 1);
                        ((EditableBinaryData) data).setByte(dataPosition, (byte) (data.getByte(dataPosition) - byteRest));
                        ((EditableBinaryData) data).setByte(dataPosition + 1, byteRest);
                    }
                } else {
                    ((EditableBinaryData) data).insert(dataPosition, 1);
                }
                setCodeValue(value);
            }
            codeArea.notifyDataChanged();
            move(NO_MODIFIER, MovementDirection.RIGHT);
            revealCursor();
        }
    } else {
        char keyChar = keyValue;
        if (keyChar > 31 && isValidChar(keyValue)) {
            BinaryData data = codeArea.getData();
            long dataPosition = caretPosition.getDataPosition();
            byte[] bytes = charToBytes(keyChar);
            if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
                if (dataPosition < codeArea.getDataSize()) {
                    int length = bytes.length;
                    if (dataPosition + length > codeArea.getDataSize()) {
                        length = (int) (codeArea.getDataSize() - dataPosition);
                    }
                    ((EditableBinaryData) data).remove(dataPosition, length);
                }
            }
            ((EditableBinaryData) data).insert(dataPosition, bytes);
            codeArea.notifyDataChanged();
            ((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(dataPosition + bytes.length - 1);
            move(NO_MODIFIER, MovementDirection.RIGHT);
            revealCursor();
        }
    }
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) CaretPosition(org.exbin.deltahex.CaretPosition) CodeType(org.exbin.deltahex.CodeType) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) BinaryData(org.exbin.utils.binary_data.BinaryData)

Example 5 with CaretPosition

use of org.exbin.deltahex.CaretPosition in project deltahex-java by exbin.

the class CodeAreaOperationCommandHandler method move.

public void move(int modifiers, @Nonnull MovementDirection direction) {
    DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
    CaretPosition caretPosition = caret.getCaretPosition();
    CaretPosition movePosition = codeArea.getWorker().computeMovePosition(caretPosition, direction);
    if (!caretPosition.equals(movePosition)) {
        caret.setCaretPosition(movePosition);
        updateSelection((modifiers & KeyEvent.SHIFT_DOWN_MASK) > 0, movePosition);
        notifyCaretMoved();
    }
}
Also used : DefaultCodeAreaCaret(org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret) CaretPosition(org.exbin.deltahex.CaretPosition)

Aggregations

CaretPosition (org.exbin.deltahex.CaretPosition)8 CaretCapable (org.exbin.deltahex.capability.CaretCapable)4 CodeType (org.exbin.deltahex.CodeType)3 DefaultCodeAreaCaret (org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret)2 BinaryData (org.exbin.utils.binary_data.BinaryData)2 EditableBinaryData (org.exbin.utils.binary_data.EditableBinaryData)2 Clipboard (java.awt.datatransfer.Clipboard)1 FlavorEvent (java.awt.datatransfer.FlavorEvent)1 FlavorListener (java.awt.datatransfer.FlavorListener)1 CaretMovedListener (org.exbin.deltahex.CaretMovedListener)1 EditationMode (org.exbin.deltahex.EditationMode)1 EditationModeChangedListener (org.exbin.deltahex.EditationModeChangedListener)1 SelectionChangedListener (org.exbin.deltahex.SelectionChangedListener)1 SelectionRange (org.exbin.deltahex.SelectionRange)1 EditationModeCapable (org.exbin.deltahex.capability.EditationModeCapable)1 SelectionCapable (org.exbin.deltahex.capability.SelectionCapable)1 BinaryDataCommand (org.exbin.deltahex.operation.BinaryDataCommand)1 BinaryDataOperationException (org.exbin.deltahex.operation.BinaryDataOperationException)1 EditCharDataCommand (org.exbin.deltahex.operation.swing.command.EditCharDataCommand)1 EditCodeDataCommand (org.exbin.deltahex.operation.swing.command.EditCodeDataCommand)1