MOA 12.03
Real Time Analytics for Data Streams
ClassOptionSelectionPanel.java
Go to the documentation of this file.
00001 /*
00002  *    ClassOptionSelectionPanel.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.gui;
00021 
00022 import java.awt.BorderLayout;
00023 import java.awt.Component;
00024 import java.awt.Window;
00025 import java.awt.event.ActionEvent;
00026 import java.awt.event.ActionListener;
00027 
00028 import javax.swing.JComboBox;
00029 import javax.swing.JComponent;
00030 import javax.swing.JDialog;
00031 import javax.swing.JOptionPane;
00032 import javax.swing.JPanel;
00033 
00034 import moa.core.AutoClassDiscovery;
00035 import moa.core.AutoExpandVector;
00036 import moa.options.ClassOption;
00037 import moa.options.OptionHandler;
00038 import moa.tasks.Task;
00039 
00048 public class ClassOptionSelectionPanel extends JPanel {
00049 
00050     // TODO: idea - why not retain matching options between classes when the
00051     // class type is changed
00052     // e.g. if user switches from LearnModel to EvaluateLearner, retain whatever
00053     // the 'stream' option was set to
00054     private static final long serialVersionUID = 1L;
00055 
00056     protected JComboBox classChoiceBox;
00057 
00058     protected JComponent chosenObjectEditor;
00059 
00060     protected Object chosenObject;
00061 
00062     public ClassOptionSelectionPanel(Class<?> requiredType,
00063             String initialCLIString, String nullString) {
00064         // Class<?>[] classesFound = AutoClassDiscovery.findClassesOfType("moa",
00065         // requiredType);
00066         Class<?>[] classesFound = findSuitableClasses(requiredType);
00067         this.classChoiceBox = new JComboBox(classesFound);
00068         setLayout(new BorderLayout());
00069         add(this.classChoiceBox, BorderLayout.NORTH);
00070         Object initialObject = null;
00071         try {
00072             initialObject = ClassOption.cliStringToObject(initialCLIString,
00073                     requiredType, null);
00074         } catch (Exception ignored) {
00075             // ignore exception
00076         }
00077         if (initialObject != null) {
00078             this.classChoiceBox.setSelectedItem(initialObject.getClass());
00079             classChoiceChanged(initialObject);
00080         } else {
00081             try {
00082                 Object chosen = ((Class<?>) ClassOptionSelectionPanel.this.classChoiceBox.getSelectedItem()).newInstance();
00083                 classChoiceChanged(chosen);
00084             } catch (Exception ex) {
00085                 GUIUtils.showExceptionDialog(ClassOptionSelectionPanel.this,
00086                         "Problem", ex);
00087             }
00088         }
00089         this.classChoiceBox.addActionListener(new ActionListener() {
00090 
00091             @Override
00092             public void actionPerformed(ActionEvent arg0) {
00093                 try {
00094                     Object chosen = ((Class<?>) ClassOptionSelectionPanel.this.classChoiceBox.getSelectedItem()).newInstance();
00095                     classChoiceChanged(chosen);
00096                 } catch (Exception ex) {
00097                     GUIUtils.showExceptionDialog(
00098                             ClassOptionSelectionPanel.this, "Problem", ex);
00099                 }
00100             }
00101         });
00102     }
00103 
00104     public Class<?>[] findSuitableClasses(Class<?> requiredType) {
00105         AutoExpandVector<Class<?>> finalClasses = new AutoExpandVector<Class<?>>();
00106         Class<?>[] classesFound = AutoClassDiscovery.findClassesOfType("moa",
00107                 requiredType);
00108         for (Class<?> foundClass : classesFound) {
00109             finalClasses.add(foundClass);
00110         }
00111         Class<?>[] tasksFound = AutoClassDiscovery.findClassesOfType("moa",
00112                 Task.class);
00113         for (Class<?> foundTask : tasksFound) {
00114             try {
00115                 Task task = (Task) foundTask.newInstance();
00116                 if (requiredType.isAssignableFrom(task.getTaskResultType())) {
00117                     finalClasses.add(foundTask);
00118                 }
00119             } catch (Exception e) {
00120                 // ignore
00121             }
00122         }
00123         return finalClasses.toArray(new Class<?>[finalClasses.size()]);
00124     }
00125 
00126     public static String showSelectClassDialog(Component parent, String title,
00127             Class<?> requiredType, String initialCLIString, String nullString) {
00128         ClassOptionSelectionPanel panel = new ClassOptionSelectionPanel(
00129                 requiredType, initialCLIString, nullString);
00130         if (JOptionPane.showOptionDialog(parent, panel, title,
00131                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
00132                 null, null) == JOptionPane.OK_OPTION) {
00133             return panel.getChosenObjectCLIString(requiredType);
00134         }
00135         return initialCLIString;
00136     }
00137 
00138     public String getChosenObjectCLIString(Class<?> requiredType) {
00139         if (this.chosenObjectEditor instanceof OptionsConfigurationPanel) {
00140             ((OptionsConfigurationPanel) this.chosenObjectEditor).applyChanges();
00141         }
00142         return ClassOption.objectToCLIString(this.chosenObject, requiredType);
00143     }
00144 
00145     public void classChoiceChanged(Object chosen) {
00146         this.chosenObject = chosen;
00147         JComponent newChosenObjectEditor = null;
00148         if (this.chosenObject instanceof OptionHandler) {
00149             OptionHandler chosenOptionHandler = (OptionHandler) this.chosenObject;
00150             newChosenObjectEditor = new OptionsConfigurationPanel(
00151                     chosenOptionHandler.getPurposeString(), chosenOptionHandler.getOptions());
00152         }
00153         if (this.chosenObjectEditor != null) {
00154             remove(this.chosenObjectEditor);
00155         }
00156         this.chosenObjectEditor = newChosenObjectEditor;
00157         if (this.chosenObjectEditor != null) {
00158             add(this.chosenObjectEditor, BorderLayout.CENTER);
00159         }
00160         Component component = this;
00161         while ((component != null) && !(component instanceof JDialog)) {
00162             component = component.getParent();
00163         }
00164         if (component != null) {
00165             Window window = (Window) component;
00166             window.pack();
00167         }
00168     }
00169 }
 All Classes Namespaces Files Functions Variables Enumerations