Search in sources :

Example 11 with BinaryData

use of org.exbin.utils.binary_data.BinaryData 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 12 with BinaryData

use of org.exbin.utils.binary_data.BinaryData in project deltahex-java by exbin.

the class CodeAreaOperationCommandHandler method copyAsCode.

@Override
public void copyAsCode() {
    SelectionRange selection = ((SelectionCapable) codeArea.getWorker()).getSelection();
    if (!selection.isEmpty()) {
        long first = selection.getFirst();
        long last = selection.getLast();
        BinaryData copy = codeArea.getData().copy(first, last - first + 1);
        CodeType codeType = ((CodeTypeCapable) codeArea.getWorker()).getCodeType();
        CodeCharactersCase charactersCase = ((CodeCharactersCaseCapable) codeArea.getWorker()).getCodeCharactersCase();
        CodeAreaUtils.CodeDataClipboardData binaryData = new CodeAreaUtils.CodeDataClipboardData(copy, binaryDataFlavor, codeType, charactersCase);
        setClipboardContent(binaryData);
    }
}
Also used : CodeAreaUtils(org.exbin.deltahex.CodeAreaUtils) CodeCharactersCase(org.exbin.deltahex.CodeCharactersCase) SelectionRange(org.exbin.deltahex.SelectionRange) CodeTypeCapable(org.exbin.deltahex.capability.CodeTypeCapable) CodeType(org.exbin.deltahex.CodeType) CodeCharactersCaseCapable(org.exbin.deltahex.capability.CodeCharactersCaseCapable) BinaryData(org.exbin.utils.binary_data.BinaryData) SelectionCapable(org.exbin.deltahex.capability.SelectionCapable)

Example 13 with BinaryData

use of org.exbin.utils.binary_data.BinaryData in project deltahex-java by exbin.

the class DeltaDocumentSaveTest method testOverwriteCopyEndSaveDocument.

@Test
public void testOverwriteCopyEndSaveDocument() {
    DeltaDocument document = openTempDeltaDocument();
    assertEquals(SAMPLE_ALLBYTES_SIZE, document.getDataSize());
    BinaryData copy = document.copy(0x40, 2);
    document.replace(254, copy);
    document.validatePointerPosition();
    copy.dispose();
    try {
        document.save();
        InputStream comparisionFile;
        try (InputStream dataInputStream = document.getDataInputStream()) {
            comparisionFile = new FileInputStream(DeltaDocumentSaveTest.class.getResource(SAMPLE_OVERWRITTEN_END).getFile());
            TestUtils.assertEqualsInputStream(comparisionFile, dataInputStream);
        }
        comparisionFile.close();
    } catch (IOException ex) {
        Logger.getLogger(DeltaDocumentSaveTest.class.getName()).log(Level.SEVERE, null, ex);
        fail("Exception: " + ex.getMessage());
    }
    document.validatePointerPosition();
    document.clear();
    assertEquals(0, document.getSegments().size());
    closeTempDeltaDocument(document);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) BinaryData(org.exbin.utils.binary_data.BinaryData) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 14 with BinaryData

use of org.exbin.utils.binary_data.BinaryData in project deltahex-java by exbin.

the class DeltaDocumentSaveTest method testInsertCopyMiddleSaveDocument.

@Test
public void testInsertCopyMiddleSaveDocument() {
    DeltaDocument document = openTempDeltaDocument();
    assertEquals(SAMPLE_ALLBYTES_SIZE, document.getDataSize());
    BinaryData copy = document.copy(0x40, 2);
    document.insert(120, copy);
    document.validatePointerPosition();
    copy.dispose();
    try {
        document.save();
        InputStream comparisionFile;
        try (InputStream dataInputStream = document.getDataInputStream()) {
            comparisionFile = new FileInputStream(DeltaDocumentSaveTest.class.getResource(SAMPLE_INSERTED_MIDDLE).getFile());
            TestUtils.assertEqualsInputStream(comparisionFile, dataInputStream);
        }
        comparisionFile.close();
    } catch (IOException ex) {
        Logger.getLogger(DeltaDocumentSaveTest.class.getName()).log(Level.SEVERE, null, ex);
        fail("Exception: " + ex.getMessage());
    }
    document.validatePointerPosition();
    document.clear();
    assertEquals(0, document.getSegments().size());
    closeTempDeltaDocument(document);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) BinaryData(org.exbin.utils.binary_data.BinaryData) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 15 with BinaryData

use of org.exbin.utils.binary_data.BinaryData in project deltahex-java by exbin.

the class DefaultCodeAreaCommandHandler method copyAsCode.

@Override
public void copyAsCode() {
    SelectionRange selection = ((SelectionCapable) codeArea.getWorker()).getSelection();
    if (!selection.isEmpty()) {
        BinaryData data = codeArea.getData();
        if (data == null) {
            return;
        }
        long first = selection.getFirst();
        long last = selection.getLast();
        BinaryData copy = data.copy(first, last - first + 1);
        CodeType codeType = ((CodeTypeCapable) codeArea.getWorker()).getCodeType();
        CodeCharactersCase charactersCase = ((CodeCharactersCaseCapable) codeArea.getWorker()).getCodeCharactersCase();
        CodeAreaUtils.CodeDataClipboardData binaryData = new CodeAreaUtils.CodeDataClipboardData(copy, binaryDataFlavor, codeType, charactersCase);
        setClipboardContent(binaryData);
    }
}
Also used : CodeAreaUtils(org.exbin.deltahex.CodeAreaUtils) CodeCharactersCase(org.exbin.deltahex.CodeCharactersCase) SelectionRange(org.exbin.deltahex.SelectionRange) CodeTypeCapable(org.exbin.deltahex.capability.CodeTypeCapable) CodeType(org.exbin.deltahex.CodeType) CodeCharactersCaseCapable(org.exbin.deltahex.capability.CodeCharactersCaseCapable) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) BinaryData(org.exbin.utils.binary_data.BinaryData) SelectionCapable(org.exbin.deltahex.capability.SelectionCapable)

Aggregations

BinaryData (org.exbin.utils.binary_data.BinaryData)18 EditableBinaryData (org.exbin.utils.binary_data.EditableBinaryData)14 IOException (java.io.IOException)8 Test (org.junit.Test)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 SelectionRange (org.exbin.deltahex.SelectionRange)5 SelectionCapable (org.exbin.deltahex.capability.SelectionCapable)5 CodeAreaUtils (org.exbin.deltahex.CodeAreaUtils)4 CodeType (org.exbin.deltahex.CodeType)4 CaretPosition (org.exbin.deltahex.CaretPosition)2 CodeCharactersCase (org.exbin.deltahex.CodeCharactersCase)2 CodeCharactersCaseCapable (org.exbin.deltahex.capability.CodeCharactersCaseCapable)2 CodeTypeCapable (org.exbin.deltahex.capability.CodeTypeCapable)2 BasicStroke (java.awt.BasicStroke)1 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 Stroke (java.awt.Stroke)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 CaretCapable (org.exbin.deltahex.capability.CaretCapable)1