1 package net.matuschek.jobo; 2 3 import javax.swing.BoxLayout ; 4 import javax.swing.JButton ; 5 import javax.swing.JCheckBox ; 6 import javax.swing.JOptionPane ; 7 import javax.swing.JPanel ; 8 import javax.swing.JTextField ; 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 18 19 20 21 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 mainPanel = new JPanel (); 41 mainPanel.setLayout(new BoxLayout (mainPanel,BoxLayout.Y_AXIS)); 42 setContentPane(mainPanel); 43 44 OptionPanel dialogPanel = new OptionPanel(2); 45 46 JPanel buttonPanel = new JPanel (); 47 buttonPanel.setLayout(new BoxLayout (buttonPanel,BoxLayout.X_AXIS)); 48 49 mainPanel.add(dialogPanel); 50 mainPanel.add(buttonPanel); 51 52 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 (); 79 dialogPanel.add("Enable cookies",cookiesEnabled); 80 81 allowWholeHost = new JCheckBox (); 82 dialogPanel.add("Allow all URLs on start host",allowWholeHost); 83 84 allowWholeDomain = new JCheckBox (); 85 dialogPanel.add("Allow all URLs in the same domain",allowWholeDomain); 86 87 flexibleHostCheck = new JCheckBox (); 88 dialogPanel.add("Do flexible host checking",flexibleHostCheck); 89 90 ignoreRobotsTxt = new JCheckBox (); 91 dialogPanel.add("Ignore robots.txt",ignoreRobotsTxt); 92 93 localizeLinks = new JCheckBox (); 94 dialogPanel.add("Localize links",localizeLinks); 95 96 storeCGI = new JCheckBox (); 97 dialogPanel.add("Save dynamic pages",storeCGI); 98 99 allowCaching = new JCheckBox (); 100 dialogPanel.add("Don't retrieve pages that are already on disk " 101 +"(resume download)", 102 allowCaching); 103 104 105 106 107 JButton okButton = new JButton (); 108 okButton.setText("OK"); 109 okButton.addActionListener(new java.awt.event.ActionListener () { 110 public void actionPerformed(java.awt.event.ActionEvent evt) { 111 updateAndHide(); 112 } 113 }); 114 buttonPanel.add(okButton); 115 116 JButton closeButton = new JButton (); 117 closeButton.setText("Cancel"); 118 closeButton.addActionListener(new java.awt.event.ActionListener () { 119 public void actionPerformed(java.awt.event.ActionEvent evt) { 120 exitForm(); 121 } 122 }); 123 buttonPanel.add(closeButton); 124 125 126 127 pack (); 128 updateFormFromRobot(); 129 } 130 131 132 135 protected void updateAndHide() { 136 if (updateRobotFromForm()) { 137 this.setVisible(false); 138 } 139 } 140 141 142 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 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 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 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 e) { 197 JOptionPane.showMessageDialog(this, 198 "Sleep time must be a number", 199 "Error", 200 JOptionPane.ERROR_MESSAGE); 201 return false; 202 } 203 204 try { 206 robot.setBandwidth(Integer.parseInt(bandwidthField.getText())); 207 } catch (NumberFormatException e) { 208 JOptionPane.showMessageDialog(this, 209 "Bandwidth must be an integer", 210 "Error", 211 JOptionPane.ERROR_MESSAGE); 212 return false; 213 } 214 215 if (! maxAgeField.getText().equals("")) { 217 try { 218 int age = Integer.parseInt(maxAgeField.getText()); 219 robot.setMaxDocumentAge(age*86400); 221 } catch (NumberFormatException 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 251 WebRobot robot = null; 252 253 254 JoBoBase jobobase = null; 255 256 257 JTextField agentNameField = null; 259 JTextField startRefererField = null; 260 JTextField proxyField = null; 261 JTextField maxDepthField = null; 262 JTextField sleepField = null; 263 JTextField bandwidthField = null; 264 JTextField maxAgeField = null; 265 JCheckBox cookiesEnabled = null; 266 JCheckBox allowWholeHost = null; 267 JCheckBox allowWholeDomain = null; 268 JCheckBox flexibleHostCheck = null; 269 JCheckBox ignoreRobotsTxt = null; 270 JCheckBox localizeLinks = null; 271 JCheckBox storeCGI = null; 272 JCheckBox allowCaching = null; 273 275 } | Popular Tags |