Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: diagapplet/CodeDisplayPanel.java


1   package diagapplet;
2   
3   import java.awt.*;
4   import java.util.*;
5   import diagapplet.utils.URLLoadInfoPanel;
6   import rcs.utils.URL_and_FileLoader;
7   
8   class LineInfo extends Object
9   {
10    public boolean line_starts_inside_comment;
11    public String line;
12  }
13  
14  class CodeFileInfo extends Object
15  {
16    Vector codeLinesVector = null;
17    int line_number = 0;
18    int max_line_width = 0;
19    public String fileName = null;
20  }
21  
22  /*
23   *
24   * CodeDisplayPanel
25   *
26   */
27  public class CodeDisplayPanel extends Panel
28  {
29    private boolean force_info_new = false;
30    public URLLoadInfoPanel m_loadingPanel = null;
31    boolean info_is_new = false;
32    Hashtable state_table_files = new Hashtable();
33    CodeFileInfo currentFileInfo = null;
34    Color comment_color = new Color(0,0,128);
35    int x = 0;
36    int y = 0;
37    Dimension d = null;
38    int num_lines_on_screen;
39    int total_lines;
40    Font fn;
41    FontMetrics fn_metrics; 
42    int line_height;
43    int first_line_to_show = 0;
44    int max_width;
45    long last_scroll_y_update = 0;
46    boolean use_color = true;
47  
48    public CodeDisplayPanel()
49    {
50      this(700, 500);
51    }
52  
53    public CodeDisplayPanel(int new_width, int new_height)
54    {
55      try
56      {
57        if(new_width > 1024)
58        {
59          new_width = 1024;
60        }
61        if(new_height > 1024)
62        {
63          new_height = 1024;
64        }
65        d = new Dimension(new_width, new_height);
66        resize(new_width, new_height);
67        try
68        {
69          setFont(new Font("Courier", Font.PLAIN,12));
70        }
71        catch(Exception e)
72        {
73        }
74        fn = getFont();
75        if(null != fn)
76        {
77          fn_metrics = getFontMetrics(fn);
78          line_height = fn_metrics.getHeight();
79        }
80        else
81        {
82          line_height = 10;
83        }
84        if(line_height < 10)
85        {
86          line_height = 10;
87        }
88        //line_height *= 1.2;
89        num_lines_on_screen = d.height / line_height;
90      }
91      catch(Exception e)
92      {
93        e.printStackTrace();
94      }
95      setBackground(Color.white);  
96    }
97  
98    public Dimension preferredSize()
99    {
100     return d;
101   }
102 
103   public Dimension minimumSize()
104   {
105     return d;
106   }
107 
108 
109   public void LoadCodeFile(String code_file_name)
110   {
111     try
112     {
113       boolean inside_comment = false;
114       int slash_index = -1;
115       String line;
116       if(null == code_file_name)
117       {
118         return;
119       }
120       if(code_file_name.equals("0") || code_file_name.equals("null") ||
121         code_file_name.equals("unknown") || code_file_name.equals(""))
122       {
123         info_is_new = false;
124         return;
125       }
126       if(null != currentFileInfo)
127       {
128         if(code_file_name.equals(currentFileInfo.fileName))
129         {
130           info_is_new = false;
131           return;
132         }
133       }
134       if(null == state_table_files)
135       {
136         state_table_files = new Hashtable();
137       }
138       currentFileInfo = (CodeFileInfo) state_table_files.get(code_file_name);
139       info_is_new = true;
140       if(null != currentFileInfo)
141       {
142         total_lines = currentFileInfo.codeLinesVector.size();
143         max_width = currentFileInfo.max_line_width;
144         last_scroll_y_update = 0;
145         first_line_to_show = 0;
146         repaint();
147         return;
148       }
149       info_is_new = true;
150       //System.out.println("code_file_name = "+code_file_name);
151       URL_and_FileLoader loader = new URL_and_FileLoader(code_file_name);
152       currentFileInfo = new CodeFileInfo();
153       currentFileInfo.fileName = code_file_name;
154       if(null == currentFileInfo.codeLinesVector)
155       {
156         currentFileInfo.codeLinesVector = new Vector();
157       }
158 
159 FileReadLoop: while(null != (line = loader.readLine()))
160       {
161         LineInfo li = new LineInfo();
162         li.line_starts_inside_comment = inside_comment;
163         int tab_index = line.indexOf('\t');
164         while(tab_index != -1)
165         {
166           line = line.substring(0,tab_index) +"   "+line.substring(tab_index+1);
167           tab_index = line.indexOf('\t');
168         }
169         li.line = line;
170         //System.out.println("line = "+line);
171         //System.out.println("li.line_starts_inside_comment ="+li.line_starts_inside_comment);
172         if(null != fn_metrics)
173         {
174           int current_line_width = fn_metrics.stringWidth(line);
175           if(currentFileInfo.max_line_width < current_line_width)
176           {
177             currentFileInfo.max_line_width = current_line_width;
178           }
179         }
180         currentFileInfo.codeLinesVector.addElement(li);
181         slash_index = -1;
182         while(true)
183         {
184           slash_index = line.indexOf('/',slash_index+1);
185           if(slash_index < 0 || slash_index >= line.length())
186           {
187             break;
188           }
189           //System.out.println("slash_index = "+slash_index);
190           if(inside_comment && slash_index > 0)
191           {
192             if(line.charAt(slash_index-1) == '*')
193             {
194               inside_comment = false;
195             }
196             continue;
197           }
198           if(slash_index >= line.length() -1)
199           {
200             break;
201           }
202           if(!inside_comment && line.charAt(slash_index+1) == '/')
203           {
204             continue FileReadLoop;
205           }
206           if(!inside_comment && line.charAt(slash_index+1) == '*')
207           {
208             inside_comment = true;
209           }
210         } 
211       }
212       total_lines = currentFileInfo.codeLinesVector.size();
213       max_width = currentFileInfo.max_line_width;
214       currentFileInfo.line_number = 1;
215       state_table_files.put(code_file_name, currentFileInfo);
216       last_scroll_y_update = 0;
217       first_line_to_show = 0;
218       repaint();
219     }
220     catch(Exception e)
221     {
222       e.printStackTrace();
223     }
224   }
225 
226   public void paint(Graphics g)
227   {
228     if(null == currentFileInfo)
229     {
230       return;
231     }
232     for(int i = 0; i < num_lines_on_screen && i < total_lines - first_line_to_show; i++)
233     {
234       LineInfo li = (LineInfo) currentFileInfo.codeLinesVector.elementAt(i+first_line_to_show);
235       if(currentFileInfo.line_number == i+first_line_to_show)
236       {
237         if(use_color)
238         {
239           g.setColor(Color.red);
240         }
241         else
242         {
243           g.setColor(Color.black);
244         }
245         g.fillRect(0, (int) (i*line_height+line_height*0.25), d.width, (int) (line_height*0.9));
246         if(!use_color)
247         {
248           g.setColor(Color.white);
249           g.drawString(li.line, -x, (i+1)*line_height);
250           continue;
251         }
252       }
253       if(li.line_starts_inside_comment && use_color)
254       {
255         g.setColor(comment_color);
256         g.drawString(li.line, -x, (i+1)*line_height);
257       }
258       else
259       {
260         int cpp_comment_index = li.line.indexOf("//");
261         int c_comment_index = li.line.indexOf("/*");
262         int comment_index = -1;
263         if(c_comment_index > -1)
264         {
265           comment_index = c_comment_index;
266         }
267         else if(cpp_comment_index > -1)
268         {
269           comment_index = cpp_comment_index;
270         }
271         if(comment_index > -1 && fn_metrics != null && use_color)
272         {
273           g.setColor(Color.black);
274           g.drawString(li.line.substring(0,comment_index), -x, (i+1)*line_height);
275           int start_string_width = fn_metrics.stringWidth(li.line.substring(0,comment_index));
276           g.setColor(comment_color);
277           g.drawString(li.line.substring(comment_index), start_string_width-x, (i+1)*line_height);
278         }
279         else
280         {
281           g.setColor(Color.black);
282           g.drawString(li.line, -x, (i+1)*line_height);
283         }
284       }
285     }
286   }
287   
288   public void setScrollx(int newx)
289   {
290     if(x != newx)
291     {
292       x = newx;
293       repaint();
294     }
295   }
296 
297   public void setScrolly(int newy)
298   {
299     if(newy < 0)
300     {
301       return;
302     }
303     last_scroll_y_update = System.currentTimeMillis();
304     if(y != newy)
305     {
306       first_line_to_show = newy;
307       y = newy;
308       last_scroll_y_update = System.currentTimeMillis();
309       repaint();
310     }
311   }
312 
313   public boolean mouseDown(Event  evt, int  x, int  y)
314   {
315     last_scroll_y_update = 0;
316     first_line_to_show = currentFileInfo.line_number - (currentFileInfo.line_number%(num_lines_on_screen/2));
317     force_info_new = true;
318     repaint();
319     return super.mouseDown(evt,x,y);
320   }
321   
322   public void setLineNumber(int new_line_number)
323   {
324     if(force_info_new)
325     {
326       info_is_new = true;
327       force_info_new = false;
328     }
329     new_line_number--;
330     //System.out.println("new_line_number = "+new_line_number);
331     if(new_line_number <= 0)
332     {
333       return;
334     }
335     
336     if(currentFileInfo.line_number != new_line_number)
337     {
338       currentFileInfo.line_number = new_line_number ;
339       info_is_new = true;
340       if(new_line_number - first_line_to_show < num_lines_on_screen 
341         && new_line_number > first_line_to_show && first_line_to_show > -1)
342       {
343         repaint();
344         return;
345       }
346       if(System.currentTimeMillis() - last_scroll_y_update  < 5000 && last_scroll_y_update != 0)
347       {
348         repaint();
349         return;
350       }
351       if(new_line_number > num_lines_on_screen/2)
352       {
353         first_line_to_show = new_line_number - (new_line_number%(num_lines_on_screen/2));
354       }
355       else
356       {
357         first_line_to_show = 0;  
358       }
359       repaint();
360     }
361   }
362 }