Search in sources :

Example 31 with BadLocationException

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

the class JTextPaneAppender method append.

/**
     * @see
     * org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
     */
@Override
public void append(LoggingEvent event) {
    if (myTextPane == null) {
        LogLog.warn("TextPane is not initialized");
        return;
    }
    // if myTextPane == null
    String text = this.layout.format(event);
    String[] stackTrace = event.getThrowableStrRep();
    if (stackTrace != null) {
        StringBuffer sb = new StringBuffer(text);
        for (int i = 0; i < stackTrace.length; i++) {
            sb.append("    ").append(stackTrace[i]).append("\n");
        }
        // for i
        text = sb.toString();
    }
    StyledDocument myDoc = myTextPane.getStyledDocument();
    try {
        myDoc.insertString(myDoc.getLength(), text, myAttributeSet.get(event.getLevel().toString()));
    } catch (BadLocationException badex) {
        // can't log this, as it would be recursive error
        System.err.println(badex);
    }
    myTextPane.setCaretPosition(myDoc.getLength());
}
Also used : StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 32 with BadLocationException

use of javax.swing.text.BadLocationException in project jabref by JabRef.

the class JEditorPaneWithHighlighting method highlightPattern.

public void highlightPattern(Optional<Pattern> highlightPattern) {
    Highlighter highlighter = getHighlighter();
    highlighter.removeAllHighlights();
    if ((highlightPattern == null) || !highlightPattern.isPresent()) {
        return;
    }
    String text = getDocumentText();
    Matcher matcher = highlightPattern.get().matcher(text);
    LayerPainter painter = DefaultHighlighter.DefaultPainter;
    while (matcher.find()) {
        try {
            highlighter.addHighlight(matcher.start(), matcher.end(), painter);
        } catch (BadLocationException ble) {
            // should not occur if matcher works right
            LOGGER.warn("Highlighting not possible, bad location", ble);
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter) LayerPainter(javax.swing.text.LayeredHighlighter.LayerPainter)

Example 33 with BadLocationException

use of javax.swing.text.BadLocationException in project jabref by JabRef.

the class JTextAreaWithHighlighting method highLight.

/**
     * Highlight words in the Textarea
     *
     * @param words to highlight
     */
private void highLight() {
    // highlight all characters that appear in charsToHighlight
    Highlighter highlighter = getHighlighter();
    highlighter.removeAllHighlights();
    if ((highlightPattern == null) || !highlightPattern.isPresent()) {
        return;
    }
    String content = getText();
    if (content.isEmpty()) {
        return;
    }
    highlightPattern.ifPresent(pattern -> {
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            try {
                highlighter.addHighlight(matcher.start(), matcher.end(), DefaultHighlighter.DefaultPainter);
            } catch (BadLocationException ble) {
                LOGGER.warn("Highlighting not possible, bad location", ble);
            }
        }
    });
}
Also used : Matcher(java.util.regex.Matcher) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 34 with BadLocationException

use of javax.swing.text.BadLocationException in project processdash by dtuma.

the class AssignedToDocument method getWords.

public List<Word> getWords() {
    List<Word> result = new ArrayList();
    try {
        Word prev = null;
        Matcher m = wordPattern.matcher(getText(0, getLength()));
        while (m.find()) {
            Word w = new Word();
            w.beg = m.start();
            w.end = m.end();
            w.letters = m.start(1) != -1;
            w.prev = prev;
            if (prev != null)
                prev.next = w;
            result.add(w);
            prev = w;
        }
    } catch (BadLocationException ble) {
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) BadLocationException(javax.swing.text.BadLocationException)

Example 35 with BadLocationException

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

the class FileCompletionPopup method keyPressed.

/****** implementation of KeyListener of fileNameTextField ******/
@Override
public void keyPressed(KeyEvent e) {
    if (!isVisible()) {
        return;
    }
    int code = e.getKeyCode();
    switch(code) {
        case KeyEvent.VK_DOWN:
            setSelectNext();
            e.consume();
            break;
        case KeyEvent.VK_UP:
            setSelectPrevious();
            e.consume();
            break;
        case KeyEvent.VK_ESCAPE:
            setVisible(false);
            textField.requestFocus();
            e.consume();
            break;
    }
    if (isCompletionKey(code, textField)) {
        File file = (File) list.getSelectedValue();
        if (file != null) {
            if (file.equals(chooser.getCurrentDirectory())) {
                chooser.firePropertyChange(JFileChooser.DIRECTORY_CHANGED_PROPERTY, false, true);
            } else {
                chooser.setSelectedFiles(new File[] { file });
                chooser.setCurrentDirectory(file);
            }
            if (file.isDirectory()) {
                try {
                    Document doc = textField.getDocument();
                    doc.insertString(doc.getLength(), File.separator, null);
                } catch (BadLocationException ex) {
                    Logger.getLogger(getClass().getName()).log(Level.FINE, "Cannot append directory separator.", ex);
                }
            }
        }
        setVisible(false);
        textField.requestFocus();
        e.consume();
    }
}
Also used : Document(javax.swing.text.Document) File(java.io.File) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

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