-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisplayWindow.java
More file actions
121 lines (102 loc) · 3.76 KB
/
DisplayWindow.java
File metadata and controls
121 lines (102 loc) · 3.76 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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultCaret;
public class DisplayWindow {
public static void main(String[] args) {
/**
JEditorPane editor = new JEditorPane(); // the thing that the HTML file appears in
editor.setEditable(false); // so that no weird things appear on the screen (set to true to see a funny html page)
try {
editor.setPage("http://www.google.com"); // remove later - html of website we want to see
}catch (IOException e) {
editor.setContentType("text/html");
editor.setText("<html>Could not load</html>");
}**/
/**
* regular GUI stuff to make the things appear on the screen
*
*/
PageDisplay HTMLPage = new PageDisplay(null); // to change with loaded file
Editor editor = new Editor(); // to change with src code
Reader reader = new Reader(null);
editor.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent e) {
updatePage();
}
@Override
public void insertUpdate(DocumentEvent e) {
updatePage();
}
@Override
public void removeUpdate(DocumentEvent e) {
updatePage();
}
private void updatePage() {
HTMLPage.displayFromEditor(editor.getCode());
}
});
JScrollPane scrollPagePane = new JScrollPane(HTMLPage);
DefaultCaret caret = (DefaultCaret) HTMLPage.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
scrollPagePane.setPreferredSize(new Dimension(800, 600));
JFrame frame = new JFrame("HTML Editor");
JScrollPane scrollEditPane = new JScrollPane(editor);
scrollEditPane.setPreferredSize(new Dimension(600, 600));
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollEditPane, scrollPagePane);
frame.add(mainPanel, BorderLayout.CENTER);
// control panel or toolbar
final JPanel control_panel = new JPanel();
frame.add(control_panel, BorderLayout.NORTH);
final JButton load = new JButton("Load");
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
FileTools.loadFile(frame);
HTMLPage.updateFile(FileTools.getFile());
reader.updateCode(FileTools.getFile());
editor.updateCode(reader.getCode());
} catch (IllegalArgumentException ex) {
if (FileTools.getFile() != null) {
HTMLPage.errorPage();
}
}
}
});
final JButton newFile = new JButton("New");
newFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
FileTools.newFile();
HTMLPage.updateFile(FileTools.getFile());
reader.updateCode(FileTools.getFile());
editor.updateCode(reader.getCode());
} catch (IllegalArgumentException ex) {
HTMLPage.errorPage();
}
}
});
final JButton save = new JButton("Save");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileTools.saveFile(frame, editor.getCode());
}
});
control_panel.add(load);
control_panel.add(newFile);
control_panel.add(save);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(300, 100);
frame.pack();
frame.setVisible(true);
}
}