KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > ActionEditor


1 /*
2  * WebSphinx web-crawling toolkit
3  *
4  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package websphinx.workbench;
34
35 import java.awt.*;
36 import websphinx.*;
37 import rcm.awt.Constrain;
38 import rcm.awt.PopupDialog;
39 import rcm.util.Win;
40
41 // FIX: consider implementing java.beans.PropertyEditor
42
public class ActionEditor extends Panel {
43
44     ActionFeatureChoice choice;
45
46     /**
47      * Make a ActionEditor.
48      */

49     public ActionEditor () {
50         setLayout (new GridBagLayout ());
51         choice = new ActionFeatureChoice ();
52         Constrain.add (this, choice, Constrain.labelLike (0, 0));
53         Constrain.add (this, choice.getArgs(), Constrain.areaLike (0, 1));
54         setAction (null);
55     }
56
57     public void setAction (Action act) {
58         choice.setAction (act);
59     }
60
61     public Action getAction () {
62         return choice.getAction ();
63     }
64
65 }
66
67 class ActionFeatureChoice extends FeatureChoice {
68     ActionFeatureArgs args = new ActionFeatureArgs ();
69
70     final static String JavaDoc NULL_ACTION = "none";
71     final static String JavaDoc HIGHLIGHT_ACTION = "highlight";
72     final static String JavaDoc MIRROR_ACTION = "save";
73     final static String JavaDoc CONCAT_ACTION = "concatenate";
74     final static String JavaDoc EXTRACT_ACTION = "extract";
75     final static String JavaDoc SCRIPT_ACTION = "script";
76
77     public ActionFeatureChoice () {
78         addItem (NULL_ACTION);
79         addItem (MIRROR_ACTION);
80         addItem (CONCAT_ACTION);
81         addItem (EXTRACT_ACTION);
82         addItem (HIGHLIGHT_ACTION);
83         addItem (SCRIPT_ACTION);
84     }
85     
86     public void setAction (Action act) {
87         if (act == null) {
88             select (NULL_ACTION);
89         }
90         else if (act instanceof HighlightAction) {
91             HighlightAction highlight = (HighlightAction)act;
92             select (HIGHLIGHT_ACTION);
93             args.setColor (highlight.getColor ());
94             args.setScale (highlight.getScale ());
95             args.setIcon (highlight.getIcon ());
96         }
97         else if (act instanceof MirrorAction) {
98             MirrorAction mirror = (MirrorAction)act;
99             select (MIRROR_ACTION);
100             args.setMirrorDirectory (mirror.getDirectory ());
101             args.setMirrorUseBrowser (mirror.getUseBrowser ());
102         }
103         else if (act instanceof ConcatAction) {
104             ConcatAction concat = (ConcatAction)act;
105             select (CONCAT_ACTION);
106             args.setConcatFilename (concat.getFilename ());
107             args.setConcatUseBrowser (concat.getUseBrowser ());
108             args.prolog = concat.prolog != null ? concat.prolog : Concatenator.defaultProlog;
109             args.header = concat.header != null ? concat.header : Concatenator.defaultHeader;
110             args.footer = concat.footer != null ? concat.footer : Concatenator.defaultFooter;
111             args.divider = concat.divider != null ? concat.divider : Concatenator.defaultDivider;
112             args.epilog = concat.epilog != null ? concat.epilog : Concatenator.defaultEpilog;
113         }
114         else if (act instanceof ExtractAction) {
115             ExtractAction extract = (ExtractAction)act;
116             select (EXTRACT_ACTION);
117             args.setExtractFilename (extract.getFilename ());
118             args.setExtractUseBrowser (extract.getUseBrowser ());
119             args.setExtractPattern (extract.getPattern ().toString ());
120             args.setTextOnly (extract.getTextOnly ());
121         }
122         else if (act instanceof Script) {
123             Script script = (Script)act;
124             select (SCRIPT_ACTION);
125             args.setScript (script.getScript ());
126         }
127         else {
128             select (NULL_ACTION);
129         }
130     }
131
132     public Panel getArgs () {
133         return args;
134     }
135
136     public Action getAction () {
137         String JavaDoc actn = getSelectedItem ();
138         if (actn.equals (HIGHLIGHT_ACTION))
139             return new HighlightAction (args.getColor (),
140                                         args.getScale (),
141                                         args.getIcon ());
142         else if (actn.equals (MIRROR_ACTION))
143             return new MirrorAction (args.getMirrorDirectory (),
144                                      args.getMirrorUseBrowser ());
145         else if (actn.equals (CONCAT_ACTION))
146             return new ConcatAction (args.getConcatFilename (),
147                                      args.getConcatUseBrowser (),
148                                      args.prolog, args.header, args.footer,
149                                      args.divider, args.epilog);
150         else if (actn.equals (EXTRACT_ACTION))
151             return new ExtractAction (new Tagexp (args.getExtractPattern()),
152                                       args.getExtractUseBrowser (),
153                                       args.getExtractFilename (),
154                                       args.getTextOnly ());
155         else if (actn.equals (SCRIPT_ACTION))
156             return new Script (args.getScript (), false);
157         else
158             return null;
159     }
160 }
161
162 class ActionFeatureArgs extends Panel {
163
164     static final String JavaDoc TEMPORARY_DIR = "(temporary directory)";
165     static final String JavaDoc TEMPORARY_FILE = "(temporary file)";
166
167     Choice color;
168     Choice scale;
169     //Choice icon;
170
TextField mirrorDirectory;
171     Checkbox mirrorUseBrowser;
172
173     TextField concatFilename;
174     Checkbox concatUseBrowser;
175     Button optionsButton;
176     String JavaDoc prolog = Concatenator.defaultProlog;
177     String JavaDoc header = Concatenator.defaultHeader;
178     String JavaDoc footer = Concatenator.defaultFooter;
179     String JavaDoc divider = Concatenator.defaultDivider;
180     String JavaDoc epilog = Concatenator.defaultEpilog;
181
182     TextField extractFilename;
183     TextArea extractPattern;
184     Choice extractMedium;
185     Checkbox extractUseBrowser;
186
187     TextArea script;
188
189     Button browseMirrorDirectory;
190     Button browseConcatFilename;
191     Button browseExtractFilename;
192
193     public ActionFeatureArgs () {
194         Panel panel;
195
196         setLayout (new CardLayout ());
197
198         add (ActionFeatureChoice.NULL_ACTION, panel = new Panel ());
199
200         add (ActionFeatureChoice.HIGHLIGHT_ACTION, panel = Constrain.makeConstrainedPanel (4, 1));
201         Constrain.add (panel, new Label (" with color "), Constrain.labelLike (0, 0));
202         Constrain.add (panel, color = new Choice (), Constrain.fieldLike (1, 0));
203         color.addItem ("black");
204         color.addItem ("blue");
205         color.addItem ("cyan");
206         color.addItem ("green");
207         color.addItem ("magenta");
208         color.addItem ("orange");
209         color.addItem ("pink");
210         color.addItem ("red");
211         color.addItem ("white");
212         color.addItem ("yellow");
213         color.select ("blue");
214         scale = new Choice ();
215         /*Constrain.add (panel, new Label (" and scale "), Constrain.labelLike (2, 0));
216         Constrain.add (panel, scale, Constrain.fieldLike (3, 0));
217         scale.addItem ("small");
218         scale.addItem ("normal");
219         scale.addItem ("large");
220         scale.select ("normal");*/

221         // NIY: icon
222
//Constrain.add (panel, new Label (" and icon "), Constrain.labelLike (4, 0));
223
//Constrain.add (panel, icon = new Choice (), Constrain.fieldLike (5, 0));
224

225         add (ActionFeatureChoice.MIRROR_ACTION, panel = Constrain.makeConstrainedPanel (3, 2));
226         Constrain.add (panel, new Label ("to directory: "), Constrain.labelLike (0, 0));
227         Constrain.add (panel, mirrorDirectory = new TextField(), Constrain.fieldLike (1, 0));
228         Constrain.add (panel, browseMirrorDirectory = new Button ("..."), Constrain.labelLike (2, 0));
229         mirrorUseBrowser = new Checkbox ("Display directory in browser");
230         mirrorUseBrowser.setState (true);
231         if (Context.getBrowser() != null) {
232             mirrorDirectory.setText (TEMPORARY_DIR);
233             Constrain.add (panel, mirrorUseBrowser, Constrain.labelLike (1, 1));
234         }
235             
236         add (ActionFeatureChoice.CONCAT_ACTION, panel = Constrain.makeConstrainedPanel (4, 2));
237         Constrain.add (panel, new Label ("to file: "), Constrain.labelLike (0, 0));
238         Constrain.add (panel, concatFilename = new TextField(), Constrain.fieldLike (1, 0, 2));
239         Constrain.add (panel, browseConcatFilename = new Button ("..."), Constrain.labelLike (3, 0));
240         concatUseBrowser = new Checkbox ("Display in browser");
241         concatUseBrowser.setState (true);
242         if (Context.getBrowser() != null) {
243             concatFilename.setText (TEMPORARY_FILE);
244             Constrain.add (panel, concatUseBrowser, Constrain.labelLike (1, 1));
245         }
246         Constrain.add (panel, optionsButton = new Button ("Options..."),
247                        Constrain.labelLike (2, 1));
248         
249         add (ActionFeatureChoice.EXTRACT_ACTION, panel = Constrain.makeConstrainedPanel (5, 4));
250         Constrain.add (panel, new Label ("regions matching the HTML tag expression:"), Constrain.labelLike (0, 0, 5));
251         Constrain.add (panel, extractPattern = new TextArea(3, 40), Constrain.fieldLike (0, 1, 5));
252         Constrain.add (panel, new Label ("as"), Constrain.labelLike (0, 2));
253         Constrain.add (panel, extractMedium = new Choice (), Constrain.labelLike (1, 2));
254         extractMedium.addItem ("HTML");
255         extractMedium.addItem ("text");
256         Constrain.add (panel, new Label ("to file: "), Constrain.labelLike (2, 2));
257         Constrain.add (panel, extractFilename = new TextField(), Constrain.fieldLike (3, 2));
258         Constrain.add (panel, browseExtractFilename = new Button ("..."), Constrain.labelLike (4, 2));
259         extractUseBrowser = new Checkbox ("Display in browser");
260         extractUseBrowser.setState (true);
261         if (Context.getBrowser() != null) {
262             extractFilename.setText (TEMPORARY_FILE);
263             Constrain.add (panel, extractUseBrowser, Constrain.labelLike (3, 3));
264         }
265             
266         ScriptInterpreter interp = Context.getScriptInterpreter ();
267
268         script = new TextArea (4,40);
269         if (interp != null) {
270             add (ActionFeatureChoice.SCRIPT_ACTION,
271                  panel = Constrain.makeConstrainedPanel (1, 2));
272             Constrain.add (panel, new Label (interp.getLanguage() + " Function (crawler, page)"),
273                            Constrain.labelLike (0, 0));
274             Constrain.add (panel, script, Constrain.areaLike (0, 1));
275         }
276         else {
277             add (ActionFeatureChoice.SCRIPT_ACTION,
278                  panel = Constrain.makeConstrainedPanel (1, 1));
279             Constrain.add (panel, new Label ("No scripting language is available."),
280                            Constrain.labelLike (0, 0));
281         }
282     }
283     
284     public boolean handleEvent (Event event) {
285         if (event.id == Event.ACTION_EVENT) {
286             if (event.target == browseMirrorDirectory)
287                 browse ("Save Pages in Directory", mirrorDirectory);
288             else if (event.target == browseConcatFilename)
289                 browse ("Save Concatenation As", concatFilename);
290             else if (event.target == browseExtractFilename)
291                 browse ("Save Extracts As", extractFilename);
292             else if (event.target == optionsButton)
293                 new ConcatOptions(this).show ();
294             else
295                 return super.handleEvent (event);
296         }
297         else
298             return super.handleEvent (event);
299             
300         return true;
301     }
302         
303     
304     void browse (String JavaDoc title, TextField target) {
305         String JavaDoc fn = PopupDialog.askFilename (this, title, target.getText(), false);
306         if (fn != null)
307             target.setText (fn);
308     }
309                        
310     public void setColor (String JavaDoc color) {
311         this.color.select (color);
312     }
313
314     public String JavaDoc getColor () {
315         return color.getSelectedItem ();
316     }
317
318     public void setScale (String JavaDoc scale) {
319         try {
320             double d = Double.valueOf (scale).doubleValue();
321             // FIX: allow user to enter any scale factor ?
322
if (d < 1)
323                 this.scale.select ("small");
324             else if (d > 1)
325                 this.scale.select ("large");
326             else
327                 this.scale.select ("normal");
328         } catch (NumberFormatException JavaDoc e) {
329             this.scale.select ("normal");
330         }
331     }
332
333     public String JavaDoc getScale () {
334         switch (scale.getSelectedIndex()) {
335             case 0: return "0.5";
336             case 2: return "2.0";
337             default: return "1.0";
338         }
339     }
340
341     public void setIcon (String JavaDoc icon) {
342         //this.icon.select (color);
343
}
344
345     public String JavaDoc getIcon () {
346         return null;
347         //return icon.getSelectedItem ();
348
}
349
350     
351     public void setMirrorDirectory (String JavaDoc directory) {
352         mirrorDirectory.setText (directory != null ? directory : TEMPORARY_DIR);
353     }
354
355     public String JavaDoc getMirrorDirectory () {
356         String JavaDoc f = mirrorDirectory.getText ();
357         return f.equals (TEMPORARY_DIR) ? null : f;
358     }
359
360     public void setMirrorUseBrowser (boolean use) {
361         mirrorUseBrowser.setState (use);
362     }
363
364     public boolean getMirrorUseBrowser () {
365         return mirrorUseBrowser.getState ();
366     }
367
368     public void setConcatFilename (String JavaDoc filename) {
369         concatFilename.setText (filename != null ? filename : TEMPORARY_FILE);
370     }
371
372     public String JavaDoc getConcatFilename () {
373         String JavaDoc f = concatFilename.getText ();
374         return f.equals (TEMPORARY_FILE) ? null : f;
375     }
376
377     public void setConcatUseBrowser (boolean use) {
378         concatUseBrowser.setState (use);
379     }
380
381     public boolean getConcatUseBrowser () {
382         return concatUseBrowser.getState ();
383     }
384
385     public void setExtractFilename (String JavaDoc filename) {
386         extractFilename.setText (filename != null ? filename : TEMPORARY_FILE);
387     }
388
389     public String JavaDoc getExtractFilename () {
390         String JavaDoc f = extractFilename.getText ();
391         return f.equals (TEMPORARY_FILE) ? null : f;
392     }
393
394     public void setExtractUseBrowser (boolean use) {
395         extractUseBrowser.setState (use);
396     }
397
398     public boolean getExtractUseBrowser () {
399         return extractUseBrowser.getState ();
400     }
401
402     public void setExtractPattern (String JavaDoc pattern) {
403         extractPattern.setText (pattern);
404     }
405
406     public String JavaDoc getExtractPattern () {
407         return extractPattern.getText ();
408     }
409     
410     public void setTextOnly (boolean f) {
411         extractMedium.select (f ? "text" : "HTML");
412     }
413     
414     public boolean getTextOnly () {
415         return extractMedium.getSelectedItem().equals ("text");
416     }
417
418     public void setScript (String JavaDoc script) {
419         this.script.setText (script);
420     }
421
422     public String JavaDoc getScript () {
423         return script.getText ();
424     }
425 }
426
427 class ConcatOptions extends PopupDialog {
428     ActionFeatureArgs e;
429
430     TextArea prolog, header, footer, divider, epilog;
431     
432     Button applyButton;
433     Button okButton;
434     Button cancelButton;
435
436     public ConcatOptions (ActionFeatureArgs e) {
437         super (Win.findFrame (e), "Concatenate Options", true);
438         this.e = e;
439
440         setLayout (new GridBagLayout ());
441
442         Constrain.add (this, new Label ("Prolog:"),
443                        Constrain.labelLike (0, 0));
444         Constrain.add (this, prolog = new TextArea (e.prolog, 3, 40),
445                        Constrain.areaLike (1, 0));
446
447         Constrain.add (this, new Label ("Page Header:"),
448                        Constrain.labelLike (0, 1));
449         Constrain.add (this, header = new TextArea (e.header, 3, 40),
450                        Constrain.areaLike (1, 1));
451
452         Constrain.add (this, new Label ("Page Footer:"),
453                        Constrain.labelLike (0, 2));
454         Constrain.add (this, footer = new TextArea (e.footer, 3, 40),
455                        Constrain.areaLike (1, 2));
456
457         Constrain.add (this, new Label ("Page Divider:"),
458                        Constrain.labelLike (0, 3));
459         Constrain.add (this, divider = new TextArea (e.divider, 3, 40),
460                        Constrain.areaLike (1, 3));
461
462         Constrain.add (this, new Label ("Epilog:"),
463                        Constrain.labelLike (0, 4));
464         Constrain.add (this, epilog = new TextArea (e.epilog, 3, 40),
465                        Constrain.areaLike (1, 4));
466
467         Panel panel;
468         Constrain.add (this, panel = new Panel(),
469                        Constrain.centered (Constrain.labelLike (0, 5, 2)));
470         panel.add (applyButton = new Button ("Apply"));
471         panel.add (okButton = new Button ("OK"));
472         panel.add (cancelButton = new Button ("Cancel"));
473
474         pack ();
475     }
476
477     void writeBack () {
478         e.prolog = prolog.getText();
479         e.header = header.getText();
480         e.footer = footer.getText();
481         e.divider = divider.getText();
482         e.epilog = epilog.getText();
483     }
484     
485     
486     public boolean handleEvent (Event event) {
487         if (event.id == Event.ACTION_EVENT) {
488             if (event.target == applyButton)
489                 writeBack ();
490             else if (event.target == okButton) {
491                 writeBack ();
492                 close ();
493             }
494             else if (event.target == cancelButton)
495                 close ();
496             else
497                 return super.handleEvent (event);
498         }
499         else
500             return super.handleEvent (event);
501
502         return true;
503     }
504 }
505
Popular Tags