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
}
}
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();
}
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);
}
}
Aggregations