MOA 12.03
Real Time Analytics for Data Streams
WEKAClassOptionEditComponent.java
Go to the documentation of this file.
00001 /*
00002  *    WEKAClassOptionEditComponent.java
00003  *    Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *    @author FracPete (fracpete at waikato dot ac dot nz)
00006  *
00007  *    This program is free software; you can redistribute it and/or modify
00008  *    it under the terms of the GNU General Public License as published by
00009  *    the Free Software Foundation; either version 3 of the License, or
00010  *    (at your option) any later version.
00011  *
00012  *    This program is distributed in the hope that it will be useful,
00013  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *    GNU General Public License for more details.
00016  *
00017  *    You should have received a copy of the GNU General Public License
00018  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00019  *    
00020  */
00021 package moa.gui;
00022 
00023 import weka.core.Utils;
00024 import weka.gui.GenericObjectEditor;
00025 import weka.gui.PropertyDialog;
00026 import weka.gui.GenericObjectEditor.GOEPanel;
00027 
00028 import java.awt.BorderLayout;
00029 import java.awt.event.ActionEvent;
00030 import java.awt.event.ActionListener;
00031 
00032 import javax.swing.JButton;
00033 import javax.swing.JPanel;
00034 import javax.swing.JTextField;
00035 
00036 import moa.options.Option;
00037 import moa.options.WEKAClassOption;
00038 
00046 public class WEKAClassOptionEditComponent
00047         extends JPanel
00048         implements OptionEditComponent {
00049 
00050     private static final long serialVersionUID = 1L;
00051 
00052     protected WEKAClassOption editedOption;
00053 
00054     protected JTextField textField = new JTextField();
00055 
00056     protected JButton editButton = new JButton("Edit");
00057 
00058     public WEKAClassOptionEditComponent(WEKAClassOption option) {
00059         this.editedOption = option;
00060         this.textField.setEditable(false);
00061         setLayout(new BorderLayout());
00062         add(this.textField, BorderLayout.CENTER);
00063         add(this.editButton, BorderLayout.EAST);
00064         this.editButton.addActionListener(new ActionListener() {
00065 
00066             @Override
00067             public void actionPerformed(ActionEvent arg0) {
00068                 editObject();
00069             }
00070         });
00071         setEditState(this.editedOption.getValueAsCLIString());
00072     }
00073 
00074     @Override
00075     public void applyState() {
00076         this.editedOption.setValueViaCLIString(this.textField.getText());
00077     }
00078 
00079     @Override
00080     public Option getEditedOption() {
00081         return this.editedOption;
00082     }
00083 
00084     @Override
00085     public void setEditState(String cliString) {
00086         this.textField.setText(cliString);
00087     }
00088 
00089     public void editObject() {
00090         final GenericObjectEditor goe = new GenericObjectEditor(true);
00091         goe.setClassType(editedOption.getRequiredType());
00092         try {
00093             String[] options = Utils.splitOptions(editedOption.getValueAsCLIString());
00094             String classname = options[0];
00095             options[0] = "";
00096             Object obj = Class.forName(classname).newInstance();
00097             if (obj instanceof weka.core.OptionHandler) {
00098                 ((weka.core.OptionHandler) obj).setOptions(options);
00099             }
00100             goe.setValue(obj);
00101             ((GOEPanel) goe.getCustomEditor()).addOkListener(new ActionListener() {
00102 
00103                 @Override
00104                 public void actionPerformed(ActionEvent e) {
00105                     Object obj = goe.getValue();
00106                     String s = obj.getClass().getName();
00107                     if (obj instanceof weka.core.OptionHandler) {
00108                         s += " " + Utils.joinOptions(((weka.core.OptionHandler) obj).getOptions());
00109                     }
00110                     setEditState(s.trim());
00111                 }
00112             });
00113             PropertyDialog dialog;
00114             if (PropertyDialog.getParentDialog(this) != null) {
00115                 dialog = new PropertyDialog(PropertyDialog.getParentDialog(this), goe);
00116             } else {
00117                 dialog = new PropertyDialog(PropertyDialog.getParentFrame(this), goe);
00118             }
00119             dialog.setModal(true);
00120             dialog.setVisible(true);
00121         } catch (Exception e) {
00122             e.printStackTrace();
00123         }
00124     }
00125 }
 All Classes Namespaces Files Functions Variables Enumerations