Search in sources :

Example 61 with BadLocationException

use of javax.swing.text.BadLocationException in project mc-dev by Bukkit.

the class ServerGUI method a.

public void a(JTextArea jtextarea, JScrollPane jscrollpane, String s) {
    if (!SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new ServerGuiInvokeRunnable(this, jtextarea, jscrollpane, s));
    } else {
        Document document = jtextarea.getDocument();
        JScrollBar jscrollbar = jscrollpane.getVerticalScrollBar();
        boolean flag = false;
        if (jscrollpane.getViewport().getView() == jtextarea) {
            flag = (double) jscrollbar.getValue() + jscrollbar.getSize().getHeight() + (double) (a.getSize() * 4) > (double) jscrollbar.getMaximum();
        }
        try {
            document.insertString(document.getLength(), s, (AttributeSet) null);
        } catch (BadLocationException badlocationexception) {
            ;
        }
        if (flag) {
            jscrollbar.setValue(Integer.MAX_VALUE);
        }
    }
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException) JScrollBar(javax.swing.JScrollBar)

Example 62 with BadLocationException

use of javax.swing.text.BadLocationException in project enclojure by EricThorsen.

the class ClojureCodeCompletionItem method doSubstitute.

private void doSubstitute(final JTextComponent component, final String toAdd, final int backOffset) {
    final StyledDocument doc = (StyledDocument) component.getDocument();
    class AtomicChange implements Runnable {

        public void run() {
            int caretOffset = component.getCaretPosition();
            String cname = component.getClass().getName();
            Boolean _isReplPanel = false;
            if (cname.equals("javax.swing.JEditorPane"))
                _isReplPanel = true;
            String value = getText();
            String javaList = "";
            if (toAdd != null) {
                value += toAdd;
            }
            try {
                String c = component.getText(caretOffset - 1, 1);
                if (//java package containing the Class at the end
                !_isClojure && _isNamespaceOrPkg) {
                    value = setValueForClass(value, component);
                    javaList = getJavaImportListStr(value);
                    if ((javaList.contains("(")) && (!_isReplPanel)) {
                        int messageret = (JOptionPane.showConfirmDialog(component, "Do you want to add the import " + value + " to your ns imports?", "add import ?", 0));
                        if (messageret == 0) {
                            PersistentArrayMap entry = addImportList(component, javaList);
                            //component.setCaretPosition(caretOffset + javaList.length());
                            value = getClassPart(value);
                            if (entry != null) {
                                String origNS = (String) entry.get(Keyword.intern(Symbol.create("orignodestr")));
                                String newNS = (String) entry.get(Keyword.intern(Symbol.create("newnodestr")));
                                int insertOffset = newNS.length() - origNS.length();
                                _carretOffset = _carretOffset + insertOffset;
                                _dotOffset = _dotOffset + insertOffset;
                            }
                        }
                    }
                }
                if (!c.equals("/") && (_layout != backSlashAfterParen) && (_layout != backSlashNoParen)) {
                    switch(_layout) {
                        case LowercaseAfterParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case UppercaseAfterParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case UppercaseAfterParenWithDot:
                            if (//java method
                            _isMethodOrFunction && (!_isClojure)) {
                                if (_isConstructor) {
                                    value = "";
                                } else if (Character.isLetter(value.charAt(0))) {
                                    {
                                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                        value = "." + value;
                                    }
                                }
                            } else
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case LowercaseAfterParenWithDot:
                            if (//java method
                            _isMethodOrFunction && (!_isClojure)) {
                                if (_isConstructor) {
                                    value = "";
                                } else if (Character.isLetter(value.charAt(0))) {
                                    {
                                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                        value = "." + value;
                                    }
                                }
                            } else
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case backSlashAfterParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case FirstDotAfterParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case FirstDotNoParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case UppercaseNoParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case UppercaseNoParenWithDot:
                            if (//java method
                            _isMethodOrFunction && (!_isClojure)) {
                                if (_isConstructor) {
                                    value = "";
                                } else if (Character.isLetter(value.charAt(0))) {
                                    {
                                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                        value = "." + value;
                                    }
                                }
                            } else
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case LowercaseNoParenWithDot:
                            if (//java method
                            _isMethodOrFunction && (!_isClojure)) {
                                if (_isConstructor) {
                                    value = "";
                                } else if (Character.isLetter(value.charAt(0))) {
                                    {
                                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                        value = "." + value;
                                    }
                                }
                            } else
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case LowercaseNoParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                    }
                    doc.insertString(_dotOffset, value, null);
                } else {
                    if (!c.equals("/")) {
                        int backSlashOffset = indexOfBackSlash(component);
                        if (backSlashOffset == -1)
                            backSlashOffset = caretOffset;
                        if (_isStatic || _layout == backSlashAfterParen || _layout == backSlashNoParen) {
                            if (_isClojure && _isMethodOrFunction) {
                                if (isInCurrentNamespace(_fullclassname, component)) {
                                    doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                    doc.insertString(_dotOffset, setValue(value), null);
                                } else {
                                    doc.remove(backSlashOffset + 1, caretOffset - backSlashOffset - 1);
                                    doc.insertString(backSlashOffset + 1, value, null);
                                }
                            } else {
                                doc.remove(backSlashOffset + 1, caretOffset - backSlashOffset - 1);
                                doc.insertString(backSlashOffset + 1, value, null);
                            }
                        } else {
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            doc.insertString(_dotOffset, setValue(value), null);
                        }
                    } else if (_isStatic || _layout == backSlashAfterParen || _layout == backSlashNoParen)
                        if (_isClojure && _isMethodOrFunction) {
                            if (isInCurrentNamespace(_fullclassname, component)) {
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                doc.insertString(_dotOffset, setValue(value), null);
                            } else
                                doc.insertString(_carretOffset, value, null);
                        } else
                            doc.insertString(_carretOffset, value, null);
                    else {
                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                        doc.insertString(_dotOffset, setValue(value), null);
                    }
                }
                component.setCaretPosition(component.getCaretPosition() - backOffset);
            } catch (BadLocationException e) {
                LOG.log(Level.FINEST, e.getMessage());
            }
        }
    }
    AtomicChange change = new AtomicChange();
    try {
        NbDocument.runAtomicAsUser(doc, change);
    } catch (BadLocationException ex) {
        LOG.log(Level.FINEST, ex.getMessage());
    }
}
Also used : PersistentArrayMap(clojure.lang.PersistentArrayMap) StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 63 with BadLocationException

use of javax.swing.text.BadLocationException in project Botnak by Gocnak.

the class ListenerUserChat method showSuggestion.

protected void showSuggestion() {
    hideSuggestion();
    int position = userChat.getCaretPosition();
    Point location;
    try {
        location = userChat.modelToView(position).getLocation();
    } catch (BadLocationException e2) {
        e2.printStackTrace();
        return;
    }
    String text = userChat.getText();
    int start = Math.max(0, position - 1);
    while (start > 0) {
        if (!(text.charAt(start) == '@' || text.charAt(start) == ' ')) {
            start--;
        } else {
            break;
        }
    }
    if (start > position) {
        return;
    }
    String subText = text.substring(start, position).toLowerCase();
    if ("".equals(subText)) {
        //|| !subText.contains("@")) {
        shouldShow = false;
        return;
    }
    String subWord = subText.replace("@", "");
    subWord = subWord.trim();
    if (subWord.length() < 2) {
        return;
    }
    User[] users = Settings.channelManager.getUsers(subWord);
    if (users.length > 0) {
        ArrayList<String> names = new ArrayList<>();
        for (User u : users) {
            names.add(u.getLowerNick());
        }
        suggestion = new SuggestionPanel(position, subWord, location, names.toArray(new String[names.size()]));
        SwingUtilities.invokeLater(userChat::requestFocusInWindow);
    }
}
Also used : User(lib.pircbot.User) ArrayList(java.util.ArrayList) BadLocationException(javax.swing.text.BadLocationException)

Example 64 with BadLocationException

use of javax.swing.text.BadLocationException in project enclojure by EricThorsen.

the class ReplPanel method resultReceived.

public void resultReceived(JEditorPane pane, String result) {
    Document doc = (Document) pane.getDocument();
    try {
        if (pane != _replEditorPane)
            result = "\n" + result;
        doc.insertString(doc.getLength(), result, null);
        pane.setCaretPosition(doc.getLength());
    } catch (BadLocationException ex) {
        Logger.getLogger(ReplPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (pane == jEditorPaneOut && result.contains("Listening for transport dt_socket at address")) {
        //Listening for transport dt_socket at address: 58896
        String portString = result.substring(result.indexOf(":") + 1);
        _debugPort = Integer.parseInt(portString.trim());
    }
    if (pane == _replEditorPane) {
        _promptPos = _replEditorPane.getDocument().getLength();
        jTabbedPane1.setSelectedIndex(0);
        _undoManager.die();
    }
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 65 with BadLocationException

use of javax.swing.text.BadLocationException in project intellij-community by JetBrains.

the class SwingHelper method scrollToReference.

public static boolean scrollToReference(JEditorPane view, String reference) {
    reference = StringUtil.trimStart(reference, "#");
    List<String> toCheck = Arrays.asList("a", "h1", "h2", "h3", "h4");
    Document document = view.getDocument();
    if (document instanceof HTMLDocument) {
        List<Element> list = new ArrayList<>();
        for (Element root : document.getRootElements()) {
            getAllElements(root, list, toCheck);
        }
        for (Element element : list) {
            AttributeSet attributes = element.getAttributes();
            String nm = (String) attributes.getAttribute(HTML.Attribute.NAME);
            if (nm == null)
                nm = (String) attributes.getAttribute(HTML.Attribute.ID);
            if ((nm != null) && nm.equals(reference)) {
                try {
                    int pos = element.getStartOffset();
                    Rectangle r = view.modelToView(pos);
                    if (r != null) {
                        Rectangle vis = view.getVisibleRect();
                        r.y -= 5;
                        r.height = vis.height;
                        view.scrollRectToVisible(r);
                        return true;
                    }
                } catch (BadLocationException ex) {
                //ignore
                }
            }
        }
    }
    return false;
}
Also used : AttributeSet(javax.swing.text.AttributeSet) HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document) 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