forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWebPageEx.java
More file actions
159 lines (119 loc) · 4.13 KB
/
ReadWebPageEx.java
File metadata and controls
159 lines (119 loc) · 4.13 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
package com.zetcode;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* This program reads HTML code from a selected webpage.
* It utilizes an ExecutorService to do the expensive work.
*/
public class ReadWebPageEx extends JFrame {
private JTextField field;
private JTextArea area;
public ReadWebPageEx() {
initUI();
}
private void initUI() {
field = new JTextField(30);
field.addActionListener(new EnterAction());
area = new JTextArea(10, 30);
var scrollPane = new JScrollPane();
scrollPane.getViewport().add(area);
createLayout(field, scrollPane);
setTitle("Reading web page");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private class EnterAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
readPage();
}
private void readPage() {
var webPage = field.getText();
ExecutorService executor
= Executors.newSingleThreadExecutor();
Callable<List<String>> callable = new MyTask(webPage);
Future<List<String>> future = executor.submit(callable);
try {
List<String> lines = future.get(15, TimeUnit.SECONDS);
if (!area.getText().isEmpty()) {
area.setText("");
}
for (String line : lines) {
area.append(line + "\n");
}
} catch (InterruptedException | TimeoutException |
ExecutionException ex) {
Logger.getLogger(ReadWebPageEx.class.getName()).log(
Level.INFO, null, ex);
}
executor.shutdown();
}
}
private class MyTask implements Callable<List<String>> {
private final String page;
public MyTask(String page) {
this.page = page;
}
@Override
public List<String> call() throws IOException {
List<String> lines = new ArrayList<>();
var url = new URL(page);
try (var br = new BufferedReader(
new InputStreamReader(url.openStream()))) {
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
}
return lines;
}
}
private void createLayout(JComponent... arg) {
var pane = getContentPane();
var gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(arg[0], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE)
.addComponent(arg[1])
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addGap(10)
.addComponent(arg[1])
);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var ex = new ReadWebPageEx();
ex.setVisible(true);
});
}
}