001: /*
002: * FAQ.java Copyright (c) 2006,07 Swaroop Belur
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008:
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013:
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: *
018: */
019:
020: package net.sf.jdec.ui.util;
021:
022: import javax.swing.*;
023: import javax.swing.text.*;
024: import java.awt.*;
025:
026: public class FAQ extends JFrame {
027:
028: public static void main(String[] args) {
029: new FAQ();
030: }
031:
032: public FAQ() {
033: super ("Jdec FAQ");
034: createAllComponents();
035: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
036: int x = (int) d.getWidth() / 2 - 700 / 2;
037: int y = (int) d.getHeight() / 2 - 150;
038: setBounds(x, y, 700, 300);
039:
040: setVisible(true);
041:
042: }
043:
044: private void createAllComponents() {
045: JEditorPane editor = new JEditorPane();
046: JScrollPane sp = new JScrollPane(editor);
047: getContentPane().add(sp, BorderLayout.CENTER);
048: EditorKit kit = new FaqKit();
049: editor.setEditorKit(kit);
050: editor.setContentType("text/plain");
051: editor.setEditorKitForContentType("text/plain", kit);
052: editor.setText(getFAQ());
053: editor.setCaretPosition(0);
054: // sp.move(0,0);
055: }
056:
057: private String getFAQ() {
058: StringBuffer sb = new StringBuffer("");
059: String q = "Question: I try to decompile a class file, but jdec does not show me any output";
060: String a = "Answer: Please check the following\n";
061: a += "\tOutput folder exists in the file system\n";
062: a += "\tLog folder exists in the file system\n";
063: String qa = q + "\n" + a + "\n";
064: sb.append(qa);
065: sb
066: .append("\n-----------------------------------------------------------------\n");
067: q = "Question: All the settings are fine, but still jdec does not show me any output";
068: a = "Answer: Please check the following\n";
069: a += "\tOpen the log files and check for any exceptions\n";
070: a += "\tif there is any exception probably it means jdec could not produce\n";
071: a += "\tOutput file\n";
072: a += "\tIn this case please report the error to project admin \n\tfor jdec\n";
073: qa = q + "\n" + a + "\n\n";
074: sb.append(qa);
075: sb
076: .append("\n-----------------------------------------------------------------\n");
077: q = "Question: Sometimes jdec takes considerable time to render the output.Why is this?";
078: a = "Answer: Right now jdec is not a very fast decompiler\n";
079: a += "\tBut there can be a case where class file is really big\n";
080: a += "\tand jdec takes time not only for processing class file \n";
081: a += "\tbut also for rendering the output\n";
082: qa = q + "\n" + a + "\n";
083: sb.append(qa);
084: sb
085: .append("\n-----------------------------------------------------------------\n");
086: q = "Question: How can i make the rendering process faster?";
087: a = "Answer: Please turn off syntax highlighting for decompiled output\n";
088: a += " This can be done by going to preferences\n";
089: qa = q + "\n" + a;
090:
091: sb.append(qa);
092: sb
093: .append("\n-----------------------------------------------------------------\n");
094: return sb.toString();
095: }
096:
097: class FaqKit extends StyledEditorKit {
098:
099: public Document createDefaultDocument() {
100:
101: FAQDoc doc = new FAQDoc();
102: return doc;
103:
104: }
105:
106: }
107:
108: class FAQDoc extends DefaultStyledDocument {
109: String text = "";
110: int currentOffset = -1;
111: String gotString = "";
112: private int endOffset = -1;
113: private int startOffset = -1;
114: private Element root = null;
115:
116: FAQDoc() {
117: root = this .getDefaultRootElement();
118: }
119:
120: public void insertString(int offset, String str, AttributeSet a) {
121:
122: try {
123: super .insertString(offset, str, a);
124: currentOffset = offset;
125: gotString = str;
126: text = this .getText(0, this .getLength());
127: updateSyntaxandColorForGotText();
128: } catch (BadLocationException e) {
129:
130: }
131: }
132:
133: private void updateSyntaxandColorForGotText() {
134: int offset = currentOffset;
135: int stringLength = gotString.length();
136: int end = offset + stringLength;
137: // update the syntax/color/font for the total number of children/lines
138:
139: int startFrom = root.getElementIndex(offset); // From where to Begin
140: int goTo = root.getElementIndex(end); // Go till this child pos/line
141:
142: int z = startFrom;
143: while (z <= goTo) {
144: processChild(text, z);
145: z++;
146: }
147: }
148:
149: private void processChild(String text, int childIndex) {
150:
151: startOffset = root.getElement(childIndex).getStartOffset();
152: endOffset = root.getElement(childIndex).getEndOffset() - 1;
153:
154: //((DefaultStyledDocument)thisDoc).setCharacterAttributes(startOffset, childLength, other, true);
155: if (endOffset <= startOffset)
156: return;
157: scanTextForQA(text, startOffset, endOffset);
158:
159: }
160:
161: private void scanTextForQA(String text, int startOffset,
162: int endOffset) {
163:
164: for (int z1 = startOffset; z1 < endOffset; z1++) {
165:
166: if (z1 < text.length() && text.charAt(z1) == 'Q') {
167: char ar[] = new char[8];
168: ar[0] = 'Q';
169: int counter = 1;
170: for (int e = z1 + 1; e < endOffset
171: && e < text.length(); e++) {
172: if (text.charAt(e) == ' '
173: || text.charAt(e) == ':') {
174: break;
175: }
176: ar[counter] = text.charAt(e);
177: counter++;
178:
179: }
180: String q = new String(ar);
181: if (q.equals("Question")) {
182: SimpleAttributeSet red = new SimpleAttributeSet();
183: StyleConstants.setForeground(red, Color.RED);
184: this .setCharacterAttributes(z1, 8, red, true);
185: z1 += 8;
186: }
187:
188: } else if (z1 < text.length() && text.charAt(z1) == 'A') {
189: char ar[] = new char[6];
190: ar[0] = 'A';
191: int counter = 1;
192: for (int e = z1 + 1; e < endOffset
193: && e < text.length(); e++) {
194: if (text.charAt(e) == ' '
195: || text.charAt(e) == ':') {
196: break;
197: }
198: ar[counter] = text.charAt(e);
199: counter++;
200:
201: }
202: String q = new String(ar);
203: if (q.equals("Answer")) {
204: SimpleAttributeSet blue = new SimpleAttributeSet();
205: StyleConstants.setForeground(blue, Color.blue);
206: this .setCharacterAttributes(z1, 6, blue, true);
207: z1 += 6;
208: }
209: }
210:
211: }
212:
213: }
214:
215: }
216:
217: }
|