Search in sources :

Example 11 with BadLocationException

use of javax.swing.text.BadLocationException in project antlrworks by antlr.

the class AutoCompletionMenu method completePartialWord.

public void completePartialWord(String word) {
    try {
        Document doc = getTextComponent().getDocument();
        doc.remove(insertionStartIndex, insertionEndIndex - insertionStartIndex);
        doc.insertString(insertionStartIndex, word, null);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 12 with BadLocationException

use of javax.swing.text.BadLocationException in project CoreNLP by stanfordnlp.

the class HighlightUtils method addHighlight.

/**
   * Highlight the given label from the first mouse event to the second
   * Returns true if the highlight was successful, false otherwise.
   */
public static boolean addHighlight(JTextField label, MouseEvent mouseEvent1, MouseEvent mouseEvent2) {
    FontMetrics fm = label.getFontMetrics(label.getFont());
    int firstXpos = mouseEvent1.getX();
    int lastXpos = mouseEvent2.getX();
    int firstOffset = getCharOffset(fm, label.getText(), firstXpos);
    int lastOffset = getCharOffset(fm, label.getText(), lastXpos);
    if (lastOffset != firstOffset) {
        if (firstOffset > lastOffset) {
            int tmp = firstOffset;
            firstOffset = lastOffset;
            lastOffset = tmp;
        }
        try {
            label.getHighlighter().removeAllHighlights();
            label.getHighlighter().addHighlight(firstOffset, lastOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
            return true;
        } catch (BadLocationException e1) {
            return false;
        }
    } else
        return false;
}
Also used : FontMetrics(java.awt.FontMetrics) DefaultHighlighter(javax.swing.text.DefaultHighlighter) BadLocationException(javax.swing.text.BadLocationException)

Example 13 with BadLocationException

use of javax.swing.text.BadLocationException in project binnavi by google.

the class BaseTypeTableCellRenderer method renderStruct.

private static void renderStruct(final TypeInstance instance, final StyledDocument document, final boolean renderData) {
    final Style structNameStyle = createDeclarationStyle(document);
    final Style structMemberStyle = document.addStyle("STRUCTMEMBERSTYLE", structNameStyle);
    StyleConstants.setForeground(structMemberStyle, Color.GRAY);
    final Style structContentStyle = document.addStyle("STRUCTCONTENTSTYLE", structNameStyle);
    StyleConstants.setForeground(structContentStyle, Color.BLUE);
    StyleConstants.setAlignment(structNameStyle, StyleConstants.ALIGN_RIGHT);
    final BaseType baseType = instance.getBaseType();
    int maxMemberLength = 0;
    for (final TypeMember member : baseType) {
        if (member.getBaseType().getName().length() > maxMemberLength) {
            maxMemberLength = member.getBaseType().getName().length();
        }
    }
    int maxNameLength = 0;
    for (final TypeMember member : baseType) {
        if (member.getName().length() > maxNameLength) {
            maxNameLength = member.getName().length();
        }
    }
    /* Renders type information for structures - construct a string such as:
     *
     * struct STRUCT_NAME { BASE_TYPE_NAME
     */
    try {
        document.remove(0, document.getLength());
        appendString(document, "struct " + baseType.getName() + " {\n", structNameStyle);
        long memberOffset = 0;
        for (final TypeMember member : baseType) {
            appendString(document, "  " + member.getBaseType().getName(), structNameStyle);
            final String separator = Strings.repeat(" ", maxMemberLength - member.getBaseType().getName().length() + 1);
            appendString(document, separator + member.getName(), structMemberStyle);
            appendString(document, ";", structMemberStyle);
            if (renderData) {
                final String dataSeperator = Strings.repeat(".", maxNameLength - member.getName().length() + 1);
                appendString(document, dataSeperator, structNameStyle);
                appendString(document, renderInstanceData(member.getBaseType(), instance.getAddress().getOffset() + memberOffset, instance.getSection()), createDataStyle(document));
                memberOffset += member.getBaseType().getByteSize();
            }
            appendString(document, "\n", structMemberStyle);
        }
        appendString(document, "};", structNameStyle);
    } catch (final BadLocationException exception) {
        CUtilityFunctions.logException(exception);
    }
}
Also used : BaseType(com.google.security.zynamics.binnavi.disassembly.types.BaseType) TypeMember(com.google.security.zynamics.binnavi.disassembly.types.TypeMember) Style(javax.swing.text.Style) BadLocationException(javax.swing.text.BadLocationException)

Example 14 with BadLocationException

use of javax.swing.text.BadLocationException in project android-classyshark by google.

the class DisplayArea method displayClass.

@Override
public void displayClass(String classString) {
    displayDataState = DisplayDataState.INSIDE_CLASS;
    try {
        String currentText = jTextPane.getDocument().getText(0, jTextPane.getDocument().getLength());
        if (currentText.equals(getOneColorFormattedOutput(classString))) {
            return;
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    clearText();
    StyleConstants.setFontSize(style, 18);
    Document doc = new DefaultStyledDocument();
    try {
        doc.insertString(doc.getLength(), getOneColorFormattedOutput(classString), style);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    jTextPane.setDocument(doc);
    jTextPane.setCaretPosition(1);
}
Also used : DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) Document(javax.swing.text.Document) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 15 with BadLocationException

use of javax.swing.text.BadLocationException in project zaproxy by zaproxy.

the class CustomScanDialog method getAddCustomButton.

private JButton getAddCustomButton() {
    if (addCustomButton == null) {
        addCustomButton = new JButton(Constant.messages.getString("ascan.custom.button.pt.add"));
        addCustomButton.setEnabled(false);
        addCustomButton.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                // Add the selected injection point
                int userDefStart = getRequestField().getSelectionStart();
                if (userDefStart >= 0) {
                    int userDefEnd = getRequestField().getSelectionEnd();
                    Highlighter hl = getRequestField().getHighlighter();
                    HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
                    try {
                        Highlight hlt = (Highlight) hl.addHighlight(userDefStart, userDefEnd, painter);
                        injectionPointModel.addElement(hlt);
                        // Unselect the text
                        getRequestField().setSelectionStart(userDefEnd);
                        getRequestField().setSelectionEnd(userDefEnd);
                        getRequestField().getCaret().setVisible(true);
                    } catch (BadLocationException e1) {
                        logger.error(e1.getMessage(), e1);
                    }
                }
            }
        });
    }
    return addCustomButton;
}
Also used : ActionListener(java.awt.event.ActionListener) Highlight(javax.swing.text.Highlighter.Highlight) HighlightPainter(javax.swing.text.Highlighter.HighlightPainter) JButton(javax.swing.JButton) DefaultHighlighter(javax.swing.text.DefaultHighlighter) ActionEvent(java.awt.event.ActionEvent) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Aggregations

BadLocationException (javax.swing.text.BadLocationException)88 Document (javax.swing.text.Document)24 Element (javax.swing.text.Element)13 Point (java.awt.Point)10 IOException (java.io.IOException)9 DefaultHighlighter (javax.swing.text.DefaultHighlighter)8 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)7 Highlighter (javax.swing.text.Highlighter)7 ArrayList (java.util.ArrayList)6 DocumentEvent (javax.swing.event.DocumentEvent)6 StyledDocument (javax.swing.text.StyledDocument)6 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)5 Dimension (java.awt.Dimension)4 Rectangle (java.awt.Rectangle)4 ActionEvent (java.awt.event.ActionEvent)4 ActionListener (java.awt.event.ActionListener)4 File (java.io.File)4 Date (java.util.Date)4 Style (javax.swing.text.Style)4 HTMLDocument (javax.swing.text.html.HTMLDocument)4