Search in sources :

Example 21 with EditableBinaryData

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

the class StatePanel 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)

Example 22 with EditableBinaryData

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

the class CodeAreaTest method testCopyPasteAtTheEnd.

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

Example 23 with EditableBinaryData

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

the class CodeAreaTest method testCopyPasteInInsertMode.

@Test
public void testCopyPasteInInsertMode() {
    CodeArea codeArea = new CodeArea();
    ((EditationModeCapable) codeArea.getWorker()).setEditationMode(EditationMode.INSERT);
    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 * 2);
}
Also used : EditationModeCapable(org.exbin.deltahex.capability.EditationModeCapable) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) Test(org.junit.Test)

Example 24 with EditableBinaryData

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

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

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