⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericobjecteditor.java

📁 :<<数据挖掘--实用机器学习技术及java实现>>一书的配套源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /** Called to update the list of values to be selected from */    protected void updateChooser() {      //System.err.println("GOE::updateChooser()");      String objectName = m_Object.getClass().getName();      boolean found = false;      for (int i = 0; i < m_ObjectNames.getSize(); i++) {	if (objectName.equals((String)m_ObjectNames.getElementAt(i))) {	  found = true;	  break;	}      }      if (!found) {	m_ObjectNames.addElement(objectName);      }      m_ObjectChooser.setSelectedItem(objectName);    }    /** Updates the child property sheet, and creates if needed */    public void updateChildPropertySheet() {      //System.err.println("GOE::updateChildPropertySheet()");      // Set the object as the target of the propertysheet      m_ChildPropertySheet.setTarget(m_Object);      // Adjust size of containing window if possible      if ((getTopLevelAncestor() != null)	  && (getTopLevelAncestor() instanceof Window)) {	((Window) getTopLevelAncestor()).pack();      }    }    /**     * When the chooser selection is changed, ensures that the Object     * is changed appropriately.     *     * @param e a value of type 'ItemEvent'     */    public void itemStateChanged(ItemEvent e) {      //System.err.println("GOE::itemStateChanged()");      if ((e.getSource() == m_ObjectChooser)	  && (e.getStateChange() == ItemEvent.SELECTED)){	String className = (String)m_ObjectChooser			     .getSelectedItem();	try {	  //System.err.println("Setting object from chooser");	  setObject((Object)Class		    .forName(className)		    .newInstance());	  //System.err.println("done setting object from chooser");	} catch (Exception ex) {	  m_ObjectChooser.hidePopup();	  m_ObjectChooser.setSelectedIndex(0);	  JOptionPane.showMessageDialog(this,					"Could not create an example of\n"					+ className + "\n"					+ "from the current classpath",					"GenericObjectEditor",					JOptionPane.ERROR_MESSAGE);	}      }    }  }  /** Called when the class of object being edited changes. */  protected Vector getClassesFromProperties() {        Vector classes = new Vector();    String className = m_ClassType.getName();    String typeOptions = EDITOR_PROPERTIES.getProperty(className);    if (typeOptions == null) {      System.err.println("Warning: No configuration property found in\n"			 + PROPERTY_FILE + "\n"			 + "for " + className);    } else {      StringTokenizer st = new StringTokenizer(typeOptions, ", ");      while (st.hasMoreTokens()) {	String current = st.nextToken().trim();	if (false) {	  // Verify that class names are OK. Slow -- with Java2 we could	  // try Class.forName(current, false, getClass().getClassLoader());	  try {	    Class c = Class.forName(current);	    classes.addElement(current);	  } catch (Exception ex) {	    System.err.println("Couldn't find class with name" + current);	  }	} else {	  classes.addElement(current);	}      }    }    return classes;  }  /**   * Sets whether the editor is "enabled", meaning that the current   * values will be painted.   *   * @param newVal a value of type 'boolean'   */  public void setEnabled(boolean newVal) {    if (newVal != m_Enabled) {      m_Enabled = newVal;      /*      if (m_EditorComponent != null) {	m_EditorComponent.setEnabled(m_Enabled);      }      */    }  }    /**   * Sets the class of values that can be edited.   *   * @param type a value of type 'Class'   */  public void setClassType(Class type) {    //System.err.println("setClassType("    //		       + (type == null? "<null>" : type.getName()) + ")");    m_ClassType = type;    if (m_EditorComponent != null) {      m_EditorComponent.updateClassType();    }  }  /**   * Sets the current object to be the default, taken as the first item in   * the chooser   */  public void setDefaultValue() {    if (m_ClassType == null) {      System.err.println("No ClassType set up for GenericObjectEditor!!");      return;    }    Vector v = getClassesFromProperties();    try {      if (v.size() > 0) {	setObject((Object)Class.forName((String)v.elementAt(0)).newInstance());      }    } catch (Exception ex) {    }  }    /**   * Sets the current Object. If the Object is in the   * Object chooser, this becomes the selected item (and added   * to the chooser if necessary).   *   * @param o an object that must be a Object.   */  public void setValue(Object o) {    //System.err.println("setValue()");    if (m_ClassType == null) {      System.err.println("No ClassType set up for GenericObjectEditor!!");      return;    }    if (!m_ClassType.isAssignableFrom(o.getClass())) {      System.err.println("setValue object not of correct type!");      return;    }        setObject((Object)o);          if (m_EditorComponent != null) {      m_EditorComponent.updateChooser();    }  }  /**   * Sets the current Object, but doesn't worry about updating   * the state of the Object chooser.   *   * @param c a value of type 'Object'   */  private void setObject(Object c) {    // This should really call equals() for comparison.    boolean trueChange = (c != getValue());    /*    System.err.println("Didn't even try to make a Object copy!! "		       + "(using original)");    */    m_Backup = m_Object;    m_Object = c;    if (m_EditorComponent != null) {      m_EditorComponent.updateChildPropertySheet();      if (trueChange) {	m_Support.firePropertyChange("", null, null);      }    }  }    /**   * Gets the current Object.   *   * @return the current Object   */  public Object getValue() {    //System.err.println("getValue()");    return m_Object;  }    /**   * Supposedly returns an initialization string to create a Object   * identical to the current one, including it's state, but this doesn't   * appear possible given that the initialization string isn't supposed to   * contain multiple statements.   *   * @return the java source code initialisation string   */  public String getJavaInitializationString() {    return "new " + m_Object.getClass().getName() + "()";  }  /**   * Returns true to indicate that we can paint a representation of the   * Object.   *   * @return true   */  public boolean isPaintable() {    return true;  }  /**   * Paints a representation of the current Object.   *   * @param gfx the graphics context to use   * @param box the area we are allowed to paint into   */  public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {    if (m_Enabled && m_Object != null) {      String rep = m_Object.getClass().getName();      int dotPos = rep.lastIndexOf('.');      if (dotPos != -1) {	rep = rep.substring(dotPos + 1);      }      if (m_Object instanceof OptionHandler) {	rep += " " + Utils.joinOptions(((OptionHandler)m_Object)				       .getOptions());      }      FontMetrics fm = gfx.getFontMetrics();      int vpad = (box.height - fm.getHeight()) / 2;      gfx.drawString(rep, 2, fm.getHeight() + vpad);    } else {    }  }  /**   * Returns null as we don't support getting/setting values as text.   *   * @return null   */  public String getAsText() {    return null;  }  /**   * Returns null as we don't support getting/setting values as text.    *   * @param text the text value   * @exception IllegalArgumentException as we don't support   * getting/setting values as text.   */  public void setAsText(String text) throws IllegalArgumentException {    throw new IllegalArgumentException(text);  }  /**   * Returns null as we don't support getting values as tags.   *   * @return null   */  public String[] getTags() {    return null;  }  /**   * Returns true because we do support a custom editor.   *   * @return true   */  public boolean supportsCustomEditor() {    return true;  }    /**   * Returns the array editing component.   *   * @return a value of type 'java.awt.Component'   */  public java.awt.Component getCustomEditor() {    //System.err.println("getCustomEditor()");    if (m_EditorComponent == null) {      //System.err.println("creating new editing component");      m_EditorComponent = new GOEPanel();    }    return m_EditorComponent;  }  /**   * Adds a PropertyChangeListener who will be notified of value changes.   *   * @param l a value of type 'PropertyChangeListener'   */  public void addPropertyChangeListener(PropertyChangeListener l) {    m_Support.addPropertyChangeListener(l);  }  /**   * Removes a PropertyChangeListener.   *   * @param l a value of type 'PropertyChangeListener'   */  public void removePropertyChangeListener(PropertyChangeListener l) {    m_Support.removePropertyChangeListener(l);  }  /**   * Tests out the Object editor from the command line.   *   * @param args may contain the class name of a Object to edit   */  public static void main(String [] args) {    try {      System.err.println("---Registering Weka Editors---");      java.beans.PropertyEditorManager	.registerEditor(weka.experiment.ResultProducer.class,			GenericObjectEditor.class);      java.beans.PropertyEditorManager	.registerEditor(weka.experiment.SplitEvaluator.class,			GenericObjectEditor.class);      java.beans.PropertyEditorManager	.registerEditor(weka.classifiers.Classifier.class,			GenericObjectEditor.class);      java.beans.PropertyEditorManager	.registerEditor(weka.attributeSelection.ASEvaluation.class,			GenericObjectEditor.class);      java.beans.PropertyEditorManager	.registerEditor(weka.attributeSelection.ASSearch.class,			GenericObjectEditor.class);      java.beans.PropertyEditorManager	.registerEditor(SelectedTag.class,			SelectedTagEditor.class);      java.beans.PropertyEditorManager	.registerEditor(java.io.File.class,			FileEditor.class);      GenericObjectEditor ce = new GenericObjectEditor();      ce.setClassType(weka.filters.Filter.class);      Object initial = new weka.filters.AddFilter();      if (args.length > 0) {	initial = (Object)Class.forName(args[0]).newInstance();      }      ce.setValue(initial);      PropertyDialog pd = new PropertyDialog(ce, 100, 100);      pd.addWindowListener(new WindowAdapter() {	public void windowClosing(WindowEvent e) {	  PropertyEditor pe = ((PropertyDialog)e.getSource()).getEditor();	  Object c = (Object)pe.getValue();	  String options = "";	  if (c instanceof OptionHandler) {	    options = Utils.joinOptions(((OptionHandler)c).getOptions());	  }	  System.out.println(c.getClass().getName() + " " + options);	  System.exit(0);	}      });    } catch (Exception ex) {      ex.printStackTrace();      System.err.println(ex.getMessage());    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -