KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > JavaSourceCanvas


1 package de.java2html;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.Frame JavaDoc;
7 import java.awt.Graphics JavaDoc;
8 import java.awt.event.WindowAdapter JavaDoc;
9 import java.awt.event.WindowEvent JavaDoc;
10 import java.util.StringTokenizer JavaDoc;
11
12 import javax.swing.JComponent JavaDoc;
13
14 import de.java2html.javasource.JavaSource;
15 import de.java2html.javasource.JavaSourceParser;
16 import de.java2html.javasource.JavaSourceType;
17 import de.java2html.options.JavaSourceStyleTable;
18 import de.java2html.util.RGB;
19
20 /**
21 * Experimental: A <code>java.awt.Canvas</code> for displaying parsed java source code as one pixel
22 * per character. The code will be displayed in colored pixels with one square block of pixels for
23 * each character in the code. At the moment there is not really any use for this class yet, however
24 * it could be cool to write a plugin for IDEs to be able to navigate in a source code by clicking in
25 * this overview component... Just an idea ;-)
26 *
27 * For questions, suggestions, bug-reports, enhancement-requests etc.
28 * I may be contacted at:
29 * <a HREF="mailto:markus@jave.de">markus@jave.de</a>
30 *
31 * The Java2html home page is located at:
32 * <a HREF="http://www.java2html.de">http://www.java2html.de</a>
33 *
34 * @author <a HREF="mailto:markus@jave.de">Markus Gebhard</a>
35 * @version 2.0, 05/07/02
36 *
37 * Copyright (C) Markus Gebhard 2000-2002
38 *
39 * This program is free software; you can redistribute it and/or
40 * modify it under the terms of the GNU General Public License
41 * as published by the Free Software Foundation; either version 2
42 * of the License, or (at your option) any later version.
43 *
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
48 *
49 * You should have received a copy of the GNU General Public License
50 * along with this program; if not, write to the Free Software
51 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
52 */

53 public class JavaSourceCanvas extends JComponent JavaDoc{
54   protected JavaSource source;
55   protected int scale=1; //number of pixels per character
56
private JavaSourceStyleTable colorTable = JavaSourceStyleTable.getDefault();
57   
58   public JavaSourceCanvas(JavaSource source){
59     this.source=source;
60   }
61   
62   /**
63   * This method was once used for drawing the source code
64   * (a pixel for each character)
65   * I will leave it here - maybe someone will use it some day
66   */

67   public Dimension JavaDoc getPreferredSize(){
68     return new Dimension JavaDoc(scale*source.getMaxLineLength(),
69                          scale*source.getLineCount());
70   }
71
72   /**
73   * This method was once used for drawing the source code
74   * (a pixel for each character).
75   * I will leave it here - maybe someone will use it some day
76   */

77   public void paint(Graphics JavaDoc g){
78     //White background where source code
79
g.setColor(Color.white);
80     Dimension JavaDoc d=getPreferredSize();
81     g.fillRect(0,0,d.width,d.height);
82   
83     int y=0;
84   
85     int index=0;
86     StringTokenizer JavaDoc st=new StringTokenizer JavaDoc(source.getCode(),"\n\r",true);
87     while (st.hasMoreTokens()){
88       String JavaDoc line=st.nextToken();
89   
90       if (line.charAt(0)=='\n' || line.charAt(0)=='\r'){
91         ++index;
92         ++y;
93       }else{
94         paint(g, y, index, index+line.length()-1);
95         index+=line.length();
96       }
97     }
98   }
99
100   /**
101   *
102   */

103   protected void paint(Graphics JavaDoc g, int y, int start, int end){
104     int x=0;
105     int index1=start;
106     int index2=start;
107   
108     JavaSourceType[] sourceTypes=source.getClassification();
109
110     while(index2<=end){
111       while(index2<end && sourceTypes[index2+1]==sourceTypes[index1]){
112         ++index2;
113       }
114       
115       if (sourceTypes[index1]!=JavaSourceType.BACKGROUND){
116         g.setColor(getAwtColor(colorTable.get(sourceTypes[index1]).getColor()));
117         
118         if (scale==1)
119           g.drawLine(x, y,
120                      (x+index2-index1), y);
121         else
122         if (scale==2){
123           g.drawLine(2*x, 2*y,
124                      2*(x+index2-index1)+1, 2*y);
125           g.drawLine(2*x, 2*y+1,
126                      2*(x+index2-index1)+1, 2*y+1);
127         }
128         else
129           System.err.println("scale>2 not implemented yet!");
130       }
131       
132       x+=index2-index1+1;
133       
134       index1=index2+1;
135       index2=index1;
136     }
137   }
138
139   private Color JavaDoc getAwtColor(RGB rgb) {
140     return new Color JavaDoc(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
141   }
142
143   public static void main(String JavaDoc args[]) throws Exception JavaDoc{
144     JavaSourceParser parser = new JavaSourceParser();
145     JavaSource j = parser.parse(new java.io.File JavaDoc("f:\\eclipse\\de\\java2html\\JavaSource2TeXConverter.java"));
146     JavaSourceCanvas canny=new JavaSourceCanvas(j);
147     Frame JavaDoc f=new Frame JavaDoc();
148     f.addWindowListener(new WindowAdapter JavaDoc(){
149       public void windowClosing(WindowEvent JavaDoc e){
150         System.exit(0);
151       }
152     });
153   
154     f.setLayout(new BorderLayout JavaDoc());
155     f.add(canny, BorderLayout.CENTER);
156     f.pack();
157     f.setVisible(true);
158   }
159 }
Popular Tags