-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextEditor.java
More file actions
469 lines (399 loc) · 15.7 KB
/
Copy pathTextEditor.java
File metadata and controls
469 lines (399 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/**
* Author : Dongming Ma
* TextEditor made by bugs and rage from a useless programmer.
*/
package hw3;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import javax.tools.*;
public class TextEditor extends JFrame {
static public File currentFile = null;
public boolean fileEdited = false;
public JTextArea t, console;
public JMenu fileMenu, buildMenu, toolsMenu;
public JDialog closingwindow;
public Font f = new Font("Times", Font.PLAIN, 24);
public Font fs = new Font("Times", Font.PLAIN, 20);
public ArrayList<Integer> errorLineNumbers = new ArrayList<>(5);
public int CurrentError;
public void createFileMenu() {
fileMenu = new JMenu("File(F)");
fileMenu.setMnemonic(KeyEvent.VK_F);
fileMenu.setFont(f);
JMenuItem newfile = new JMenuItem("New");
newfile.setFont(fs);
newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
newfile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CurrentError = 0;
new TextEditor();
}
});
JMenuItem open = new JMenuItem("Open");
open.setFont(fs);
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CurrentError = 0;
if (!TextEditor.this.fileEdited) {
TextEditor.this.openFile();
} else {
// TODO choose save file or not first if file edited
TextEditor.this.openFile();
}
}
});
JMenuItem save = new JMenuItem("Save");
save.setFont(fs);
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CurrentError = 0;
try {
TextEditor.this.saveFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
JMenuItem quit = new JMenuItem("Quit");
quit.setFont(fs);
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
quit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (TextEditor.this.fileEdited) {
TextEditor.this.closingWindow();//需要加会话框
} else {
// TODO choose save file or not first if file edited
dispose();
}
}
});
fileMenu.add(newfile);
fileMenu.add(open);
fileMenu.add(save);
fileMenu.add(quit);
}
public void createToolsMenu() {
toolsMenu = new JMenu("Tools(T)");
toolsMenu.setMnemonic(KeyEvent.VK_T);
toolsMenu.setFont(f);
JMenuItem gotoNextError = new JMenuItem("Goto next error");
gotoNextError.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0));
gotoNextError.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
t.setCaretPosition(t.getLineStartOffset(errorLineNumbers.get(CurrentError) - 1));
} catch (BadLocationException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
}
// System.out.println(errorLineNumbers.get(0));
CurrentError += 1;
if (CurrentError == errorLineNumbers.size()) {
CurrentError = 0;
}
}
});
gotoNextError.setFont(fs);
toolsMenu.add(gotoNextError);
}
public void createBuildMenu() {
buildMenu = new JMenu("Build(B)");
buildMenu.setMnemonic(KeyEvent.VK_B);
buildMenu.setFont(f);
JMenuItem run = new JMenuItem("Run");
run.setFont(fs);
run.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CurrentError = 0;
try {
runFile();
} catch (IOException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
JMenuItem compile = new JMenuItem("Compile");
compile.setFont(fs);
compile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CurrentError = 0;
try {
compileFile();
} catch (IOException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
buildMenu.add(compile);
buildMenu.add(run);
}
public void createWindow() {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.out.println("Stop debuging me.");
}
public void windowClosing(WindowEvent e) {
if (fileEdited != false) {
closingWindow();
} else {
dispose();
}
}
});
setSize(1080, 720);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
setLocation(((int) width - 1000) / 2, ((int) height - 1100) / 2);
Container c = getContentPane();
JMenuBar menubar = new JMenuBar();
this.createFileMenu();
this.createBuildMenu();
this.createToolsMenu();
t = new JTextArea();
t.getDocument().addDocumentListener(new MyDocumentListener());
t.setFont(f);
t.addKeyListener(new MyKeyListener());
// console = new JTextArea(5,20);
// console.setFont(f);
c.add(t, BorderLayout.CENTER);
// c.add(console, BorderLayout.SOUTH);
c.add(menubar, BorderLayout.NORTH);
menubar.add(fileMenu);
menubar.add(buildMenu);
menubar.add(toolsMenu);
}
public TextEditor() {
super("ultimateSimpleTextEditor(Build 0.1.0)");
createWindow();
setVisible(true);
}
public void gotoNextError(long linenumber) {
t.setCaretPosition((int) linenumber);
}
public void openFile() {
JFileChooser openFile = new JFileChooser();
int returnVal = openFile.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = openFile.getSelectedFile();
TextEditor.this.currentFile = file;
try {
Scanner sc = new Scanner(file);
String str = "";
while (sc.hasNextLine()) {
str += (sc.nextLine() + '\n');
}
TextEditor.this.t.setText(str);
sc.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (returnVal == JFileChooser.CANCEL_OPTION) {
// TODO cancel clicked
;
} else {
// TODO error occurred
;
}
System.out.println("You opened a file at " + currentFile.getAbsolutePath() + " called " + currentFile.getName());
}
public void saveFile() throws IOException {
if (this.fileEdited && this.currentFile != null) {
// just save
this.writeFile(this.currentFile);
fileEdited = false;
} else if (this.fileEdited && this.currentFile == null) {
// open save dialog, get path and save
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.showSaveDialog(null);
File fileToSave = chooser.getSelectedFile();
this.writeFile(fileToSave);
fileEdited = false;
this.currentFile = fileToSave;
}
}
// public void compileFile() throws IOException, InterruptedException {
// String path = currentFile.getAbsolutePath();
// Process pro = Runtime.getRuntime().exec("javac " + path);
// printLines("out:", pro.getInputStream());
// printLines("err:", pro.getErrorStream());
// pro.waitFor();
// }
public void compileFile() throws IOException, InterruptedException {
errorLineNumbers.clear();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics
= new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
String path = currentFile.getAbsolutePath();
// int compilationResult = compiler.run(null, null, null, path);
compiler.getTask(null, fileManager, diagnostics, null, null, fileManager.getJavaFileObjects(path)).call();
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
System.out.format("Error on line %d in %s%n",
diagnostic.getLineNumber(),
diagnostic.getSource());
errorLineNumbers.add((int) diagnostic.getLineNumber());
// errorLineNumbers.add(diagnostic.getStartPosition());
}
fileManager.close();
}
private static void printLines(String str, InputStream input) throws IOException {
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(input));
while ((line = in.readLine()) != null) {
System.out.println(str + " " + line);
}
}
public void runFile() throws IOException, InterruptedException {
String pathSplit[] = currentFile.getAbsolutePath().split("\\\\");
String path = "";
for (int i = 0; i < pathSplit.length - 1; i++) {
path += pathSplit[i] + "\\";
}
String[] fileName = currentFile.getName().split("\\.");
Process pro = Runtime.getRuntime().exec("java -cp " + path + " " + fileName[0]);
printLines("out:", pro.getInputStream());
printLines("err:", pro.getErrorStream());
pro.waitFor();
}
public void writeFile(File file) throws IOException {
String text = this.t.getText();
// make sure \n in text replaced by the 'true line separator'
BufferedReader reader = new BufferedReader(new StringReader(text));
PrintWriter writer = new PrintWriter(new FileWriter(file));
String line;
while ((line = reader.readLine()) != null) {
writer.println(line);
}
writer.close();
}
public void closingWindow() {
JDialog askForSave = new JDialog(getOwner(), "Remember to SAVE!");
// Point p = new Point(333, 333);
// askForSave.setLocation(p);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
askForSave.setLocation(((int) width - 200) / 2, ((int) height - 300) / 2);
askForSave.setFont(f);
askForSave.setSize(400, 300);
askForSave.setVisible(true);
// Container c = getContentPane();
// c.setLayout(new BorderLayout());
// JPanel p = new JPanel();
// p.setVisible(true);
// p.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
askForSave.setLayout(new GridLayout(2, 1, 20, 20));
// c.add(p, BorderLayout.CENTER);
// p.setBackground(Color.WHITE);
JButton yes = new JButton("Oh thank you");
yes.setFont(f);
yes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
TextEditor.this.saveFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
JButton no = new JButton("Nope");
no.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
askForSave.dispose();
}
});
no.setFont(f);
askForSave.add(yes);
askForSave.add(no);
}
public void cut() {
}
public void paste() {
}
public static void main(String[] args) {
new TextEditor();
}
// HashMap<String, Command>
abstract class Command {
}
class CompileCommand extends Command {
public void doit() {
}
}
class MyKeyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
// System.out.print(e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
class MyDocumentListener implements DocumentListener {
@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
if (!TextEditor.this.fileEdited) {
// System.out.println("file edited");
TextEditor.this.fileEdited = true;
}
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
if (!TextEditor.this.fileEdited) {
// System.out.println("file edited");
TextEditor.this.fileEdited = true;
}
}
@Override
public void changedUpdate(DocumentEvent e) {
// TODO Plain text components do not fire these events
}
}
}