Search in sources :

Example 6 with EditableBinaryData

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

the class OverwriteCodeEditDataOperation method appendEdit.

@Override
public void appendEdit(byte value) {
    EditableBinaryData data = (EditableBinaryData) codeArea.getData();
    long editedDataPosition = startPosition + length;
    byte byteValue = 0;
    if (codeOffset > 0) {
        if (editedDataPosition <= data.getDataSize()) {
            byteValue = data.getByte(editedDataPosition - 1);
        }
        editedDataPosition--;
    } else {
        if (editedDataPosition < data.getDataSize()) {
            if (undoData == null) {
                undoData = (EditableBinaryData) data.copy(editedDataPosition, 1);
                byteValue = undoData.getByte(0);
            } else {
                undoData.insert(undoData.getDataSize(), data, editedDataPosition, 1);
            }
        } else if (editedDataPosition > data.getDataSize()) {
            throw new IllegalStateException("Cannot overwrite outside of the document");
        } else {
            data.insertUninitialized(editedDataPosition, 1);
        }
        length++;
    }
    byteValue = CodeAreaUtils.setCodeValue(byteValue, value, codeOffset, codeType);
    data.setByte(editedDataPosition, byteValue);
    codeOffset++;
    if (codeOffset == codeType.getMaxDigitsForByte()) {
        codeOffset = 0;
    }
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData)

Example 7 with EditableBinaryData

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

the class CodeAreaTest method testCopyPasteInOverwriteMode.

@Test
public void testCopyPasteInOverwriteMode() {
    CodeArea codeArea = new CodeArea();
    EditableBinaryData sampleData = getSampleData(SAMPLE_ALLBYTES);
    codeArea.setData(sampleData);
    long dataSize = sampleData.getDataSize();
    codeArea.selectAll();
    codeArea.copy();
    codeArea.clearSelection();
    codeArea.paste();
    Assert.assertTrue(codeArea.getDataSize() == dataSize);
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) Test(org.junit.Test)

Example 8 with EditableBinaryData

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

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

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

the class StatePanelEx method loadDataButtonActionPerformed.

// GEN-LAST:event_activeSectionComboBoxActionPerformed
private void loadDataButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_loadDataButtonActionPerformed
    JFileChooser openFC = new JFileChooser();
    openFC.removeChoosableFileFilter(openFC.getAcceptAllFileFilter());
    openFC.addChoosableFileFilter(new FileFilter() {

        @Override
        public boolean accept(File file) {
            return file.isFile();
        }

        @Override
        public String getDescription() {
            return "All Files (*)";
        }
    });
    if (openFC.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        try {
            File selectedFile = openFC.getSelectedFile();
            try (FileInputStream stream = new FileInputStream(selectedFile)) {
                ((EditableBinaryData) codeArea.getData()).loadFromStream(stream);
                codeArea.notifyDataChanged();
            // codeArea.resetPosition();
            }
        } catch (IOException ex) {
            Logger.getLogger(DeltaHexExampleBasicPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
Also used : DeltaHexExampleBasicPanel(org.exbin.deltahex.swing.example.DeltaHexExampleBasicPanel) JFileChooser(javax.swing.JFileChooser) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

EditableBinaryData (org.exbin.utils.binary_data.EditableBinaryData)27 Test (org.junit.Test)11 IOException (java.io.IOException)10 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)5 BinaryData (org.exbin.utils.binary_data.BinaryData)5 JFileChooser (javax.swing.JFileChooser)3 EditationModeCapable (org.exbin.deltahex.capability.EditationModeCapable)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 Charset (java.nio.charset.Charset)2 FileFilter (javax.swing.filechooser.FileFilter)2 CaretPosition (org.exbin.deltahex.CaretPosition)2 CodeType (org.exbin.deltahex.CodeType)2 CharsetCapable (org.exbin.deltahex.capability.CharsetCapable)2 DeltaHexExampleBasicPanel (org.exbin.deltahex.swing.example.DeltaHexExampleBasicPanel)2 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 FileOutputStream (java.io.FileOutputStream)1 SelectionRange (org.exbin.deltahex.SelectionRange)1 CaretCapable (org.exbin.deltahex.capability.CaretCapable)1