001: package net.sourceforge.pmd.util.designer;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import java.io.BufferedReader;
006: import java.io.File;
007: import java.io.FileReader;
008: import java.io.FileWriter;
009: import java.io.IOException;
010: import java.util.StringTokenizer;
011:
012: import javax.swing.JTextPane;
013:
014: import net.sourceforge.pmd.ast.SimpleNode;
015: import net.sourceforge.pmd.util.LineGetter;
016:
017: public class CodeEditorTextPane extends JTextPane implements
018: LineGetter, ActionListener {
019:
020: private static final String SETTINGS_FILE_NAME = System
021: .getProperty("user.home")
022: + System.getProperty("file.separator") + ".pmd_designer";
023: private static final String LINE_SEPARATOR = System
024: .getProperty("line.separator");
025:
026: public CodeEditorTextPane() {
027: setText(loadCode());
028: }
029:
030: public String getLine(int number) {
031: int count = 1;
032: for (StringTokenizer st = new StringTokenizer(getText(), "\n"); st
033: .hasMoreTokens();) {
034: String tok = st.nextToken();
035: if (count == number) {
036: return tok;
037: }
038: count++;
039: }
040: throw new RuntimeException("Line number " + number
041: + " not found");
042: }
043:
044: private int getPosition(String[] lines, int line, int column) {
045: int pos = 0;
046: for (int count = 0; count < lines.length;) {
047: String tok = lines[count++];
048: if (count == line) {
049: int linePos = 0;
050: int i;
051: for (i = 0; linePos < column; i++) {
052: linePos++;
053: if (tok.charAt(i) == '\t') {
054: linePos--;
055: linePos += (8 - (linePos & 07));
056: }
057: }
058:
059: return pos + i - 1;
060: }
061: pos += tok.length() + 1;
062: }
063: throw new RuntimeException("Line " + line + " not found");
064: }
065:
066: public void select(SimpleNode node) {
067: String[] lines = getText().split(LINE_SEPARATOR);
068: setSelectionStart(getPosition(lines, node.getBeginLine(), node
069: .getBeginColumn()));
070: setSelectionEnd(getPosition(lines, node.getEndLine(), node
071: .getEndColumn()) + 1);
072: requestFocus();
073: }
074:
075: public void actionPerformed(ActionEvent ae) {
076: FileWriter fw = null;
077: try {
078: fw = new FileWriter(new File(SETTINGS_FILE_NAME));
079: fw.write(getText());
080: } catch (IOException ioe) {
081: } finally {
082: try {
083: if (fw != null)
084: fw.close();
085: } catch (IOException ioe) {
086: ioe.printStackTrace();
087: }
088: }
089: }
090:
091: private String loadCode() {
092: BufferedReader br = null;
093: try {
094: br = new BufferedReader(new FileReader(new File(
095: SETTINGS_FILE_NAME)));
096: StringBuffer text = new StringBuffer();
097: String hold;
098: while ((hold = br.readLine()) != null) {
099: text.append(hold).append(LINE_SEPARATOR);
100: }
101: return text.toString();
102: } catch (IOException e) {
103: e.printStackTrace();
104: return "";
105: } finally {
106: try {
107: if (br != null)
108: br.close();
109: } catch (IOException e) {
110: e.printStackTrace();
111: }
112: }
113: }
114: }
|