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
}
}
Aggregations