knowledgeflow.java

来自「wekaUT是 university texas austin 开发的基于wek」· Java 代码 · 共 1,209 行 · 第 1/3 页

JAVA
1,209
字号
/* *    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. *//* *    KnowledgeFlow.java *    Copyright (C) 2002 Mark Hall * */package weka.gui.beans;import weka.core.Utils;import weka.gui.ListSelectorDialog;import weka.gui.LogPanel;import java.io.OutputStream;import java.io.InputStream;import java.io.FileOutputStream;import java.io.FileInputStream;import java.io.ObjectOutputStream;import java.io.ObjectInputStream;import java.io.File;import java.lang.reflect.Method;import java.util.Enumeration;import java.util.StringTokenizer;import java.util.Vector;import java.util.Properties;import java.util.Enumeration;import java.util.Date;import java.text.SimpleDateFormat;import java.beans.Customizer;import java.beans.EventSetDescriptor;import java.beans.Beans;import java.beans.PropertyDescriptor;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeEvent;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JToggleButton;import javax.swing.JButton;import javax.swing.DefaultListModel;import javax.swing.JList;import javax.swing.JLabel;import javax.swing.JComponent;import javax.swing.JPopupMenu;import javax.swing.JMenuItem;import javax.swing.JTabbedPane;import javax.swing.JToolBar;import javax.swing.JScrollPane;import javax.swing.ButtonGroup;import javax.swing.ButtonModel;import javax.swing.ImageIcon;import javax.swing.SwingConstants;import javax.swing.JFileChooser;import java.awt.BorderLayout;import java.awt.Cursor;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Dimension;import java.awt.event.MouseEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseMotionAdapter;import java.awt.Point;import java.awt.Font;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.Image;import java.awt.Toolkit;import java.awt.Insets;import java.awt.Component;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.IntrospectionException;/** * Main GUI class for the KnowledgeFlow * * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a> * @version  $Revision: 1.1.1.1 $ * @since 1.0 * @see JPanel * @see PropertyChangeListener */public class KnowledgeFlow extends JPanel implements PropertyChangeListener {  /**   * Location of the property file for the KnowledgeFlow   */  protected static String PROPERTY_FILE = "weka/gui/beans/Beans.props";  /** Contains the editor properties */  private static Properties BEAN_PROPERTIES;  /**   * Holds the details needed to construct button bars for various supported   * classes of weka algorithms/tools    */  private static Vector TOOLBARS = new Vector();  /** Loads the configuration property file */  static {    // Allow a properties file in the current directory to override    try {      BEAN_PROPERTIES = Utils.readProperties(PROPERTY_FILE);      java.util.Enumeration keys =        (java.util.Enumeration)BEAN_PROPERTIES.propertyNames();      if (!keys.hasMoreElements()) {        throw new Exception( "Could not read a configuration file for the bean\n"         +"panel. An example file is included with the Weka distribution.\n"         +"This file should be named \"" + PROPERTY_FILE + "\" and\n"         +"should be placed either in your user home (which is set\n"         + "to \"" + System.getProperties().getProperty("user.home") + "\")\n"         + "or the directory that java was started from\n");      }           String standardToolBarNames = 	BEAN_PROPERTIES.	getProperty("weka.gui.beans.KnowledgeFlow.standardToolBars");      StringTokenizer st = new StringTokenizer(standardToolBarNames, ", ");       while (st.hasMoreTokens()) {	 String tempBarName = st.nextToken().trim();	 // construct details for this toolbar	 Vector newV = new Vector();	 // add the name of the toolbar	 newV.addElement(tempBarName);	 // indicate that this is a standard toolbar (no wrapper bean)	 newV.addElement("null");	 String toolBarContents = 	   BEAN_PROPERTIES.	   getProperty("weka.gui.beans.KnowledgeFlow."+tempBarName);	 StringTokenizer st2 = new StringTokenizer(toolBarContents, ", ");	 while (st2.hasMoreTokens()) {	   String tempBeanName = st2.nextToken().trim();	   newV.addElement(tempBeanName);	 }	 TOOLBARS.addElement(newV);       }           } catch (Exception ex) {      JOptionPane.showMessageDialog(null,				    ex.getMessage(),				    "KnowledgeFlow",				    JOptionPane.ERROR_MESSAGE);    }    try {      /* now process the keys in the GenericObjectEditor.props. For each       key that has an entry in the Beans.props associating it with a      bean component a button tool bar will be created */      Properties GEOProps = 	Utils.readProperties("weka/gui/GenericObjectEditor.props");      Enumeration en = GEOProps.propertyNames();      while (en.hasMoreElements()) {	String geoKey = (String)en.nextElement();	// try to match this key with one in the Beans.props file	String beanCompName = BEAN_PROPERTIES.getProperty(geoKey);	if (beanCompName != null) {	  // add details necessary to construct a button bar for this class	  // of algorithms	  Vector newV = new Vector();	  // check for a naming alias for this toolbar	  String toolBarNameAlias = 	    BEAN_PROPERTIES.getProperty(geoKey+".alias");	  String toolBarName = (toolBarNameAlias != null) ?	    toolBarNameAlias :	    geoKey.substring(geoKey.lastIndexOf('.')+1, geoKey.length());	  // Name for the toolbar (name of weka algorithm class)	  newV.addElement(toolBarName);	  // Name of bean capable of handling this class of algorithm	  newV.addElement(beanCompName);	  // add the root package for this key	  String rootPackage = geoKey.substring(0, geoKey.lastIndexOf('.'));	  newV.addElement(rootPackage);	  // All the weka algorithms of this class of algorithm	  String wekaAlgs = GEOProps.getProperty(geoKey);	  //------ test the HierarchyPropertyParser	  weka.gui.HierarchyPropertyParser hpp = 	    new weka.gui.HierarchyPropertyParser();	  hpp.build(wekaAlgs, ", ");	  //	  System.err.println(hpp.showTree());	  // ----- end test the HierarchyPropertyParser	  newV.addElement(hpp); // add the hierarchical property parser	  StringTokenizer st = new StringTokenizer(wekaAlgs, ", ");	  while (st.hasMoreTokens()) {	    String current = st.nextToken().trim();	    newV.addElement(current);	  }	  TOOLBARS.addElement(newV);	}      }          } catch (Exception ex) {      JOptionPane.showMessageDialog(null,          "Could not read a configuration file for the generic objecte editor"         +". An example file is included with the Weka distribution.\n"         +"This file should be named \"GenericObjectEditor.props\" and\n"         +"should be placed either in your user home (which is set\n"         + "to \"" + System.getProperties().getProperty("user.home") + "\")\n"         + "or the directory that java was started from\n",         "KnowledgeFlow",         JOptionPane.ERROR_MESSAGE);    }  }     /**   * Used for displaying the bean components and their visible   * connections   *   * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>   * @version $Revision: 1.1.1.1 $   * @since 1.0   * @see JPanel   */  protected class BeanLayout extends JPanel {    public void paintComponent(Graphics gx) {      super.paintComponent(gx);      BeanInstance.paintLabels(gx);      BeanConnection.paintConnections(gx);      //      BeanInstance.paintConnections(gx);    }    public void doLayout() {      super.doLayout();      Vector comps = BeanInstance.getBeanInstances();      for (int i = 0; i < comps.size(); i++) {	BeanInstance bi = (BeanInstance)comps.elementAt(i);	JComponent c = (JComponent)bi.getBean();	Dimension d = c.getPreferredSize();	c.setBounds(bi.getX(), bi.getY(), d.width, d.height);	c.revalidate();      }    }  }  // constants for operations in progress  protected static final int NONE = 0;  protected static final int MOVING = 1;  protected static final int CONNECTING = 2;  protected static final int ADDING = 3;  // which operation is in progress  private int m_mode = NONE;  /**   * Button group to manage all toolbar buttons   */  private ButtonGroup m_toolBarGroup = new ButtonGroup();  /**   * Holds the selected toolbar bean   */  private Object m_toolBarBean;  /**   * The layout area   */  private BeanLayout m_beanLayout = new BeanLayout();  /**   * Tabbed pane to hold tool bars   */  private JTabbedPane m_toolBars = new JTabbedPane();  private JToggleButton m_pointerB;  private JButton m_saveB;  private JButton m_loadB;  private JButton m_stopB;  /**   * Reference to bean being manipulated   */  private BeanInstance m_editElement;  /**   * Event set descriptor for the bean being manipulated   */  private EventSetDescriptor m_sourceEventSetDescriptor;  /**   * Used to record screen coordinates during move and connect   * operations   */  private int m_oldX, m_oldY;  private int m_startX, m_startY;    /** The file chooser for selecting layout files */  protected JFileChooser m_FileChooser     = new JFileChooser(new File(System.getProperty("user.dir")));  protected LogPanel m_logPanel = new LogPanel();  /**   * Creates a new <code>KnowledgeFlow</code> instance.   */  public KnowledgeFlow() {    m_beanLayout.setLayout(null);    // handle mouse events    m_beanLayout.addMouseListener(new MouseAdapter() {	public void mousePressed(MouseEvent me) {	  if (m_toolBarBean == null) {	    if (((me.getModifiers() & InputEvent.BUTTON1_MASK)		 == InputEvent.BUTTON1_MASK) && m_mode == NONE) {	      BeanInstance bi = BeanInstance.findInstance(me.getPoint());	      JComponent bc = null;	      if (bi != null) {		bc = (JComponent)(bi.getBean());	      }	      if (bc != null && (bc instanceof Visible)) {		m_editElement = bi;		m_oldX = me.getX();		m_oldY = me.getY();		m_mode = MOVING;	      }	    }	  }	}	public void mouseReleased(MouseEvent me) {	  if (m_editElement != null && m_mode == MOVING) {	    m_editElement = null;	    revalidate();	    m_beanLayout.repaint();	    m_mode = NONE;	  }	}	public void mouseClicked(MouseEvent me) {	  BeanInstance bi = BeanInstance.findInstance(me.getPoint());	  if (m_mode == ADDING || m_mode == NONE) {	    // try and popup a context sensitive menu if we have	    // been clicked over a bean.	    if (bi != null) {	      JComponent bc = (JComponent)bi.getBean();	      if ((me.getModifiers() & InputEvent.BUTTON1_MASK)		  != InputEvent.BUTTON1_MASK) {		doPopup(me.getPoint(), bi, me.getX(), me.getY());	      }	    } else {	      if ((me.getModifiers() & InputEvent.BUTTON1_MASK)		  != InputEvent.BUTTON1_MASK) {		// find connections if any close to this point		int delta = 10;		deleteConnectionPopup(BeanConnection.		      getClosestConnections(new Point(me.getX(), me.getY()), 					    delta), me.getX(), me.getY());	      } else if (m_toolBarBean != null) {		// otherwise, if a toolbar button is active then 		// add the component		addComponent(me.getX(), me.getY());	      }	    }	  }		  if (m_mode == CONNECTING) {	    // undraw rubberbanding line and turn off connecting points	    Vector beanInstances = BeanInstance.getBeanInstances();	    for (int i = 0; i < beanInstances.size(); i++) {	      JComponent bean = 		(JComponent)((BeanInstance)beanInstances.elementAt(i)).		getBean();	      if (bean instanceof Visible) {		((Visible)bean).getVisual().setDisplayConnectors(false);	      }	    }	    Graphics2D gx = (Graphics2D)m_beanLayout.getGraphics();	    gx.setXORMode(java.awt.Color.white);	    // remove the old rubberbanded line	    gx.drawLine(m_startX, m_startY, m_oldX, m_oldY);	    if (bi != null) {	      boolean doConnection = false;	      if (!(bi.getBean() instanceof BeanCommon)) {		doConnection = true;	      } else {		// Give the target bean a chance to veto the proposed		// connection		if (((BeanCommon)bi.getBean()).		    connectionAllowed(m_sourceEventSetDescriptor.getName())) {

⌨️ 快捷键说明

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