-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileTools
More file actions
80 lines (72 loc) · 1.84 KB
/
FileTools
File metadata and controls
80 lines (72 loc) · 1.84 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
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author Victoria
*
* Tools/methods to use to manipulate files: Load, Create new file, Save
*
*/
public class FileTools {
private static File file;
private final static JFileChooser chooser = new JFileChooser();
private static boolean isNew = true;
/**
* Choose file to load
*
* @throws IllegalArgumentException if not .html or .htm or .php
*/
public static void loadFile(Component comp) {
FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML & PHP files", "html", "htm", "php");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(comp);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
if (! filter.accept(chooser.getSelectedFile())) {
throw new IllegalArgumentException();
} else {
System.out.println(chooser.getSelectedFile().getName() + " was loaded");
isNew = false;
}
}
/**
* Create a new HTML file
*/
public static void newFile() {
isNew = true;
try {
// check if saved first
file = Files.createTempFile(null, ".html").toFile();
} catch (IOException e) {
file = null;
throw new IllegalArgumentException();
}
}
/**
* Saves and writes to file
*
* @param comp JFrame displaying the panes
* @param newCode edited code that will overwrite old file
*/
public static void saveFile(Component comp, String newCode) {
if (isNew) {
chooser.showSaveDialog(comp);
} else {
// TODO
// find file location
// write newCode to file
// file already has a location --> filewriter
}
}
/**
* Returns chosen file to display
*/
public static File getFile() {
return file;
}
}