elementfactory.java

来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 1,039 行 · 第 1/3 页

JAVA
1,039
字号
      heightValue = "0";
    }
    final float w = ParserUtil.parseRelativeFloat(widthValue, "Element width not specified");
    final float h = ParserUtil.parseRelativeFloat(heightValue, "Element height not specified");
    final Dimension2D minSize = new FloatDimension(w, h);
    band.getStyle().setStyleProperty
        (ElementStyleSheet.MINIMUMSIZE, minSize);

    final FontFactory.FontInformation fi = FontFactory.createFont(attr);
    FontFactory.applyFontInformation(band.getBandDefaults(), fi);

    final String valign = attr.getValue(VALIGNMENT_ATT);
    if (valign != null)
    {
      band.getBandDefaults().setStyleProperty(ElementStyleSheet.VALIGNMENT,
          ReportParserUtil.parseVerticalElementAlignment(valign));
    }
    final String halign = attr.getValue(ALIGNMENT_ATT);
    if (halign != null)
    {
      band.getBandDefaults().setStyleProperty(ElementStyleSheet.ALIGNMENT,
          ReportParserUtil.parseHorizontalElementAlignment(halign));
    }

    currentBand.addElement(band);
    getParser().pushFactory(new ElementFactory(getReportParser(), BAND_TAG, band));
    subbandActive = true;
  }

  /**
   * Create a ImageElement with an static ImageDataSource. The ImageData is read from
   * the supplied URL (attribute "src") in conjunction with the contentbase defined in the
   * ReportDefinitionContentHandler.
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startImageRef(final Attributes atts) throws SAXException
  {
    final String elementName = atts.getValue("name");
    final String elementSource = atts.getValue("src");
    final Boolean elementScale = parseBoolean(atts.getValue("scale"));
    final Boolean elementARatio = parseBoolean(atts.getValue("keepAspectRatio"));
    final Point2D absPos = getElementPosition(atts);
    final Dimension2D minSize = getElementDimension(atts);
    final Boolean elementDynamic = parseBoolean(atts.getValue("dynamic"));

    // Log.debug("Loading: " + getContentBase() + " " + elementSource + " as image");
    final StaticImageURLElementFactory factory = new StaticImageURLElementFactory();
    factory.setName(elementName);
    factory.setScale(elementScale);
    factory.setKeepAspectRatio(elementARatio);
    factory.setAbsolutePosition(absPos);
    factory.setMinimumSize(minSize);
    factory.setDynamicHeight(elementDynamic);
    factory.setBaseURL(getContentBase());
    factory.setContent(elementSource);
    getCurrentBand().addElement(factory.createElement());

  }

  /**
   * Create a ImageElement with an static ImageDataSource. The ImageData is read from
   * the supplied URL (attribute "src") in conjunction with the contentbase defined in the
   * ReportDefintionContentHandler
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startImageField(final Attributes atts) throws SAXException
  {
    final String elementName = atts.getValue(NAME_ATT);
    final String elementSource = atts.getValue(FIELDNAME_ATT);

    final Boolean elementScale = parseBoolean(atts.getValue("scale"));
    final Boolean elementARatio = parseBoolean(atts.getValue("keepAspectRatio"));
    final Point2D absPos = getElementPosition(atts);
    final Dimension2D minSize = getElementDimension(atts);
    final Boolean elementDynamic = parseBoolean(atts.getValue("dynamic"));

    final ImageFieldElementFactory factory = new ImageFieldElementFactory();
    factory.setName(elementName);
    factory.setScale(elementScale);
    factory.setKeepAspectRatio(elementARatio);
    factory.setFieldname(elementSource);
    factory.setAbsolutePosition(absPos);
    factory.setMinimumSize(minSize);
    factory.setDynamicHeight(elementDynamic);
    getCurrentBand().addElement(factory.createElement());

  }

  /**
   * Create a ImageElement with an static ImageDataSource. The ImageData is read from
   * the supplied URL (attribute "src") in conjunction with the contentbase defined in the
   * ReportDefintionContentHandler
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startImageURLField(final Attributes atts) throws SAXException
  {
    final String elementName = atts.getValue(NAME_ATT);
    final String elementSource = atts.getValue(FIELDNAME_ATT);

    final Boolean elementScale = parseBoolean(atts.getValue("scale"));
    final Boolean elementARatio = parseBoolean(atts.getValue("keepAspectRatio"));
    final Point2D absPos = getElementPosition(atts);
    final Dimension2D minSize = getElementDimension(atts);
    final Boolean elementDynamic = parseBoolean(atts.getValue("dynamic"));

    final ImageURLFieldElementFactory factory = new ImageURLFieldElementFactory();
    factory.setName(elementName);
    factory.setScale(elementScale);
    factory.setKeepAspectRatio(elementARatio);
    factory.setFieldname(elementSource);
    factory.setAbsolutePosition(absPos);
    factory.setMinimumSize(minSize);
    factory.setDynamicHeight(elementDynamic);
    getCurrentBand().addElement(factory.createElement());
  }

  /**
   * Starts a drawable field.
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX error.
   */
  private void startDrawableField(final Attributes atts) throws SAXException
  {
    final String elementName = atts.getValue(NAME_ATT);
    final String elementSource = atts.getValue(FIELDNAME_ATT);
    final Point2D absPos = getElementPosition(atts);
    final Dimension2D minSize = getElementDimension(atts);

    final DrawableFieldElementFactory factory = new DrawableFieldElementFactory();
    factory.setFieldname(elementSource);
    factory.setName(elementName);
    factory.setAbsolutePosition(absPos);
    factory.setMinimumSize(minSize);

    getCurrentBand().addElement(factory.createElement());

  }

  /**
   * Creates a LineShapeElement.
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startLine(final Attributes atts) throws SAXException
  {
    final String name = atts.getValue(NAME_ATT);
    final Color c = ParserUtil.parseColor(atts.getValue(COLOR_ATT));
    final float x1 = ParserUtil.parseFloat(atts.getValue("x1"), "Element x1 not specified");
    final float y1 = ParserUtil.parseFloat(atts.getValue("y1"), "Element y1 not specified");
    final float x2 = ParserUtil.parseFloat(atts.getValue("x2"), "Element x2 not specified");
    final float y2 = ParserUtil.parseFloat(atts.getValue("y2"), "Element y2 not specified");

    final Line2D line = new Line2D.Float(x1, y1, x2, y2);
    final ShapeElement element = StaticShapeElementFactory.createLineShapeElement(
        name,
        c,
        ParserUtil.parseStroke(atts.getValue("weight")),
        line);
    getCurrentBand().addElement(element);
  }

  /**
   * Creates a RectangleShapeElement.
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startRectangle(final Attributes atts) throws SAXException
  {
    final String name = atts.getValue(NAME_ATT);
    final Color c = ParserUtil.parseColor(atts.getValue(COLOR_ATT));
    final Rectangle2D rect = ParserUtil.getElementPosition(atts);
    final boolean shouldDraw = ParserUtil.parseBoolean(atts.getValue("draw"), false);
    final boolean shouldFill = ParserUtil.parseBoolean(atts.getValue("fill"), true);

    final ShapeElement element = StaticShapeElementFactory.createRectangleShapeElement(
        name,
        c,
        ParserUtil.parseStroke(atts.getValue("weight")),
        rect, shouldDraw, shouldFill);
    getCurrentBand().addElement(element);
  }

  /**
   * Parses the element position.
   *
   * @param atts the attribute set containing the "x" and "y" attributes.
   * @return the parsed element position, never null.
   * @throws SAXException if parsing the element position failed.
   */
  private Point2D getElementPosition(final Attributes atts) throws SAXException
  {
    final float x = ParserUtil.parseRelativeFloat(atts.getValue("x"),
        "Element x not specified");
    final float y = ParserUtil.parseRelativeFloat(atts.getValue("y"),
        "Element y not specified");
    return new Point2D.Float(x, y);
  }

  /**
   * Parses the element dimension.
   *
   * @param atts the attribute set containing the "width" and "height" attributes.
   * @return the parsed element dimensions, never null.
   * @throws SAXException if parsing the element dimensions failed.
   */
  private Dimension2D getElementDimension(final Attributes atts) throws SAXException
  {
    final float w = ParserUtil.parseRelativeFloat(atts.getValue("width"),
        "Element width not specified");
    final float h = ParserUtil.parseRelativeFloat(atts.getValue("height"),
        "Element height not specified");
    return new FloatDimension(w, h);
  }

  /**
   * Parses a simple font style for text elements. These styles contain "bold", "italic"
   * and "bold-italic". The style constants are included for compatibility with older
   * releases and should no longer be used. Use the boolean flags instead.
   *
   * @param fontStyle the font style string.
   * @param target the text element factory that should receive the parsed values.
   */
  private void parseSimpleFontStyle
      (final String fontStyle, final TextElementFactory target)
  {
    if (fontStyle != null)
    {
      if (fontStyle.equals("bold"))
      {
        target.setBold(Boolean.TRUE);
        target.setItalic(Boolean.FALSE);
      }
      else if (fontStyle.equals("italic"))
      {
        target.setBold(Boolean.FALSE);
        target.setItalic(Boolean.TRUE);
      }
      else if (fontStyle.equals("bold-italic"))
      {
        target.setBold(Boolean.TRUE);
        target.setItalic(Boolean.TRUE);
      }
      else if (fontStyle.equals("plain"))
      {
        target.setBold(Boolean.FALSE);
        target.setItalic(Boolean.FALSE);
      }
    }
  }

  /**
   * Translates an boolean string ("true" or "false") into the corresponding
   * Boolean object.
   *
   * @param value the string that represents the boolean.
   * @return Boolean.TRUE or Boolean.FALSE
   * @throws SAXException if an parse error occured.
   */
  private Boolean parseBoolean(final String value) throws SAXException
  {
    if (value == null)
    {
      return null;
    }
    if (value.equals("true"))
    {
      return Boolean.TRUE;
    }
    else if (value.equals("false"))
    {
      return Boolean.FALSE;
    }
    throw new SAXException("Failed to parse value: Expected 'true' or 'false'");
  }

  /**
   * Creates a RectangleShapeElement.
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startShapeField(final Attributes atts) throws SAXException
  {
    final String name = atts.getValue(NAME_ATT);
    final String elementSource = atts.getValue(FIELDNAME_ATT);
    final Color c = ParserUtil.parseColor(atts.getValue(COLOR_ATT));
    final Point2D absPos = getElementPosition(atts);
    final Dimension2D minSize = getElementDimension(atts);
    final Boolean shouldDraw = parseBoolean(atts.getValue("draw"));
    final Boolean shouldFill = parseBoolean(atts.getValue("fill"));
    final Boolean scale = parseBoolean(atts.getValue("keepAspectRatio"));
    final Boolean kar = parseBoolean(atts.getValue("scale"));
    final Boolean dynamic = parseBoolean(atts.getValue("dynamic"));
    final Stroke stroke = ParserUtil.parseStroke(atts.getValue("weight"));

    final ShapeFieldElementFactory factory = new ShapeFieldElementFactory();
    factory.setName(name);
    factory.setAbsolutePosition(absPos);
    factory.setMinimumSize(minSize);
    factory.setColor(c);
    factory.setFieldname(elementSource);
    factory.setShouldDraw(shouldDraw);
    factory.setShouldFill(shouldFill);
    factory.setScale(scale);
    factory.setKeepAspectRatio(kar);
    factory.setStroke(stroke);
    factory.setDynamicHeight(dynamic);
    getCurrentBand().addElement(factory.createElement());
  }

  /**
   * Creates a label element, an text element with an static datasource attached.
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startLabel(final Attributes atts) throws SAXException
  {
    textElementFactory = new LabelElementFactory();
    getTextElementAttributes(atts, textElementFactory);
    clearCurrentText();
  }

  /**
   * Creates a text element. In ancient times there was a difference between string elements
   * (single line) and multiline fields. This is resolved in the text element class which
   * handles all cases of printing text in reports.
   *
   * @param atts  the attributes.
   *

⌨️ 快捷键说明

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