001    package org.objectweb.jac.aspects.gui.swing;
002    
003    import javax.swing.*;
004    import java.awt.*;
005    import java.awt.image.*;
006    
007    /* numberPanel is a JPanel used by JAVAEditorPane to display linenumbers.
008     */
009     
010    public class NumberPanel extends JComponent implements Runnable
011    {
012       BufferedImage buffer;
013       Graphics2D gc;
014       String text;
015       FontMetrics fm;
016       
017       boolean painting = false;
018       boolean needRepaint = false;
019       
020       int lineToMark = -1;
021       
022       JScrollPane sp;
023       
024       Thread paint = null;
025       
026       NumberPanel()
027       {
028          setDoubleBuffered(false);
029          setFont(new Font("MonoSpaced", Font.PLAIN, 12));
030          
031       }
032       
033       public void setJScrollPane(JScrollPane s)
034       {
035          sp = s;
036       }
037       
038       public void run()
039       {
040          repaint();
041       }
042          
043       public void paint(Graphics g)
044       {
045          /*buffer = createImage(getSize().width, getSize().height);
046          Graphics gc = buffer.getGraphics();*/
047          
048          if (buffer == null || buffer.getWidth(null) < getSize().width || buffer.getHeight(null) < getSize().height)
049          {
050             buffer = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_3BYTE_BGR);
051             gc = buffer.createGraphics();
052             
053             gc.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
054             fm = gc.getFontMetrics();
055          }
056          
057          //g.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
058          gc.setColor(getBackground());
059          gc.fillRect(0, 0, getSize().width, getSize().height);
060          gc.setColor(new Color(200, 0, 0));
061          
062          fm = g.getFontMetrics();
063          /*if(needRepaint)
064          {/*
065             int lines = 1;
066             for(int i = 0; i < text.length(); i ++)
067             {
068                if(text.charAt(i) == '\n')lines ++;
069             }
070             
071             int posY = fm.getHeight();
072             
073             for(int i = 1; i <= lines; i ++)
074             {
075                gc.drawString("" + i, 55 - fm.charsWidth(("" + i).toCharArray(), 0, ("" + i).length()), posY);
076                if (lineToMark == i)
077                {
078                   gc.setColor(Color.green);
079                   gc.fillPolygon(new int[]
080                   {
081                      58, 63, 58
082                   }, new int[]
083                   {
084                      posY - (int)(fm.getHeight() / 1.5d), posY - (int)(fm.getHeight() / 3d), posY
085                   },
086                   3);
087                   
088                   gc.setColor(new Color(200, 0, 0));      
089                }
090                posY += fm.getHeight();
091             }*/
092             
093             int posY = fm.getHeight();
094            
095             for(int i = 0; posY < getSize().height; i ++)
096             {
097                gc.drawString("" + i, 55 - fm.charsWidth(("" + i).toCharArray(), 0, ("" + i).length()), posY);
098                if (lineToMark == i)
099                {
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 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    }