Search in sources :

Example 1 with DefaultCodeAreaCaret

use of org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret in project deltahex-java by exbin.

the class CodeAreaOperationCommandHandler method updateSelection.

public void updateSelection(boolean selecting, @Nonnull CaretPosition caretPosition) {
    DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
    SelectionRange selection = ((SelectionCapable) codeArea.getWorker()).getSelection();
    if (selecting) {
        ((SelectionCapable) codeArea.getWorker()).setSelection(selection.getStart(), caret.getDataPosition());
    } else {
        ((SelectionCapable) codeArea.getWorker()).setSelection(caret.getDataPosition(), caret.getDataPosition());
    }
}
Also used : DefaultCodeAreaCaret(org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret) SelectionRange(org.exbin.deltahex.SelectionRange) SelectionCapable(org.exbin.deltahex.capability.SelectionCapable)

Example 2 with DefaultCodeAreaCaret

use of org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret in project deltahex-java by exbin.

the class CodeAreaOperationCommandHandler method move.

public void move(int modifiers, @Nonnull MovementDirection direction) {
    DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
    CaretPosition caretPosition = caret.getCaretPosition();
    CaretPosition movePosition = codeArea.getWorker().computeMovePosition(caretPosition, direction);
    if (!caretPosition.equals(movePosition)) {
        caret.setCaretPosition(movePosition);
        updateSelection((modifiers & KeyEvent.SHIFT_DOWN_MASK) > 0, movePosition);
        notifyCaretMoved();
    }
}
Also used : DefaultCodeAreaCaret(org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret) CaretPosition(org.exbin.deltahex.CaretPosition)

Example 3 with DefaultCodeAreaCaret

use of org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret 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();
        }
    }
}
Also used : DefaultCodeAreaCaret(org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret) EditCodeDataCommand(org.exbin.deltahex.operation.swing.command.EditCodeDataCommand) CaretCapable(org.exbin.deltahex.capability.CaretCapable) CaretPosition(org.exbin.deltahex.CaretPosition) BinaryDataOperationException(org.exbin.deltahex.operation.BinaryDataOperationException) CodeType(org.exbin.deltahex.CodeType) EditCharDataCommand(org.exbin.deltahex.operation.swing.command.EditCharDataCommand) HexCompoundCommand(org.exbin.deltahex.operation.swing.command.HexCompoundCommand)

Aggregations

DefaultCodeAreaCaret (org.exbin.deltahex.swing.basic.DefaultCodeAreaCaret)3 CaretPosition (org.exbin.deltahex.CaretPosition)2 CodeType (org.exbin.deltahex.CodeType)1 SelectionRange (org.exbin.deltahex.SelectionRange)1 CaretCapable (org.exbin.deltahex.capability.CaretCapable)1 SelectionCapable (org.exbin.deltahex.capability.SelectionCapable)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 HexCompoundCommand (org.exbin.deltahex.operation.swing.command.HexCompoundCommand)1