Пример #1
0
 /**
  * Determines the preferred span for this view along an axis.
  *
  * @param axis may be either View.X_AXIS or View.Y_AXIS
  * @return the span the view would like to be rendered into >= 0. Typically the view is told to
  *     render into the span that is returned, although there is no guarantee. The parent may
  *     choose to resize or break the view.
  */
 public float getPreferredSpan(int axis) {
   switch (axis) {
     case View.X_AXIS:
       Segment buff = SegmentCache.getSharedSegment();
       Document doc = getDocument();
       int width;
       try {
         FontMetrics fm = getFontMetrics();
         doc.getText(0, doc.getLength(), buff);
         width = Utilities.getTabbedTextWidth(buff, fm, 0, this, 0);
         if (buff.count > 0) {
           Component c = getContainer();
           firstLineOffset =
               j86.sun.swing.SwingUtilities2.getLeftSideBearing(
                   (c instanceof JComponent) ? (JComponent) c : null, fm, buff.array[buff.offset]);
           firstLineOffset = Math.max(0, -firstLineOffset);
         } else {
           firstLineOffset = 0;
         }
       } catch (BadLocationException bl) {
         width = 0;
       }
       SegmentCache.releaseSharedSegment(buff);
       return width + firstLineOffset;
     default:
       return super.getPreferredSpan(axis);
   }
 }
Пример #2
0
 /**
  * Update the visibility model with the associated JTextField (if there is one) to reflect the
  * current visibility as a result of changes to the document model. The bounded range properties
  * are updated. If the view hasn't yet been shown the extent will be zero and we just set it to be
  * full until determined otherwise.
  */
 void updateVisibilityModel() {
   Component c = getContainer();
   if (c instanceof JTextField) {
     JTextField field = (JTextField) c;
     BoundedRangeModel vis = field.getHorizontalVisibility();
     int hspan = (int) getPreferredSpan(X_AXIS);
     int extent = vis.getExtent();
     int maximum = Math.max(hspan, extent);
     extent = (extent == 0) ? maximum : extent;
     int value = maximum - extent;
     int oldValue = vis.getValue();
     if ((oldValue + extent) > maximum) {
       oldValue = maximum - extent;
     }
     value = Math.max(0, Math.min(value, oldValue));
     vis.setRangeProperties(value, extent, 0, maximum, false);
   }
 }
Пример #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;
  }
Пример #4
0
  @Override
  public void paintIcon(SynthContext context, Graphics g, int x, int y, int w, int h) {
    Painter painter = null;
    if (context != null) {
      painter = (Painter) context.getStyle().get(context, key);
    }
    if (painter == null) {
      painter = (Painter) UIManager.get(prefix + "[Enabled]." + key);
    }

    if (painter != null && context != null) {
      JComponent c = context.getComponent();
      boolean rotate = false;
      boolean flip = false;
      // translatex and translatey are additional translations that
      // must occur on the graphics context when rendering a toolbar
      // icon
      int translatex = 0;
      int translatey = 0;
      if (c instanceof JToolBar) {
        JToolBar toolbar = (JToolBar) c;
        rotate = toolbar.getOrientation() == JToolBar.VERTICAL;
        flip = !toolbar.getComponentOrientation().isLeftToRight();
        Object o = NimbusLookAndFeel.resolveToolbarConstraint(toolbar);
        // we only do the +1 hack for UIResource borders, assuming
        // that the border is probably going to be our border
        if (toolbar.getBorder() instanceof UIResource) {
          if (o == BorderLayout.SOUTH) {
            translatey = 1;
          } else if (o == BorderLayout.EAST) {
            translatex = 1;
          }
        }
      } else if (c instanceof JMenu) {
        flip = !c.getComponentOrientation().isLeftToRight();
      }
      if (g instanceof Graphics2D) {
        Graphics2D gfx = (Graphics2D) g;
        gfx.translate(x, y);
        gfx.translate(translatex, translatey);
        if (rotate) {
          gfx.rotate(Math.toRadians(90));
          gfx.translate(0, -w);
          painter.paint(gfx, context.getComponent(), h, w);
          gfx.translate(0, w);
          gfx.rotate(Math.toRadians(-90));
        } else if (flip) {
          gfx.scale(-1, 1);
          gfx.translate(-w, 0);
          painter.paint(gfx, context.getComponent(), w, h);
          gfx.translate(w, 0);
          gfx.scale(-1, 1);
        } else {
          painter.paint(gfx, context.getComponent(), w, h);
        }
        gfx.translate(-translatex, -translatey);
        gfx.translate(-x, -y);
      } else {
        // use image if we are printing to a Java 1.1 PrintGraphics as
        // it is not a instance of Graphics2D
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gfx = img.createGraphics();
        if (rotate) {
          gfx.rotate(Math.toRadians(90));
          gfx.translate(0, -w);
          painter.paint(gfx, context.getComponent(), h, w);
        } else if (flip) {
          gfx.scale(-1, 1);
          gfx.translate(-w, 0);
          painter.paint(gfx, context.getComponent(), w, h);
        } else {
          painter.paint(gfx, context.getComponent(), w, h);
        }
        gfx.dispose();
        g.drawImage(img, x, y, null);
        img = null;
      }
    }
  }
Пример #5
0
    public void paintIcon(Component c, Graphics g, int x0, int y0) {
      int width = getIconWidth();
      int height = getIconHeight();

      XPStyle xp = XPStyle.getXP();
      if (xp != null) {
        Skin skin = xp.getSkin(c, part);
        AbstractButton b = (AbstractButton) c;
        ButtonModel model = b.getModel();

        // Find out if frame is inactive
        JInternalFrame jif =
            (JInternalFrame) SwingUtilities.getAncestorOfClass(JInternalFrame.class, b);
        boolean jifSelected = (jif != null && jif.isSelected());

        State state;
        if (jifSelected) {
          if (!model.isEnabled()) {
            state = State.DISABLED;
          } else if (model.isArmed() && model.isPressed()) {
            state = State.PUSHED;
          } else if (model.isRollover()) {
            state = State.HOT;
          } else {
            state = State.NORMAL;
          }
        } else {
          if (!model.isEnabled()) {
            state = State.INACTIVEDISABLED;
          } else if (model.isArmed() && model.isPressed()) {
            state = State.INACTIVEPUSHED;
          } else if (model.isRollover()) {
            state = State.INACTIVEHOT;
          } else {
            state = State.INACTIVENORMAL;
          }
        }
        skin.paintSkin(g, 0, 0, width, height, state);
      } else {
        g.setColor(Color.black);
        int x = width / 12 + 2;
        int y = height / 5;
        int h = height - y * 2 - 1;
        int w = width * 3 / 4 - 3;
        int thickness2 = Math.max(height / 8, 2);
        int thickness = Math.max(width / 15, 1);
        if (part == Part.WP_CLOSEBUTTON) {
          int lineWidth;
          if (width > 47) lineWidth = 6;
          else if (width > 37) lineWidth = 5;
          else if (width > 26) lineWidth = 4;
          else if (width > 16) lineWidth = 3;
          else if (width > 12) lineWidth = 2;
          else lineWidth = 1;
          y = height / 12 + 2;
          if (lineWidth == 1) {
            if (w % 2 == 1) {
              x++;
              w++;
            }
            g.drawLine(x, y, x + w - 2, y + w - 2);
            g.drawLine(x + w - 2, y, x, y + w - 2);
          } else if (lineWidth == 2) {
            if (w > 6) {
              x++;
              w--;
            }
            g.drawLine(x, y, x + w - 2, y + w - 2);
            g.drawLine(x + w - 2, y, x, y + w - 2);
            g.drawLine(x + 1, y, x + w - 1, y + w - 2);
            g.drawLine(x + w - 1, y, x + 1, y + w - 2);
          } else {
            x += 2;
            y++;
            w -= 2;
            g.drawLine(x, y, x + w - 1, y + w - 1);
            g.drawLine(x + w - 1, y, x, y + w - 1);
            g.drawLine(x + 1, y, x + w - 1, y + w - 2);
            g.drawLine(x + w - 2, y, x, y + w - 2);
            g.drawLine(x, y + 1, x + w - 2, y + w - 1);
            g.drawLine(x + w - 1, y + 1, x + 1, y + w - 1);
            for (int i = 4; i <= lineWidth; i++) {
              g.drawLine(x + i - 2, y, x + w - 1, y + w - i + 1);
              g.drawLine(x, y + i - 2, x + w - i + 1, y + w - 1);
              g.drawLine(x + w - i + 1, y, x, y + w - i + 1);
              g.drawLine(x + w - 1, y + i - 2, x + i - 2, y + w - 1);
            }
          }
        } else if (part == Part.WP_MINBUTTON) {
          g.fillRect(x, y + h - thickness2, w - w / 3, thickness2);
        } else if (part == Part.WP_MAXBUTTON) {
          g.fillRect(x, y, w, thickness2);
          g.fillRect(x, y, thickness, h);
          g.fillRect(x + w - thickness, y, thickness, h);
          g.fillRect(x, y + h - thickness, w, thickness);
        } else if (part == Part.WP_RESTOREBUTTON) {
          g.fillRect(x + w / 3, y, w - w / 3, thickness2);
          g.fillRect(x + w / 3, y, thickness, h / 3);
          g.fillRect(x + w - thickness, y, thickness, h - h / 3);
          g.fillRect(x + w - w / 3, y + h - h / 3 - thickness, w / 3, thickness);

          g.fillRect(x, y + h / 3, w - w / 3, thickness2);
          g.fillRect(x, y + h / 3, thickness, h - h / 3);
          g.fillRect(x + w - w / 3 - thickness, y + h / 3, thickness, h - h / 3);
          g.fillRect(x, y + h - thickness, w - w / 3, thickness);
        }
      }
    }