Search in sources :

Example 1 with IZyEditableObject

use of com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject in project binnavi by google.

the class CAbstractKeyBehavior method deleteSelection.

protected void deleteSelection() {
    if (isSelection() && isDeleteableSelection()) {
        int mouseStartX = Math.min(getCaretMousePressedX(), getCaretMouseReleasedX());
        final int mouseEndX = Math.max(getCaretMousePressedX(), getCaretMouseReleasedX());
        int mouseStartY = Math.min(getCaretMousePressedY(), getCaretMouseReleasedY());
        final int mouseEndY = Math.max(getCaretMousePressedY(), getCaretMouseReleasedY());
        int caretStartX = Math.min(getCaretStartPosX(), getCaretEndPosX());
        final int firstModelLine = m_labelContent.getFirstLineIndexOfModelAt(mouseStartY);
        final int noneCommentLine = m_labelContent.getNonPureCommentLineIndexOfModelAt(mouseStartY);
        final int lastModelLine = m_labelContent.getLastLineIndexOfModelAt(mouseStartY);
        int firstObjectLine = firstModelLine;
        int lastObjectLine = lastModelLine;
        if (// It's is not a label comment.
        noneCommentLine != -1) {
            if (// It's a above line comment.
            mouseStartY < noneCommentLine) {
                lastObjectLine = noneCommentLine - 1;
            } else // Its' a behind line comment.
            {
                firstObjectLine = noneCommentLine;
            }
        }
        StringBuilder changedText = new StringBuilder();
        ZyLineContent lineContent = getLineContent(mouseStartY);
        IZyEditableObject editObject = lineContent.getLineFragmentObjectAt(mouseStartX);
        if (editObject == null) {
            for (int lineIndex = firstObjectLine; lineIndex <= lastObjectLine; ++lineIndex) {
                final ZyLineContent curLineContent = getLineContent(lineIndex);
                editObject = curLineContent.getLineFragmentObjectAt(mouseStartX);
                if (editObject != null) {
                    break;
                }
            }
        }
        if ((firstObjectLine == lastObjectLine) && !isComment(caretStartX, mouseStartY)) {
            if (editObject.isCommentDelimiter()) {
                mouseStartX = editObject.getEnd();
                caretStartX = editObject.getEnd();
                editObject = lineContent.getLineFragmentObjectAt(caretStartX);
            }
            final String text = lineContent.getText();
            changedText = new StringBuilder(String.format("%s%s", text.substring(editObject.getStart(), mouseStartX), text.substring(mouseEndX)));
        } else {
            for (int lineIndex = firstObjectLine; lineIndex <= lastObjectLine; ++lineIndex) {
                // Iterates over each line
                final ZyLineContent curLineContent = getLineContent(lineIndex);
                IZyEditableObject curEditObject = curLineContent.getLineFragmentObjectAt(mouseStartX);
                if (curEditObject == null) {
                    curEditObject = curLineContent.getLineFragmentObjectAt(getLastLineXPos(lineIndex));
                }
                int tempMouseStartX = mouseStartX;
                int tempMouseEndX = mouseEndX;
                if (curEditObject.isCommentDelimiter()) {
                    curEditObject = curLineContent.getLineFragmentObjectAt(curEditObject.getEnd());
                    tempMouseStartX = curEditObject.getStart();
                    if (mouseEndX < tempMouseStartX) {
                        tempMouseEndX = tempMouseStartX;
                    }
                }
                final String text = curLineContent.getText();
                final int curEndX = Math.min(text.length(), tempMouseEndX);
                if ((lineIndex >= mouseStartY) && (lineIndex <= mouseEndY) && (text.length() > tempMouseStartX)) {
                    final String afterDeletion = String.format("%s%s", text.substring(curEditObject.getStart(), tempMouseStartX), text.substring(curEndX));
                    if (!afterDeletion.equals("\n") && !afterDeletion.equals("\r")) {
                        changedText.append(afterDeletion);
                    }
                } else {
                    changedText.append(curLineContent.getText(curEditObject));
                }
            }
        }
        if (changedText.toString().endsWith("\n")) {
            changedText = new StringBuilder(changedText.substring(0, changedText.length() - 1));
        }
        if (!changedText.toString().endsWith("\r")) {
            changedText.append("\r");
        }
        // Get new valid caret position.
        if ((editObject != null) && editObject.isCommentDelimiter()) {
            caretStartX = editObject.getEnd();
            mouseStartX = editObject.getEnd();
        }
        for (int lineIndex = firstObjectLine; lineIndex <= lastObjectLine; ++lineIndex) {
            lineContent = getLineContent(lineIndex);
            if ((lineContent.getText().length() >= mouseStartX) && (mouseStartY <= lineIndex)) {
                mouseStartY = lineIndex;
                caretStartX = mouseStartX;
                break;
            }
        }
        // Update object, recreate lines and set caret.
        if (editObject != null) {
            editObject.update(changedText.toString());
            getLabelContent().getLineEditor().recreateLabelLines(getLabelContent(), editObject.getPersistentModel());
        }
        setCaret(caretStartX, mouseStartX, mouseStartY, caretStartX, mouseStartX, mouseStartY);
    }
}
Also used : IZyEditableObject(com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject) ZyLineContent(com.google.security.zynamics.zylib.gui.zygraph.realizers.ZyLineContent) Point(java.awt.Point)

Example 2 with IZyEditableObject

use of com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject in project binnavi by google.

the class CAbstractKeyBehavior method updateLabelSize.

private void updateLabelSize() {
    final ZyLabelContent labelContent = getLabelContent();
    final IZyEditableObject labelModel = labelContent.getModel();
    labelContent.getLineEditor().recreateLabelLines(labelContent, labelModel);
}
Also used : ZyLabelContent(com.google.security.zynamics.zylib.gui.zygraph.realizers.ZyLabelContent) IZyEditableObject(com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject)

Example 3 with IZyEditableObject

use of com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject in project binnavi by google.

the class CBackspaceKeyBehavior method isCommentDelimiter.

private boolean isCommentDelimiter(final int x, final int y) {
    final ZyLineContent lineContent = getLineContent(y);
    final IZyEditableObject fragment = lineContent.getLineFragmentObjectAt(x);
    IZyEditableObject leftNeighbourFragment = x > 0 ? leftNeighbourFragment = lineContent.getLineFragmentObjectAt(x - 1) : null;
    return ((fragment != null) && fragment.isCommentDelimiter()) || ((leftNeighbourFragment != null) && leftNeighbourFragment.isCommentDelimiter());
}
Also used : IZyEditableObject(com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject) ZyLineContent(com.google.security.zynamics.zylib.gui.zygraph.realizers.ZyLineContent)

Example 4 with IZyEditableObject

use of com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject in project binnavi by google.

the class CBackspaceKeyBehavior method handleLineComments.

private void handleLineComments() {
    int x = getCaretEndPosX();
    final int y = getCaretMouseReleasedY();
    final ZyLineContent lineContent = getLineContent(y);
    if (m_editableObject == null) {
        // Caret is not within an editable object
        return;
    }
    final int firstModelLine = getLabelContent().getFirstLineIndexOfModelAt(y);
    final int nonCommentLine = getLabelContent().getNonPureCommentLineIndexOfModelAt(y);
    final int firstCommentLine = (nonCommentLine != -1) && m_isBehindComment ? nonCommentLine : firstModelLine;
    final String lineText = lineContent.getText();
    String changedText = "";
    if (!isCommentDelimiter(x, y)) {
        if ((lineText.length() > 0) && (lineText.length() == m_editableObject.getEnd()) && (x == lineText.length())) {
            final char lastChar = lineText.charAt(lineText.length() - 1);
            if ((lastChar == '\n') || (lastChar == '\r')) {
                x = -1;
            }
        }
        changedText = String.format("%s%s", lineText.substring(m_editableObject.getStart(), x - 1), lineText.substring(x, m_editableObject.getEnd()));
        m_caretY = y;
        m_caretX = x - 1;
        changedText = getMultilineComment(m_caretY, changedText);
    } else {
        // Caret's left hand side or caret itself is within a comment delimiter
        final List<IZyEditableObject> editableObjectList = lineContent.getLineFragmentObjectList();
        m_editableObject = editableObjectList.get(editableObjectList.size() - 1);
        if (firstCommentLine == y) {
            if (m_editableObject.getLength() == 1) {
                if (y > 0) {
                    m_caretY = y - 1;
                    final ZyLineContent prevLineContent = getLineContent(m_caretY);
                    m_caretX = prevLineContent.getText().length();
                }
                changedText = getMultilineComment(y, changedText);
            } else {
                // First comment has more than one character. Do nothing.
                m_caretX = getCaretEndPosX();
                m_caretY = getCaretMouseReleasedY();
                return;
            }
        } else {
            // Caret is not within the first comment line. Remove this line delimiter and concatenate
            // this line with the previous if existing.
            final ZyLineContent prevLineContent = getLineContent(y - 1);
            final String prevLineText = prevLineContent.getText();
            if (!prevLineText.isEmpty() && !prevLineContent.getLineObject().isPlaceholder()) {
                final IZyEditableObject prevLineFragmentObject = prevLineContent.getLineFragmentObjectAt(prevLineText.length() - 1);
                changedText = prevLineText.substring(prevLineFragmentObject.getStart(), prevLineFragmentObject.getEnd() - 1);
                m_caretY = y - 1;
                m_caretX = prevLineText.length() - 1;
                changedText = getMultilineComment(m_caretY, changedText);
            } else {
                m_caretY = y - 1;
                m_caretX = 0;
            }
        }
    }
    if (changedText.endsWith("\n")) {
        changedText = changedText.substring(0, changedText.length() - 1) + "\r";
    }
    if (m_isAboveComment) {
        m_editableObject.updateComment(changedText, ECommentPlacement.ABOVE_LINE);
    } else if (m_isBehindComment) {
        m_editableObject.updateComment(changedText, ECommentPlacement.BEHIND_LINE);
    } else if (m_isLabelComment) {
        m_editableObject.update(changedText);
    }
    getLabelContent().getLineEditor().recreateLabelLines(getLabelContent(), m_editableObject.getPersistentModel());
}
Also used : IZyEditableObject(com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject) ZyLineContent(com.google.security.zynamics.zylib.gui.zygraph.realizers.ZyLineContent)

Example 5 with IZyEditableObject

use of com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject in project binnavi by google.

the class CCharKeyBehavior method updateLabelContent.

@Override
protected void updateLabelContent() {
    if (m_wasUneditableSelection) {
        m_caretX = getCaretEndPosX();
        return;
    }
    final int x = getCaretEndPosX();
    final int y = getCaretMouseReleasedY();
    m_caretX = x;
    if (isComment(x, y)) {
        // Cursor is within an existing comment
        m_caretX += 1;
        final ZyLineContent lineContent = getLineContent(y);
        final IZyEditableObject editableObject = lineContent.getLineFragmentObjectAt(x);
        if ((editableObject == null) || editableObject.isCommentDelimiter()) {
            return;
        }
        final int textCursor = x - editableObject.getStart();
        final String text = lineContent.getText().substring(editableObject.getStart(), editableObject.getEnd());
        final String chr = String.valueOf(getEvent().getKeyChar());
        String changedText = String.format("%s%s%s", text.substring(0, textCursor), chr, text.substring(textCursor));
        if (isComment(x, y)) {
            changedText = getMultilineComment(y, changedText);
        }
        editableObject.update(changedText);
        getLabelContent().getLineEditor().recreateLabelLines(getLabelContent(), editableObject.getPersistentModel());
    } else if (isEditable(x, y)) {
        throw new RuntimeException("Not implemented yet.");
    } else {
        // Underlying object is NOT editable
        final ZyLineContent lineContent = getLineContent(y);
        if ((x == lineContent.getText().length()) && (lineContent.getLineObject() != null)) {
            // Create new behind line comment
            lineContent.getLineObject().updateComment(Character.toString(getEvent().getKeyChar()), ECommentPlacement.BEHIND_LINE);
            getLabelContent().getLineEditor().recreateLabelLines(getLabelContent(), lineContent.getLineObject().getPersistentModel());
            m_caretX = getLineContent(y).getText().length();
        }
    }
}
Also used : IZyEditableObject(com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject) ZyLineContent(com.google.security.zynamics.zylib.gui.zygraph.realizers.ZyLineContent)

Aggregations

IZyEditableObject (com.google.security.zynamics.zylib.gui.zygraph.realizers.IZyEditableObject)27 ZyLineContent (com.google.security.zynamics.zylib.gui.zygraph.realizers.ZyLineContent)24 Point (java.awt.Point)12 ZyLabelContent (com.google.security.zynamics.zylib.gui.zygraph.realizers.ZyLabelContent)2 ECommentPlacement (com.google.security.zynamics.zylib.gui.zygraph.realizers.ECommentPlacement)1