@Override protected int drawUnselectedText(Graphics graphics, int x, int y, int p0, int p1) throws BadLocationException { try { Document doc = getDocument(); String text = doc.getText(p0, p1 - p0); Segment segment = getLineBuffer(); int initialXpos = x; SortedMap<Integer, Integer> startMap = new TreeMap<Integer, Integer>(); SortedMap<Integer, Color> colorMap = new TreeMap<Integer, Color>(); // Match all regexes on this snippet, store positions for (Map.Entry<Pattern, Color> entry : patternColors.entrySet()) { Matcher matcher = entry.getKey().matcher(text); while (matcher.find()) { startMap.put(matcher.start(1), matcher.end(1)); colorMap.put(matcher.start(1), entry.getValue()); } } // TODO: check the map for overlapping parts int i = 0; // add tag highlighted background colors if (!TAG_HIGHLIGHTED.isEmpty()) { Matcher highlightMatcher = Pattern.compile(TAG_HIGHLIGHTED).matcher(text); while (highlightMatcher.find()) { int start = highlightMatcher.start(1); int end = highlightMatcher.end(1); if (i < start) { graphics.setColor(Color.black); doc.getText(p0 + i, start - i, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, i); } graphics.setColor(TAG_HIGHLIGHTED_COLOR); i = end; doc.getText(p0 + start, i - start, segment); int width = Utilities.getTabbedTextWidth( segment, graphics.getFontMetrics(), x, this, p0 + start); // graphics.drawLine(x, y, width, y);graphics.getFontMetrics() graphics.fillRect( x, y - graphics.getFontMetrics().getHeight() + 2, width, graphics.getFontMetrics().getHeight()); graphics.setColor(Color.black); doc.getText(p0 + start, i - start, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, start); } } x = initialXpos; i = 0; // add highlighted background colors based on position // String textx = doc.getText(p0, p1 - p0); if ((HIGHLIGHTED_START < p1 && HIGHLIGHTED_START >= p0) || (HIGHLIGHTED_END <= p1 && HIGHLIGHTED_END > p0) || (HIGHLIGHTED_START < p1 && HIGHLIGHTED_END > p0)) { // select whole line int start = 0; int end = text.length(); // test to see if only partial line is needed. if (HIGHLIGHTED_START > p0) start = HIGHLIGHTED_START - p0; if (HIGHLIGHTED_END < p1) end -= p1 - HIGHLIGHTED_END; if (i < start) { // fill in normal color if start highlight isn't at the beginning graphics.setColor(Color.black); doc.getText(p0 + i, start - i, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, i); } graphics.setColor(HIGHLIGHTED_COLOR); // fill in the highlight color i = end; if (i - start > 0) { doc.getText(p0 + start, i - start, segment); int width = Utilities.getTabbedTextWidth( segment, graphics.getFontMetrics(), x, this, p0 + start); // graphics.drawLine(x, y, width, y);graphics.getFontMetrics() graphics.fillRect( x, y - graphics.getFontMetrics().getHeight() + 2, width, graphics.getFontMetrics().getHeight()); graphics.setColor(Color.black); doc.getText(p0 + start, i - start, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, start); } // else // System.out.println("invalid highlighting " + (i - start) + " is <= 0 (" + p0 + "-" + // p1 + "=" + (p1 - p0) +") " + start + ", " + end + " len=" + text.length()); } x = initialXpos; i = 0; // Color the parts of xml foreground font for (Map.Entry<Integer, Integer> entry : startMap.entrySet()) { int start = entry.getKey(); int end = entry.getValue(); if (i < start) { graphics.setColor(Color.black); doc.getText(p0 + i, start - i, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, i); } graphics.setColor(colorMap.get(start)); i = end; doc.getText(p0 + start, i - start, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, start); } // Paint possible remaining text black if (i < text.length()) { graphics.setColor(Color.black); doc.getText(p0 + i, text.length() - i, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, i); } } catch (Exception e) { e.printStackTrace(); } return x; }