public void setPlotBackground(final Node plotBackgroundNode) {
   if (plotBackgroundNode != null) {
     Node backgroundTypeNode =
         plotBackgroundNode.selectSingleNode(ChartDefinition.BACKGROUND_TYPE_ATTRIBUTE_NAME);
     if (backgroundTypeNode != null) {
       String backgroundTypeStr = backgroundTypeNode.getText();
       if (ChartDefinition.COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
         setPlotBackgroundPaint(JFreeChartEngine.getPaint(plotBackgroundNode));
         setPlotBackgroundImage((Image) null);
       } else if (ChartDefinition.IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
         setPlotBackgroundImage(plotBackgroundNode);
         setPlotBackgroundPaint(null);
       } else if (ChartDefinition.TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
         setPlotBackgroundPaint(
             JFreeChartEngine.getTexturePaint(
                 plotBackgroundNode, getWidth(), getHeight(), getSession()));
         setPlotBackgroundImage((Image) null);
       } else if (ChartDefinition.GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
         setPlotBackgroundPaint(
             JFreeChartEngine.getGradientPaint(plotBackgroundNode, getWidth(), getHeight()));
         setPlotBackgroundImage((Image) null);
       }
     }
   }
 }
 public void setPaintSequence(final Node paletteNode) {
   if (paletteNode != null) {
     List colorNodes = paletteNode.selectNodes(ChartDefinition.COLOR_NODE_NAME);
     Paint[] paints = new Paint[colorNodes.size()];
     for (int i = 0; i < colorNodes.size(); i++) {
       paints[i] = JFreeChartEngine.getPaint((Node) colorNodes.get(i));
     }
     setPaintSequence(paints);
   }
 }
  /**
   * Create a dial image.
   *
   * <ul>
   *   <li>Load the specified XML document describing the dial definition
   *   <li>Create a dial definition object from the XML definition
   *   <li>Use the JFreeChartEngine to create a dial image
   *   <li>Create an XML document describing the dial
   *   <li>Return the XML document
   * </ul>
   *
   * @return The XML document describing this dial
   */
  @Override
  public Document getXmlContent() {

    WidgetDefinition widget = null;
    if (type == DashboardWidgetComponent.TYPE_DIAL) {

      // load the XML document that defines the dial
      IActionSequenceResource resource =
          new ActionSequenceResource(
              title,
              IActionSequenceResource.SOLUTION_FILE_RESOURCE,
              "text/xml", //$NON-NLS-1$
              definitionPath);
      Document dialDefinition = null;
      try {
        org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader();
        reader.setEntityResolver(new SolutionURIResolver());
        dialDefinition =
            reader.read(
                resource.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale()));
      } catch (Throwable t) {
        // XML document can't be read. We'll just return a null document.
      }

      if (dialDefinition == null) {
        error(
            Messages.getInstance()
                .getErrorString(
                    "Widget.ERROR_0002_INVALID_RESOURCE", definitionPath)); // $NON-NLS-1$
        return null;
      }
      // create a dial definition from the XML definition
      widget = new DialWidgetDefinition(dialDefinition, 0, width, height, getSession());

      if (widget != null) {
        // set the value to be displayed on the dial
        widget.setValue(new Double(value));
      }
    }
    /*
     * else if( type == TYPE_THERMOMETER ) { // load the XML document that
     * defines the thermometer
     *
     * ActionResource resource = new ActionResource( title,
     * IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$
     * PentahoSystem.getApplicationContext().getSolutionPath( definitionPath ) );
     * //$NON-NLS-1$ Document thermometerDefinition = null; try {
     * thermometerDefinition = PentahoSystem.getResourceAsDocument( resource ); }
     * catch (IOException e) {} // create a dial definition from the XML
     * definition widget = createThermometer( thermometerDefinition );
     *
     * if( widget != null ) { // set the value to be displayed on the dial
     * widget.setValue( new Double(value) ); // Set the XSL file to be used
     * to generate the HTML setXsl( "text/html", "DialWidget.xsl" );
     * //$NON-NLS-1$ //$NON-NLS-2$ } else { error(
     * Messages.getInstance().getString("Widget.ERROR_0001_COULD_NOT_CREATE") );
     * //$NON-NLS-1$ return null; } }
     */
    if (widget == null) {
      error(Messages.getInstance().getString("Widget.ERROR_0001_COULD_NOT_CREATE")); // $NON-NLS-1$
      return null;
    }
    // create an image for the dial using the JFreeChart engine
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    // create temporary file names
    String solutionDir = "system/tmp/"; // $NON-NLS-1$
    String fileNamePrefix = "tmp_pie_"; // $NON-NLS-1$
    String extension = ".png"; // $NON-NLS-1$
    String fileName = null;
    String filePathWithoutExtension = null;
    try {
      File file =
          PentahoSystem.getApplicationContext()
              .createTempFile(getSession(), fileNamePrefix, extension, true);
      fileName = file.getName();
      filePathWithoutExtension = solutionDir + fileName.substring(0, fileName.indexOf('.'));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    String dialTitle = ""; // $NON-NLS-1$
    JFreeChartEngine.saveChart(
        widget,
        dialTitle,
        units,
        filePathWithoutExtension,
        width,
        height,
        JFreeChartEngine.OUTPUT_PNG,
        printWriter,
        this);

    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    setXslProperty(
        "baseUrl", requestContext.getContextPath()); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty(
        "fullyQualifiedServerUrl",
        PentahoSystem.getApplicationContext()
            .getFullyQualifiedServerURL()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Element root = result.addElement("widget"); // $NON-NLS-1$
    root.addElement("title").setText(title); // $NON-NLS-1$
    root.addElement("units").setText(units); // $NON-NLS-1$
    root.addElement("width").setText(Integer.toString(width)); // $NON-NLS-1$
    root.addElement("height").setText(Integer.toString(height)); // $NON-NLS-1$
    Element valueNode = root.addElement("value"); // $NON-NLS-1$
    valueNode.setText(Double.toString(value));
    valueNode.addAttribute(
        "in-image", Boolean.toString(widget.getValueFont() != null)); // $NON-NLS-1$
    root.addElement("image").setText(fileName); // $NON-NLS-1$
    return result;
  }
 public void setRangeTitleFont(final Node titleFontNode) {
   Font font = JFreeChartEngine.getFont(titleFontNode);
   if (font != null) {
     setRangeTitleFont(font);
   }
 }
 public void setPlotBackgroundImage(final Node plotBackgroundImageNode) {
   setPlotBackgroundImage(JFreeChartEngine.getImage(plotBackgroundImageNode, getSession()));
 }
  private void setChartAttributes(final Node chartAttributes) {
    if (chartAttributes == null) {
      return;
    }
    // get the chart type from the chart node -- this overrides the current
    // chart type
    setChartType(chartAttributes.selectSingleNode(ChartDefinition.TYPE_NODE_NAME));

    // set the chart background
    setChartBackground(
        chartAttributes.selectSingleNode(ChartDefinition.CHART_BACKGROUND_NODE_NAME));

    // set the plot background
    setPlotBackground(chartAttributes.selectSingleNode(ChartDefinition.PLOT_BACKGROUND_NODE_NAME));

    // set the orientation
    setOrientation(chartAttributes.selectSingleNode(XYChartDefinition.ORIENTATION_NODE_NAME));

    // do we want a legend
    setLegendIncluded(chartAttributes.selectSingleNode(ChartDefinition.INCLUDE_LEGEND_NODE_NAME));

    // get the chart title
    setTitle(chartAttributes.selectSingleNode(ChartDefinition.TITLE_NODE_NAME));

    // A list of <subtitle> nodes should not be allowed to exist as a child of the main XML element
    // (for XML schema to
    // be well constructed and validate the XML .
    // We have deprecated <subtitle> as a child of the main node , and now require a <subtitles>
    // parent node
    // under which <subtitle> can exist.

    List subtitles = chartAttributes.selectNodes(ChartDefinition.SUBTITLE_NODE_NAME);

    if ((subtitles == null) || (subtitles.isEmpty())) {
      Node subTitlesNode = chartAttributes.selectSingleNode(ChartDefinition.SUBTITLES_NODE_NAME);
      if (subTitlesNode != null) {
        subtitles = subTitlesNode.selectNodes(ChartDefinition.SUBTITLE_NODE_NAME);
      }
    } else {
      // log a deprecation warning for this property...
      getLogger()
          .warn(
              Messages.getInstance()
                  .getString(
                      "CHART.WARN_DEPRECATED_CHILD",
                      ChartDefinition.SUBTITLE_NODE_NAME,
                      ChartDefinition.SUBTITLES_NODE_NAME)); // $NON-NLS-1$
      getLogger()
          .warn(
              Messages.getInstance()
                  .getString(
                      "CHART.WARN_PROPERTY_WILL_NOT_VALIDATE",
                      ChartDefinition.SUBTITLE_NODE_NAME)); // $NON-NLS-1$
    }

    if (subtitles != null) {
      addSubTitles(subtitles);
    }

    // get the paint sequence
    setPaintSequence(chartAttributes.selectSingleNode(ChartDefinition.PALETTE_NODE_NAME));
    Node backgroundAlphaNode =
        chartAttributes.selectSingleNode(ChartDefinition.BACKGROUND_ALPHA_NODE_NAME);
    Node foregroundAlphaNode =
        chartAttributes.selectSingleNode(ChartDefinition.FOREGROUND_ALPHA_NODE_NAME);

    if (backgroundAlphaNode != null) {
      setBackgroundAlpha(
          chartAttributes.selectSingleNode(ChartDefinition.BACKGROUND_ALPHA_NODE_NAME));
    }
    if (foregroundAlphaNode != null) {
      setForegroundAlpha(
          chartAttributes.selectSingleNode(ChartDefinition.FOREGROUND_ALPHA_NODE_NAME));
    }
    // get the stacked value
    setStacked(chartAttributes.selectSingleNode(ChartDefinition.STACKED_NODE_NAME));

    // get the 3D value
    setThreeD(chartAttributes.selectSingleNode(ChartDefinition.THREED_NODE_NAME));

    // set the width
    setWidth(chartAttributes.selectSingleNode(ChartDefinition.WIDTH_NODE_NAME));

    // set the height
    setHeight(chartAttributes.selectSingleNode(ChartDefinition.HEIGHT_NODE_NAME));

    // set the dot width
    setDotWidth(chartAttributes.selectSingleNode(ChartDefinition.DOT_WIDTH_NODE_NAME));

    // set the dot height
    setDotHeight(chartAttributes.selectSingleNode(ChartDefinition.DOT_HEIGHT_NODE_NAME));

    // set vertical tick labels flag
    setDomainVerticalTickLabels(
        chartAttributes.selectSingleNode(XYChartDefinition.DOMAIN_VERTICAL_TICK_LABELS_NODE_NAME));

    // set the border on or off
    setBorderVisible(
        chartAttributes.selectSingleNode(ChartDefinition.CHART_BORDER_VISIBLE_NODE_NAME));

    // set the border Paint
    setBorderPaint(
        JFreeChartEngine.getPaint(
            chartAttributes.selectSingleNode(XYChartDefinition.CHART_BORDER_PAINT_NODE_NAME)));

    // set the title location
    setTitlePosition(chartAttributes.selectSingleNode(ChartDefinition.TITLE_POSITION_NODE_NAME));

    // set the legend location
    setLegendPosition(chartAttributes.selectSingleNode(ChartDefinition.LEGEND_POSITION_NODE_NAME));

    // set the title font
    setTitleFont(chartAttributes.selectSingleNode(ChartDefinition.TITLE_FONT_NODE_NAME));

    // set the domain title
    setDomainTitle(chartAttributes.selectSingleNode(XYChartDefinition.DOMAIN_TITLE_NODE_NAME));

    // set the domain font
    setDomainTitleFont(
        chartAttributes.selectSingleNode(XYChartDefinition.DOMAIN_TITLE_FONT_NODE_NAME));

    // set the range title
    setRangeTitle(chartAttributes.selectSingleNode(XYChartDefinition.RANGE_TITLE_NODE_NAME));

    // the the range font
    setRangeTitleFont(
        chartAttributes.selectSingleNode(XYChartDefinition.RANGE_TITLE_FONT_NODE_NAME));

    // set the range minimum
    setRangeMinimum(chartAttributes.selectSingleNode(XYChartDefinition.RANGE_MINIMUM_NODE_NAME));

    // set the range minimum
    setRangeMaximum(chartAttributes.selectSingleNode(XYChartDefinition.RANGE_MAXIMUM_NODE_NAME));

    // set the date minimum
    setDateMinimum(
        chartAttributes.selectSingleNode(
            TimeSeriesCollectionChartDefinition.DATE_MINIMUM_NODE_NAME));

    // set the date minimum
    setDateMaximum(
        chartAttributes.selectSingleNode(
            TimeSeriesCollectionChartDefinition.DATE_MAXIMUM_NODE_NAME));

    // set the Period type
    setDomainPeriodType(
        chartAttributes.selectSingleNode(XYChartDefinition.DOMAIN_PERIOD_TYPE_NODE_NAME));

    // set the line style
    setLineStyle(chartAttributes.selectSingleNode(ChartDefinition.LINE_STYLE_NODE_NAME));

    // set the line width
    setLineWidth(chartAttributes.selectSingleNode(ChartDefinition.LINE_WIDTH_NODE_NAME));

    // set the marker visibility
    setMarkersVisible(chartAttributes.selectSingleNode(ChartDefinition.MARKER_VISIBLE_NODE_NAME));

    // set legend font
    setLegendFont(chartAttributes.selectSingleNode(ChartDefinition.LEGEND_FONT_NODE_NAME));

    // set legend border visible
    setLegendBorderVisible(
        chartAttributes.selectSingleNode(ChartDefinition.DISPLAY_LEGEND_BORDER_NODE_NAME));

    setTooltipContent(
        chartAttributes.selectSingleNode(XYChartDefinition.TOOLTIP_CONTENT_NODE_NAME));

    setTooltipYFormat(
        chartAttributes.selectSingleNode(XYChartDefinition.TOOLTIP_Y_FORMAT_NODE_NAME));

    setTooltipXFormat(
        chartAttributes.selectSingleNode(XYChartDefinition.TOOLTIP_X_FORMAT_NODE_NAME));
  }
 public void setLegendFont(final Node legendFontNode) {
   Font font = JFreeChartEngine.getFont(legendFontNode);
   if (font != null) {
     setLegendFont(font);
   }
 }
  @Override
  public Document getXmlContent() {

    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    setXslProperty(
        "baseUrl", requestContext.getContextPath()); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty(
        "fullyQualifiedServerUrl",
        PentahoSystem.getApplicationContext()
            .getFullyQualifiedServerURL()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String mapName = "chart" + AbstractChartComponent.chartCount++; // $NON-NLS-1$
    Document chartDefinition =
        jcrHelper.getSolutionDocument(definitionPath, RepositoryFilePermission.READ);

    if (chartDefinition == null) {
      Element errorElement = result.addElement("error"); // $NON-NLS-1$
      errorElement
          .addElement("title")
          .setText(
              Messages.getInstance()
                  .getString(
                      "ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
                                                                                     // //$NON-NLS-2$
      String message =
          Messages.getInstance()
              .getString(
                  "CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", definitionPath); // $NON-NLS-1$
      errorElement.addElement("message").setText(message); // $NON-NLS-1$
      error(message);
      return result;
    }
    // create a pie definition from the XML definition
    dataDefinition = createChart(chartDefinition);

    if (dataDefinition == null) {
      Element errorElement = result.addElement("error"); // $NON-NLS-1$
      errorElement
          .addElement("title")
          .setText(
              Messages.getInstance()
                  .getString(
                      "ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
                                                                                     // //$NON-NLS-2$
      String message =
          Messages.getInstance()
              .getString(
                  "CHARTS.ERROR_0002_CHART_DATA_MISSING",
                  actionPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      errorElement.addElement("message").setText(message); // $NON-NLS-1$
      // System .out.println( result.asXML() );
      return result;
    }

    // create an image for the dial using the JFreeChart engine
    PrintWriter printWriter = new PrintWriter(new StringWriter());
    // we'll dispay the title in HTML so that the dial image does not have
    // to
    // accommodate it
    String chartTitle = ""; // $NON-NLS-1$
    try {
      if (width == -1) {
        width =
            Integer.parseInt(
                chartDefinition.selectSingleNode("/chart/width").getText()); // $NON-NLS-1$
      }
      if (height == -1) {
        height =
            Integer.parseInt(
                chartDefinition.selectSingleNode("/chart/height").getText()); // $NON-NLS-1$
      }
    } catch (Exception e) {
      // go with the default
    }
    if (chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME)
        != null) { //$NON-NLS-1$
      urlTemplate =
          chartDefinition
              .selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME)
              .getText(); //$NON-NLS-1$
    }

    if (chartDefinition.selectSingleNode("/chart/paramName") != null) { // $NON-NLS-1$
      paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); // $NON-NLS-1$
    }

    Element root = result.addElement("charts"); // $NON-NLS-1$
    TimeSeriesCollection chartDataDefinition = (TimeSeriesCollection) dataDefinition;
    if (chartDataDefinition.getSeriesCount() > 0) {
      // create temporary file names
      String[] tempFileInfo = createTempFile();
      String fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX];
      String filePathWithoutExtension =
          tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX];

      ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
      JFreeChartEngine.saveChart(
          chartDataDefinition,
          chartTitle,
          "",
          filePathWithoutExtension,
          width,
          height,
          JFreeChartEngine.OUTPUT_PNG,
          printWriter,
          info,
          this); //$NON-NLS-1$
      applyOuterURLTemplateParam();
      populateInfo(info);
      Element chartElement = root.addElement("chart"); // $NON-NLS-1$
      chartElement.addElement("mapName").setText(mapName); // $NON-NLS-1$
      chartElement.addElement("width").setText(Integer.toString(width)); // $NON-NLS-1$
      chartElement.addElement("height").setText(Integer.toString(height)); // $NON-NLS-1$
      for (int row = 0; row < chartDataDefinition.getSeriesCount(); row++) {
        for (int column = 0; column < chartDataDefinition.getItemCount(row); column++) {
          Number value = chartDataDefinition.getY(row, column);
          Comparable rowKey = chartDataDefinition.getSeriesKey(row);
          RegularTimePeriod columnKey = chartDataDefinition.getSeries(row).getTimePeriod(column);
          Element valueElement = chartElement.addElement("value2D"); // $NON-NLS-1$
          valueElement.addElement("value").setText(value.toString()); // $NON-NLS-1$
          valueElement.addElement("row-key").setText(rowKey.toString()); // $NON-NLS-1$
          valueElement.addElement("column-key").setText(columnKey.toString()); // $NON-NLS-1$
        }
      }
      String mapString = ImageMapUtilities.getImageMap(mapName, info);
      chartElement.addElement("imageMap").setText(mapString); // $NON-NLS-1$
      chartElement.addElement("image").setText(fileName); // $NON-NLS-1$
    }
    return result;
  }