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