001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
018: */
019:
020: package edu.umd.cs.findbugs.sourceViewer;
021:
022: import java.awt.Color;
023: import java.awt.Font;
024: import java.awt.FontMetrics;
025: import java.awt.Toolkit;
026: import java.io.IOException;
027: import java.io.Reader;
028: import java.io.StringReader;
029:
030: import javax.swing.text.BadLocationException;
031: import javax.swing.text.DefaultStyledDocument;
032: import javax.swing.text.Document;
033: import javax.swing.text.Element;
034: import javax.swing.text.SimpleAttributeSet;
035: import javax.swing.text.StyleConstants;
036: import javax.swing.text.StyleContext;
037: import javax.swing.text.StyledDocument;
038: import javax.swing.text.TabSet;
039: import javax.swing.text.TabStop;
040:
041: import edu.umd.cs.findbugs.ba.SourceFile;
042: import edu.umd.cs.findbugs.gui2.Debug;
043: import edu.umd.cs.findbugs.gui2.Driver;
044: import edu.umd.cs.findbugs.gui2.GUISaveState;
045:
046: public class JavaSourceDocument {
047:
048: static final SimpleAttributeSet commentAttributes = new SimpleAttributeSet();
049:
050: static final SimpleAttributeSet javadocAttributes = new SimpleAttributeSet();
051:
052: static final SimpleAttributeSet quotesAttributes = new SimpleAttributeSet();
053:
054: static final SimpleAttributeSet keywordsAttributes = new SimpleAttributeSet();
055:
056: static final SimpleAttributeSet whiteAttributes = new SimpleAttributeSet();
057:
058: static Font sourceFont = new Font("Monospaced", Font.PLAIN,
059: (int) Driver.getFontSize());
060: final static Color HIGHLIGHT_COLOR = new Color(1f, 1f, .3f);
061: TabSet TAB_SET;
062: static {
063: commentAttributes.addAttribute(StyleConstants.Foreground,
064: new Color(0.0f, 0.5f, 0.0f));
065: javadocAttributes.addAttribute(StyleConstants.Foreground,
066: new Color(0.25f, 0.37f, 0.75f));
067: quotesAttributes.addAttribute(StyleConstants.Foreground,
068: new Color(0.0f, 0.0f, 1.0f));
069: keywordsAttributes.addAttribute(StyleConstants.Foreground,
070: new Color(0.5f, 0.0f, 0.5f));
071: keywordsAttributes.addAttribute(StyleConstants.Bold, true);
072:
073: }
074:
075: final HighlightInformation highlights = new HighlightInformation();
076: final NumberedEditorKit dek = new NumberedEditorKit(highlights);
077:
078: final StyleContext styleContext = new StyleContext();
079:
080: final Element root;
081:
082: final DefaultStyledDocument doc;
083:
084: final SourceFile sourceFile;
085:
086: public HighlightInformation getHighlightInformation() {
087: return highlights;
088: }
089:
090: public StyledDocument getDocument() {
091: return doc;
092: }
093:
094: public NumberedEditorKit getEditorKit() {
095: return dek;
096: }
097:
098: private final String title;
099:
100: public String getTitle() {
101: return title;
102: }
103:
104: public SourceFile getSourceFile() {
105: return sourceFile;
106: }
107:
108: public JavaSourceDocument(String title, Reader in,
109: SourceFile theSource) throws IOException {
110: doc = new DefaultStyledDocument();
111: this .title = title;
112: this .sourceFile = theSource;
113: Debug.println("Created JavaSourceDocument for " + title);
114: try {
115: dek.read(in, doc, 0);
116: } catch (BadLocationException e) {
117: throw new RuntimeException(e);
118: }
119: in.close();
120: doc.putProperty(Document.TitleProperty, title);
121: root = doc.getDefaultRootElement();
122: Toolkit toolkit = Toolkit.getDefaultToolkit();
123: FontMetrics fontMetrics = toolkit.getFontMetrics(sourceFont);
124: TabStop[] tabs = new TabStop[50];
125: float width = fontMetrics.stringWidth(" ");
126:
127: int tabSize = GUISaveState.getInstance().getTabSize();
128: for (int i = 0; i < tabs.length; i++)
129: tabs[i] = new TabStop(width * (tabSize + tabSize * i));
130: TAB_SET = new TabSet(tabs);
131: StyleConstants.setTabSet(commentAttributes, TAB_SET);
132: StyleConstants.setTabSet(javadocAttributes, TAB_SET);
133:
134: StyleConstants.setTabSet(quotesAttributes, TAB_SET);
135:
136: StyleConstants.setTabSet(keywordsAttributes, TAB_SET);
137:
138: StyleConstants.setTabSet(commentAttributes, TAB_SET);
139:
140: StyleConstants.setTabSet(whiteAttributes, TAB_SET);
141: StyleConstants.setFontFamily(whiteAttributes, sourceFont
142: .getFamily());
143: StyleConstants.setFontSize(whiteAttributes, sourceFont
144: .getSize());
145: StyleConstants.setLeftIndent(whiteAttributes,
146: NumberedParagraphView.NUMBERS_WIDTH);
147:
148: doc.setParagraphAttributes(0, doc.getLength(), whiteAttributes,
149: true);
150: JavaScanner parser = new JavaScanner(
151: new DocumentCharacterIterator(doc));
152: while (parser.next() != JavaScanner.EOF) {
153: int kind = parser.getKind();
154: switch (kind) {
155: case JavaScanner.COMMENT:
156: doc.setCharacterAttributes(parser.getStartPosition(),
157: parser.getLength(), commentAttributes, true);
158:
159: break;
160: case JavaScanner.KEYWORD:
161: doc.setCharacterAttributes(parser.getStartPosition(),
162: parser.getLength(), keywordsAttributes, true);
163:
164: break;
165: case JavaScanner.JAVADOC:
166: doc.setCharacterAttributes(parser.getStartPosition(),
167: parser.getLength(), javadocAttributes, true);
168:
169: break;
170: case JavaScanner.QUOTE:
171: doc.setCharacterAttributes(parser.getStartPosition(),
172: parser.getLength(), quotesAttributes, true);
173:
174: break;
175: }
176:
177: }
178:
179: }
180:
181: private static final long serialVersionUID = 0L;
182: public static final JavaSourceDocument UNKNOWNSOURCE;
183: static {
184: try {
185: UNKNOWNSOURCE = new JavaSourceDocument("Unknown source",
186: new StringReader("Unable to find source"), null);
187: } catch (IOException e) {
188: throw new RuntimeException(e);
189: }
190: }
191:
192: }
|