KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > NumberPanel


1 package org.objectweb.jac.aspects.gui.swing;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.image.*;
6
7 /* numberPanel is a JPanel used by JAVAEditorPane to display linenumbers.
8  */

9  
10 public class NumberPanel extends JComponent implements Runnable JavaDoc
11 {
12    BufferedImage buffer;
13    Graphics2D gc;
14    String JavaDoc text;
15    FontMetrics fm;
16    
17    boolean painting = false;
18    boolean needRepaint = false;
19    
20    int lineToMark = -1;
21    
22    JScrollPane sp;
23    
24    Thread JavaDoc paint = null;
25    
26    NumberPanel()
27    {
28       setDoubleBuffered(false);
29       setFont(new Font("MonoSpaced", Font.PLAIN, 12));
30       
31    }
32    
33    public void setJScrollPane(JScrollPane s)
34    {
35       sp = s;
36    }
37    
38    public void run()
39    {
40       repaint();
41    }
42       
43    public void paint(Graphics g)
44    {
45       /*buffer = createImage(getSize().width, getSize().height);
46       Graphics gc = buffer.getGraphics();*/

47       
48       if (buffer == null || buffer.getWidth(null) < getSize().width || buffer.getHeight(null) < getSize().height)
49       {
50          buffer = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_3BYTE_BGR);
51          gc = buffer.createGraphics();
52          
53          gc.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
54          fm = gc.getFontMetrics();
55       }
56       
57       //g.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
58
gc.setColor(getBackground());
59       gc.fillRect(0, 0, getSize().width, getSize().height);
60       gc.setColor(new Color(200, 0, 0));
61       
62       fm = g.getFontMetrics();
63       /*if(needRepaint)
64       {/*
65          int lines = 1;
66          for(int i = 0; i < text.length(); i ++)
67          {
68             if(text.charAt(i) == '\n')lines ++;
69          }
70          
71          int posY = fm.getHeight();
72          
73          for(int i = 1; i <= lines; i ++)
74          {
75             gc.drawString("" + i, 55 - fm.charsWidth(("" + i).toCharArray(), 0, ("" + i).length()), posY);
76             if (lineToMark == i)
77             {
78                gc.setColor(Color.green);
79                gc.fillPolygon(new int[]
80                {
81                   58, 63, 58
82                }, new int[]
83                {
84                   posY - (int)(fm.getHeight() / 1.5d), posY - (int)(fm.getHeight() / 3d), posY
85                },
86                3);
87                
88                gc.setColor(new Color(200, 0, 0));
89             }
90             posY += fm.getHeight();
91          }*/

92          
93          int posY = fm.getHeight();
94         
95          for(int i = 0; posY < getSize().height; i ++)
96          {
97             gc.drawString("" + i, 55 - fm.charsWidth(("" + i).toCharArray(), 0, ("" + i).length()), posY);
98             if (lineToMark == i)
99             {
100                gc.setColor(Color.green);
101                gc.fillPolygon(new int[]
102                {
103                   58, 63, 58
104                }, new int[]
105                {
106                   posY - (int)(fm.getHeight() / 1.5d), posY - (int)(fm.getHeight() / 3d), posY
107                },
108                3);
109                
110                gc.setColor(new Color(200, 0, 0));
111             }
112             posY += fm.getHeight();
113          }
114       //}
115
//needRepaint = false;
116
((Graphics2D)g).drawImage(buffer, 0, 0, null);
117    }
118    
119    public void scrollToLine(int l)
120    {
121       lineToMark = l;
122       if(fm != null)
123       {
124          int lines = 1;
125          for(int i = 0; i < text.length(); i ++)
126          {
127             if(text.charAt(i) == '\n')lines ++;
128          }
129          
130          int posY = fm.getHeight();
131          
132          for(int i = 1; i <= lines && i != l; i ++)
133          {
134             posY += fm.getHeight();
135          }
136          
137          if (sp != null)
138          {
139             System.out.println("Line: " + l);
140
141             if(posY - fm.getHeight() < sp.getVerticalScrollBar().getValue() || posY - fm.getHeight() > sp.getVerticalScrollBar().getValue() + sp.getVerticalScrollBar().getVisibleAmount()) sp.getVerticalScrollBar().setValue(posY - fm.getHeight());
142          }
143       }
144       repaint();
145    }
146    
147    public void update(String JavaDoc t, FontMetrics f)
148    {
149       //fm = f;
150
if (!text.equals(t))
151       {
152          text = t;
153          needRepaint = true;
154          repaint();
155       }
156       /*if (paint == null)
157       {
158          paint = new Thread(this);
159          paint.start();
160       }*/

161    }
162    
163    public void update(Graphics g)
164    {
165       paint(g);
166    }
167 }
168
Popular Tags