Example #1
0
  @SuppressWarnings("deprecation")
  public final void runOneComponent(
      Component comp, Rectangle bounds, Graphics g, Shape clip, int weightFlags) {
    if (comp == null || comp.getPeer() == null || !comp.isVisible()) {
      return;
    }
    boolean lightweight = comp.isLightweight();
    if ((lightweight && (weightFlags & LIGHTWEIGHTS) == 0)
        || (!lightweight && (weightFlags & HEAVYWEIGHTS) == 0)) {
      return;
    }

    if (bounds == null) {
      bounds = comp.getBounds();
    }

    if (clip == null || clip.intersects(bounds)) {
      Graphics cg = g.create();
      try {
        constrainGraphics(cg, bounds);
        cg.setFont(comp.getFont());
        cg.setColor(comp.getForeground());
        if (cg instanceof Graphics2D) {
          ((Graphics2D) cg).setBackground(comp.getBackground());
        } else if (cg instanceof Graphics2Delegate) {
          ((Graphics2Delegate) cg).setBackground(comp.getBackground());
        }
        run(comp, cg);
      } finally {
        cg.dispose();
      }
    }
  }
Example #2
0
  public final void runComponents(Component[] comps, Graphics g, int weightFlags) {
    int ncomponents = comps.length;
    Shape clip = g.getClip();

    if (log.isLoggable(PlatformLogger.Level.FINER) && (clip != null)) {
      Rectangle newrect = clip.getBounds();
      log.finer(
          "x = "
              + newrect.x
              + ", y = "
              + newrect.y
              + ", width = "
              + newrect.width
              + ", height = "
              + newrect.height);
    }

    // A seriously sad hack--
    // Lightweight components always paint behind peered components,
    // even if they are at the top of the Z order. We emulate this
    // behavior by making two printing passes: the first for lightweights;
    // the second for heavyweights.
    //
    // ToDo(dpm): Either build a list of heavyweights during the
    // lightweight pass, or redesign the components array to keep
    // lightweights and heavyweights separate.
    if ((weightFlags & TWO_PASSES) != 0) {
      for (int i = ncomponents - 1; i >= 0; i--) {
        runOneComponent(comps[i], null, g, clip, LIGHTWEIGHTS);
      }
      for (int i = ncomponents - 1; i >= 0; i--) {
        runOneComponent(comps[i], null, g, clip, HEAVYWEIGHTS);
      }
    } else {
      for (int i = ncomponents - 1; i >= 0; i--) {
        runOneComponent(comps[i], null, g, clip, weightFlags);
      }
    }
  }
Example #3
0
  /**
   * Adjusts the allocation given to the view to be a suitable allocation for a text field. If the
   * view has been allocated more than the preferred span vertically, the allocation is changed to
   * be centered vertically. Horizontally the view is adjusted according to the horizontal alignment
   * property set on the associated JTextField (if that is the type of the hosting component).
   *
   * @param a the allocation given to the view, which may need to be adjusted.
   * @return the allocation that the superclass should use.
   */
  protected Shape adjustAllocation(Shape a) {
    if (a != null) {
      Rectangle bounds = a.getBounds();
      int vspan = (int) getPreferredSpan(Y_AXIS);
      int hspan = (int) getPreferredSpan(X_AXIS);
      if (bounds.height != vspan) {
        int slop = bounds.height - vspan;
        bounds.y += slop / 2;
        bounds.height -= slop;
      }

      // horizontal adjustments
      Component c = getContainer();
      if (c instanceof JTextField) {
        JTextField field = (JTextField) c;
        BoundedRangeModel vis = field.getHorizontalVisibility();
        int max = Math.max(hspan, bounds.width);
        int value = vis.getValue();
        int extent = Math.min(max, bounds.width - 1);
        if ((value + extent) > max) {
          value = max - extent;
        }
        vis.setRangeProperties(value, extent, vis.getMinimum(), max, false);
        if (hspan < bounds.width) {
          // horizontally align the interior
          int slop = bounds.width - 1 - hspan;

          int align = ((JTextField) c).getHorizontalAlignment();
          if (Utilities.isLeftToRight(c)) {
            if (align == LEADING) {
              align = LEFT;
            } else if (align == TRAILING) {
              align = RIGHT;
            }
          } else {
            if (align == LEADING) {
              align = RIGHT;
            } else if (align == TRAILING) {
              align = LEFT;
            }
          }

          switch (align) {
            case SwingConstants.CENTER:
              bounds.x += slop / 2;
              bounds.width -= slop;
              break;
            case SwingConstants.RIGHT:
              bounds.x += slop;
              bounds.width -= slop;
              break;
          }
        } else {
          // adjust the allocation to match the bounded range.
          bounds.width = hspan;
          bounds.x -= vis.getValue();
        }
      }
      return bounds;
    }
    return null;
  }