KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > ClassifierListEditor


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 websphinx.*;
36 import java.awt.*;
37 import rcm.awt.Constrain;
38 import rcm.awt.PopupDialog;
39
40 public class ClassifierListEditor extends Panel {
41
42     List classifierList;
43     Button newClassifierButton;
44     Button loadClassifierButton;
45     Button removeClassifierButton;
46
47     Crawler crawler;
48     Classifier[] classifiers;
49
50     public ClassifierListEditor () {
51         setLayout (new GridBagLayout ());
52
53         Constrain.add (this, new Label ("Classifiers:"), Constrain.labelLike (0, 0));
54         Constrain.add (this, classifierList = new List (5, false), Constrain.areaLike (0, 1));
55
56         Panel panel = new Panel ();
57         Constrain.add (this, panel, Constrain.fieldLike (0, 2));
58
59         panel.add (newClassifierButton = new Button ("New..."));
60         panel.add (loadClassifierButton = new Button ("Load..."));
61         loadClassifierButton.disable ();
62         panel.add (removeClassifierButton = new Button ("Remove"));
63         removeClassifierButton.disable ();
64     }
65
66     public boolean handleEvent (Event event) {
67         if (event.target == classifierList) {
68             if (classifierList.getSelectedIndex () != -1)
69                 removeClassifierButton.enable ();
70             else
71                 removeClassifierButton.disable ();
72         }
73         else if (event.id == Event.ACTION_EVENT) {
74             if (event.target == newClassifierButton)
75                 newClassifier (null);
76             else if (event.target == loadClassifierButton)
77                     ; // NIY
78
else if (event.target == removeClassifierButton)
79                 removeSelectedClassifier ();
80             else
81                 return super.handleEvent (event);
82         }
83         else
84             return super.handleEvent (event);
85
86         return true;
87     }
88
89     public void setCrawler (Crawler crawler) {
90         this.crawler = crawler;
91         scan ();
92     }
93
94     public Crawler getCrawler () {
95         return crawler;
96     }
97
98     private void newClassifier (String JavaDoc className) {
99         if (className == null || className.length() == 0) {
100             className = PopupDialog.ask (this,
101                                          "New Classifier",
102                                          "Create an instance of class:");
103             if (className == null)
104                 return;
105         }
106         
107         try {
108             Class JavaDoc classifierClass = (Class JavaDoc)Class.forName (className);
109             Classifier cl = (Classifier)classifierClass.newInstance ();
110             crawler.addClassifier (cl);
111         } catch (Exception JavaDoc e) {
112             PopupDialog.warn (this,
113                               "Error",
114                               e.toString());
115         }
116         
117         scan ();
118     }
119
120     private void removeSelectedClassifier () {
121         int i = classifierList.getSelectedIndex ();
122         if (i < 0 || i >= classifiers.length) {
123             removeClassifierButton.disable ();
124             return;
125         }
126
127         crawler.removeClassifier (classifiers[i]);
128         scan ();
129     }
130
131     private void scan () {
132         classifiers = crawler.getClassifiers ();
133         classifierList.clear ();
134         for (int i=0; i<classifiers.length; ++i)
135             classifierList.addItem (classifiers[i].getClass().getName());
136     }
137 }
138
Popular Tags