1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Locale;
28
29 import com.puppycrawl.tools.checkstyle.JavaParser;
30 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31 import com.puppycrawl.tools.checkstyle.api.DetailAST;
32 import com.puppycrawl.tools.checkstyle.api.FileText;
33
34
35
36
37 public class MainFrameModel {
38
39
40
41
42 public enum ParseMode {
43
44
45 PLAIN_JAVA("Plain Java"),
46
47
48 JAVA_WITH_COMMENTS("Java with comments"),
49
50
51
52
53
54 JAVA_WITH_JAVADOC_AND_COMMENTS("Java with comments and Javadocs");
55
56
57
58
59 private final String description;
60
61
62
63
64
65
66 ParseMode(String descr) {
67 description = descr;
68 }
69
70 @Override
71 public String toString() {
72 return description;
73 }
74
75 }
76
77
78 private final ParseTreeTableModel parseTreeTableModel;
79
80
81 private List<Integer> linesToPosition = new ArrayList<>();
82
83
84 private ParseMode parseMode = ParseMode.PLAIN_JAVA;
85
86
87 private File currentFile;
88
89
90 private String text;
91
92
93 private String title = "Checkstyle GUI";
94
95
96 private boolean reloadActionEnabled;
97
98
99 public MainFrameModel() {
100 parseTreeTableModel = new ParseTreeTableModel(null);
101 }
102
103
104
105
106
107
108 public void setParseMode(ParseMode mode) {
109 parseMode = mode;
110 }
111
112
113
114
115
116
117 public ParseTreeTableModel getParseTreeTableModel() {
118 return parseTreeTableModel;
119 }
120
121
122
123
124
125
126 public String getText() {
127 return text;
128 }
129
130
131
132
133
134
135 public String getTitle() {
136 return title;
137 }
138
139
140
141
142
143
144 public boolean isReloadActionEnabled() {
145 return reloadActionEnabled;
146 }
147
148
149
150
151
152
153
154 public static boolean shouldAcceptFile(File file) {
155 return file.isDirectory() || file.getName().endsWith(".java");
156 }
157
158
159
160
161
162
163 public File getLastDirectory() {
164 File lastDirectory = null;
165 if (currentFile != null) {
166 lastDirectory = currentFile.getParentFile();
167 }
168 return lastDirectory;
169 }
170
171
172
173
174
175
176 public File getCurrentFile() {
177 return currentFile;
178 }
179
180
181
182
183
184
185
186
187
188 public List<Integer> getLinesToPosition() {
189 return new ArrayList<>(linesToPosition);
190 }
191
192
193
194
195
196
197
198
199 public void openFile(File file) throws CheckstyleException {
200 if (file != null) {
201 try {
202 currentFile = file;
203 title = "Checkstyle GUI : " + file.getName();
204 reloadActionEnabled = true;
205 final DetailAST parseTree;
206
207 if (parseMode == ParseMode.PLAIN_JAVA) {
208 parseTree = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
209 }
210 else if (parseMode == ParseMode.JAVA_WITH_COMMENTS
211 || parseMode == ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS) {
212 parseTree = JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS);
213 }
214 else {
215 throw new IllegalArgumentException("Unknown mode: " + parseMode);
216 }
217
218 parseTreeTableModel.setParseTree(parseTree);
219 parseTreeTableModel.setParseMode(parseMode);
220 final String[] sourceLines = getFileText(file).toLinesArray();
221
222 final List<Integer> linesToPositionTemp = new ArrayList<>(sourceLines.length + 1);
223
224 linesToPositionTemp.add(0);
225
226 final StringBuilder sb = new StringBuilder(1024);
227
228 for (final String element : sourceLines) {
229 linesToPositionTemp.add(sb.length());
230 sb.append(element).append(System.lineSeparator());
231 }
232 linesToPosition = linesToPositionTemp;
233 text = sb.toString();
234 }
235 catch (IOException ex) {
236 final String exceptionMsg = String.format(Locale.ROOT,
237 "%s occurred while opening file %s.",
238 ex.getClass().getSimpleName(), file.getPath());
239 throw new CheckstyleException(exceptionMsg, ex);
240 }
241 }
242 }
243
244
245
246
247
248
249
250
251 private static FileText getFileText(File file) throws IOException {
252 return new FileText(file.getAbsoluteFile(),
253 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
254 }
255
256 }