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.JavaSource;
015: import de.java2html.javasource.JavaSourceParser;
016: import de.java2html.javasource.JavaSourceType;
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 JavaSource source;
055: protected int scale = 1; //number of pixels per character
056: private JavaSourceStyleTable colorTable = JavaSourceStyleTable
057: .getDefault();
058:
059: public JavaSourceCanvas(JavaSource source) {
060: this .source = source;
061: }
062:
063: /**
064: * This method was once used for drawing the source code
065: * (a pixel for each character)
066: * I will leave it here - maybe someone will use it some day
067: */
068: public Dimension getPreferredSize() {
069: return new Dimension(scale * source.getMaxLineLength(), scale
070: * 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: public void paint(Graphics g) {
079: //White background where source code
080: g.setColor(Color.white);
081: Dimension d = getPreferredSize();
082: g.fillRect(0, 0, d.width, d.height);
083:
084: int y = 0;
085:
086: int index = 0;
087: StringTokenizer st = new StringTokenizer(source.getCode(),
088: "\n\r", true);
089: while (st.hasMoreTokens()) {
090: 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: JavaSourceType[] sourceTypes = source.getClassification();
111:
112: while (index2 <= end) {
113: while (index2 < end
114: && sourceTypes[index2 + 1] == sourceTypes[index1]) {
115: ++index2;
116: }
117:
118: if (sourceTypes[index1] != JavaSourceType.BACKGROUND) {
119: g.setColor(getAwtColor(colorTable.get(
120: sourceTypes[index1]).getColor()));
121:
122: if (scale == 1)
123: g.drawLine(x, y, (x + index2 - index1), y);
124: else if (scale == 2) {
125: g.drawLine(2 * x, 2 * y,
126: 2 * (x + index2 - index1) + 1, 2 * y);
127: g.drawLine(2 * x, 2 * y + 1,
128: 2 * (x + index2 - index1) + 1, 2 * y + 1);
129: } else
130: System.err.println("scale>2 not implemented yet!");
131: }
132:
133: x += index2 - index1 + 1;
134:
135: index1 = index2 + 1;
136: index2 = index1;
137: }
138: }
139:
140: private Color getAwtColor(RGB rgb) {
141: return new Color(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
142: }
143:
144: public static void main(String args[]) throws Exception {
145: JavaSourceParser parser = new JavaSourceParser();
146: JavaSource j = parser
147: .parse(new java.io.File(
148: "f:\\eclipse\\de\\java2html\\JavaSource2TeXConverter.java"));
149: JavaSourceCanvas canny = new JavaSourceCanvas(j);
150: Frame f = new Frame();
151: f.addWindowListener(new WindowAdapter() {
152: public void windowClosing(WindowEvent e) {
153: System.exit(0);
154: }
155: });
156:
157: f.setLayout(new BorderLayout());
158: f.add(canny, BorderLayout.CENTER);
159: f.pack();
160: f.setVisible(true);
161: }
162: }
|