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

📄 metabean.java

📁 MacroWeka扩展了著名数据挖掘工具weka
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *    MetaBean.java
 *    Copyright (C) 2005 Mark Hall
 *
 */

package weka.gui.beans;

import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.Dimension;
import java.io.Serializable;
import java.beans.EventSetDescriptor;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.beans.PropertyChangeListener;

import weka.gui.Logger;

/**
 * A meta bean that encapsulates several other regular beans, useful for 
 * grouping large KnowledgeFlows.
 *
 *
 * @author Mark Hall (mhall at cs dot waikato dot ac dot nz)
 * @version $Revision: 1.1 $
 */
public class MetaBean extends JPanel 
implements BeanCommon, Visible, EventConstraints,
           Serializable, UserRequestAcceptor {

  protected BeanVisual m_visual = 
    new BeanVisual("Group",
		   BeanVisual.ICON_PATH+"DiamondPlain.gif",
		   BeanVisual.ICON_PATH+"DiamondPlain.gif");

  private transient Logger m_log = null;

  protected Vector m_subFlow = new Vector();
  protected Vector m_inputs = new Vector();
  protected Vector m_outputs = new Vector();

  // the internal connections for the grouping
  protected Vector m_associatedConnections = new Vector();

  public MetaBean() {
    setLayout(new BorderLayout());
    add(m_visual, BorderLayout.CENTER);
  }

  public void setAssociatedConnections(Vector ac) {
    m_associatedConnections = ac;
  }

  public Vector getAssociatedConnections() {
    return m_associatedConnections;
  }

  public void setSubFlow(Vector sub) {
    m_subFlow = sub;
  }

  public Vector getSubFlow() {
    return m_subFlow;
  }

  public void setInputs(Vector inputs) {
    m_inputs = inputs;
  }

  public Vector getInputs() {
    return m_inputs;
  }

  public void setOutputs(Vector outputs) {
    m_outputs = outputs;
  }

  public Vector getOutputs() {
    return m_outputs;
  }

  private Vector getBeans(Vector beans, int type) {
    Vector comps = new Vector();
    for (int i = 0; i < beans.size(); i++) {
      BeanInstance temp = (BeanInstance)beans.elementAt(i);
      // need to check for sub MetaBean!
      if (temp.getBean() instanceof MetaBean) {
        switch (type) {
        case 0 : 
          comps.addAll(((MetaBean)temp.getBean()).getBeansInSubFlow());
          break;
        case 1 : 
          comps.addAll(((MetaBean)temp.getBean()).getBeansInInputs());
          break;
        case 2:
          comps.addAll(((MetaBean)temp.getBean()).getBeansInOutputs());
          break;
        }
      } else {
        comps.add(temp);
      }
    }
    return comps;
  }

  /**
   * Return all the beans in the sub flow
   *
   * @return a Vector of all the beans in the sub flow
   */
  public Vector getBeansInSubFlow() {
    return getBeans(m_subFlow, 0);
  }

  /**
   * Return all the beans in the inputs
   *
   * @return a Vector of all the beans in the inputs
   */
  public Vector getBeansInInputs() {
    return getBeans(m_inputs, 1);
  }

  /**
   * Return all the beans in the outputs
   *
   * @return a Vector of all the beans in the outputs
   */
  public Vector getBeansInOutputs() {
    return getBeans(m_outputs, 2);
  }

  private Vector getBeanInfos(Vector beans, int type) {
    Vector infos = new Vector();
    for (int i = 0; i < beans.size(); i++) {
      BeanInstance temp = (BeanInstance)beans.elementAt(i);
      if (temp.getBean() instanceof MetaBean) {
        switch (type) {
        case 0: 
          infos.addAll(((MetaBean)temp.getBean()).getBeanInfoSubFlow());
          break;
        case 1: 
          infos.addAll(((MetaBean)temp.getBean()).getBeanInfoInputs());
          break;
        case 2:
          infos.addAll(((MetaBean)temp.getBean()).getBeanInfoOutputs());
        }
      } else {
        try {
          infos.add(Introspector.getBeanInfo(temp.getBean().getClass()));
        } catch (IntrospectionException ex) {
          ex.printStackTrace();
        }
      }
    }
    return infos;
  }

  public Vector getBeanInfoSubFlow() {
    return getBeanInfos(m_subFlow, 0);
  }

  public Vector getBeanInfoInputs() {
    return getBeanInfos(m_inputs, 1);
  }

  public Vector getBeanInfoOutputs() {
    return getBeanInfos(m_outputs, 2);
  }

  // stores the original position of the beans 
  // when this group is created. Used
  // to restore their locations if the group is ungrouped.
  private Vector m_originalCoords;

  /**
   * returns the vector containing the original coordinates (instances of class
   * Point) for the inputs
   * @return the containing the coord Points of the original inputs
   */
  public Vector getOriginalCoords() {
    return m_originalCoords;
  }
  
  /**
   * sets the vector containing the original coordinates (instances of class
   * Point) for the inputs
   * @param value the vector containing the points of the coords of the original inputs
   */
  public void setOriginalCoords(Vector value) {
    m_originalCoords = value;
  }

  /**
   * Move coords of all inputs and outputs of this meta bean
   * to the coords of the supplied BeanInstance. Typically
   * the supplied BeanInstance is the BeanInstance that encapsulates
   * this meta bean; the result in this case is that all inputs
   * and outputs are shifted so that their coords coincide with
   * the meta bean and all connections to them appear (visually) to
   * go to/from the meta bean.
   *
   * @param toShiftTo the BeanInstance whos coordinates will
   * be used.
   * @param save true if coordinates are to be saved.
   */
  public void shiftBeans(BeanInstance toShiftTo, 
                         boolean save) {
    if (save) {
      m_originalCoords = new Vector();
    }
    int targetX = toShiftTo.getX();
    int targetY = toShiftTo.getY();

    for (int i = 0; i < m_subFlow.size(); i++) {
      BeanInstance temp = (BeanInstance)m_subFlow.elementAt(i);
      if (save) {
        Point p = new Point(temp.getX(), temp.getY());
        m_originalCoords.add(p);
      }
      temp.setX(targetX); temp.setY(targetY);
    }
  }

  public void restoreBeans() {
    for (int i = 0; i < m_subFlow.size(); i++) {
      BeanInstance temp = (BeanInstance)m_subFlow.elementAt(i);
      Point p = (Point)m_originalCoords.elementAt(i);
      JComponent c = (JComponent)temp.getBean();
      Dimension d = c.getPreferredSize();
      int dx = (int)(d.getWidth() / 2);

⌨️ 快捷键说明

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