001 package de.java2html;
002 
003 import java.awt.BorderLayout;
004 import java.awt.Color;
005 import java.awt.Dimension;
006 import java.awt.Frame;
007 import java.awt.Graphics;
008 import java.awt.event.WindowAdapter;
009 import java.awt.event.WindowEvent;
010 import java.util.StringTokenizer;
011 
012 import javax.swing.JComponent;
013 
014 import de.java2html.javasource.TypedSource;
015 import de.java2html.javasource.JavaSourceParser;
016 import de.java2html.javasource.SourceType;
017 import de.java2html.options.JavaSourceStyleTable;
018 import de.java2html.util.RGB;
019 
020 /**
021 * Experimental: A <code>java.awt.Canvas</code> for displaying parsed java source code as one pixel
022 * per character. The code will be displayed in colored pixels with one square block of pixels for
023 * each character in the code. At the moment there is not really any use for this class yet, however
024 * it could be cool to write a plugin for IDEs to be able to navigate in a source code by clicking in
025 * this overview component... Just an idea ;-)
026 *
027 * For questions, suggestions, bug-reports, enhancement-requests etc.
028 * I may be contacted at:
029 *   <a href="mailto:markus@jave.de">markus@jave.de</a>
030 *
031 * The Java2html home page is located at:
032 *   <a href="http://www.java2html.de">http://www.java2html.de</a>
033 *
034 @author  <a href="mailto:markus@jave.de">Markus Gebhard</a>
035 @version 2.0, 05/07/02
036 *
037 * Copyright (C) Markus Gebhard 2000-2002
038 *
039 * This program is free software; you can redistribute it and/or
040 * modify it under the terms of the GNU General Public License
041 * as published by the Free Software Foundation; either version 2
042 * of the License, or (at your option) any later version.
043 *
044 * This program is distributed in the hope that it will be useful,
045 * but WITHOUT ANY WARRANTY; without even the implied warranty of
046 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
047 * GNU General Public License for more details.
048 
049 * You should have received a copy of the GNU General Public License
050 * along with this program; if not, write to the Free Software
051 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
052 */
053 public class JavaSourceCanvas extends JComponent{
054   protected TypedSource source;
055   protected int scale=1//number of pixels per character
056   private final JavaSourceStyleTable colorTable = JavaSourceStyleTable.getDefault()
057   
058   public JavaSourceCanvas(TypedSource source){
059     this.source=source;
060   }
061   
062   /**
063   * This method was once used for drawing the source code
064   * (a pixel for each character)
065   * I will leave it here - maybe someone will use it some day 
066   */
067   @Override
068   public Dimension getPreferredSize(){
069     return new Dimension(scale*source.getMaxLineLength(),
070                          scale*source.getLineCount());
071   }
072 
073   /**
074   * This method was once used for drawing the source code
075   * (a pixel for each character).
076   * I will leave it here - maybe someone will use it some day 
077   */
078   @Override
079   public void paint(Graphics g){
080     //White background where source code
081     g.setColor(Color.white);
082     final Dimension d=getPreferredSize();
083     g.fillRect(0,0,d.width,d.height);
084   
085     int y=0;
086   
087     int index=0;
088     final StringTokenizer st=new StringTokenizer(source.getCode(),"\n\r",true);
089     while (st.hasMoreTokens()){
090       final String line=st.nextToken();
091   
092       if (line.charAt(0)=='\n' || line.charAt(0)=='\r'){
093         ++index;
094         ++y;
095       }else{
096         paint(g, y, index, index+line.length()-1);
097         index+=line.length();
098       }
099     }
100   }
101 
102   /**
103   *
104   */
105   protected void paint(Graphics g, int y, int start, int end){
106     int x=0;
107     int index1=start;
108     int index2=start;
109   
110     final SourceType[] sourceTypes=source.getClassification();
111 
112     while(index2<=end){
113       while(index2<end && sourceTypes[index2+1]==sourceTypes[index1]){
114         ++index2;
115       }
116       
117       if (sourceTypes[index1]!=SourceType.BACKGROUND){
118         g.setColor(getAwtColor(colorTable.get(sourceTypes[index1]).getColor()));
119         
120         if (scale==1) {
121           g.drawLine(x,                 y,
122                      (x+index2-index1), y);
123         }
124         else
125         if (scale==2){
126           g.drawLine(2*x,                   2*y,
127                      2*(x+index2-index1)+12*y);
128           g.drawLine(2*x,                   2*y+1,
129                      2*(x+index2-index1)+12*y+1);
130         }
131         else {
132           System.err.println("scale>2 not implemented yet!");
133         }
134       }
135       
136       x+=index2-index1+1;
137       
138       index1=index2+1;
139       index2=index1;
140     }
141   }
142 
143   private Color getAwtColor(RGB rgb) {
144     return new Color(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
145   }
146 
147   public static void main(String args[]) throws Exception{
148     final JavaSourceParser parser = new JavaSourceParser();
149     final TypedSource j = parser.parse(new java.io.File("f:\\eclipse\\de\\java2html\\JavaSource2TeXConverter.java"));
150     final JavaSourceCanvas canny=new JavaSourceCanvas(j);
151     final Frame f=new Frame();
152     f.addWindowListener(new WindowAdapter(){
153       @Override
154       public void windowClosing(WindowEvent e){
155         System.exit(0);
156       }
157     });
158   
159     f.setLayout(new BorderLayout());
160     f.add(canny, BorderLayout.CENTER);
161     f.pack();
162     f.setVisible(true);
163   }
164 }