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

📄 beanconnection.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            angle = Math.atan(a);          } catch(Exception ex) {            angle = Math.PI / 2;          }          //	Point arrowstart = new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);          Point arrowstart = new Point(bestTargetPt.x,                                       bestTargetPt.y);          Point arrowoffset = new Point((int)(7 * Math.cos(angle)),                                         (int)(7 * Math.sin(angle)));          Point arrowend;          if (bestSourcePt.getX() >= bestTargetPt.getX()) {	              arrowend = new Point(arrowstart.x + arrowoffset.x,                                  arrowstart.y + arrowoffset.y);          } else {            arrowend = new Point(arrowstart.x - arrowoffset.x,                                  arrowstart.y - arrowoffset.y);          }          int xs[] = { arrowstart.x,                       arrowend.x + (int)(7 * Math.cos(angle + (Math.PI / 2))),                       arrowend.x + (int)(7 * Math.cos(angle - (Math.PI / 2)))};          int ys[] = { arrowstart.y,                       arrowend.y + (int)(7 * Math.sin(angle + (Math.PI / 2))),                       arrowend.y + (int)(7 * Math.sin(angle - (Math.PI / 2)))};          gx.fillPolygon(xs, ys, 3);          // ----          // paint the connection name          int midx = (int)bestSourcePt.getX();          midx += (int)((bestTargetPt.getX() - bestSourcePt.getX()) / 2);          int midy = (int)bestSourcePt.getY();          midy += (int)((bestTargetPt.getY() - bestSourcePt.getY()) / 2) - 2 ;          gx.setColor((active) ? Color.blue : Color.gray);          if (previousLink(source, target, i)) {            midy -= 15;          }          gx.drawString(srcEsd.getName(), midx, midy);        }      }    }  }  /**   * Return a list of connections within some delta of a point   *   * @param pt the point at which to look for connections   * @param delta connections have to be within this delta of the point   * @return a list of connections   */  public static Vector getClosestConnections(Point pt, int delta) {    Vector closestConnections = new Vector();        for (int i = 0; i < CONNECTIONS.size(); i++) {      BeanConnection bc = (BeanConnection)CONNECTIONS.elementAt(i);      BeanInstance source = bc.getSource();      BeanInstance target = bc.getTarget();      EventSetDescriptor srcEsd = bc.getSourceEventSetDescriptor();      BeanVisual sourceVisual = (source.getBean() instanceof Visible) ?	((Visible)source.getBean()).getVisual() :	null;      BeanVisual targetVisual = (target.getBean() instanceof Visible) ?	((Visible)target.getBean()).getVisual() :	null;      if (sourceVisual != null && targetVisual != null) {	Point bestSourcePt = 	  sourceVisual.getClosestConnectorPoint(		       new Point((target.getX()+(target.getWidth()/2)), 				 (target.getY() + (target.getHeight() / 2))));	Point bestTargetPt = 	  targetVisual.getClosestConnectorPoint(		       new Point((source.getX()+(source.getWidth()/2)), 				 (source.getY() + (source.getHeight() / 2))));	int minx = (int) Math.min(bestSourcePt.getX(), bestTargetPt.getX());	int maxx = (int) Math.max(bestSourcePt.getX(), bestTargetPt.getX());	int miny = (int) Math.min(bestSourcePt.getY(), bestTargetPt.getY());	int maxy = (int) Math.max(bestSourcePt.getY(), bestTargetPt.getY());	// check to see if supplied pt is inside bounding box	if (pt.getX() >= minx-delta && pt.getX() <= maxx+delta && 	    pt.getY() >= miny-delta && pt.getY() <= maxy+delta) {	  // now see if the point is within delta of the line	  // formulate ax + by + c = 0	  double a = bestSourcePt.getY() - bestTargetPt.getY();	  double b = bestTargetPt.getX() - bestSourcePt.getX();	  double c = (bestSourcePt.getX() * bestTargetPt.getY()) -	    (bestTargetPt.getX() * bestSourcePt.getY());	  	  double distance = Math.abs((a * pt.getX()) + (b * pt.getY()) + c);	  distance /= Math.abs(Math.sqrt((a*a) + (b*b)));	  if (distance <= delta) {	    closestConnections.addElement(bc);	  }	}      }    }    return closestConnections;  }  /**   * Remove all connections for a bean. If the bean is a target for   * receiving events then it gets deregistered from the corresonding   * source bean. If the bean is a source of events then all targets    * implementing BeanCommon are notified via their   * disconnectionNotification methods that the source (and hence the   * connection) is going away.   *   * @param instance the bean to remove connections to/from   */  public static void removeConnections(BeanInstance instance) {        Vector instancesToRemoveFor = new Vector();    if (instance.getBean() instanceof MetaBean) {      instancesToRemoveFor =         ((MetaBean)instance.getBean()).getBeansInSubFlow();    } else {      instancesToRemoveFor.add(instance);    }    Vector removeVector = new Vector();    for (int j = 0; j < instancesToRemoveFor.size(); j++) {      BeanInstance tempInstance =         (BeanInstance)instancesToRemoveFor.elementAt(j);      for (int i = 0; i < CONNECTIONS.size(); i++) {        // In cases where this instance is the target, deregister it        // as a listener for the source        BeanConnection bc = (BeanConnection)CONNECTIONS.elementAt(i);        BeanInstance tempTarget = bc.getTarget();        BeanInstance tempSource = bc.getSource();        EventSetDescriptor tempEsd = bc.getSourceEventSetDescriptor();        if (tempInstance == tempTarget) {          // try to deregister the target as a listener for the source          try {            Method deregisterMethod = tempEsd.getRemoveListenerMethod();            Object targetBean = tempTarget.getBean();            Object [] args = new Object[1];            args[0] = targetBean;            deregisterMethod.invoke(tempSource.getBean(), args);            System.err.println("Deregistering listener");            removeVector.addElement(bc);          } catch (Exception ex) {            ex.printStackTrace();          }        } else if (tempInstance == tempSource) {          removeVector.addElement(bc);          if (tempTarget.getBean() instanceof BeanCommon) {            // tell the target that the source is going away, therefore            // this type of connection is as well            ((BeanCommon)tempTarget.getBean()).              disconnectionNotification(tempEsd.getName(),                                        tempSource.getBean());          }        }      }    }    for (int i = 0; i < removeVector.size(); i++) {      System.err.println("removing connection");      CONNECTIONS.removeElement((BeanConnection)removeVector.elementAt(i));    }  }  public static void doMetaConnection(BeanInstance source, BeanInstance target,                                      final EventSetDescriptor esd,                                      final JComponent displayComponent) {    Object targetBean = target.getBean();    BeanInstance realTarget = null;    final BeanInstance realSource = source;    if (targetBean instanceof MetaBean) {      Vector receivers = ((MetaBean)targetBean).getSuitableTargets(esd);      if (receivers.size() == 1) {        realTarget = (BeanInstance)receivers.elementAt(0);        BeanConnection bc = new BeanConnection(realSource, realTarget,                                               esd);        //        m_target = (BeanInstance)receivers.elementAt(0);      } else {        // have to do the popup thing here        int menuItemCount = 0;        JPopupMenu targetConnectionMenu = new JPopupMenu();        targetConnectionMenu.insert(new JLabel("Select target",                                               SwingConstants.CENTER),                                    menuItemCount++);        for (int i = 0; i < receivers.size(); i++) {          final BeanInstance tempTarget =             (BeanInstance)receivers.elementAt(i);          String tName = ""+(i+1)+": "             + tempTarget.getBean().getClass().getName();          JMenuItem targetItem = new JMenuItem(tName);          targetItem.addActionListener(new ActionListener() {              public void actionPerformed(ActionEvent e) {                //    finalTarget.add(tempTarget);                BeanConnection bc =                   new BeanConnection(realSource, tempTarget,                                     esd);                displayComponent.repaint();              }            });          targetConnectionMenu.add(targetItem);          menuItemCount++;        }        targetConnectionMenu.show(displayComponent, target.getX(),                                  target.getY());        //        m_target = (BeanInstance)finalTarget.elementAt(0);      }    }  }  /**   * Creates a new <code>BeanConnection</code> instance.   *   * @param source the source bean   * @param target the target bean   * @param esd the EventSetDescriptor for the connection   * be displayed   */  public BeanConnection(BeanInstance source, BeanInstance target,			EventSetDescriptor esd) {    m_source = source;    m_target = target;    //    m_sourceEsd = sourceEsd;    m_eventName = esd.getName();    System.err.println(m_eventName);    // attempt to connect source and target beans    Method registrationMethod =       //      m_sourceEsd.getAddListenerMethod();      //      getSourceEventSetDescriptor().getAddListenerMethod();      esd.getAddListenerMethod();    Object targetBean = m_target.getBean();    Object [] args = new Object[1];    args[0] = targetBean;    Class listenerClass = esd.getListenerType();    if (listenerClass.isInstance(targetBean)) {      try {	registrationMethod.invoke(m_source.getBean(), args);	// if the target implements BeanCommon, then inform	// it that it has been registered as a listener with a source via	// the named listener interface	if (targetBean instanceof BeanCommon) {	  ((BeanCommon)targetBean).	    connectionNotification(esd.getName(), m_source.getBean());	}	CONNECTIONS.addElement(this);      } catch (Exception ex) {	System.err.println("Unable to connect beans (BeanConnection)");	ex.printStackTrace();      }    } else {      System.err.println("Unable to connect beans (BeanConnection)");    }  }  /**   * Make this connection invisible on the display   *   * @param hidden true to make the connection invisible   */  public void setHidden(boolean hidden) {    m_hidden = hidden;  }  /**   * Returns true if this connection is invisible   *   * @return true if connection is invisible   */  public boolean isHidden() {    return m_hidden;  }  /**   * Remove this connection   */  public void remove() {    EventSetDescriptor tempEsd = getSourceEventSetDescriptor();    // try to deregister the target as a listener for the source    try {      Method deregisterMethod = tempEsd.getRemoveListenerMethod();      Object targetBean = getTarget().getBean();      Object [] args = new Object[1];      args[0] = targetBean;      deregisterMethod.invoke(getSource().getBean(), args);      System.err.println("Deregistering listener");    } catch (Exception ex) {      ex.printStackTrace();    }    if (getTarget().getBean() instanceof BeanCommon) {      // tell the target that this connection is going away      ((BeanCommon)getTarget().getBean()).	disconnectionNotification(tempEsd.getName(),				  getSource().getBean());    }    CONNECTIONS.remove(this);  }  /**   * returns the source BeanInstance for this connection   *   * @return a <code>BeanInstance</code> value   */  public BeanInstance getSource() {    return m_source;  }  /**   * Returns the target BeanInstance for this connection   *   * @return a <code>BeanInstance</code> value   */  public BeanInstance getTarget() {    return m_target;  }    /**   * Returns the name of the event for this conncetion   *    * @return the name of the event for this connection   */  public String getEventName() {    return m_eventName;  }  /**   * Returns the event set descriptor for the event generated by the source   * for this connection   *   * @return an <code>EventSetDescriptor</code> value   */  protected EventSetDescriptor getSourceEventSetDescriptor() {    JComponent bc = (JComponent)m_source.getBean();     try {       BeanInfo sourceInfo = Introspector.getBeanInfo(bc.getClass());       if (sourceInfo == null) {       System.err.println("Error");       } else {	 EventSetDescriptor [] esds = sourceInfo.getEventSetDescriptors();	 for (int i = 0; i < esds.length; i++) {	   if (esds[i].getName().compareTo(m_eventName) == 0) {	     return esds[i];	   }	 }       }     } catch (Exception ex) {       System.err.println("Problem retrieving event set descriptor (BeanConnection)");     }     return null;          //    return m_sourceEsd;  }}

⌨️ 快捷键说明

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