Search in sources :

Example 1 with EditationModeCapable

use of org.exbin.deltahex.capability.EditationModeCapable in project deltahex-java by exbin.

the class DeltaHexEditorBasic method editationModeLabelMouseClicked.

// GEN-LAST:event_aboutMenuItemActionPerformed
private void editationModeLabelMouseClicked(java.awt.event.MouseEvent evt) {
    // GEN-FIRST:event_editationModeLabelMouseClicked
    if (evt.getButton() == MouseEvent.BUTTON1) {
        EditationMode editationMode;
        if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.INSERT) {
            editationMode = EditationMode.OVERWRITE;
        } else {
            editationMode = EditationMode.INSERT;
        }
        ((EditationModeCapable) codeArea.getWorker()).setEditationMode(editationMode);
    }
}
Also used : EditationModeCapable(org.exbin.deltahex.capability.EditationModeCapable) EditationMode(org.exbin.deltahex.EditationMode)

Example 2 with EditationModeCapable

use of org.exbin.deltahex.capability.EditationModeCapable in project deltahex-java by exbin.

the class ExtendedCodeAreaTest 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 3 with EditationModeCapable

use of org.exbin.deltahex.capability.EditationModeCapable in project deltahex-java by exbin.

the class DeltaHexEditorBasic method postInit.

private void postInit() {
    codeArea.setComponentPopupMenu(mainPopupMenu);
    setIconImage(new javax.swing.ImageIcon(getClass().getResource("/org/exbin/deltahex/editor/basic/resources/icons/icon.png")).getImage());
    undoHandler.addUndoUpdateListener(new BinaryDataUndoUpdateListener() {

        @Override
        public void undoCommandPositionChanged() {
            updateUndoState();
            codeArea.repaint();
        }

        @Override
        public void undoCommandAdded(@Nonnull BinaryDataCommand command) {
            updateUndoState();
            codeArea.repaint();
        }
    });
    ((EditationModeCapable) codeArea.getWorker()).addEditationModeChangedListener(new EditationModeChangedListener() {

        @Override
        public void editationModeChanged(EditationMode editationMode) {
            switch(editationMode) {
                case INSERT:
                    {
                        editationModeLabel.setText("INS");
                        break;
                    }
                case OVERWRITE:
                    {
                        editationModeLabel.setText("OVR");
                        break;
                    }
                default:
                    {
                        throw new IllegalStateException("Unexpected editation mode " + editationMode.name());
                    }
            }
        }
    });
    ((CaretCapable) codeArea.getWorker()).addCaretMovedListener(new CaretMovedListener() {

        @Override
        public void caretMoved(CaretPosition caretPosition) {
            positionLabel.setText(caretPosition.getDataPosition() + ":" + caretPosition.getCodeOffset());
        }
    });
    ((SelectionCapable) codeArea.getWorker()).addSelectionChangedListener(new SelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionRange selection) {
            updateClipboardState();
        }
    });
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.addFlavorListener(new FlavorListener() {

        @Override
        public void flavorsChanged(FlavorEvent e) {
            updateClipboardState();
        }
    });
    updateUndoState();
    updateClipboardState();
    openFileButton.setText("Open");
}
Also used : SelectionChangedListener(org.exbin.deltahex.SelectionChangedListener) FlavorEvent(java.awt.datatransfer.FlavorEvent) EditationModeCapable(org.exbin.deltahex.capability.EditationModeCapable) EditationMode(org.exbin.deltahex.EditationMode) BinaryDataCommand(org.exbin.deltahex.operation.BinaryDataCommand) CaretCapable(org.exbin.deltahex.capability.CaretCapable) BinaryDataUndoUpdateListener(org.exbin.deltahex.operation.undo.BinaryDataUndoUpdateListener) CaretMovedListener(org.exbin.deltahex.CaretMovedListener) SelectionRange(org.exbin.deltahex.SelectionRange) CaretPosition(org.exbin.deltahex.CaretPosition) Clipboard(java.awt.datatransfer.Clipboard) FlavorListener(java.awt.datatransfer.FlavorListener) EditationModeChangedListener(org.exbin.deltahex.EditationModeChangedListener) SelectionCapable(org.exbin.deltahex.capability.SelectionCapable)

Example 4 with EditationModeCapable

use of org.exbin.deltahex.capability.EditationModeCapable 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 5 with EditationModeCapable

use of org.exbin.deltahex.capability.EditationModeCapable 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)

Aggregations

EditationModeCapable (org.exbin.deltahex.capability.EditationModeCapable)5 EditableBinaryData (org.exbin.utils.binary_data.EditableBinaryData)3 EditationMode (org.exbin.deltahex.EditationMode)2 CaretCapable (org.exbin.deltahex.capability.CaretCapable)2 Test (org.junit.Test)2 Clipboard (java.awt.datatransfer.Clipboard)1 FlavorEvent (java.awt.datatransfer.FlavorEvent)1 FlavorListener (java.awt.datatransfer.FlavorListener)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 IOException (java.io.IOException)1 CaretMovedListener (org.exbin.deltahex.CaretMovedListener)1 CaretPosition (org.exbin.deltahex.CaretPosition)1 EditationModeChangedListener (org.exbin.deltahex.EditationModeChangedListener)1 SelectionChangedListener (org.exbin.deltahex.SelectionChangedListener)1 SelectionRange (org.exbin.deltahex.SelectionRange)1 SelectionCapable (org.exbin.deltahex.capability.SelectionCapable)1 BinaryDataCommand (org.exbin.deltahex.operation.BinaryDataCommand)1 BinaryDataUndoUpdateListener (org.exbin.deltahex.operation.undo.BinaryDataUndoUpdateListener)1 BinaryData (org.exbin.utils.binary_data.BinaryData)1