KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bluej > editor > moe > BracketMatchPainter


1 // Copyright (c) 2000, 2005 BlueJ Group, Deakin University
2
//
3
// This software is made available under the terms of the "MIT License"
4
// A copy of this license is included with this source distribution
5
// in "license.txt" and is also available at:
6
// http://www.opensource.org/licenses/mit-license.html
7
// Any queries should be directed to Michael Kolling mik@bluej.org
8

9 package bluej.editor.moe;
10
11 import java.awt.*;
12
13 import javax.swing.text.*;
14
15 import bluej.utility.Debug;
16
17 /**
18  * Specialised highlight painter for painting matching bracket in text component as
19  * a rectangle.
20  */

21 public class BracketMatchPainter extends DefaultHighlighter.DefaultHighlightPainter
22 {
23      
24     public BracketMatchPainter(Color colour)
25     {
26         super(colour);
27     }
28         
29     /**
30      * Paints a rectangle around a matching bracket
31      * This seems to be the only method we need to over-ride.
32      *
33      * @return area highlighted
34      */

35     public Shape paintLayer(Graphics g, int begin, int end, Shape bounds, JTextComponent comp, View view)
36     {
37         g.setColor(getColor());
38         Rectangle rect = null;
39         try {
40             Shape shape = view.modelToView(begin, Position.Bias.Forward,
41                                            end,Position.Bias.Backward,
42                                            bounds);
43             rect = shape.getBounds();
44             g.drawRect(rect.x, rect.y, rect.width -1, rect.height - 1);
45         } catch (BadLocationException ble) {
46             Debug.reportError("bad location exception thrown");
47             ble.printStackTrace();
48         }
49         return rect;
50     }
51     
52 }
Popular Tags