/* * StructureBackgroundHighlighter - a jEdit BSH script that replaces the * box-around structure highlighter by highlighting the background of the match * Copyright © 2011 Björn "Vampire" Kautler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Version 1.0 */ import org.gjt.sp.jedit.textarea.StructureMatcher.Highlight; import org.gjt.sp.jedit.textarea.StructureMatcher.Match; class StructureBackgroundHighlighter extends TextAreaExtension { private Point point = new Point(); private TextArea textArea; StructureBackgroundHighlighter(TextArea textArea) { this.textArea = textArea; } public void paintValidLine(Graphics2D gfx, int screenLine, int physicalLine, int start, int end, int y) { if (!textArea.getPainter().isStructureHighlightEnabled()) { return; } Match match = textArea.getStructureMatch(); if (match != null) { int caret = textArea.getCaretPosition(); int from; int to; if (match.start < caret) { from = Math.max(match.start, start); to = Math.min(caret, end - 1); } else { from = Math.max(caret, start); to = Math.min(match.end, end - 1); } if ((from <= end) && (to >= start)) { highlight(textArea.getPainter().getStructureHighlightColor(), gfx, physicalLine, from - start, to - start, y); } } } private void highlight(Color highlightColor, Graphics2D gfx, int physicalLine, int startOffset, int endOffset, int y) { Point p = textArea.offsetToXY(physicalLine, startOffset, point); if (p == null) { // The start offset was not visible return; } int startX = p.x; p = textArea.offsetToXY(physicalLine, endOffset, point); if (p == null) { // The end offset was not visible return; } int endX = p.x; Color oldColor = gfx.getColor(); gfx.setColor(highlightColor); gfx.fillRect(startX, y, endX - startX, textArea.getPainter().getFontMetrics().getHeight() - 1); gfx.setColor(oldColor); } } class Attacher implements EBComponent { public void handleMessage(EBMessage message) { if (message instanceof EditPaneUpdate) { JEditTextArea textArea = ((EditPaneUpdate) message).getEditPane().getTextArea(); Object what = ((EditPaneUpdate) message).getWhat(); if (what == EditPaneUpdate.CREATED) { StructureBackgroundHighlighter highlighter = new StructureBackgroundHighlighter(textArea); textArea.getPainter().addExtension(TextAreaPainter.BRACKET_HIGHLIGHT_LAYER, highlighter); textArea.putClientProperty(StructureBackgroundHighlighter.class, highlighter); Iterator iter = Arrays.asList(textArea.getPainter().getExtensions()).iterator(); while (iter.hasNext()) { TextAreaExtension extension = (TextAreaExtension) iter.next(); if (extension instanceof Highlight) { textArea.getPainter().removeExtension(extension); } } textArea.revalidate(); } else if (what == EditPaneUpdate.DESTROYED) { StructureBackgroundHighlighter highlighter = (StructureBackgroundHighlighter) textArea.getClientProperty(StructureBackgroundHighlighter.class); if (highlighter != null) { textArea.getPainter().removeExtension(highlighter); textArea.putClientProperty(StructureBackgroundHighlighter.class, null); } } } } } EditBus.addToBus(new Attacher());