Exemple #1
0
  /**
   * Looks for all beans (if any) located within the supplied bounding box. Also adjusts the
   * bounding box to be a tight fit around all contained beans
   *
   * @param boundingBox the bounding rectangle
   * @return a Vector of BeanInstances
   */
  public static Vector<Object> findInstances(Rectangle boundingBox, Integer... tab) {
    int index = 0;
    if (tab.length > 0) {
      index = tab[0].intValue();
    }

    Vector<Object> components = null;
    if (TABBED_COMPONENTS.size() > 0 && index < TABBED_COMPONENTS.size()) {
      components = TABBED_COMPONENTS.get(index);
    }

    Graphics gx = null;
    FontMetrics fm = null;

    int centerX, centerY;
    int minX = Integer.MAX_VALUE;
    int minY = Integer.MAX_VALUE;
    int maxX = Integer.MIN_VALUE;
    int maxY = Integer.MIN_VALUE;
    Vector<Object> result = new Vector<Object>();
    for (int i = 0; i < components.size(); i++) {
      BeanInstance t = (BeanInstance) components.elementAt(i);
      centerX = t.getX() + (t.getWidth() / 2);
      centerY = t.getY() + (t.getHeight() / 2);
      if (boundingBox.contains(centerX, centerY)) {
        result.addElement(t);

        // adjust bounding box stuff
        // int hf = 0;
        if (gx == null) {
          gx = ((JComponent) t.getBean()).getGraphics();
          gx.setFont(new Font(null, Font.PLAIN, 9));
          fm = gx.getFontMetrics();
          // hf = fm.getAscent();
        }
        String label = "";
        if (t.getBean() instanceof Visible) {
          label = ((Visible) t.getBean()).getVisual().getText();
        }
        int labelwidth = fm.stringWidth(label);
        /*
         * if (label.length() == 0) { heightMultiplier = 0; }
         */
        int brx = 0;
        int blx = 0;
        if (centerX - (labelwidth / 2) - 2 < t.getX()) {
          blx = (centerX - (labelwidth / 2) - 2);
          brx = centerX + (labelwidth / 2) + 2;
        } else {
          blx = t.getX() - 2;
          brx = t.getX() + t.getWidth() + 2;
        }

        if (blx < minX) {
          minX = blx;
        }
        if (brx > maxX) {
          maxX = brx;
        }
        if (t.getY() - 2 < minY) {
          minY = t.getY() - 2;
        }
        if (t.getY() + t.getHeight() + 2 > maxY) {
          maxY = t.getY() + t.getHeight() + 2;
        }
      }
    }
    boundingBox.setBounds(minX, minY, maxX - minX, maxY - minY);

    return result;
  }
Exemple #2
0
  /**
   * Renders the textual labels for the beans.
   *
   * @param gx a <code>Graphics</code> object on which to render the labels
   */
  public static void paintLabels(Graphics gx, Integer... tab) {
    int index = 0;
    if (tab.length > 0) {
      index = tab[0].intValue();
    }

    Vector<Object> components = null;
    if (TABBED_COMPONENTS.size() > 0 && index < TABBED_COMPONENTS.size()) {
      components = TABBED_COMPONENTS.get(index);
    }

    if (components != null) {
      gx.setFont(new Font(null, Font.PLAIN, 9));
      FontMetrics fm = gx.getFontMetrics();
      int hf = fm.getAscent();
      for (int i = 0; i < components.size(); i++) {
        BeanInstance bi = (BeanInstance) components.elementAt(i);
        if (!(bi.getBean() instanceof Visible)) {
          continue;
        }
        int cx = bi.getX();
        int cy = bi.getY();
        int width = ((JComponent) bi.getBean()).getWidth();
        int height = ((JComponent) bi.getBean()).getHeight();
        String label = ((Visible) bi.getBean()).getVisual().getText();
        int labelwidth = fm.stringWidth(label);
        if (labelwidth < width) {
          gx.drawString(label, (cx + (width / 2)) - (labelwidth / 2), cy + height + hf + 2);
        } else {
          // split label

          // find mid point
          int mid = label.length() / 2;
          // look for split point closest to the mid
          int closest = label.length();
          int closestI = -1;
          for (int z = 0; z < label.length(); z++) {
            if (label.charAt(z) < 'a') {
              if (Math.abs(mid - z) < closest) {
                closest = Math.abs(mid - z);
                closestI = z;
              }
            }
          }
          if (closestI != -1) {
            String left = label.substring(0, closestI);
            String right = label.substring(closestI, label.length());
            if (left.length() > 1 && right.length() > 1) {
              gx.drawString(
                  left,
                  (cx + (width / 2)) - (fm.stringWidth(left) / 2),
                  cy + height + (hf * 1) + 2);
              gx.drawString(
                  right,
                  (cx + (width / 2)) - (fm.stringWidth(right) / 2),
                  cy + height + (hf * 2) + 2);
            } else {
              gx.drawString(
                  label,
                  (cx + (width / 2)) - (fm.stringWidth(label) / 2),
                  cy + height + (hf * 1) + 2);
            }
          } else {
            gx.drawString(
                label,
                (cx + (width / 2)) - (fm.stringWidth(label) / 2),
                cy + height + (hf * 1) + 2);
          }
        }
      }
    }
  }