Repository navigation
Expand file tree
/
Copy pathFunctions.java
More file actions
79 lines (74 loc) · 2.34 KB
/
Copy pathFunctions.java
File metadata and controls
79 lines (74 loc) · 2.34 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
package org.example;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
public class Functions {
String fileName;
String fileAddress;
GUI gui;
public Functions(GUI gui) {
this.gui = gui;
}
public void newFile() {
gui.textPane.setText("");
gui.window.setTitle("New Document - "+gui.appName);
fileName = null;
fileAddress = null;
}
public void openFile(){
FileDialog fd = new FileDialog(gui.window,"Open",FileDialog.LOAD);
fd.setVisible(true);
if (fd.getFile()!=null){
fileName = fd.getFile();
fileAddress = fd.getDirectory();
gui.window.setTitle(fileName+" - "+gui.appName);
}
try {
BufferedReader br = new BufferedReader(new FileReader(fileAddress + fileName));
gui.textPane.setText("");
String line = null;
while ((line = br.readLine())!=null){
StyledDocument doc = gui.textPane.getStyledDocument();
doc.insertString(doc.getLength(), line + "\n", null);
}
br.close();
} catch (Exception e){
System.out.println("File Not Opened");
}
}
public void save(){
if (fileName==null){
saveAs();
} else {
try {
FileWriter fw = new FileWriter(fileAddress + fileName);
fw.write(gui.textPane.getText());
gui.window.setTitle(fileName + " - " + gui.appName);
fw.close();
}catch (Exception e){
System.out.println("something went wrong");
}
}
}
public void saveAs(){
FileDialog fd = new FileDialog(gui.window,"Save us",FileDialog.SAVE);
fd.setVisible(true);
if (fd.getFile()!=null){
fileName = fd.getFile();
fileAddress = fd.getDirectory();
gui.window.setTitle(fileName+" - "+gui.appName);
}
try {
FileWriter fw = new FileWriter(fileAddress + fileName);
fw.write(gui.textPane.getText());
fw.close();
}catch (Exception e){
System.out.println("Could not save the file");
}
}
public void exit(){
System.exit(0);
}
}