Search in sources :

Example 71 with Element

use of javax.swing.text.Element in project blue by kunstmusik.

the class CsoundOrcCompletionProvider method getRowFirstNonWhite.

// if (isCsoundVariable(word)) {
// ArrayList varMatches = findMatches(getTextBeforeWord(word),
// word);
// options.addAll(varMatches);
// }
// 
// Collections.<String>sort(options);
static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException {
    Element lineElement = doc.getParagraphElement(offset);
    int start = lineElement.getStartOffset();
    while (start + 1 < lineElement.getEndOffset()) {
        try {
            if (doc.getText(start, 1).charAt(0) != ' ') {
                break;
            }
        } catch (BadLocationException ex) {
            throw (BadLocationException) new BadLocationException("calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start).initCause(ex);
        }
        start++;
    }
    return start;
}
Also used : Element(javax.swing.text.Element) BadLocationException(javax.swing.text.BadLocationException)

Example 72 with Element

use of javax.swing.text.Element in project energy3d by concord-consortium.

the class MyEditorPane method setText.

public void setText(final String text) {
    editorPane.setText(text);
    if (editorPane.getDocument() instanceof HTMLDocument) {
        final HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
        final ElementIterator it = new ElementIterator(doc);
        Element element;
        while ((element = it.next()) != null) {
            final AttributeSet as = element.getAttributes();
            final Enumeration<?> en = as.getAttributeNames();
            DefaultButtonModel buttonModel = null;
            PlainDocument document = null;
            String action = null;
            String question = null;
            String choice = null;
            String key = null;
            String dataName = null;
            while (en.hasMoreElements()) {
                final Object n = en.nextElement();
                final Object v = as.getAttribute(n);
                if (v instanceof DefaultButtonModel) {
                    buttonModel = (DefaultButtonModel) v;
                } else if (v instanceof PlainDocument) {
                    document = (PlainDocument) v;
                } else if (n.toString().equals("action")) {
                    action = v.toString();
                } else if (n.toString().equals("question")) {
                    question = v.toString();
                } else if (n.toString().equals("choice")) {
                    choice = v.toString();
                } else if (n.toString().equals("key")) {
                    key = v.toString();
                } else if (n.toString().equals("data")) {
                    dataName = v.toString();
                }
            }
            if (action != null) {
                final String a = action;
                if (document != null) {
                    final String n = dataName;
                    final PlainDocument d = document;
                    document.addDocumentListener(new DocumentListener() {

                        @Override
                        public void removeUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }

                        @Override
                        public void insertUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }

                        @Override
                        public void changedUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }
                    });
                }
                if (buttonModel != null) {
                    final QuestionnaireModel qm;
                    if (question != null && choice != null) {
                        boolean isKey = false;
                        if ("yes".equalsIgnoreCase(key) || "true".equalsIgnoreCase(key)) {
                            isKey = true;
                        }
                        qm = new QuestionnaireModel(question, choice, isKey);
                    } else {
                        qm = null;
                    }
                    final DefaultButtonModel bm = buttonModel;
                    if (buttonModel instanceof JToggleButton.ToggleButtonModel) {
                        buttonModel.addItemListener(new ItemListener() {

                            @Override
                            public void itemStateChanged(final ItemEvent e) {
                                if (qm != null) {
                                    // fire only one for questionnaires
                                    if (e.getStateChange() == ItemEvent.SELECTED) {
                                        buttonActionPerformed(a, qm, bm);
                                    }
                                } else {
                                    buttonActionPerformed(a, qm, bm);
                                }
                            }
                        });
                    } else {
                        buttonModel.addActionListener(new ActionListener() {

                            @Override
                            public void actionPerformed(final ActionEvent e) {
                                buttonActionPerformed(a, qm, bm);
                            }
                        });
                    }
                }
            }
        }
    }
}
Also used : ElementIterator(javax.swing.text.ElementIterator) DocumentListener(javax.swing.event.DocumentListener) DefaultButtonModel(javax.swing.DefaultButtonModel) ItemEvent(java.awt.event.ItemEvent) HTMLDocument(javax.swing.text.html.HTMLDocument) ActionEvent(java.awt.event.ActionEvent) Element(javax.swing.text.Element) EventString(org.concord.energy3d.agents.EventString) PlainDocument(javax.swing.text.PlainDocument) DocumentEvent(javax.swing.event.DocumentEvent) ActionListener(java.awt.event.ActionListener) AttributeSet(javax.swing.text.AttributeSet) ItemListener(java.awt.event.ItemListener) QuestionnaireModel(org.concord.energy3d.agents.QuestionnaireModel)

Example 73 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method setPreferredWidth.

/**
 *  Calculate the width needed to display the maximum line number
 */
private void setPreferredWidth() {
    Element root = component.getDocument().getDefaultRootElement();
    int lines = root.getElementCount();
    int digits = Math.max(String.valueOf(lines).length(), minimumDisplayDigits);
    if (lastDigits != digits) {
        lastDigits = digits;
        FontMetrics fontMetrics = getFontMetrics(getFont());
        int width = fontMetrics.charWidth('0') * digits;
        Insets insets = getInsets();
        int preferredWidth = insets.left + insets.right + width;
        Dimension d = getPreferredSize();
        d.setSize(preferredWidth, HEIGHT);
        setPreferredSize(d);
        setSize(d);
    }
}
Also used : Insets(java.awt.Insets) FontMetrics(java.awt.FontMetrics) Element(javax.swing.text.Element) Dimension(java.awt.Dimension) Point(java.awt.Point)

Example 74 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method caretUpdate.

// 
// Implement CaretListener interface
// 
@Override
public void caretUpdate(CaretEvent e) {
    // Get the line the caret is positioned on
    int caretPosition = component.getCaretPosition();
    Element root = component.getDocument().getDefaultRootElement();
    int currentLine = root.getElementIndex(caretPosition);
    if (lastLine != currentLine) {
        repaint();
        lastLine = currentLine;
    }
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

Example 75 with Element

use of javax.swing.text.Element in project omegat by omegat-org.

the class DictionariesTextArea method callDictionary.

/**
 * Move position in pane to the currently selected word.
 */
protected void callDictionary(String word) {
    UIThreadsUtil.mustBeSwingThread();
    HTMLDocument doc = (HTMLDocument) getDocument();
    // multiple entry can be existed for each query words,
    // try to get first one.
    int index = displayedWords.indexOf(word.toLowerCase());
    if (index == -1 && manager.doFuzzyMatching()) {
        // when not found and fuzzy matching allowed,retry with stemmed word
        String[] stemmed = manager.getStemmedWords(word);
        if (stemmed.length == 0) {
            return;
        }
        index = displayedWords.indexOf(stemmed[0]);
        if (index == -1) {
            return;
        }
    }
    Element el = doc.getElement(Integer.toString(index));
    if (el == null) {
        return;
    }
    int start = el.getStartOffset();
    // When trying to select the last word, Swing cannot make the rectangle
    // if the end is pointing past the last character.
    int end = Math.max(el.getEndOffset() - 1, start);
    try {
        // Start position of article
        Rectangle startRect = Java8Compat.modelToView(this, start);
        // End position of article
        Rectangle endRect = Java8Compat.modelToView(this, end);
        // when initiating scroll from below the target article.
        if (endRect != null) {
            scrollRectToVisible(endRect);
        }
        if (startRect != null) {
            scrollRectToVisible(startRect);
        }
    } catch (BadLocationException ex) {
        Log.log(ex);
    }
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) Rectangle(java.awt.Rectangle) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

Element (javax.swing.text.Element)100 BadLocationException (javax.swing.text.BadLocationException)35 Point (java.awt.Point)19 AttributeSet (javax.swing.text.AttributeSet)15 Document (javax.swing.text.Document)15 HTMLDocument (javax.swing.text.html.HTMLDocument)11 FontMetrics (java.awt.FontMetrics)6 Dimension (java.awt.Dimension)5 Rectangle (java.awt.Rectangle)5 View (javax.swing.text.View)5 Font (java.awt.Font)4 Insets (java.awt.Insets)4 IOException (java.io.IOException)4 AbstractDocument (javax.swing.text.AbstractDocument)4 StyledDocument (javax.swing.text.StyledDocument)4 java.awt (java.awt)3 Objects (java.util.Objects)3 javax.swing (javax.swing)3 AbstractElement (javax.swing.text.AbstractDocument.AbstractElement)3 HTML (javax.swing.text.html.HTML)3