Example #1
0
  /** Constructs a fill context. */
  public JRFillContext(JasperReportsContext jasperReportsContext) {
    this.jasperReportsContext = jasperReportsContext;

    loadedImages = new HashMap<Object, JRPrintImage>();
    loadedSubreports = new HashMap<Object, JasperReport>();
    loadedTemplates = new HashMap<Object, JRTemplate>();
    deduplicableRegistry = new DeduplicableRegistry();

    JRFontUtil.resetThreadMissingFontsCache();
  }
    public void animatorFinished(AnimatorEvent arg0) {
      animations--;
      if (animations == 0) {
        for (Iterator iter = getNewSizes().keySet().iterator(); iter.hasNext(); ) {
          JRDesignElement element = (JRDesignElement) iter.next();
          Rectangle rectangle = getNewSizes().get(element);
          element.setX(rectangle.x);
          element.setY(rectangle.y);
          element.setWidth(rectangle.width);
          element.setHeight(rectangle.height);

          // Calculate the best font height...
          if (element instanceof JRDesignTextElement) {
            JRStyledTextParser styledTextParser = JRStyledTextParser.getInstance();
            JRDesignTextElement dte = (JRDesignTextElement) element;
            dte.setFontSize(null);
            for (int i = dte.getFontSize() - 1; i > 1; --i) {
              String text = "test";

              // Convert the element in a print element...
              Map<Attribute, Object> attributes =
                  JRFontUtil.getAttributes(new HashMap(), dte, Locale.getDefault());

              JRStyledText styledText =
                  styledTextParser.getStyledText(
                      attributes,
                      text,
                      JRCommonText.MARKUP_STYLED_TEXT.equals(
                          dte
                              .getMarkup()), // FIXMEMARKUP only static styled text appears on
                                             // preview. no other markup
                      Locale.getDefault());

              JasperReportsContext context = IRLocalJasperReportsContext.getInstance();

              JRTextMeasurerUtil measurerUtil = JRTextMeasurerUtil.getInstance(context);
              JRTextMeasurer measurer = measurerUtil.createTextMeasurer(dte);
              JRMeasuredText measuredText = measurer.measure(styledText, 0, dte.getHeight(), true);

              if (measuredText.getTextHeight() > dte.getHeight()) {
                if (i > 1) {
                  dte.setFontSize(i);
                } else {
                  break;
                }
              } else {
                break;
              }
            }
          }
        }
      }
    }
  protected Map getStyledTextAttributes() {
    JRStyle style = getStyle();
    Map styledTextAttributes = (Map) styledTextAttributesMap.get(style);
    if (styledTextAttributes == null) {
      styledTextAttributes = new HashMap();
      JRFontUtil.getAttributes(styledTextAttributes, this, filler.getLocale());
      styledTextAttributes.put(TextAttribute.FOREGROUND, getForecolor());
      if (getMode() == JRElement.MODE_OPAQUE) {
        styledTextAttributes.put(TextAttribute.BACKGROUND, getBackcolor());
      }
      styledTextAttributesMap.put(style, styledTextAttributes);
    }

    return styledTextAttributes;
  }
  public static Renderable evaluateRenderable(
      JasperReportsContext jasperReportsContext,
      JRComponentElement element,
      SpiderChartSharedBean spiderchartBean,
      ChartCustomizer chartCustomizer,
      String defaultRenderType,
      String datasetType) {
    SpiderChartComponent chartComponent = (SpiderChartComponent) element.getComponent();
    ChartSettings chartSettings = chartComponent.getChartSettings();
    SpiderPlot plot = (SpiderPlot) chartComponent.getPlot();

    DefaultCategoryDataset dataset = null;
    StandardCategoryItemLabelGenerator labelGenerator = null;

    if (FILL_DATASET.equals(datasetType)) {
      dataset = ((FillSpiderDataset) spiderchartBean.getDataset()).getCustomDataset();
      labelGenerator = ((FillSpiderDataset) spiderchartBean.getDataset()).getLabelGenerator();
    } else {
      dataset = getSampleDataset();
      labelGenerator = new StandardCategoryItemLabelGenerator();
    }

    SpiderWebPlot spiderWebPlot = new SpiderWebPlot(dataset);

    if (plot.getAxisLineColor() != null) {
      spiderWebPlot.setAxisLinePaint(plot.getAxisLineColor());
    }
    if (plot.getAxisLineWidth() != null) {
      spiderWebPlot.setAxisLineStroke(new BasicStroke(plot.getAxisLineWidth()));
    }
    if (plot.getBackcolor() != null) {
      spiderWebPlot.setBackgroundPaint(plot.getBackcolor());
    }
    if (plot.getBackgroundAlpha() != null) {
      spiderWebPlot.setBackgroundAlpha(plot.getBackgroundAlpha());
    }
    if (plot.getForegroundAlpha() != null) {
      spiderWebPlot.setForegroundAlpha(plot.getForegroundAlpha());
    }
    if (plot.getHeadPercent() != null) {
      spiderWebPlot.setHeadPercent(plot.getHeadPercent());
    }
    if (plot.getInteriorGap() != null) {
      spiderWebPlot.setInteriorGap(plot.getInteriorGap());
    }
    if (plot.getLabelColor() != null) {
      spiderWebPlot.setLabelPaint(plot.getLabelColor());
    }
    if (plot.getLabelFont() != null) {
      spiderWebPlot.setLabelFont(JRFontUtil.getAwtFont(plot.getLabelFont(), Locale.getDefault()));
    }
    if (plot.getLabelGap() != null) {
      spiderWebPlot.setAxisLabelGap(plot.getLabelGap());
    }
    if (spiderchartBean.getMaxValue() != null) {
      spiderWebPlot.setMaxValue(spiderchartBean.getMaxValue());
    }
    if (plot.getRotation() != null) {
      spiderWebPlot.setDirection(plot.getRotation().getRotation());
    }
    if (plot.getStartAngle() != null) {
      spiderWebPlot.setStartAngle(plot.getStartAngle());
    }
    if (plot.getTableOrder() != null) {
      spiderWebPlot.setDataExtractOrder(plot.getTableOrder().getOrder());
    }
    if (plot.getWebFilled() != null) {
      spiderWebPlot.setWebFilled(plot.getWebFilled());
    }

    spiderWebPlot.setToolTipGenerator(new StandardCategoryToolTipGenerator());
    spiderWebPlot.setLabelGenerator(labelGenerator);

    Font titleFont =
        chartSettings.getTitleFont() != null
            ? JRFontUtil.getAwtFont(chartSettings.getTitleFont(), Locale.getDefault())
            : TextTitle.DEFAULT_FONT;

    String titleText = spiderchartBean.getTitleText();

    JFreeChart jfreechart = new JFreeChart(titleText, titleFont, spiderWebPlot, true);

    Color backcolor =
        chartSettings.getBackcolor() != null
            ? chartSettings.getBackcolor()
            : element.getBackcolor();
    if (backcolor != null) {
      jfreechart.setBackgroundPaint(backcolor);
    }

    RectangleEdge titleEdge = getEdge(chartSettings.getTitlePosition(), RectangleEdge.TOP);

    if (titleText != null) {
      TextTitle title = jfreechart.getTitle();
      title.setText(titleText);
      if (chartSettings.getTitleColor() != null) {
        title.setPaint(chartSettings.getTitleColor());
      }

      title.setFont(titleFont);
      title.setPosition(titleEdge);
      jfreechart.setTitle(title);
    }

    String subtitleText = spiderchartBean.getSubtitleText();
    if (subtitleText != null) {
      TextTitle subtitle = new TextTitle(subtitleText);
      subtitle.setText(subtitleText);
      if (chartSettings.getSubtitleColor() != null) {
        subtitle.setPaint(chartSettings.getSubtitleColor());
      }

      if (chartSettings.getSubtitleColor() != null) {
        Font subtitleFont =
            chartSettings.getSubtitleFont() != null
                ? JRFontUtil.getAwtFont(chartSettings.getSubtitleFont(), Locale.getDefault())
                : TextTitle.DEFAULT_FONT;
        subtitle.setFont(subtitleFont);
      }

      subtitle.setPosition(titleEdge);

      jfreechart.addSubtitle(subtitle);
    }

    // Apply all of the legend formatting options
    LegendTitle legend = jfreechart.getLegend();

    if (legend != null) {
      legend.setVisible((chartSettings.getShowLegend() == null || chartSettings.getShowLegend()));
      if (legend.isVisible()) {
        if (chartSettings.getLegendColor() != null) {
          legend.setItemPaint(chartSettings.getLegendColor());
        }
        if (chartSettings.getLegendBackgroundColor() != null) {
          legend.setBackgroundPaint(chartSettings.getLegendBackgroundColor());
        }

        if (chartSettings.getLegendFont() != null) {
          legend.setItemFont(
              JRFontUtil.getAwtFont(chartSettings.getLegendFont(), Locale.getDefault()));
        }
        legend.setPosition(getEdge(chartSettings.getLegendPosition(), RectangleEdge.BOTTOM));
      }
    }

    String renderType =
        chartSettings.getRenderType() == null ? defaultRenderType : chartSettings.getRenderType();
    Rectangle2D rectangle = new Rectangle2D.Double(0, 0, element.getWidth(), element.getHeight());

    if (chartCustomizer != null) {
      chartCustomizer.customize(jfreechart, chartComponent);
    }

    return ChartUtil.getInstance(jasperReportsContext)
        .getChartRenderableFactory(renderType)
        .getRenderable(
            jasperReportsContext, jfreechart, spiderchartBean.getHyperlinkProvider(), rectangle);
  }