use of org.exbin.deltahex.CodeType in project deltahex-java by exbin.
the class DefaultCodeAreaCommandHandler method setCodeValue.
private void setCodeValue(int value) {
CaretPosition caretPosition = ((CaretCapable) codeArea.getWorker()).getCaret().getCaretPosition();
long dataPosition = caretPosition.getDataPosition();
int codeOffset = caretPosition.getCodeOffset();
BinaryData data = codeArea.getData();
CodeType codeType = getCodeType();
byte byteValue = data.getByte(dataPosition);
byte outputValue = CodeAreaUtils.setCodeValue(byteValue, value, codeOffset, codeType);
((EditableBinaryData) data).setByte(dataPosition, outputValue);
}
use of org.exbin.deltahex.CodeType in project deltahex-java by exbin.
the class DefaultCodeAreaCommandHandler method keyTyped.
@Override
public void keyTyped(@Nonnull KeyEvent keyEvent) {
char keyValue = keyEvent.getKeyChar();
// TODO Add support for high unicode codes
if (keyValue == KeyEvent.CHAR_UNDEFINED) {
return;
}
if (!((EditationModeCapable) codeArea.getWorker()).isEditable()) {
return;
}
DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
CaretPosition caretPosition = caret.getCaretPosition();
if (caretPosition.getSection() == BasicCodeAreaSection.CODE_MATRIX.getSection()) {
long dataPosition = caretPosition.getDataPosition();
int codeOffset = caretPosition.getCodeOffset();
CodeType codeType = getCodeType();
boolean validKey = CodeAreaUtils.isValidCodeKeyValue(keyValue, codeOffset, codeType);
if (validKey) {
if (codeArea.hasSelection()) {
deleteSelection();
}
int value;
if (keyValue >= '0' && keyValue <= '9') {
value = keyValue - '0';
} else {
value = Character.toLowerCase(keyValue) - 'a' + 10;
}
BinaryData data = codeArea.getData();
if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
if (dataPosition == codeArea.getDataSize()) {
((EditableBinaryData) data).insert(dataPosition, 1);
}
setCodeValue(value);
} else {
if (codeOffset > 0) {
byte byteRest = data.getByte(dataPosition);
switch(codeType) {
case BINARY:
{
byteRest = (byte) (byteRest & (0xff >> codeOffset));
break;
}
case DECIMAL:
{
byteRest = (byte) (byteRest % (codeOffset == 1 ? 100 : 10));
break;
}
case OCTAL:
{
byteRest = (byte) (byteRest % (codeOffset == 1 ? 64 : 8));
break;
}
case HEXADECIMAL:
{
byteRest = (byte) (byteRest & 0xf);
break;
}
default:
throw new IllegalStateException("Unexpected code type " + codeType.name());
}
if (byteRest > 0) {
((EditableBinaryData) data).insert(dataPosition + 1, 1);
((EditableBinaryData) data).setByte(dataPosition, (byte) (data.getByte(dataPosition) - byteRest));
((EditableBinaryData) data).setByte(dataPosition + 1, byteRest);
}
} else {
((EditableBinaryData) data).insert(dataPosition, 1);
}
setCodeValue(value);
}
codeArea.notifyDataChanged();
move(NO_MODIFIER, MovementDirection.RIGHT);
revealCursor();
}
} else {
char keyChar = keyValue;
if (keyChar > 31 && isValidChar(keyValue)) {
BinaryData data = codeArea.getData();
long dataPosition = caretPosition.getDataPosition();
byte[] bytes = charToBytes(keyChar);
if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
if (dataPosition < codeArea.getDataSize()) {
int length = bytes.length;
if (dataPosition + length > codeArea.getDataSize()) {
length = (int) (codeArea.getDataSize() - dataPosition);
}
((EditableBinaryData) data).remove(dataPosition, length);
}
}
((EditableBinaryData) data).insert(dataPosition, bytes);
codeArea.notifyDataChanged();
((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(dataPosition + bytes.length - 1);
move(NO_MODIFIER, MovementDirection.RIGHT);
revealCursor();
}
}
}
use of org.exbin.deltahex.CodeType in project deltahex-java by exbin.
the class CodeAreaOperationCommandHandler method copyAsCode.
@Override
public void copyAsCode() {
SelectionRange selection = ((SelectionCapable) codeArea.getWorker()).getSelection();
if (!selection.isEmpty()) {
long first = selection.getFirst();
long last = selection.getLast();
BinaryData copy = codeArea.getData().copy(first, last - first + 1);
CodeType codeType = ((CodeTypeCapable) codeArea.getWorker()).getCodeType();
CodeCharactersCase charactersCase = ((CodeCharactersCaseCapable) codeArea.getWorker()).getCodeCharactersCase();
CodeAreaUtils.CodeDataClipboardData binaryData = new CodeAreaUtils.CodeDataClipboardData(copy, binaryDataFlavor, codeType, charactersCase);
setClipboardContent(binaryData);
}
}
use of org.exbin.deltahex.CodeType in project deltahex-java by exbin.
the class CodeAreaOperationCommandHandler method keyTyped.
@Override
public void keyTyped(KeyEvent keyEvent) {
char keyValue = keyEvent.getKeyChar();
if (keyValue == KeyEvent.CHAR_UNDEFINED) {
return;
}
if (!((EditationModeCapable) codeArea.getWorker()).isEditable()) {
return;
}
DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
CaretPosition caretPosition = caret.getCaretPosition();
if (caretPosition.getSection() == BasicCodeAreaSection.CODE_MATRIX.getSection()) {
long dataPosition = caretPosition.getDataPosition();
int startCodeOffset = caretPosition.getCodeOffset();
CodeType codeType = getCodeType();
boolean validKey = CodeAreaUtils.isValidCodeKeyValue(keyValue, startCodeOffset, codeType);
if (validKey) {
DeleteSelectionCommand deleteSelectionCommand = null;
if (codeArea.hasSelection()) {
long selectionStart = ((SelectionCapable) codeArea.getWorker()).getSelection().getFirst();
deleteSelectionCommand = new DeleteSelectionCommand(codeArea);
((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(selectionStart);
}
int value;
if (keyValue >= '0' && keyValue <= '9') {
value = keyValue - '0';
} else {
value = Character.toLowerCase(keyValue) - 'a' + 10;
}
// }
if (editCommand != null && editCommand.wasReverted()) {
editCommand = null;
}
int codeOffset = ((CaretCapable) codeArea.getWorker()).getCaret().getCaretPosition().getCodeOffset();
if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
if (editCommand == null || !(editCommand instanceof EditCodeDataCommand) || editCommand.getCommandType() != EditDataCommand.EditCommandType.OVERWRITE) {
editCommand = new EditCodeDataCommand(codeArea, EditCodeDataCommand.EditCommandType.OVERWRITE, dataPosition, codeOffset);
if (deleteSelectionCommand != null) {
HexCompoundCommand compoundCommand = new HexCompoundCommand(codeArea);
compoundCommand.appendCommand(deleteSelectionCommand);
try {
undoHandler.execute(compoundCommand);
} catch (BinaryDataOperationException ex) {
Logger.getLogger(CodeAreaOperationCommandHandler.class.getName()).log(Level.SEVERE, null, ex);
}
compoundCommand.appendCommand(editCommand);
} else {
undoHandler.addCommand(editCommand);
}
}
((EditCodeDataCommand) editCommand).appendEdit((byte) value);
} else {
if (editCommand == null || !(editCommand instanceof EditCodeDataCommand) || editCommand.getCommandType() != EditCodeDataCommand.EditCommandType.INSERT) {
editCommand = new EditCodeDataCommand(codeArea, EditCharDataCommand.EditCommandType.INSERT, dataPosition, codeOffset);
if (deleteSelectionCommand != null) {
HexCompoundCommand compoundCommand = new HexCompoundCommand(codeArea);
compoundCommand.appendCommand(deleteSelectionCommand);
try {
undoHandler.execute(compoundCommand);
} catch (BinaryDataOperationException ex) {
Logger.getLogger(CodeAreaOperationCommandHandler.class.getName()).log(Level.SEVERE, null, ex);
}
compoundCommand.appendCommand(editCommand);
} else {
undoHandler.addCommand(editCommand);
}
}
((EditCodeDataCommand) editCommand).appendEdit((byte) value);
}
codeArea.notifyDataChanged();
move(NO_MODIFIER, MovementDirection.RIGHT);
revealCursor();
}
} else {
char keyChar = keyValue;
boolean validKey = keyChar > 31 && keyChar != DELETE_CHAR && isValidChar(keyValue);
if (validKey) {
if (editCommand != null && editCommand.wasReverted()) {
editCommand = null;
}
long dataPosition = ((CaretCapable) codeArea.getWorker()).getCaret().getCaretPosition().getDataPosition();
DeleteSelectionCommand deleteCommand = null;
if (codeArea.hasSelection()) {
deleteCommand = new DeleteSelectionCommand(codeArea);
}
if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
if (editCommand == null || !(editCommand instanceof EditCharDataCommand) || editCommand.getCommandType() != EditCodeDataCommand.EditCommandType.OVERWRITE) {
editCommand = new EditCharDataCommand(codeArea, EditCodeDataCommand.EditCommandType.OVERWRITE, dataPosition);
if (deleteCommand != null) {
HexCompoundCommand compoundCommand = new HexCompoundCommand(codeArea);
compoundCommand.appendCommand(deleteCommand);
try {
undoHandler.execute(compoundCommand);
} catch (BinaryDataOperationException ex) {
Logger.getLogger(CodeAreaOperationCommandHandler.class.getName()).log(Level.SEVERE, null, ex);
}
compoundCommand.appendCommand(editCommand);
} else {
undoHandler.addCommand(editCommand);
}
}
((EditCharDataCommand) editCommand).appendEdit(keyChar);
} else {
if (editCommand == null || !(editCommand instanceof EditCharDataCommand) || editCommand.getCommandType() != EditCodeDataCommand.EditCommandType.INSERT) {
editCommand = new EditCharDataCommand(codeArea, EditCodeDataCommand.EditCommandType.INSERT, dataPosition);
if (deleteCommand != null) {
HexCompoundCommand compoundCommand = new HexCompoundCommand(codeArea);
compoundCommand.appendCommand(deleteCommand);
try {
undoHandler.execute(compoundCommand);
} catch (BinaryDataOperationException ex) {
Logger.getLogger(CodeAreaOperationCommandHandler.class.getName()).log(Level.SEVERE, null, ex);
}
compoundCommand.appendCommand(editCommand);
} else {
undoHandler.addCommand(editCommand);
}
}
((EditCharDataCommand) editCommand).appendEdit(keyChar);
}
codeArea.notifyDataChanged();
revealCursor();
codeArea.repaint();
}
}
}
use of org.exbin.deltahex.CodeType in project deltahex-java by exbin.
the class DefaultCodeAreaCommandHandler method copyAsCode.
@Override
public void copyAsCode() {
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);
CodeType codeType = ((CodeTypeCapable) codeArea.getWorker()).getCodeType();
CodeCharactersCase charactersCase = ((CodeCharactersCaseCapable) codeArea.getWorker()).getCodeCharactersCase();
CodeAreaUtils.CodeDataClipboardData binaryData = new CodeAreaUtils.CodeDataClipboardData(copy, binaryDataFlavor, codeType, charactersCase);
setClipboardContent(binaryData);
}
}
Aggregations