Package javax.swing.text

Examples of javax.swing.text.Highlighter$Highlight


        throw new UnsupportedOperationException();
    }
   
    static void highlightAll(JTextComponent text) {
        try {
            Highlighter hl = text.getHighlighter();
            hl.removeAllHighlights();
            hl.addHighlight(0, text.getDocument().getLength(),
                            new DefaultHighlighter.DefaultHighlightPainter(
                                            text.getSelectionColor()));
        }
        catch (BadLocationException ex) {
            ex.printStackTrace();
View Full Code Here


    }

    public void clearAllMarkers() {
        markers.clear();
        highlights.clear();
        Highlighter hl = textComponent.getHighlighter();
        hl.removeAllHighlights();
    }
View Full Code Here

    }

    public void clearMarker(String mark) {
        markers.remove(mark);
        Collection<Object> tags = highlights.removeAll(mark);
        Highlighter hl = textComponent.getHighlighter();
        for (Object tag : tags) {
            hl.removeHighlight(tag);
        }
    }
View Full Code Here

        markers.put(mark, color);
        highlightMark(mark, color);
    }

    private void highlightMark(String mark, MarkerColor color) {
        Highlighter hl = textComponent.getHighlighter();
        Document doc = textComponent.getDocument();
        // We get the text from the document, rather than from getText().
        // The reason is that otherwise we will get offsets in the case where
        // the text contains CRLFs, and the document contains only
        // LFs. This can happen e.g. if the text component was populated from
        // a file, using its read() method.
        String text = DocumentUtils.getText(doc, 0, doc.getLength());
        try {
            int pos = 0;
            while (pos < doc.getLength()) {
                int index = text.indexOf(mark, pos);
                if (index == -1) {
                    break;
                }
                DefaultHighlighter.DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(
                                color.color);
                int end = index + mark.length();
                Object tag = hl.addHighlight(index, end, painter);
                highlights.put(mark, tag);
                pos = end;
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
View Full Code Here

            highlight(startAt, noMatchPainter);
        }

        private void highlight(int offset, HighlightPainter painter) {
            try {
                Highlighter hl = textComponent.getHighlighter();
                Object tag = hl.addHighlight(offset, offset + 1, painter);
                highlights.add(tag);
            }
            catch (BadLocationException ex) {
                // Nothing to do
            }
View Full Code Here

                // Nothing to do
            }
        }

        public void reset(boolean removeAll) {
            Highlighter hl = textComponent.getHighlighter();
            if (removeAll) {
                hl.removeAllHighlights();
            }
            else {
                for (Object tag : highlights) {
                    hl.removeHighlight(tag);
                }
                highlights.clear();
            }
        }
View Full Code Here

  // The code gets complicated because of the need to be efficient.
  // We don't want to call the tokenizer unless necessary.  We especially
  // don't want to retokenize the whole document unless necessary.
  public void caretUpdate(javax.swing.event.CaretEvent e) {
    EditorArea<?> source = (EditorArea<?>) e.getSource();
    Highlighter highlighter = source.getHighlighter();
    removeOldHighlights(highlighter);
    int dot = e.getDot();
    // only highlight if there is no selection and there is
    // a character to the left
    if (dot != e.getMark() || dot == 0) {
View Full Code Here

    }
   
    // Highlights all occurrences found of the specified pattern.
    public void search(String pattern, boolean caseSensitive) throws PatternSyntaxException {
        pos = -1;
        Highlighter highlighter = comp.getHighlighter();
       
        // Remove any existing highlights for last search
        Highlighter.Highlight[] highlights = highlighter.getHighlights();
        for (int i = 0; i < highlights.length; i++) {
            Highlighter.Highlight h = highlights[i];
            if (h.getPainter() == this.matchPainter ||
                    h.getPainter() == this.selectionPainter) {
                highlighter.removeHighlight(h);
            }
        }
       
        if (pattern == null || "".equals(pattern))
            return;
       
        // Look for the pattern we are given - insensitive search
        String content = null;
        try {
            Document d = comp.getDocument();
            content = d.getText(0, d.getLength());
        } catch (BadLocationException e) {
            // Cannot happen
            return;
        }
       
        int flags = Pattern.DOTALL | Pattern.MULTILINE;
        if (!caseSensitive) flags |= Pattern.CASE_INSENSITIVE;
        Pattern p = Pattern.compile(pattern, flags);
        Matcher m = p.matcher(content);
        while (m.find()) {
            for (int i=(m.groupCount()>0?1:0); i<=m.groupCount(); i++) {
                try {
                    highlighter.addHighlight(m.start(i), m.end(i), matchPainter);
                } catch (BadLocationException e) {}
            }
        }
    }
View Full Code Here

            }
        }
    }
   
    public int previousMatch() {
        Highlighter highlighter = comp.getHighlighter();
        Highlighter.Highlight[] highlights = highlighter.getHighlights();
        Highlighter.Highlight last = null, previous = null, current = null;
       
        for (int i = 0; i < highlights.length; i++) {
            Highlighter.Highlight h = highlights[i];
            if (h.getPainter() == matchPainter) {
                if (last == null || h.getStartOffset() > last.getStartOffset())
                    last = h;
                if (h.getStartOffset() < pos) {
                    if (previous == null) {
                        previous = h;
                    } else if (previous.getStartOffset() < h.getStartOffset()) {
                        previous = h;
                    }
                }
            } else if (h.getPainter() == selectionPainter) {
                current = h;
            }
        }
       
        if (previous == null)
            previous = last;
        if (previous == null)
            previous = current;
       
        if (previous == null) {
            pos = -1;
        } else {
            if (previous != current)
                try {
                    if (current != null) {
                        highlighter.removeHighlight(current);
                        highlighter.addHighlight(current.getStartOffset(), current.getEndOffset(), matchPainter);
                    }
                    highlighter.removeHighlight(previous);
                    highlighter.addHighlight(previous.getStartOffset(), previous.getEndOffset(), selectionPainter);
                    center(previous);
                } catch (BadLocationException ble) {
                    // impossible
                }
            pos = previous.getStartOffset();
View Full Code Here

        }
        return pos;
    }
   
    public int nextMatch() {
        Highlighter highlighter = comp.getHighlighter();
        Highlighter.Highlight[] highlights = highlighter.getHighlights();
        Highlighter.Highlight first = null, next = null, current = null;
       
        for (int i = 0; i < highlights.length; i++) {
            Highlighter.Highlight h = highlights[i];
            if (h.getPainter() == matchPainter) {
                if (first == null || h.getStartOffset() < first.getStartOffset())
                    first = h;
                if (h.getStartOffset() > pos) {
                    if (next == null) {
                        next = h;
                    } else if (next.getStartOffset() > h.getStartOffset()) {
                        next = h;
                    }
                }
            } else if (h.getPainter() == selectionPainter) {
                current = h;
            }
        }
       
        if (next == null)
            next = first;
        if (next == null)
            next = current;
       
        if (next == null) {
            pos = -1;
        } else {
            if (next != current)
                try {
                    if (current != null) {
                        highlighter.removeHighlight(current);
                        highlighter.addHighlight(current.getStartOffset(), current.getEndOffset(), matchPainter);
                    }
                    highlighter.removeHighlight(next);
                    highlighter.addHighlight(next.getStartOffset(), next.getEndOffset(), selectionPainter);
                    center(next);
                } catch (BadLocationException ble) {
                    // impossible
                }
            pos = next.getStartOffset();
View Full Code Here

TOP

Related Classes of javax.swing.text.Highlighter$Highlight

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.