KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > jobo > RobotConfigFrame


1 package net.matuschek.jobo;
2
3 import javax.swing.BoxLayout JavaDoc;
4 import javax.swing.JButton JavaDoc;
5 import javax.swing.JCheckBox JavaDoc;
6 import javax.swing.JOptionPane JavaDoc;
7 import javax.swing.JPanel JavaDoc;
8 import javax.swing.JTextField JavaDoc;
9
10 import net.matuschek.http.HttpException;
11 import net.matuschek.spider.WebRobot;
12 import net.matuschek.swing.JHideFrame;
13 import net.matuschek.swing.OptionPanel;
14 import net.matuschek.swing.SwingHelper;
15 /*********************************************
16     Copyright (c) 2001 by Daniel Matuschek
17  *********************************************/

18
19
20
21 /**
22  * Configuration dialog for the robot
23  *
24  * @author Daniel Matuschek
25  * @version $Id: RobotConfigFrame.java,v 1.20 2004/08/09 13:06:56 matuschd Exp $
26  */

27 public class RobotConfigFrame extends JHideFrame {
28
29     private static final long serialVersionUID = 8977779246579784214L;
30
31     public RobotConfigFrame(JoBoBase jobobase) {
32         super("Robot configuration");
33         this.robot = jobobase.getRobot();
34         this.jobobase = jobobase;
35         initComponents();
36     }
37
38
39     protected void initComponents() {
40         JPanel JavaDoc mainPanel = new JPanel JavaDoc();
41         mainPanel.setLayout(new BoxLayout JavaDoc(mainPanel,BoxLayout.Y_AXIS));
42         setContentPane(mainPanel);
43
44         OptionPanel dialogPanel = new OptionPanel(2);
45
46         JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
47         buttonPanel.setLayout(new BoxLayout JavaDoc(buttonPanel,BoxLayout.X_AXIS));
48
49         mainPanel.add(dialogPanel);
50         mainPanel.add(buttonPanel);
51
52         /**
53        Input fields
54          **/

55         agentNameField = SwingHelper.createInputField(25);
56         dialogPanel.add("Agent name:",agentNameField);
57
58         startRefererField = SwingHelper.createInputField(25);
59         dialogPanel.add("Start referer:",startRefererField);
60
61         proxyField = SwingHelper.createInputField(25);
62         dialogPanel.add("Proxy (host:port or empty):",proxyField);
63
64         maxDepthField = SwingHelper.createInputField(5);
65         dialogPanel.add("Maximal search depth:",maxDepthField);
66
67         sleepField = SwingHelper.createInputField(5);
68         dialogPanel.add("Sleep time (in seconds)",sleepField);
69
70         bandwidthField = SwingHelper.createInputField(5);
71         dialogPanel.add("Bandwidth limit in Bytes/s (0 to disable)",
72                 bandwidthField);
73
74         maxAgeField = SwingHelper.createInputField(5);
75         dialogPanel.add("Maximum age in days (leave empty to disable)",
76                 maxAgeField);
77
78         cookiesEnabled = new JCheckBox JavaDoc();
79         dialogPanel.add("Enable cookies",cookiesEnabled);
80
81         allowWholeHost = new JCheckBox JavaDoc();
82         dialogPanel.add("Allow all URLs on start host",allowWholeHost);
83
84         allowWholeDomain = new JCheckBox JavaDoc();
85         dialogPanel.add("Allow all URLs in the same domain",allowWholeDomain);
86
87         flexibleHostCheck = new JCheckBox JavaDoc();
88         dialogPanel.add("Do flexible host checking",flexibleHostCheck);
89
90         ignoreRobotsTxt = new JCheckBox JavaDoc();
91         dialogPanel.add("Ignore robots.txt",ignoreRobotsTxt);
92
93         localizeLinks = new JCheckBox JavaDoc();
94         dialogPanel.add("Localize links",localizeLinks);
95
96         storeCGI = new JCheckBox JavaDoc();
97         dialogPanel.add("Save dynamic pages",storeCGI);
98
99         allowCaching = new JCheckBox JavaDoc();
100         dialogPanel.add("Don't retrieve pages that are already on disk "
101                 +"(resume download)",
102                 allowCaching);
103
104         /** End of input fields */
105
106         /** Button panel */
107         JButton JavaDoc okButton = new JButton JavaDoc();
108         okButton.setText("OK");
109         okButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
110             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
111                 updateAndHide();
112             }
113         });
114         buttonPanel.add(okButton);
115
116         JButton JavaDoc closeButton = new JButton JavaDoc();
117         closeButton.setText("Cancel");
118         closeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
119             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
120                 exitForm();
121             }
122         });
123         buttonPanel.add(closeButton);
124
125         /** End of button panel */
126
127         pack ();
128         updateFormFromRobot();
129     }
130
131
132     /**
133      * update the robot settings from the form contents and hide window
134      */

135     protected void updateAndHide() {
136         if (updateRobotFromForm()) {
137             this.setVisible(false);
138         }
139     }
140
141
142     /**
143      * update the content of the form field from the current robot
144      * settings
145      */

146     protected void updateFormFromRobot() {
147         agentNameField.setText(robot.getAgentName());
148         startRefererField.setText(robot.getStartReferer());
149         proxyField.setText(robot.getProxy());
150         maxDepthField.setText(String.valueOf(robot.getMaxDepth()));
151         sleepField.setText(String.valueOf(robot.getSleepTime()));
152         bandwidthField.setText(String.valueOf(robot.getBandwidth()));
153
154         // a day has 86400 seconds
155
long maxAge=robot.getMaxDocumentAge();
156         if (maxAge>0) {
157             maxAgeField.setText(Long.toString(maxAge/86400));
158         } else {
159             maxAgeField.setText("");
160         }
161
162         cookiesEnabled.setSelected(robot.getEnableCookies());
163         allowWholeHost.setSelected(robot.getAllowWholeHost());
164         allowWholeDomain.setSelected(robot.getAllowWholeDomain());
165         flexibleHostCheck.setSelected(robot.getFlexibleHostCheck());
166         ignoreRobotsTxt.setSelected(robot.getIgnoreRobotsTxt());
167         localizeLinks.setSelected(jobobase.getLocalizeLinks());
168         storeCGI.setSelected(jobobase.getStoreCGI());
169         allowCaching.setSelected(robot.getAllowCaching());
170     }
171
172
173     /**
174      * update the robot settings from the current fields
175      * @return true if everything is okay, false otherwise
176      */

177     protected boolean updateRobotFromForm() {
178         robot.setAgentName(agentNameField.getText());
179         robot.setStartReferer(startRefererField.getText());
180         try {
181             robot.setProxy(proxyField.getText());
182         } catch (HttpException e) {}
183
184         try {
185             robot.setMaxDepth(Integer.parseInt(maxDepthField.getText()));
186         } catch (NumberFormatException JavaDoc e) {
187             JOptionPane.showMessageDialog(this,
188                     "Maximal depth must be a number",
189                     "Error",
190                     JOptionPane.ERROR_MESSAGE);
191             return false;
192         }
193
194         try {
195             robot.setSleepTime(Integer.parseInt(sleepField.getText()));
196         } catch (NumberFormatException JavaDoc e) {
197             JOptionPane.showMessageDialog(this,
198                     "Sleep time must be a number",
199                     "Error",
200                     JOptionPane.ERROR_MESSAGE);
201             return false;
202         }
203
204         // bandwidth limitation
205
try {
206             robot.setBandwidth(Integer.parseInt(bandwidthField.getText()));
207         } catch (NumberFormatException JavaDoc e) {
208             JOptionPane.showMessageDialog(this,
209                     "Bandwidth must be an integer",
210                     "Error",
211                     JOptionPane.ERROR_MESSAGE);
212             return false;
213         }
214
215         // maximum age of files to download
216
if (! maxAgeField.getText().equals("")) {
217             try {
218                 int age = Integer.parseInt(maxAgeField.getText());
219                 // a day has 86400 seconds
220
robot.setMaxDocumentAge(age*86400);
221             } catch (NumberFormatException JavaDoc e) {
222                 JOptionPane.showMessageDialog(this,
223                         "Age must be an integer",
224                         "Error",
225                         JOptionPane.ERROR_MESSAGE);
226                 return false;
227             }
228         } else {
229             robot.setMaxDocumentAge(-1);
230         }
231
232
233         robot.setEnableCookies(cookiesEnabled.isSelected());
234         robot.setAllowWholeHost(allowWholeHost.isSelected());
235         robot.setAllowWholeDomain(allowWholeDomain.isSelected());
236         robot.setFlexibleHostCheck(flexibleHostCheck.isSelected());
237         robot.setIgnoreRobotsTxt(ignoreRobotsTxt.isSelected());
238         robot.setAllowCaching(allowCaching.isSelected());
239
240         jobobase.setLocalizeLinks(localizeLinks.isSelected());
241         jobobase.setStoreCGI(storeCGI.isSelected());
242
243         return true;
244     }
245
246     /**********************************************************************/
247     /**********************************************************************/
248     /**********************************************************************/
249
250     /** the web robot to configure */
251     WebRobot robot = null;
252
253     /** jobobase to configure */
254     JoBoBase jobobase = null;
255
256
257     // input fields
258
JTextField JavaDoc agentNameField = null;
259     JTextField JavaDoc startRefererField = null;
260     JTextField JavaDoc proxyField = null;
261     JTextField JavaDoc maxDepthField = null;
262     JTextField JavaDoc sleepField = null;
263     JTextField JavaDoc bandwidthField = null;
264     JTextField JavaDoc maxAgeField = null;
265     JCheckBox JavaDoc cookiesEnabled = null;
266     JCheckBox JavaDoc allowWholeHost = null;
267     JCheckBox JavaDoc allowWholeDomain = null;
268     JCheckBox JavaDoc flexibleHostCheck = null;
269     JCheckBox JavaDoc ignoreRobotsTxt = null;
270     JCheckBox JavaDoc localizeLinks = null;
271     JCheckBox JavaDoc storeCGI = null;
272     JCheckBox JavaDoc allowCaching = null;
273     //
274

275 } // RobotConfigFrame
276
Popular Tags