Search in sources :

Example 16 with BinaryData

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

the class DefaultCodeAreaCommandHandler method paste.

@Override
public void paste() {
    if (!((EditationModeCapable) codeArea.getWorker()).isEditable()) {
        return;
    }
    try {
        if (clipboard.isDataFlavorAvailable(binaryDataFlavor)) {
            if (codeArea.hasSelection()) {
                deleteSelection();
                codeArea.notifyDataChanged();
            }
            try {
                Object object = clipboard.getData(binaryDataFlavor);
                if (object instanceof BinaryData) {
                    DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
                    long dataPosition = caret.getDataPosition();
                    BinaryData clipboardData = (BinaryData) object;
                    long dataSize = clipboardData.getDataSize();
                    if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
                        long toRemove = dataSize;
                        if (dataPosition + toRemove > codeArea.getDataSize()) {
                            toRemove = codeArea.getDataSize() - dataPosition;
                        }
                        ((EditableBinaryData) codeArea.getData()).remove(dataPosition, toRemove);
                    }
                    ((EditableBinaryData) codeArea.getData()).insert(dataPosition, clipboardData);
                    codeArea.notifyDataChanged();
                    caret.setCaretPosition(caret.getDataPosition() + dataSize);
                    caret.setCodeOffset(0);
                    updateScrollBars();
                    notifyCaretMoved();
                    revealCursor();
                }
            } catch (UnsupportedFlavorException | IOException ex) {
                Logger.getLogger(DefaultCodeAreaCommandHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
            if (codeArea.hasSelection()) {
                deleteSelection();
                codeArea.notifyDataChanged();
            }
            Object insertedData;
            try {
                insertedData = clipboard.getData(DataFlavor.stringFlavor);
                if (insertedData instanceof String) {
                    DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
                    long dataPosition = caret.getDataPosition();
                    byte[] bytes = ((String) insertedData).getBytes(Charset.forName(CodeAreaUtils.DEFAULT_ENCODING));
                    int length = bytes.length;
                    if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
                        long toRemove = length;
                        if (dataPosition + toRemove > codeArea.getDataSize()) {
                            toRemove = codeArea.getDataSize() - dataPosition;
                        }
                        ((EditableBinaryData) codeArea.getData()).remove(dataPosition, toRemove);
                    }
                    ((EditableBinaryData) codeArea.getData()).insert(dataPosition, bytes);
                    codeArea.notifyDataChanged();
                    caret.setCaretPosition(caret.getDataPosition() + length);
                    caret.setCodeOffset(0);
                    updateScrollBars();
                    notifyCaretMoved();
                    revealCursor();
                }
            } catch (UnsupportedFlavorException | IOException ex) {
                Logger.getLogger(DefaultCodeAreaCommandHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } catch (IllegalStateException ex) {
    // Clipboard not available - ignore
    }
}
Also used : EditationModeCapable(org.exbin.deltahex.capability.EditationModeCapable) CaretCapable(org.exbin.deltahex.capability.CaretCapable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) BinaryData(org.exbin.utils.binary_data.BinaryData)

Example 17 with BinaryData

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

the class DefaultCodeAreaCommandHandler method deleteSelection.

private void deleteSelection() {
    BinaryData data = codeArea.getData();
    if (data == null) {
        return;
    }
    if (!(data instanceof EditableBinaryData)) {
        throw new IllegalStateException("Data is not editable");
    }
    SelectionRange selection = ((SelectionCapable) codeArea.getWorker()).getSelection();
    if (selection.isEmpty()) {
        return;
    }
    long first = selection.getFirst();
    long last = selection.getLast();
    ((EditableBinaryData) data).remove(first, last - first + 1);
    codeArea.clearSelection();
    DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
    caret.setCaretPosition(first);
    revealCursor();
    updateScrollBars();
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) SelectionRange(org.exbin.deltahex.SelectionRange) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) BinaryData(org.exbin.utils.binary_data.BinaryData) SelectionCapable(org.exbin.deltahex.capability.SelectionCapable)

Example 18 with BinaryData

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

the class DefaultCodeAreaCommandHandler method copy.

@Override
public void copy() {
    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);
        CodeAreaUtils.BinaryDataClipboardData binaryData = new CodeAreaUtils.BinaryDataClipboardData(copy, binaryDataFlavor);
        setClipboardContent(binaryData);
    }
}
Also used : CodeAreaUtils(org.exbin.deltahex.CodeAreaUtils) SelectionRange(org.exbin.deltahex.SelectionRange) 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