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 ParseMode(String descr) {
66 description = descr;
67 }
68
69 @Override
70 public String toString() {
71 return description;
72 }
73
74 }
75
76
77 private final ParseTreeTableModel parseTreeTableModel;
78
79
80 private List<Integer> linesToPosition = new ArrayList<>();
81
82
83 private ParseMode parseMode = ParseMode.PLAIN_JAVA;
84
85
86 private File currentFile;
87
88
89 private String text;
90
91
92 private String title = "Checkstyle GUI";
93
94
95 private boolean reloadActionEnabled;
96
97
98 public MainFrameModel() {
99 parseTreeTableModel = new ParseTreeTableModel(null);
100 }
101
102
103
104
105
106 public void setParseMode(ParseMode mode) {
107 parseMode = mode;
108 }
109
110
111
112
113
114 public ParseTreeTableModel getParseTreeTableModel() {
115 return parseTreeTableModel;
116 }
117
118
119
120
121
122 public String getText() {
123 return text;
124 }
125
126
127
128
129
130 public String getTitle() {
131 return title;
132 }
133
134
135
136
137
138 public boolean isReloadActionEnabled() {
139 return reloadActionEnabled;
140 }
141
142
143
144
145
146
147 public static boolean shouldAcceptFile(File file) {
148 return file.isDirectory() || file.getName().endsWith(".java");
149 }
150
151
152
153
154
155 public File getLastDirectory() {
156 File lastDirectory = null;
157 if (currentFile != null) {
158 lastDirectory = new File(currentFile.getParent());
159 }
160 return lastDirectory;
161 }
162
163
164
165
166
167 public File getCurrentFile() {
168 return currentFile;
169 }
170
171
172
173
174
175
176
177
178 public List<Integer> getLinesToPosition() {
179 return new ArrayList<>(linesToPosition);
180 }
181
182
183
184
185
186
187 public void openFile(File file) throws CheckstyleException {
188 if (file != null) {
189 try {
190 currentFile = file;
191 title = "Checkstyle GUI : " + file.getName();
192 reloadActionEnabled = true;
193 final DetailAST parseTree;
194
195 switch (parseMode) {
196 case PLAIN_JAVA:
197 parseTree = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
198 break;
199 case JAVA_WITH_COMMENTS:
200 case JAVA_WITH_JAVADOC_AND_COMMENTS:
201 parseTree = JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS);
202 break;
203 default:
204 throw new IllegalArgumentException("Unknown mode: " + parseMode);
205 }
206
207 parseTreeTableModel.setParseTree(parseTree);
208 parseTreeTableModel.setParseMode(parseMode);
209 final String[] sourceLines = getFileText(file).toLinesArray();
210
211 final List<Integer> linesToPositionTemp = new ArrayList<>();
212
213 linesToPositionTemp.add(0);
214
215 final StringBuilder sb = new StringBuilder(1024);
216
217 for (final String element : sourceLines) {
218 linesToPositionTemp.add(sb.length());
219 sb.append(element).append(System.lineSeparator());
220 }
221 linesToPosition = linesToPositionTemp;
222 text = sb.toString();
223 }
224 catch (IOException ex) {
225 final String exceptionMsg = String.format(Locale.ROOT,
226 "%s occurred while opening file %s.",
227 ex.getClass().getSimpleName(), file.getPath());
228 throw new CheckstyleException(exceptionMsg, ex);
229 }
230 }
231 }
232
233
234
235
236
237
238
239 private static FileText getFileText(File file) throws IOException {
240 return new FileText(file.getAbsoluteFile(),
241 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
242 }
243
244 }