knowledgeflow.java

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

JAVA
1,209
字号
		  doConnection = true;		}	      }	      if (doConnection) {		// attempt to connect source and target beans		BeanConnection bc = 		  new BeanConnection(m_editElement, bi, 				     m_sourceEventSetDescriptor);	      }	      m_beanLayout.repaint();	    }	    gx.dispose();	    m_mode = NONE;	    m_editElement = null;	    m_sourceEventSetDescriptor = null;	  }	}      });         m_beanLayout.addMouseMotionListener(new MouseMotionAdapter() {	public void mouseDragged(MouseEvent me) {	  if (m_editElement != null && m_mode == MOVING) {	    ImageIcon ic = ((Visible)m_editElement.getBean()).	      getVisual().getStaticIcon();	    int width = ic.getIconWidth() / 2;	    int height = ic.getIconHeight() / 2;	    m_editElement.setX(m_oldX-width);	    m_editElement.setY(m_oldY-height);	    m_beanLayout.repaint();	    	    // note the new points	    m_oldX = me.getX(); m_oldY = me.getY();	  }	}	 public void mouseMoved(MouseEvent e) {	   if (m_mode == CONNECTING) {	     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);	     // note the new coordinates	     m_oldX = e.getX(); m_oldY = e.getY();	     // draw the new rubberbanded line	     gx.drawLine(m_startX, m_startY, m_oldX, m_oldY);	     gx.dispose();	   }	 }       });          String date = (new SimpleDateFormat("EEEE, d MMMM yyyy"))       .format(new Date());     m_logPanel.logMessage("Weka Knowledge Flow was written by Mark Hall");     m_logPanel.logMessage("Weka Knowledge Flow");     m_logPanel.logMessage("(c) 2002-2003 Mark Hall");     m_logPanel.logMessage("web: http://www.cs.waikato.ac.nz/~ml/");     m_logPanel.logMessage( date);     m_logPanel.statusMessage("Welcome to the Weka Knowledge Flow");         JPanel p1 = new JPanel();     p1.setLayout(new BorderLayout());     p1.setBorder(javax.swing.BorderFactory.createCompoundBorder(			    javax.swing.BorderFactory.			    createTitledBorder("Knowledge Flow Layout"),                   javax.swing.BorderFactory.createEmptyBorder(0, 5, 5, 5)                   ));     final JScrollPane js = new JScrollPane(m_beanLayout);     p1.add(js, BorderLayout.CENTER);     setLayout(new BorderLayout());          add(p1, BorderLayout.CENTER);     m_beanLayout.setSize(1024, 768);     Dimension d = m_beanLayout.getPreferredSize();     m_beanLayout.setMinimumSize(d);     m_beanLayout.setMaximumSize(d);     m_beanLayout.setPreferredSize(d);     add(m_logPanel, BorderLayout.SOUTH);          setUpToolBars();  }    private Image loadImage(String path) {    Image pic = null;    java.net.URL imageURL = ClassLoader.getSystemResource(path);    if (imageURL == null) {      //      System.err.println("Warning: unable to load "+path);    } else {      pic = Toolkit.getDefaultToolkit().	getImage(imageURL);    }    return pic;  }  /**   * Describe <code>setUpToolBars</code> method here.   */  private void setUpToolBars() {    JPanel toolBarPanel = new JPanel();    toolBarPanel.setLayout(new BorderLayout());    // first construct the toolbar for saving, loading etc    JToolBar fixedTools = new JToolBar();    fixedTools.setOrientation(JToolBar.VERTICAL);    m_saveB = new JButton(new ImageIcon(loadImage(BeanVisual.ICON_PATH						  +"Save24.gif")));    m_saveB.setToolTipText("Save layout");    m_loadB = new JButton(new ImageIcon(loadImage(BeanVisual.ICON_PATH						  +"Open24.gif")));    m_stopB = new JButton(new ImageIcon(loadImage(BeanVisual.ICON_PATH						  +"Stop24.gif")));    m_stopB.setToolTipText("Stop all execution");    m_loadB.setToolTipText("Load layout");    Image tempI = loadImage(BeanVisual.ICON_PATH+"Pointer.gif");    m_pointerB = new JToggleButton(new ImageIcon(tempI));    m_pointerB.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  m_toolBarBean = null;	  m_mode = NONE;	  setCursor(Cursor.		    getPredefinedCursor(Cursor.DEFAULT_CURSOR));	}      });    m_toolBarGroup.add(m_pointerB);    fixedTools.add(m_pointerB);    fixedTools.add(m_saveB);    fixedTools.add(m_loadB);    fixedTools.add(m_stopB);    Dimension dP = m_saveB.getPreferredSize();    Dimension dM = m_saveB.getMaximumSize();    fixedTools.setFloatable(false);    m_pointerB.setPreferredSize(dP);    m_pointerB.setMaximumSize(dM);    toolBarPanel.add(fixedTools, BorderLayout.WEST);    m_saveB.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  saveLayout();	}      });    m_loadB.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  loadLayout();	}      });    m_stopB.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  Vector components = BeanInstance.getBeanInstances();	  for (int i = 0; i < components.size(); i++) {	    Object temp = ((BeanInstance)components.elementAt(i)).getBean();	    if (temp instanceof BeanCommon) {	      ((BeanCommon)temp).stop();	    }	  }	}      });    final int STANDARD_TOOLBAR = 0;    final int WEKAWRAPPER_TOOLBAR = 1;    int toolBarType = STANDARD_TOOLBAR;    // set up wrapper toolbars    for (int i = 0; i < TOOLBARS.size(); i++) {      Vector tempBarSpecs = (Vector)TOOLBARS.elementAt(i);            // name for the tool bar      String tempBarName = (String)tempBarSpecs.elementAt(0);      // name of the bean component to handle this class of weka algorithms      String tempBeanCompName = (String)tempBarSpecs.elementAt(1);      // a JPanel holding an instantiated bean + label ready to be added      // to the current toolbar      JPanel tempBean;      // the root package for weka algorithms      String rootPackage = "";      weka.gui.HierarchyPropertyParser hpp = null;      // Is this a wrapper toolbar?      if (tempBeanCompName.compareTo("null") != 0) {	tempBean = null;	toolBarType = WEKAWRAPPER_TOOLBAR;	rootPackage = (String)tempBarSpecs.elementAt(2);	hpp = (weka.gui.HierarchyPropertyParser)tempBarSpecs.elementAt(3);	try {	  Beans.instantiate(null, tempBeanCompName);	} catch (Exception ex) {	  // ignore	  System.err.println("Failed to instantiate: "+tempBeanCompName);	  break;	}      } else {	toolBarType = STANDARD_TOOLBAR;      }      // a toolbar to hold buttons---one for each algorithm      JToolBar tempToolBar = new JToolBar();      int z = 2;      if (toolBarType == WEKAWRAPPER_TOOLBAR) {	if (!hpp.goTo(rootPackage)) {	  System.err.println("**** Failed to locate root package in tree ");	  System.exit(1);	}	String [] primaryPackages = hpp.childrenValues();	for (int kk = 0; kk < primaryPackages.length; kk++) {	  hpp.goToChild(primaryPackages[kk]);	  // check to see if this is a leaf - if so then there are no	  // sub packages	  if (hpp.isLeafReached()) {	    // add this bean directly to the tempToolBar	    String algName = hpp.fullValue();	    tempBean = instantiateToolBarBean(true,					       tempBeanCompName, algName);	    if (tempBean != null) {	      tempToolBar.add(tempBean);	    }	    hpp.goToParent();	  } else {	    // make a titledborder JPanel to hold all the schemes in this	    // package	    JPanel holderPanel = new JPanel();	    holderPanel.setBorder(javax.swing.BorderFactory.				  createTitledBorder(primaryPackages[kk]));	    processPackage(holderPanel, tempBeanCompName, hpp);	    tempToolBar.add(holderPanel);	  }	}      } else {	for (int j = z; j < tempBarSpecs.size(); j++) {	  tempBean = null;	  tempBeanCompName = (String)tempBarSpecs.elementAt(j);	  tempBean = 	    instantiateToolBarBean((toolBarType == WEKAWRAPPER_TOOLBAR),				   tempBeanCompName, "");	  if (tempBean != null) {	    tempToolBar.add(tempBean);	  } 	}      }            // ok, now create tabbed pane to hold this toolbar      JScrollPane tempJScrollPane = new JScrollPane(tempToolBar, 					JScrollPane.VERTICAL_SCROLLBAR_NEVER,					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);      Dimension d = tempToolBar.getPreferredSize();      tempJScrollPane.setMinimumSize(new Dimension((int)d.getWidth(),						   (int)(d.getHeight()+15)));      tempJScrollPane.setPreferredSize(new Dimension((int)d.getWidth(),						    (int)(d.getHeight()+15)));      m_toolBars.addTab(tempBarName, null, 			tempJScrollPane,			tempBarName);          }    toolBarPanel.add(m_toolBars, BorderLayout.CENTER);    //    add(m_toolBars, BorderLayout.NORTH);    add(toolBarPanel, BorderLayout.NORTH);  }  private void processPackage(JPanel holderPanel,			      String tempBeanCompName,			      weka.gui.HierarchyPropertyParser hpp) {    if (hpp.isLeafReached()) {      // instantiate a bean and add it to the holderPanel      //      System.err.println("Would add "+hpp.fullValue());      String algName = hpp.fullValue();      JPanel tempBean = 	instantiateToolBarBean(true, tempBeanCompName, algName);      if (tempBean != null) {	holderPanel.add(tempBean);      }      hpp.goToParent();      return;    }    String [] children = hpp.childrenValues();    for (int i = 0; i < children.length; i++) {      hpp.goToChild(children[i]);      processPackage(holderPanel, tempBeanCompName, hpp);    }    hpp.goToParent();  }  /**   * Instantiates a bean for display in the toolbars   *   * @param wekawrapper true if the bean to be instantiated is a wekawrapper   * @param tempBeanCompName the name of the bean to instantiate   * @param algName holds the name of a weka algorithm to configure the   * bean with if it is a wekawrapper bean   * @return a JPanel holding the instantiated (and configured bean)   */  private JPanel instantiateToolBarBean(boolean wekawrapper, 					String tempBeanCompName,					String algName) {    Object tempBean;    if (wekawrapper) {      try {	tempBean = Beans.instantiate(null, tempBeanCompName);      } catch (Exception ex) {	System.err.println("Failed to instantiate :"+tempBeanCompName			   +"KnowledgeFlow.instantiateToolBarBean()");	return null;      }      if (tempBean instanceof WekaWrapper) {	//	algName = (String)tempBarSpecs.elementAt(j);	Class c = null;	try {	  c = Class.forName(algName);	} catch (Exception ex) {	  System.err.println("Can't find class called: "+algName);	  return null;	}	try {	  Object o = c.newInstance();	  ((WekaWrapper)tempBean).setWrappedAlgorithm(o);	} catch (Exception ex) {	  System.err.println("Failed to configure "+tempBeanCompName			     +" with "+algName);	  return null;	}      }    } else {      try {	tempBean = Beans.instantiate(null, tempBeanCompName);      } catch (Exception ex) {	System.err.println("Failed to instantiate :"+tempBeanCompName			   +"KnowledgeFlow.setUpToolBars()");	return null;      }    }        // ---------------------------------------    JToggleButton tempButton;    JPanel tempP = new JPanel();    JLabel tempL = new JLabel();    tempL.setFont(new Font("Monospaced", Font.PLAIN, 10));    String labelName = (wekawrapper == true)       ? algName       : tempBeanCompName;    tempL.setText(" "+labelName.		  substring(labelName.lastIndexOf('.')+1, 			    labelName.length())+" ");    tempL.setHorizontalAlignment(JLabel.CENTER);    tempP.setLayout(new BorderLayout());    if (tempBean instanceof Visible) {      BeanVisual bv = ((Visible)tempBean).getVisual();      tempButton = 	new JToggleButton(bv.getStaticIcon());    } else {      tempButton = new JToggleButton();    }    tempP.add(tempButton, BorderLayout.CENTER);    tempP.add(tempL, BorderLayout.SOUTH);        //  holderPanel.add(tempP);    //    tempToolBar.add(tempP);    m_toolBarGroup.add(tempButton);        // add an action listener for the button here    final String tempName = tempBeanCompName;    final Object tempBN = tempBean;    //	  final JToggleButton tempButton2 = tempButton;    tempButton.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  try {	    m_toolBarBean = null;	    m_toolBarBean = Beans.instantiate(null, tempName);	    if (m_toolBarBean instanceof WekaWrapper) {	      Object wrappedAlg = 		((WekaWrapper)tempBN).getWrappedAlgorithm();	      	      ((WekaWrapper)m_toolBarBean).setWrappedAlgorithm(wrappedAlg.getClass().newInstance());	      //		    tempButton2.setSelected(false);	    }	    setCursor(Cursor.		      getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));	    m_mode = ADDING;	  } catch (Exception ex) {	    System.err.	      println("Problem adding bean to data flow layout");	  }	}      });        //return tempBean;    return tempP;  }  /**   * Popup a context sensitive menu for the bean component   *   * @param pt holds the panel coordinates for the component   * @param bi the bean component over which the user right clicked the mouse   * @param x the x coordinate at which to popup the menu

⌨️ 快捷键说明

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