Search in sources :

Example 6 with CaretCapable

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

CaretCapable (org.exbin.deltahex.capability.CaretCapable)6 CaretPosition (org.exbin.deltahex.CaretPosition)4 EditationModeCapable (org.exbin.deltahex.capability.EditationModeCapable)2 Cursor (java.awt.Cursor)1 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 CodeType (org.exbin.deltahex.CodeType)1 EditationMode (org.exbin.deltahex.EditationMode)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 BinaryDataOperationException (org.exbin.deltahex.operation.BinaryDataOperationException)1 EditCharDataCommand (org.exbin.deltahex.operation.swing.command.EditCharDataCommand)1 EditCodeDataCommand (org.exbin.deltahex.operation.swing.command.EditCodeDataCommand)1