Search in sources :

Example 1 with CaretCapable

use of org.exbin.deltahex.capability.CaretCapable in project deltahex-java by exbin.

the class CodeAreaOperationCommandHandler method moveCaret.

@Override
public void moveCaret(int positionX, int positionY, boolean selecting) {
    CaretPosition caretPosition = ((CaretCapable) codeArea.getWorker()).mousePositionToClosestCaretPosition(positionX, positionY);
    if (caretPosition != null) {
        ((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(caretPosition);
        updateSelection(selecting, caretPosition);
        notifyCaretMoved();
        undoSequenceBreak();
        codeArea.repaint();
    }
}
Also used : CaretPosition(org.exbin.deltahex.CaretPosition) CaretCapable(org.exbin.deltahex.capability.CaretCapable)

Example 2 with CaretCapable

use of org.exbin.deltahex.capability.CaretCapable in project deltahex-java by exbin.

the class DefaultCodeAreaCommandHandler method moveCaret.

@Override
public void moveCaret(int positionX, int positionY, boolean selecting) {
    CaretPosition caretPosition = ((CaretCapable) codeArea.getWorker()).mousePositionToClosestCaretPosition(positionX, positionY);
    if (caretPosition != null) {
        ((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(caretPosition);
        updateSelection(selecting, caretPosition);
        notifyCaretMoved();
        undoSequenceBreak();
        codeArea.repaint();
    }
}
Also used : CaretPosition(org.exbin.deltahex.CaretPosition) CaretCapable(org.exbin.deltahex.capability.CaretCapable)

Example 3 with CaretCapable

use of org.exbin.deltahex.capability.CaretCapable in project deltahex-java by exbin.

the class DefaultCodeAreaMouseListener method updateMouseCursor.

private void updateMouseCursor(@Nonnull MouseEvent me) {
    int cursorShape = ((CaretCapable) codeArea.getWorker()).getMouseCursorShape(computeRelativeX(me), computeRelativeY(me));
    // Reuse current cursor if unchanged
    Cursor newCursor = cursorShape == 0 ? defaultCursor : textCursor;
    if (newCursor != currentCursor) {
        currentCursor = newCursor;
        codeArea.setCursor(newCursor);
    }
}
Also used : CaretCapable(org.exbin.deltahex.capability.CaretCapable) Cursor(java.awt.Cursor)

Example 4 with CaretCapable

use of org.exbin.deltahex.capability.CaretCapable 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)

Example 5 with CaretCapable

use of org.exbin.deltahex.capability.CaretCapable in project deltahex-java by exbin.

the class DeltaHexEditorBasic method postInit.

private void postInit() {
    codeArea.setComponentPopupMenu(mainPopupMenu);
    setIconImage(new javax.swing.ImageIcon(getClass().getResource("/org/exbin/deltahex/editor/basic/resources/icons/icon.png")).getImage());
    undoHandler.addUndoUpdateListener(new BinaryDataUndoUpdateListener() {

        @Override
        public void undoCommandPositionChanged() {
            updateUndoState();
            codeArea.repaint();
        }

        @Override
        public void undoCommandAdded(@Nonnull BinaryDataCommand command) {
            updateUndoState();
            codeArea.repaint();
        }
    });
    ((EditationModeCapable) codeArea.getWorker()).addEditationModeChangedListener(new EditationModeChangedListener() {

        @Override
        public void editationModeChanged(EditationMode editationMode) {
            switch(editationMode) {
                case INSERT:
                    {
                        editationModeLabel.setText("INS");
                        break;
                    }
                case OVERWRITE:
                    {
                        editationModeLabel.setText("OVR");
                        break;
                    }
                default:
                    {
                        throw new IllegalStateException("Unexpected editation mode " + editationMode.name());
                    }
            }
        }
    });
    ((CaretCapable) codeArea.getWorker()).addCaretMovedListener(new CaretMovedListener() {

        @Override
        public void caretMoved(CaretPosition caretPosition) {
            positionLabel.setText(caretPosition.getDataPosition() + ":" + caretPosition.getCodeOffset());
        }
    });
    ((SelectionCapable) codeArea.getWorker()).addSelectionChangedListener(new SelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionRange selection) {
            updateClipboardState();
        }
    });
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.addFlavorListener(new FlavorListener() {

        @Override
        public void flavorsChanged(FlavorEvent e) {
            updateClipboardState();
        }
    });
    updateUndoState();
    updateClipboardState();
    openFileButton.setText("Open");
}
Also used : SelectionChangedListener(org.exbin.deltahex.SelectionChangedListener) FlavorEvent(java.awt.datatransfer.FlavorEvent) EditationModeCapable(org.exbin.deltahex.capability.EditationModeCapable) EditationMode(org.exbin.deltahex.EditationMode) BinaryDataCommand(org.exbin.deltahex.operation.BinaryDataCommand) CaretCapable(org.exbin.deltahex.capability.CaretCapable) BinaryDataUndoUpdateListener(org.exbin.deltahex.operation.undo.BinaryDataUndoUpdateListener) CaretMovedListener(org.exbin.deltahex.CaretMovedListener) SelectionRange(org.exbin.deltahex.SelectionRange) CaretPosition(org.exbin.deltahex.CaretPosition) Clipboard(java.awt.datatransfer.Clipboard) FlavorListener(java.awt.datatransfer.FlavorListener) EditationModeChangedListener(org.exbin.deltahex.EditationModeChangedListener) SelectionCapable(org.exbin.deltahex.capability.SelectionCapable)

Aggregations

CaretCapable (org.exbin.deltahex.capability.CaretCapable)6 CaretPosition (org.exbin.deltahex.CaretPosition)4 EditationModeCapable (org.exbin.deltahex.capability.EditationModeCapable)2 Cursor (java.awt.Cursor)1 Clipboard (java.awt.datatransfer.Clipboard)1 FlavorEvent (java.awt.datatransfer.FlavorEvent)1 FlavorListener (java.awt.datatransfer.FlavorListener)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 IOException (java.io.IOException)1 CaretMovedListener (org.exbin.deltahex.CaretMovedListener)1 CodeType (org.exbin.deltahex.CodeType)1 EditationMode (org.exbin.deltahex.EditationMode)1 EditationModeChangedListener (org.exbin.deltahex.EditationModeChangedListener)1 SelectionChangedListener (org.exbin.deltahex.SelectionChangedListener)1 SelectionRange (org.exbin.deltahex.SelectionRange)1 SelectionCapable (org.exbin.deltahex.capability.SelectionCapable)1 BinaryDataCommand (org.exbin.deltahex.operation.BinaryDataCommand)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