001: package net.matuschek.jobo;
002:
003: import javax.swing.BoxLayout;
004: import javax.swing.JButton;
005: import javax.swing.JCheckBox;
006: import javax.swing.JOptionPane;
007: import javax.swing.JPanel;
008: import javax.swing.JTextField;
009:
010: import net.matuschek.http.HttpException;
011: import net.matuschek.spider.WebRobot;
012: import net.matuschek.swing.JHideFrame;
013: import net.matuschek.swing.OptionPanel;
014: import net.matuschek.swing.SwingHelper;
015:
016: /*********************************************
017: Copyright (c) 2001 by Daniel Matuschek
018: *********************************************/
019:
020: /**
021: * Configuration dialog for the robot
022: *
023: * @author Daniel Matuschek
024: * @version $Id: RobotConfigFrame.java,v 1.20 2004/08/09 13:06:56 matuschd Exp $
025: */
026: public class RobotConfigFrame extends JHideFrame {
027:
028: private static final long serialVersionUID = 8977779246579784214L;
029:
030: public RobotConfigFrame(JoBoBase jobobase) {
031: super ("Robot configuration");
032: this .robot = jobobase.getRobot();
033: this .jobobase = jobobase;
034: initComponents();
035: }
036:
037: protected void initComponents() {
038: JPanel mainPanel = new JPanel();
039: mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
040: setContentPane(mainPanel);
041:
042: OptionPanel dialogPanel = new OptionPanel(2);
043:
044: JPanel buttonPanel = new JPanel();
045: buttonPanel.setLayout(new BoxLayout(buttonPanel,
046: BoxLayout.X_AXIS));
047:
048: mainPanel.add(dialogPanel);
049: mainPanel.add(buttonPanel);
050:
051: /**
052: Input fields
053: **/
054: agentNameField = SwingHelper.createInputField(25);
055: dialogPanel.add("Agent name:", agentNameField);
056:
057: startRefererField = SwingHelper.createInputField(25);
058: dialogPanel.add("Start referer:", startRefererField);
059:
060: proxyField = SwingHelper.createInputField(25);
061: dialogPanel.add("Proxy (host:port or empty):", proxyField);
062:
063: maxDepthField = SwingHelper.createInputField(5);
064: dialogPanel.add("Maximal search depth:", maxDepthField);
065:
066: sleepField = SwingHelper.createInputField(5);
067: dialogPanel.add("Sleep time (in seconds)", sleepField);
068:
069: bandwidthField = SwingHelper.createInputField(5);
070: dialogPanel.add("Bandwidth limit in Bytes/s (0 to disable)",
071: bandwidthField);
072:
073: maxAgeField = SwingHelper.createInputField(5);
074: dialogPanel.add("Maximum age in days (leave empty to disable)",
075: maxAgeField);
076:
077: cookiesEnabled = new JCheckBox();
078: dialogPanel.add("Enable cookies", cookiesEnabled);
079:
080: allowWholeHost = new JCheckBox();
081: dialogPanel.add("Allow all URLs on start host", allowWholeHost);
082:
083: allowWholeDomain = new JCheckBox();
084: dialogPanel.add("Allow all URLs in the same domain",
085: allowWholeDomain);
086:
087: flexibleHostCheck = new JCheckBox();
088: dialogPanel.add("Do flexible host checking", flexibleHostCheck);
089:
090: ignoreRobotsTxt = new JCheckBox();
091: dialogPanel.add("Ignore robots.txt", ignoreRobotsTxt);
092:
093: localizeLinks = new JCheckBox();
094: dialogPanel.add("Localize links", localizeLinks);
095:
096: storeCGI = new JCheckBox();
097: dialogPanel.add("Save dynamic pages", storeCGI);
098:
099: allowCaching = new JCheckBox();
100: dialogPanel.add(
101: "Don't retrieve pages that are already on disk "
102: + "(resume download)", allowCaching);
103:
104: /** End of input fields */
105:
106: /** Button panel */
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
119: .addActionListener(new java.awt.event.ActionListener() {
120: public void actionPerformed(
121: java.awt.event.ActionEvent evt) {
122: exitForm();
123: }
124: });
125: buttonPanel.add(closeButton);
126:
127: /** End of button panel */
128:
129: pack();
130: updateFormFromRobot();
131: }
132:
133: /**
134: * update the robot settings from the form contents and hide window
135: */
136: protected void updateAndHide() {
137: if (updateRobotFromForm()) {
138: this .setVisible(false);
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: * update the robot settings from the current fields
174: * @return true if everything is okay, false otherwise
175: */
176: protected boolean updateRobotFromForm() {
177: robot.setAgentName(agentNameField.getText());
178: robot.setStartReferer(startRefererField.getText());
179: try {
180: robot.setProxy(proxyField.getText());
181: } catch (HttpException e) {
182: }
183:
184: try {
185: robot
186: .setMaxDepth(Integer.parseInt(maxDepthField
187: .getText()));
188: } catch (NumberFormatException e) {
189: JOptionPane.showMessageDialog(this ,
190: "Maximal depth must be a number", "Error",
191: JOptionPane.ERROR_MESSAGE);
192: return false;
193: }
194:
195: try {
196: robot.setSleepTime(Integer.parseInt(sleepField.getText()));
197: } catch (NumberFormatException e) {
198: JOptionPane.showMessageDialog(this ,
199: "Sleep time must be a number", "Error",
200: JOptionPane.ERROR_MESSAGE);
201: return false;
202: }
203:
204: // bandwidth limitation
205: try {
206: robot.setBandwidth(Integer.parseInt(bandwidthField
207: .getText()));
208: } catch (NumberFormatException e) {
209: JOptionPane.showMessageDialog(this ,
210: "Bandwidth must be an integer", "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 e) {
222: JOptionPane.showMessageDialog(this ,
223: "Age must be an integer", "Error",
224: JOptionPane.ERROR_MESSAGE);
225: return false;
226: }
227: } else {
228: robot.setMaxDocumentAge(-1);
229: }
230:
231: robot.setEnableCookies(cookiesEnabled.isSelected());
232: robot.setAllowWholeHost(allowWholeHost.isSelected());
233: robot.setAllowWholeDomain(allowWholeDomain.isSelected());
234: robot.setFlexibleHostCheck(flexibleHostCheck.isSelected());
235: robot.setIgnoreRobotsTxt(ignoreRobotsTxt.isSelected());
236: robot.setAllowCaching(allowCaching.isSelected());
237:
238: jobobase.setLocalizeLinks(localizeLinks.isSelected());
239: jobobase.setStoreCGI(storeCGI.isSelected());
240:
241: return true;
242: }
243:
244: /**********************************************************************/
245: /**********************************************************************/
246: /**********************************************************************/
247:
248: /** the web robot to configure */
249: WebRobot robot = null;
250:
251: /** jobobase to configure */
252: JoBoBase jobobase = null;
253:
254: // input fields
255: JTextField agentNameField = null;
256: JTextField startRefererField = null;
257: JTextField proxyField = null;
258: JTextField maxDepthField = null;
259: JTextField sleepField = null;
260: JTextField bandwidthField = null;
261: JTextField maxAgeField = null;
262: JCheckBox cookiesEnabled = null;
263: JCheckBox allowWholeHost = null;
264: JCheckBox allowWholeDomain = null;
265: JCheckBox flexibleHostCheck = null;
266: JCheckBox ignoreRobotsTxt = null;
267: JCheckBox localizeLinks = null;
268: JCheckBox storeCGI = null;
269: JCheckBox allowCaching = null;
270: //
271:
272: } // RobotConfigFrame
|