001: /*
002: * WebSphinx web-crawling toolkit
003: *
004: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
020: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
022: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
023: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
024: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
025: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
029: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: *
031: */
032:
033: package websphinx.workbench;
034:
035: import java.awt.*;
036: import websphinx.*;
037: import rcm.awt.Constrain;
038: import rcm.awt.PopupDialog;
039: import rcm.util.Win;
040:
041: // FIX: consider implementing java.beans.PropertyEditor
042: public class ActionEditor extends Panel {
043:
044: ActionFeatureChoice choice;
045:
046: /**
047: * Make a ActionEditor.
048: */
049: public ActionEditor() {
050: setLayout(new GridBagLayout());
051: choice = new ActionFeatureChoice();
052: Constrain.add(this , choice, Constrain.labelLike(0, 0));
053: Constrain.add(this , choice.getArgs(), Constrain.areaLike(0, 1));
054: setAction(null);
055: }
056:
057: public void setAction(Action act) {
058: choice.setAction(act);
059: }
060:
061: public Action getAction() {
062: return choice.getAction();
063: }
064:
065: }
066:
067: class ActionFeatureChoice extends FeatureChoice {
068: ActionFeatureArgs args = new ActionFeatureArgs();
069:
070: final static String NULL_ACTION = "none";
071: final static String HIGHLIGHT_ACTION = "highlight";
072: final static String MIRROR_ACTION = "save";
073: final static String CONCAT_ACTION = "concatenate";
074: final static String EXTRACT_ACTION = "extract";
075: final static String SCRIPT_ACTION = "script";
076:
077: public ActionFeatureChoice() {
078: addItem(NULL_ACTION);
079: addItem(MIRROR_ACTION);
080: addItem(CONCAT_ACTION);
081: addItem(EXTRACT_ACTION);
082: addItem(HIGHLIGHT_ACTION);
083: addItem(SCRIPT_ACTION);
084: }
085:
086: public void setAction(Action act) {
087: if (act == null) {
088: select(NULL_ACTION);
089: } else if (act instanceof HighlightAction) {
090: HighlightAction highlight = (HighlightAction) act;
091: select(HIGHLIGHT_ACTION);
092: args.setColor(highlight.getColor());
093: args.setScale(highlight.getScale());
094: args.setIcon(highlight.getIcon());
095: } else if (act instanceof MirrorAction) {
096: MirrorAction mirror = (MirrorAction) act;
097: select(MIRROR_ACTION);
098: args.setMirrorDirectory(mirror.getDirectory());
099: args.setMirrorUseBrowser(mirror.getUseBrowser());
100: } else if (act instanceof ConcatAction) {
101: ConcatAction concat = (ConcatAction) act;
102: select(CONCAT_ACTION);
103: args.setConcatFilename(concat.getFilename());
104: args.setConcatUseBrowser(concat.getUseBrowser());
105: args.prolog = concat.prolog != null ? concat.prolog
106: : Concatenator.defaultProlog;
107: args.header = concat.header != null ? concat.header
108: : Concatenator.defaultHeader;
109: args.footer = concat.footer != null ? concat.footer
110: : Concatenator.defaultFooter;
111: args.divider = concat.divider != null ? concat.divider
112: : Concatenator.defaultDivider;
113: args.epilog = concat.epilog != null ? concat.epilog
114: : Concatenator.defaultEpilog;
115: } else if (act instanceof ExtractAction) {
116: ExtractAction extract = (ExtractAction) act;
117: select(EXTRACT_ACTION);
118: args.setExtractFilename(extract.getFilename());
119: args.setExtractUseBrowser(extract.getUseBrowser());
120: args.setExtractPattern(extract.getPattern().toString());
121: args.setTextOnly(extract.getTextOnly());
122: } else if (act instanceof Script) {
123: Script script = (Script) act;
124: select(SCRIPT_ACTION);
125: args.setScript(script.getScript());
126: } else {
127: select(NULL_ACTION);
128: }
129: }
130:
131: public Panel getArgs() {
132: return args;
133: }
134:
135: public Action getAction() {
136: String actn = getSelectedItem();
137: if (actn.equals(HIGHLIGHT_ACTION))
138: return new HighlightAction(args.getColor(),
139: args.getScale(), args.getIcon());
140: else if (actn.equals(MIRROR_ACTION))
141: return new MirrorAction(args.getMirrorDirectory(), args
142: .getMirrorUseBrowser());
143: else if (actn.equals(CONCAT_ACTION))
144: return new ConcatAction(args.getConcatFilename(), args
145: .getConcatUseBrowser(), args.prolog, args.header,
146: args.footer, args.divider, args.epilog);
147: else if (actn.equals(EXTRACT_ACTION))
148: return new ExtractAction(new Tagexp(args
149: .getExtractPattern()), args.getExtractUseBrowser(),
150: args.getExtractFilename(), args.getTextOnly());
151: else if (actn.equals(SCRIPT_ACTION))
152: return new Script(args.getScript(), false);
153: else
154: return null;
155: }
156: }
157:
158: class ActionFeatureArgs extends Panel {
159:
160: static final String TEMPORARY_DIR = "(temporary directory)";
161: static final String TEMPORARY_FILE = "(temporary file)";
162:
163: Choice color;
164: Choice scale;
165: //Choice icon;
166: TextField mirrorDirectory;
167: Checkbox mirrorUseBrowser;
168:
169: TextField concatFilename;
170: Checkbox concatUseBrowser;
171: Button optionsButton;
172: String prolog = Concatenator.defaultProlog;
173: String header = Concatenator.defaultHeader;
174: String footer = Concatenator.defaultFooter;
175: String divider = Concatenator.defaultDivider;
176: String epilog = Concatenator.defaultEpilog;
177:
178: TextField extractFilename;
179: TextArea extractPattern;
180: Choice extractMedium;
181: Checkbox extractUseBrowser;
182:
183: TextArea script;
184:
185: Button browseMirrorDirectory;
186: Button browseConcatFilename;
187: Button browseExtractFilename;
188:
189: public ActionFeatureArgs() {
190: Panel panel;
191:
192: setLayout(new CardLayout());
193:
194: add(ActionFeatureChoice.NULL_ACTION, panel = new Panel());
195:
196: add(ActionFeatureChoice.HIGHLIGHT_ACTION, panel = Constrain
197: .makeConstrainedPanel(4, 1));
198: Constrain.add(panel, new Label(" with color "), Constrain
199: .labelLike(0, 0));
200: Constrain.add(panel, color = new Choice(), Constrain.fieldLike(
201: 1, 0));
202: color.addItem("black");
203: color.addItem("blue");
204: color.addItem("cyan");
205: color.addItem("green");
206: color.addItem("magenta");
207: color.addItem("orange");
208: color.addItem("pink");
209: color.addItem("red");
210: color.addItem("white");
211: color.addItem("yellow");
212: color.select("blue");
213: scale = new Choice();
214: /*Constrain.add (panel, new Label (" and scale "), Constrain.labelLike (2, 0));
215: Constrain.add (panel, scale, Constrain.fieldLike (3, 0));
216: scale.addItem ("small");
217: scale.addItem ("normal");
218: scale.addItem ("large");
219: scale.select ("normal");*/
220: // NIY: icon
221: //Constrain.add (panel, new Label (" and icon "), Constrain.labelLike (4, 0));
222: //Constrain.add (panel, icon = new Choice (), Constrain.fieldLike (5, 0));
223: add(ActionFeatureChoice.MIRROR_ACTION, panel = Constrain
224: .makeConstrainedPanel(3, 2));
225: Constrain.add(panel, new Label("to directory: "), Constrain
226: .labelLike(0, 0));
227: Constrain.add(panel, mirrorDirectory = new TextField(),
228: Constrain.fieldLike(1, 0));
229: Constrain.add(panel, browseMirrorDirectory = new Button("..."),
230: Constrain.labelLike(2, 0));
231: mirrorUseBrowser = new Checkbox("Display directory in browser");
232: mirrorUseBrowser.setState(true);
233: if (Context.getBrowser() != null) {
234: mirrorDirectory.setText(TEMPORARY_DIR);
235: Constrain.add(panel, mirrorUseBrowser, Constrain.labelLike(
236: 1, 1));
237: }
238:
239: add(ActionFeatureChoice.CONCAT_ACTION, panel = Constrain
240: .makeConstrainedPanel(4, 2));
241: Constrain.add(panel, new Label("to file: "), Constrain
242: .labelLike(0, 0));
243: Constrain.add(panel, concatFilename = new TextField(),
244: Constrain.fieldLike(1, 0, 2));
245: Constrain.add(panel, browseConcatFilename = new Button("..."),
246: Constrain.labelLike(3, 0));
247: concatUseBrowser = new Checkbox("Display in browser");
248: concatUseBrowser.setState(true);
249: if (Context.getBrowser() != null) {
250: concatFilename.setText(TEMPORARY_FILE);
251: Constrain.add(panel, concatUseBrowser, Constrain.labelLike(
252: 1, 1));
253: }
254: Constrain.add(panel, optionsButton = new Button("Options..."),
255: Constrain.labelLike(2, 1));
256:
257: add(ActionFeatureChoice.EXTRACT_ACTION, panel = Constrain
258: .makeConstrainedPanel(5, 4));
259: Constrain.add(panel, new Label(
260: "regions matching the HTML tag expression:"), Constrain
261: .labelLike(0, 0, 5));
262: Constrain.add(panel, extractPattern = new TextArea(3, 40),
263: Constrain.fieldLike(0, 1, 5));
264: Constrain
265: .add(panel, new Label("as"), Constrain.labelLike(0, 2));
266: Constrain.add(panel, extractMedium = new Choice(), Constrain
267: .labelLike(1, 2));
268: extractMedium.addItem("HTML");
269: extractMedium.addItem("text");
270: Constrain.add(panel, new Label("to file: "), Constrain
271: .labelLike(2, 2));
272: Constrain.add(panel, extractFilename = new TextField(),
273: Constrain.fieldLike(3, 2));
274: Constrain.add(panel, browseExtractFilename = new Button("..."),
275: Constrain.labelLike(4, 2));
276: extractUseBrowser = new Checkbox("Display in browser");
277: extractUseBrowser.setState(true);
278: if (Context.getBrowser() != null) {
279: extractFilename.setText(TEMPORARY_FILE);
280: Constrain.add(panel, extractUseBrowser, Constrain
281: .labelLike(3, 3));
282: }
283:
284: ScriptInterpreter interp = Context.getScriptInterpreter();
285:
286: script = new TextArea(4, 40);
287: if (interp != null) {
288: add(ActionFeatureChoice.SCRIPT_ACTION, panel = Constrain
289: .makeConstrainedPanel(1, 2));
290: Constrain.add(panel, new Label(interp.getLanguage()
291: + " Function (crawler, page)"), Constrain
292: .labelLike(0, 0));
293: Constrain.add(panel, script, Constrain.areaLike(0, 1));
294: } else {
295: add(ActionFeatureChoice.SCRIPT_ACTION, panel = Constrain
296: .makeConstrainedPanel(1, 1));
297: Constrain.add(panel, new Label(
298: "No scripting language is available."), Constrain
299: .labelLike(0, 0));
300: }
301: }
302:
303: public boolean handleEvent(Event event) {
304: if (event.id == Event.ACTION_EVENT) {
305: if (event.target == browseMirrorDirectory)
306: browse("Save Pages in Directory", mirrorDirectory);
307: else if (event.target == browseConcatFilename)
308: browse("Save Concatenation As", concatFilename);
309: else if (event.target == browseExtractFilename)
310: browse("Save Extracts As", extractFilename);
311: else if (event.target == optionsButton)
312: new ConcatOptions(this ).show();
313: else
314: return super .handleEvent(event);
315: } else
316: return super .handleEvent(event);
317:
318: return true;
319: }
320:
321: void browse(String title, TextField target) {
322: String fn = PopupDialog.askFilename(this , title, target
323: .getText(), false);
324: if (fn != null)
325: target.setText(fn);
326: }
327:
328: public void setColor(String color) {
329: this .color.select(color);
330: }
331:
332: public String getColor() {
333: return color.getSelectedItem();
334: }
335:
336: public void setScale(String scale) {
337: try {
338: double d = Double.valueOf(scale).doubleValue();
339: // FIX: allow user to enter any scale factor ?
340: if (d < 1)
341: this .scale.select("small");
342: else if (d > 1)
343: this .scale.select("large");
344: else
345: this .scale.select("normal");
346: } catch (NumberFormatException e) {
347: this .scale.select("normal");
348: }
349: }
350:
351: public String getScale() {
352: switch (scale.getSelectedIndex()) {
353: case 0:
354: return "0.5";
355: case 2:
356: return "2.0";
357: default:
358: return "1.0";
359: }
360: }
361:
362: public void setIcon(String icon) {
363: //this.icon.select (color);
364: }
365:
366: public String getIcon() {
367: return null;
368: //return icon.getSelectedItem ();
369: }
370:
371: public void setMirrorDirectory(String directory) {
372: mirrorDirectory.setText(directory != null ? directory
373: : TEMPORARY_DIR);
374: }
375:
376: public String getMirrorDirectory() {
377: String f = mirrorDirectory.getText();
378: return f.equals(TEMPORARY_DIR) ? null : f;
379: }
380:
381: public void setMirrorUseBrowser(boolean use) {
382: mirrorUseBrowser.setState(use);
383: }
384:
385: public boolean getMirrorUseBrowser() {
386: return mirrorUseBrowser.getState();
387: }
388:
389: public void setConcatFilename(String filename) {
390: concatFilename.setText(filename != null ? filename
391: : TEMPORARY_FILE);
392: }
393:
394: public String getConcatFilename() {
395: String f = concatFilename.getText();
396: return f.equals(TEMPORARY_FILE) ? null : f;
397: }
398:
399: public void setConcatUseBrowser(boolean use) {
400: concatUseBrowser.setState(use);
401: }
402:
403: public boolean getConcatUseBrowser() {
404: return concatUseBrowser.getState();
405: }
406:
407: public void setExtractFilename(String filename) {
408: extractFilename.setText(filename != null ? filename
409: : TEMPORARY_FILE);
410: }
411:
412: public String getExtractFilename() {
413: String f = extractFilename.getText();
414: return f.equals(TEMPORARY_FILE) ? null : f;
415: }
416:
417: public void setExtractUseBrowser(boolean use) {
418: extractUseBrowser.setState(use);
419: }
420:
421: public boolean getExtractUseBrowser() {
422: return extractUseBrowser.getState();
423: }
424:
425: public void setExtractPattern(String pattern) {
426: extractPattern.setText(pattern);
427: }
428:
429: public String getExtractPattern() {
430: return extractPattern.getText();
431: }
432:
433: public void setTextOnly(boolean f) {
434: extractMedium.select(f ? "text" : "HTML");
435: }
436:
437: public boolean getTextOnly() {
438: return extractMedium.getSelectedItem().equals("text");
439: }
440:
441: public void setScript(String script) {
442: this .script.setText(script);
443: }
444:
445: public String getScript() {
446: return script.getText();
447: }
448: }
449:
450: class ConcatOptions extends PopupDialog {
451: ActionFeatureArgs e;
452:
453: TextArea prolog, header, footer, divider, epilog;
454:
455: Button applyButton;
456: Button okButton;
457: Button cancelButton;
458:
459: public ConcatOptions(ActionFeatureArgs e) {
460: super (Win.findFrame(e), "Concatenate Options", true);
461: this .e = e;
462:
463: setLayout(new GridBagLayout());
464:
465: Constrain.add(this , new Label("Prolog:"), Constrain.labelLike(
466: 0, 0));
467: Constrain.add(this , prolog = new TextArea(e.prolog, 3, 40),
468: Constrain.areaLike(1, 0));
469:
470: Constrain.add(this , new Label("Page Header:"), Constrain
471: .labelLike(0, 1));
472: Constrain.add(this , header = new TextArea(e.header, 3, 40),
473: Constrain.areaLike(1, 1));
474:
475: Constrain.add(this , new Label("Page Footer:"), Constrain
476: .labelLike(0, 2));
477: Constrain.add(this , footer = new TextArea(e.footer, 3, 40),
478: Constrain.areaLike(1, 2));
479:
480: Constrain.add(this , new Label("Page Divider:"), Constrain
481: .labelLike(0, 3));
482: Constrain.add(this , divider = new TextArea(e.divider, 3, 40),
483: Constrain.areaLike(1, 3));
484:
485: Constrain.add(this , new Label("Epilog:"), Constrain.labelLike(
486: 0, 4));
487: Constrain.add(this , epilog = new TextArea(e.epilog, 3, 40),
488: Constrain.areaLike(1, 4));
489:
490: Panel panel;
491: Constrain.add(this , panel = new Panel(), Constrain
492: .centered(Constrain.labelLike(0, 5, 2)));
493: panel.add(applyButton = new Button("Apply"));
494: panel.add(okButton = new Button("OK"));
495: panel.add(cancelButton = new Button("Cancel"));
496:
497: pack();
498: }
499:
500: void writeBack() {
501: e.prolog = prolog.getText();
502: e.header = header.getText();
503: e.footer = footer.getText();
504: e.divider = divider.getText();
505: e.epilog = epilog.getText();
506: }
507:
508: public boolean handleEvent(Event event) {
509: if (event.id == Event.ACTION_EVENT) {
510: if (event.target == applyButton)
511: writeBack();
512: else if (event.target == okButton) {
513: writeBack();
514: close();
515: } else if (event.target == cancelButton)
516: close();
517: else
518: return super .handleEvent(event);
519: } else
520: return super .handleEvent(event);
521:
522: return true;
523: }
524: }
|