forked from automofearmotron/RedditScraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSControlPanel.java
More file actions
221 lines (199 loc) · 6.28 KB
/
RSControlPanel.java
File metadata and controls
221 lines (199 loc) · 6.28 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
/*
GUI for Reddit Scraper
*/
import javax.swing.*;
import java.util.*;
import javax.swing.UIManager.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URI;
public class RSControlPanel extends JFrame
{
private int WIDTH;
private int HEIGHT;
private JButton start;
private JButton stop;
private JButton clearConsole;
private JTextField redditLocationField;
private redditScraper rsScraper;
private Thread activeThread;
private RSEditorPane consolePane;
private RSEditorPane deletePane;
private redditScraper scraper;
private ArrayList<redditScraper> RSThreads = new ArrayList<redditScraper>();
private int numThreads = 0;
private String REDDIT_ROOT = "http://www.reddit.com/r/all/";
public RSControlPanel()
{
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
}
WindowAdapter windowAdapter = new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
};
addWindowListener(windowAdapter);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// The basic frame construction settings
setTitle("Reddit Scraper");
Toolkit toolkit = Toolkit.getDefaultToolkit ();
Dimension dim = toolkit.getScreenSize();
WIDTH = dim.width / 2;
HEIGHT = dim.height / 2;
setBounds(0, 0,WIDTH,HEIGHT);
//Layout
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.FIRST_LINE_END;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.8;
redditLocationField = new JTextField(100);
redditLocationField.setText(REDDIT_ROOT);
add(redditLocationField,c);
c.gridx = 1;
c.weightx = .1;
start = new JButton("Start");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
startAThread();
}
});
add(start,c);
c.gridx = 2;
stop = new JButton("Stop");
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
stopAThread();
}
});
add(stop,c);
c.gridx=3;
clearConsole = new JButton("Clear Console");
clearConsole.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
consolePane.setBody("");
}
});
add(clearConsole,c);
//---------------------------------------------
//---------------------------------------------
c.gridx = 0;
c.gridy = 1;
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 4;
c.fill = GridBagConstraints.BOTH;
JPanel leftPane = new JPanel();
JPanel rightPane = new JPanel();
consolePane = new RSEditorPane();
consolePane.setContentType("text/html");
consolePane.setEditable(false);
consolePane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent hle) {
System.out.println("Description: " + hle.getDescription() +
"Event Type: " + hle.getEventType());
if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
System.out.println("LINK CLICKED" + hle.getURL());
Desktop desktop = Desktop.getDesktop();
try {
String url = hle.getDescription();
String httpHeader = "";
if(url.contains("http://") == false && url.contains("www.") == false)
{
httpHeader = "http://";
}
String URIConversion = httpHeader + hle.getDescription();
URIConversion = URIConversion.trim();
URI website = new URI(URIConversion);
desktop.browse(website);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
deletePane = new RSEditorPane();
deletePane.setContentType("text/html");
deletePane.setEditable(false);
deletePane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent hle) {
System.out.println("Description: " + hle.getDescription() +
"Event Type: " + hle.getEventType());
if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
System.out.println("LINK CLICKED" + hle.getURL());
Desktop desktop = Desktop.getDesktop();
try {
String url = hle.getDescription();
String httpHeader = "";
if(url.contains("http://") == false && url.contains("www.") == false)
{
httpHeader = "http://";
}
String URIConversion = httpHeader + hle.getDescription();
URIConversion = URIConversion.trim();
URI website = new URI(URIConversion);
desktop.browse(website);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
JSplitPane split = new JSplitPane();
JScrollPane sp = new JScrollPane(consolePane);
split.setLeftComponent(sp);
JScrollPane sp2 = new JScrollPane(deletePane);
split.setRightComponent(sp2);
add(split,c);
setVisible(true);
}
public void startAThread()
{
if(numThreads < 3)
{
scraper = new redditScraper();
RSThreads.add(scraper);
scraper.setRedditLocation(redditLocationField.getText());
scraper.setConsoleRSPane(consolePane);
scraper.setDeleteRSPane(deletePane);
activeThread = new Thread(scraper);
activeThread.start();
numThreads++;
}
}
public void stopAThread()
{
try{
for(int i = 0;i < RSThreads.size();i++)
{
RSThreads.get(i).setWatching(false);
consolePane.append("Stopping thread # " + i + ". Threads must run their course before stopping");
}
}catch(Exception e){
}
}
public static void main(String[] args)
{
RSControlPanel visuals = new RSControlPanel();
}
}