BeanInstance.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 9k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    BeanInstance.java
  18.  *    Copyright (C) 2002 Mark Hall
  19.  *
  20.  */
  21. package weka.gui.beans;
  22. import java.util.Vector;
  23. import java.beans.Beans;
  24. import java.awt.Component;
  25. import java.awt.Rectangle;
  26. import java.awt.Point;
  27. import java.awt.Dimension;
  28. import java.awt.Graphics;
  29. import java.awt.FontMetrics;
  30. import javax.swing.JComponent;
  31. import java.io.Serializable;
  32. /**
  33.  * Class that manages a set of beans.
  34.  *
  35.  * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
  36.  * @version  $Revision: 1.1 $
  37.  * @since 1.0
  38.  */
  39. public class BeanInstance implements Serializable {
  40.   /**
  41.    * class variable holding all the beans
  42.    */
  43.   private static Vector COMPONENTS = new Vector();
  44.   public static final int IDLE = 0;
  45.   public static final int BEAN_EXECUTING = 1;
  46.   
  47.   /**
  48.    * Holds the bean encapsulated in this instance
  49.    */
  50.   private Object m_bean;
  51.   private int m_x;
  52.   private int m_y;
  53.   /**
  54.    * Reset the list of beans
  55.    */
  56.   public static void reset(JComponent container) {
  57.     // remove beans from container if necessary
  58.     removeAllBeansFromContainer(container);
  59.     COMPONENTS = new Vector();
  60.   }
  61.   /**
  62.    * Removes all beans from containing component
  63.    *
  64.    * @param container a <code>JComponent</code> value
  65.    */
  66.   public static void removeAllBeansFromContainer(JComponent container) {
  67.     if (container != null) {
  68.       if (COMPONENTS != null) {
  69. for (int i = 0; i < COMPONENTS.size(); i++) {
  70.   BeanInstance tempInstance = (BeanInstance)COMPONENTS.elementAt(i);
  71.   Object tempBean = tempInstance.getBean();
  72.   if (Beans.isInstanceOf(tempBean, JComponent.class)) {
  73.     container.remove((JComponent)tempBean);
  74.   }
  75. }
  76.       }
  77.       container.revalidate();
  78.     }
  79.   }
  80.   /**
  81.    * Adds all beans to the supplied component
  82.    *
  83.    * @param container a <code>JComponent</code> value
  84.    */
  85.   public static void addAllBeansToContainer(JComponent container) {
  86.     if (container != null) {
  87.       if (COMPONENTS != null) {
  88. for (int i = 0; i < COMPONENTS.size(); i++) {
  89.   BeanInstance tempInstance = (BeanInstance)COMPONENTS.elementAt(i);
  90.   Object tempBean = tempInstance.getBean();
  91.   if (Beans.isInstanceOf(tempBean, JComponent.class)) {
  92.     container.add((JComponent)tempBean);
  93.   }
  94. }
  95.       }
  96.       container.revalidate();
  97.     }
  98.   }
  99.   /**
  100.    * Return the list of displayed beans
  101.    *
  102.    * @return a vector of beans
  103.    */
  104.   public static Vector getBeanInstances() {
  105.     return COMPONENTS;
  106.   }
  107.   /**
  108.    * Describe <code>setBeanInstances</code> method here.
  109.    *
  110.    * @param beanInstances a <code>Vector</code> value
  111.    * @param container a <code>JComponent</code> value
  112.    */
  113.   public static void setBeanInstances(Vector beanInstances, 
  114.       JComponent container) {
  115.     reset(container);
  116.     
  117.     if (container != null) {
  118.       for (int i = 0; i < beanInstances.size(); i++) {
  119. Object bean = ((BeanInstance)beanInstances.elementAt(i)).getBean();
  120. if (Beans.isInstanceOf(bean, JComponent.class)) {
  121.   container.add((JComponent)bean);
  122. }
  123.       }
  124.       container.revalidate();
  125.       container.repaint();
  126.     }
  127.     COMPONENTS = beanInstances;
  128.   }
  129.   /**
  130.    * Renders the textual labels for the beans.
  131.    *
  132.    * @param gx a <code>Graphics</code> object on which to render
  133.    * the labels
  134.    */
  135.   public static void paintLabels(Graphics gx) {
  136.     FontMetrics fm = gx.getFontMetrics();
  137.     int hf = fm.getAscent();
  138.     for (int i = 0; i < COMPONENTS.size(); i++) {
  139.       BeanInstance bi = (BeanInstance)COMPONENTS.elementAt(i);
  140.       if (!(bi.getBean() instanceof Visible)) {
  141. continue;
  142.       }
  143.       int cx = bi.getX(); int cy = bi.getY();
  144.       int width = ((JComponent)bi.getBean()).getWidth();
  145.       int height = ((JComponent)bi.getBean()).getHeight();
  146.       String label = ((Visible)bi.getBean()).getVisual().getText();
  147.       int labelwidth = fm.stringWidth(label);
  148.       gx.drawString(label, (cx+(width/2)) - (labelwidth / 2), cy+height+hf+2);
  149.     }
  150.   }
  151.   /**
  152.    * Looks for a bean (if any) whose bounds contain the supplied point
  153.    *
  154.    * @param p a point
  155.    * @return a bean that contains the supplied point or null if no bean
  156.    * contains the point
  157.    */
  158.   public static BeanInstance findInstance(Point p) {
  159.     Rectangle tempBounds = new Rectangle();
  160.     for (int i=0; i < COMPONENTS.size(); i++) {
  161.       
  162.       BeanInstance t = (BeanInstance)COMPONENTS.elementAt(i);
  163.       JComponent temp = (JComponent)t.getBean();
  164.       
  165.       tempBounds = temp.getBounds(tempBounds);
  166.       if (tempBounds.contains(p)) {
  167. return t;
  168.       }
  169.     }
  170.     return null;
  171.   }
  172.   /**
  173.    * Creates a new <code>BeanInstance</code> instance.
  174.    *
  175.    * @param container a <code>JComponent</code> to add the bean to
  176.    * @param bean the bean to add
  177.    * @param x the x coordinate of the bean
  178.    * @param y the y coordinate of the bean
  179.    */
  180.   public BeanInstance(JComponent container, Object bean, int x, int y) {
  181.     m_bean = bean;
  182.     m_x = x;
  183.     m_y = y;
  184.     addBean(container);
  185.   }
  186.   /**
  187.    * Creates a new <code>BeanInstance</code> instance given the fully
  188.    * qualified name of the bean
  189.    *
  190.    * @param container a <code>JComponent</code> to add the bean to
  191.    * @param beanName the fully qualified name of the bean
  192.    * @param x the x coordinate of the bean
  193.    * @param y th y coordinate of the bean
  194.    */
  195.   public BeanInstance(JComponent container, String beanName, int x, int y) {
  196.     m_x = x;
  197.     m_y = y;
  198.     
  199.     // try and instantiate the named component
  200.     try {
  201.       m_bean = Beans.instantiate(null, beanName);
  202.     } catch (Exception ex) {
  203.       ex.printStackTrace();
  204.       return;
  205.     }
  206.     addBean(container);
  207.   }
  208.   /**
  209.    * Remove this bean from the list of beans and from the containing component
  210.    *
  211.    * @param container the <code>JComponent</code> that holds the bean
  212.    */
  213.   public void removeBean(JComponent container) {
  214.     for (int i = 0; i < COMPONENTS.size(); i++) {
  215.       if ((BeanInstance)COMPONENTS.elementAt(i) == this) {
  216. System.err.println("Removing bean");
  217. COMPONENTS.removeElementAt(i);
  218.       }
  219.     }
  220.     if (container != null) {
  221.       container.remove((JComponent)m_bean);
  222.       container.revalidate();
  223.       container.repaint();
  224.     }
  225.   }
  226.   private void addBean(JComponent container) {
  227.     // Ignore invisible components
  228.     if (!Beans.isInstanceOf(m_bean, JComponent.class)) {
  229.       System.err.println("Component is invisible!");
  230.       return;
  231.     }
  232.     
  233.     COMPONENTS.addElement(this);
  234.     
  235.     // Position and layout the component
  236.     JComponent c = (JComponent)m_bean;
  237.     Dimension d = c.getPreferredSize();
  238.     int dx = (int)(d.getWidth() / 2);
  239.     int dy = (int)(d.getHeight() / 2);
  240.     m_x -= dx;
  241.     m_y -= dy;
  242.     c.setLocation(m_x, m_y);
  243.     //    c.doLayout();
  244.     c.validate();
  245.     //    bp.addBean(c);
  246.     //    c.repaint();
  247.     if (container != null) {
  248.       container.add(c);
  249.       container.revalidate();
  250.     }
  251.   }
  252.   /**
  253.    * Gets the bean encapsulated in this instance
  254.    *
  255.    * @return an <code>Object</code> value
  256.    */
  257.   public Object getBean() {
  258.     return m_bean;
  259.   }
  260.   /**
  261.    * Gets the x coordinate of this bean
  262.    *
  263.    * @return an <code>int</code> value
  264.    */
  265.   public int getX() {
  266.     return m_x;
  267.   }
  268.   /**
  269.    * Gets the y coordinate of this bean
  270.    *
  271.    * @return an <code>int</code> value
  272.    */
  273.   public int getY() {
  274.     return m_y;
  275.   }
  276.   /**
  277.    * Gets the width of this bean
  278.    *
  279.    * @return an <code>int</code> value
  280.    */
  281.   public int getWidth() {
  282.     return ((JComponent)m_bean).getWidth();
  283.   }
  284.   /**
  285.    * Gets the height of this bean
  286.    *
  287.    * @return an <code>int</code> value
  288.    */
  289.   public int getHeight() {
  290.     return ((JComponent)m_bean).getHeight();
  291.   }
  292.  
  293.   /**
  294.    * Sets the x coordinate of this bean
  295.    *
  296.    * @param newX an <code>int</code> value
  297.    */
  298.   public void setX(int newX) {
  299.     m_x = newX;
  300.     ((JComponent)m_bean).setLocation(m_x, m_y);
  301.     ((JComponent)m_bean).validate();
  302.   }
  303.   /**
  304.    * Sets the y coordinate of this bean
  305.    *
  306.    * @param newY an <code>int</code> value
  307.    */
  308.   public void setY(int newY) {
  309.     m_y = newY;
  310.     ((JComponent)m_bean).setLocation(m_x, m_y);
  311.     ((JComponent)m_bean).validate();
  312.   }
  313. }