1 |
| package net.sourceforge.pmd.util.designer; |
2 |
| |
3 |
| import net.sourceforge.pmd.util.HasLines; |
4 |
| |
5 |
| import javax.swing.*; |
6 |
| import java.awt.Dimension; |
7 |
| import java.awt.event.ActionEvent; |
8 |
| import java.awt.event.ActionListener; |
9 |
| import java.io.BufferedReader; |
10 |
| import java.io.File; |
11 |
| import java.io.FileReader; |
12 |
| import java.io.FileWriter; |
13 |
| import java.io.IOException; |
14 |
| import java.util.StringTokenizer; |
15 |
| |
16 |
| public class CodeEditorTextPane extends JTextPane implements HasLines, ActionListener { |
17 |
| |
18 |
| private static final String SETTINGS_FILE_NAME = System.getProperty("user.home") + System.getProperty("file.separator") + ".pmd_designer"; |
19 |
| |
20 |
0
| public CodeEditorTextPane() {
|
21 |
0
| setPreferredSize(new Dimension(400, 200));
|
22 |
0
| setText(loadCode());
|
23 |
| } |
24 |
| |
25 |
0
| public String getLine(int number) {
|
26 |
0
| int count = 1;
|
27 |
0
| for (StringTokenizer st = new StringTokenizer(getText(), "\n"); st.hasMoreTokens();) {
|
28 |
0
| String tok = st.nextToken();
|
29 |
0
| if (count == number) {
|
30 |
0
| return tok;
|
31 |
| } |
32 |
0
| count++;
|
33 |
| } |
34 |
0
| throw new RuntimeException("Line number " + number + " not found");
|
35 |
| } |
36 |
| |
37 |
0
| public void actionPerformed(ActionEvent ae) {
|
38 |
0
| FileWriter fw = null;
|
39 |
0
| try {
|
40 |
0
| fw = new FileWriter(new File(SETTINGS_FILE_NAME));
|
41 |
0
| fw.write(getText());
|
42 |
| } catch (IOException ioe) { |
43 |
| } finally { |
44 |
0
| try {
|
45 |
0
| if (fw != null)
|
46 |
0
| fw.close();
|
47 |
| } catch (IOException ioe) { |
48 |
0
| ioe.printStackTrace();
|
49 |
| } |
50 |
| } |
51 |
| } |
52 |
| |
53 |
0
| private String loadCode() {
|
54 |
0
| BufferedReader br = null;
|
55 |
0
| try {
|
56 |
0
| br = new BufferedReader(new FileReader(new File(SETTINGS_FILE_NAME)));
|
57 |
0
| StringBuffer text = new StringBuffer();
|
58 |
0
| String hold;
|
59 |
0
| while ((hold = br.readLine()) != null) {
|
60 |
0
| text.append(hold);
|
61 |
0
| text.append(System.getProperty("line.separator"));
|
62 |
| } |
63 |
0
| return text.toString();
|
64 |
| } catch (IOException e) { |
65 |
0
| e.printStackTrace();
|
66 |
0
| return "";
|
67 |
| } finally { |
68 |
0
| try {
|
69 |
0
| if (br != null)
|
70 |
0
| br.close();
|
71 |
| } catch (IOException e) { |
72 |
0
| e.printStackTrace();
|
73 |
| } |
74 |
| } |
75 |
| } |
76 |
| } |
77 |
| |