1 package de.java2html; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.Dimension ; 6 import java.awt.Frame ; 7 import java.awt.Graphics ; 8 import java.awt.event.WindowAdapter ; 9 import java.awt.event.WindowEvent ; 10 import java.util.StringTokenizer ; 11 12 import javax.swing.JComponent ; 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 53 public class JavaSourceCanvas extends JComponent { 54 protected JavaSource source; 55 protected int scale=1; private JavaSourceStyleTable colorTable = JavaSourceStyleTable.getDefault(); 57 58 public JavaSourceCanvas(JavaSource source){ 59 this.source=source; 60 } 61 62 67 public Dimension getPreferredSize(){ 68 return new Dimension (scale*source.getMaxLineLength(), 69 scale*source.getLineCount()); 70 } 71 72 77 public void paint(Graphics g){ 78 g.setColor(Color.white); 80 Dimension d=getPreferredSize(); 81 g.fillRect(0,0,d.width,d.height); 82 83 int y=0; 84 85 int index=0; 86 StringTokenizer st=new StringTokenizer (source.getCode(),"\n\r",true); 87 while (st.hasMoreTokens()){ 88 String 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 103 protected void paint(Graphics 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 getAwtColor(RGB rgb) { 140 return new Color (rgb.getRed(), rgb.getGreen(), rgb.getBlue()); 141 } 142 143 public static void main(String args[]) throws Exception { 144 JavaSourceParser parser = new JavaSourceParser(); 145 JavaSource j = parser.parse(new java.io.File ("f:\\eclipse\\de\\java2html\\JavaSource2TeXConverter.java")); 146 JavaSourceCanvas canny=new JavaSourceCanvas(j); 147 Frame f=new Frame (); 148 f.addWindowListener(new WindowAdapter (){ 149 public void windowClosing(WindowEvent e){ 150 System.exit(0); 151 } 152 }); 153 154 f.setLayout(new BorderLayout ()); 155 f.add(canny, BorderLayout.CENTER); 156 f.pack(); 157 f.setVisible(true); 158 } 159 } | Popular Tags |