[Schmitzm-commits] r252 - in trunk/src/schmitzm: jfree/chart/style xml

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 31 14:14:27 CEST 2009


Author: mojays
Date: 2009-07-31 14:14:26 +0200 (Fri, 31 Jul 2009)
New Revision: 252

Modified:
   trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
   trunk/src/schmitzm/xml/XMLUtil.java
Log:
revert lost changes in ChartStyleXMLFactory
XMLUtil.setAttribute(..): color to hex conversion by Long

Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java	2009-07-31 11:59:50 UTC (rev 251)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java	2009-07-31 12:14:26 UTC (rev 252)
@@ -43,6 +43,7 @@
 import java.util.List;
 import java.util.Random;
 
+import org.jdom.Document;
 import org.jdom.Element;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.ui.RectangleInsets;
@@ -55,9 +56,6 @@
  * @version 1.0
  */
 public class ChartStyleXMLFactory<E extends ChartStyle> {
-  /** URL for XML schema */
-//  public static final String SCHEMA_URI = "http://www.wikisquare.de/AtlasML";
-
   /**
    * Reads a chart definition from XML element. The chart style ID
    * is taken from "id" attribute.
@@ -365,12 +363,12 @@
    */
   public void writeStyleToFile(E style, String rootElementName, File filePath) throws IOException {
     OutputStreamWriter out = new OutputStreamWriter(
-				new FileOutputStream(filePath), "utf-8");
+        new FileOutputStream(filePath), "utf-8");
     // Create XML-Document
-    XMLUtil.XML_OUTPUTTER.output( 
-        new org.jdom.Document(createElementFromStyle(style, rootElementName)),
-        out
-    );
+    Element  rootElem = createElementFromStyle(style, rootElementName);
+    Document document = new Document(rootElem); 
+    // Write document
+    XMLUtil.XML_OUTPUTTER.output(document,out);
     out.flush();
     out.close();
 
@@ -386,7 +384,6 @@
     if ( rootElementName == null )
       rootElementName = "ChartStyle";
     Element root = new Element(rootElementName);
-//    Element root = new Element(rootElementName,SCHEMA_URI);
     // write common attributes
     root.setAttribute("id", style.getID());
     root.setAttribute("type", style.getType().toString());
@@ -401,19 +398,16 @@
       root.setAttribute("orientation", "vertical" );
     
     // Element for background
-    if ( style.getBackground() != null )
-      addChildToElement(root, "background", "paint", style.getBackground() );
+    addChildToElement(root, "background", false, "paint", style.getBackground() );
     
     // Element for border
-    addChildToElement(root, "border", "visible", style.isBorderVisible() );
+    addChildToElement(root, "border", false, "visible", style.isBorderVisible() );
     
     // Chart title
-    if ( style.getTitleStyle() != null )
-      addLabelChildToElement(root, "title", style.getTitleStyle() );
+    addLabelChildToElement(root, "title", style.getTitleStyle() );
 
     // Chart description
-    if ( style.getDescStyle() != null )
-      addLabelChildToElement(root, "desc", style.getDescStyle() );
+    addLabelChildToElement(root, "desc", style.getDescStyle() );
     
     // Axis
     if ( style.getAxisCount() > 0 )
@@ -428,11 +422,33 @@
       addRendererChildToElement(root, style.getRendererStyle(i));
     
     // Plot
-    Element plotElem = addChildToElement(root, "plot");
-
+    if ( style.getPlotStyle() != null ) {
+      Element        plotElem  = addChildToElement(root, "plot");
+      ChartPlotStyle plotStyle = style.getPlotStyle();
+      addChildToElement(plotElem, "foreground", false,
+          "alpha", plotStyle.getForegroundAlpha()
+      );
+      addChildToElement(plotElem, "background", false,
+          "alpha", plotStyle.getBackgroundAlpha(),
+          "paint", plotStyle.getBackgroundPaint()
+      );
+      if ( plotStyle.getInsets() != null )
+        addChildToElement(plotElem, "insets", false,
+            "top",    plotStyle.getInsets().getTop(),
+            "bottom", plotStyle.getInsets().getBottom(),
+            "left",   plotStyle.getInsets().getLeft(),
+            "right",  plotStyle.getInsets().getRight()
+        );
+      addChildToElement(plotElem, "domainGridline", false,
+          "visible", plotStyle.isDomainGridlineVisible(),
+          "paint",   plotStyle.getDomainGridlinePaint()
+      );
+      addChildToElement(plotElem, "rangeGridline", false,
+          "visible", plotStyle.isRangeGridlineVisible(),
+          "paint",   plotStyle.getRangeGridlinePaint()
+      );
+    }    
     
-    // TODO: <renderer> + <plot>
-    
     // Type specific style properties
     if ( style instanceof BasicChartStyle ) {
       root.setAttribute("stepped", String.valueOf( ((BasicChartStyle)style).isStepped() ));
@@ -446,26 +462,44 @@
   }
   
   /**
+   * Creates an child element with no attributes. If an element is given,
+   * the new child is automatically added.
+   * @param element    if not {@code null} the new element is added as child to
+   *                   this element
+   * @param childName  name of the new child
+   * @return the new child element
+   */
+  public static Element addChildToElement(Element element, String childName) {
+    return addChildToElement(element,childName,true);
+  }
+  
+  /**
    * Creates an child element and sets some attributes. If an element is given,
-   * the new child is automatically added. 
+   * the new child is automatically added.
    * @param element    if not {@code null} the new element is added as child to
    *                   this element
    * @param childName  name of the new child
+   * @param inclNullAttr if {@code false} attributes with value {@code null}
+   *                     are not set; if ALL attributes are {@code null} also
+   *                     the child is not created!
    * @param attributes attribute/value pairs for the new child element
    * @return the new child element
    */
-  public static Element addChildToElement(Element element, String childName, Object... attributes) {
+  public static Element addChildToElement(Element element, String childName, boolean inclNullAttr, Object... attributes) {
     // Create child
     Element child = new Element(childName);
-//    Element child = new Element(childName,SCHEMA_URI);
     // Set child attributes
+    boolean anyAttrSet = false;
     for (int i=0; i<attributes.length; i+=2) {
       String attrName  = attributes[i].toString();
-      String attrValue = (attributes[i+1] != null) ? attributes[i+1].toString() : "";
-      if ( attributes[i+1] instanceof Color )
-        attrValue = Integer.toHexString( ((Color)attributes[i+1]).getRGB() );
-      child.setAttribute(attrName, attrValue);
+      Object attrValue = attributes[i+1];
+      anyAttrSet |= XMLUtil.setAttribute(child, attrName, attrValue, inclNullAttr);
     }
+    // if null attributes should be ignored and NO attribute is 
+    // actually set, also the child is not created!
+    if ( !anyAttrSet && !inclNullAttr )
+      child = null;
+    
     // If element is given, the child is added
     if ( element != null )
       element.addContent(child);
@@ -487,7 +521,8 @@
     if ( labelStyle == null )
       return null;
     // Create and add child with paint attribute
-    Element labelElem = addChildToElement(element, titleChildName, "paint", labelStyle.getPaint());
+    Element labelElem = addChildToElement(element, titleChildName);
+    XMLUtil.setNotNullAttribute(labelElem, "paint", labelStyle.getPaint());
     // Set title to element content
     labelElem.setText( labelStyle.getLabelTranslation().toOneLine() );
 
@@ -508,18 +543,18 @@
       return null;
     
     // Create and add child with visible attribute
-    Element axisElem = addChildToElement(element, axisChildName, "visible", axisStyle.isVisible()); 
+    Element axisElem = addChildToElement(element, axisChildName);
+    XMLUtil.setNotNullAttribute(axisElem, "visible", axisStyle.isVisible()); 
     
     // Create and add child <title>
     Element titleElem = addLabelChildToElement(axisElem, "title", axisStyle);
     // Add angle attribute to <title> element
-    if ( axisStyle.getLabelAngle() != null )
-      titleElem.setAttribute("angle", axisStyle.getLabelAngle().toString());
+    XMLUtil.setNotNullAttribute(titleElem, "angle", axisStyle.getLabelAngle());
     
     // Create and add child <valueLabels>
     Element valueLabelsElem = addChildToElement(axisElem, "valueLabels");
-    if ( axisStyle.getValuesAngle() != null )
-      valueLabelsElem.setAttribute("valueAngle", axisStyle.getValuesAngle().toString() );
+    XMLUtil.setNotNullAttribute(valueLabelsElem, "valueAngle", axisStyle.getValuesAngle());
+
     Format format = axisStyle.getValuesFormat();
     if ( format instanceof DateFormat )
       valueLabelsElem.setAttribute("dateFormat", format.toString());

Modified: trunk/src/schmitzm/xml/XMLUtil.java
===================================================================
--- trunk/src/schmitzm/xml/XMLUtil.java	2009-07-31 11:59:50 UTC (rev 251)
+++ trunk/src/schmitzm/xml/XMLUtil.java	2009-07-31 12:14:26 UTC (rev 252)
@@ -181,7 +181,7 @@
     String valueStr = value.toString();
     // Special: convert color to hex rgb string
     if ( value instanceof Color )
-      valueStr = Integer.toHexString( ((Color)value).getRGB() );
+      valueStr = Long.toHexString( ((Color)value).getRGB() );
     
     element.setAttribute(attrName, valueStr);
   



More information about the Schmitzm-commits mailing list