1 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 ; 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 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 classifierClass = (Class )Class.forName (className); 109 Classifier cl = (Classifier)classifierClass.newInstance (); 110 crawler.addClassifier (cl); 111 } catch (Exception 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 |