[Schmitzm-commits] r250 - trunk/src/schmitzm/jfree/chart/style
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Fri Jul 31 13:55:39 CEST 2009
Author: alfonx
Date: 2009-07-31 13:55:38 +0200 (Fri, 31 Jul 2009)
New Revision: 250
Modified:
trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java
trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
Log:
GP-Feature:
* Progress on Saving/Loading ChartStyles...
Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java 2009-07-31 11:31:55 UTC (rev 249)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java 2009-07-31 11:55:38 UTC (rev 250)
@@ -31,6 +31,7 @@
import java.io.File;
import java.io.IOException;
+import java.net.URISyntaxException;
import java.net.URL;
import org.jdom.Document;
@@ -99,15 +100,5 @@
}
}
- /**
- * Writes a chart definition to {@link File}.
- * @param chartStyle a style
- * @param chartFile target file
- */
- public static void writeStyleToXML(ChartStyle chartStyle, File chartFile) {
- // TODO: method must use a factory to differ FeatureChartStyles from
- // "normal" styles
- throw new UnsupportedOperationException("ChartStyleUtil.writeStyleToXML(.) not yet implemented!");
- }
}
Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java 2009-07-31 11:31:55 UTC (rev 249)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java 2009-07-31 11:55:38 UTC (rev 250)
@@ -31,8 +31,10 @@
import java.awt.Color;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.Format;
@@ -41,6 +43,7 @@
import java.util.List;
import java.util.Random;
+import org.apache.commons.io.output.FileWriterWithEncoding;
import org.jdom.Element;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.ui.RectangleInsets;
@@ -54,7 +57,7 @@
*/
public class ChartStyleXMLFactory<E extends ChartStyle> {
/** URL for XML schema */
- public static final String SCHEMA_URI = "http://www.wikisquare.de/AtlasML";
+// public static final String SCHEMA_URI = "http://www.wikisquare.de/AtlasML";
/**
* Reads a chart definition from XML element. The chart style ID
@@ -362,11 +365,32 @@
* @param filePath file to store the style to
*/
public void writeStyleToFile(E style, String rootElementName, File filePath) throws IOException {
- final FileWriter out = new FileWriter( filePath );
+// final FileWriter out = new FileWriter( filePath );
+// // Create XML-Document
+// XMLUtil.XML_OUTPUTTER.output(
+// createElementFromStyle(style, rootElementName),
+// out
+// );
+// out.flush();
+// out.close();
+//
+// // Create a DOM builder and parse the fragment
+// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+// Document document = null;
+// try {
+// document = factory.newDocumentBuilder().newDocument();
+// } catch (ParserConfigurationException e) {
+//
+// throw new IOException("factory.newDocumentBuilder().newDocument() failed:", e);
+// }
+//
+// document.appendChild(createElementFromStyle(style, rootElementName));
+ OutputStreamWriter out = new OutputStreamWriter(
+ new FileOutputStream(filePath), "utf-8");
// Create XML-Document
- XMLUtil.XML_OUTPUTTER.output(
- createElementFromStyle(style, rootElementName),
- out
+ XMLUtil.XML_OUTPUTTER.output( new org.jdom.Document(
+ createElementFromStyle(style, rootElementName)),
+ out
);
out.flush();
out.close();
@@ -382,7 +406,8 @@
public Element createElementFromStyle(E style, String rootElementName) {
if ( rootElementName == null )
rootElementName = "ChartStyle";
- Element root = new Element(rootElementName,SCHEMA_URI);
+ 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());
@@ -397,16 +422,19 @@
root.setAttribute("orientation", "vertical" );
// Element for background
- addChildToElement(root, "background", false, "paint", style.getBackground() );
+ if ( style.getBackground() != null )
+ addChildToElement(root, "background", "paint", style.getBackground() );
// Element for border
- addChildToElement(root, "border", false, "visible", style.isBorderVisible() );
+ addChildToElement(root, "border", "visible", style.isBorderVisible() );
// Chart title
- addLabelChildToElement(root, "title", style.getTitleStyle() );
+ if ( style.getTitleStyle() != null )
+ addLabelChildToElement(root, "title", style.getTitleStyle() );
// Chart description
- addLabelChildToElement(root, "desc", style.getDescStyle() );
+ if ( style.getDescStyle() != null )
+ addLabelChildToElement(root, "desc", style.getDescStyle() );
// Axis
if ( style.getAxisCount() > 0 )
@@ -421,33 +449,11 @@
addRendererChildToElement(root, style.getRendererStyle(i));
// 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()
- );
- }
+ Element plotElem = addChildToElement(root, "plot");
+
+ // TODO: <renderer> + <plot>
+
// Type specific style properties
if ( style instanceof BasicChartStyle ) {
root.setAttribute("stepped", String.valueOf( ((BasicChartStyle)style).isStepped() ));
@@ -461,44 +467,26 @@
}
/**
- * 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, boolean inclNullAttr, Object... attributes) {
+ public static Element addChildToElement(Element element, String childName, Object... attributes) {
// Create child
- Element child = new Element(childName,SCHEMA_URI);
+ 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();
- Object attrValue = attributes[i+1];
- anyAttrSet |= XMLUtil.setAttribute(child, attrName, attrValue, inclNullAttr);
+ 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);
}
- // 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);
@@ -520,8 +508,7 @@
if ( labelStyle == null )
return null;
// Create and add child with paint attribute
- Element labelElem = addChildToElement(element, titleChildName);
- XMLUtil.setNotNullAttribute(labelElem, "paint", labelStyle.getPaint());
+ Element labelElem = addChildToElement(element, titleChildName, "paint", labelStyle.getPaint());
// Set title to element content
labelElem.setText( labelStyle.getLabelTranslation().toOneLine() );
@@ -542,18 +529,18 @@
return null;
// Create and add child with visible attribute
- Element axisElem = addChildToElement(element, axisChildName);
- XMLUtil.setNotNullAttribute(axisElem, "visible", axisStyle.isVisible());
+ Element axisElem = addChildToElement(element, axisChildName, "visible", axisStyle.isVisible());
// Create and add child <title>
Element titleElem = addLabelChildToElement(axisElem, "title", axisStyle);
// Add angle attribute to <title> element
- XMLUtil.setNotNullAttribute(titleElem, "angle", axisStyle.getLabelAngle());
+ if ( axisStyle.getLabelAngle() != null )
+ titleElem.setAttribute("angle", axisStyle.getLabelAngle().toString());
// Create and add child <valueLabels>
Element valueLabelsElem = addChildToElement(axisElem, "valueLabels");
- XMLUtil.setNotNullAttribute(valueLabelsElem, "valueAngle", axisStyle.getValuesAngle());
-
+ if ( axisStyle.getValuesAngle() != null )
+ valueLabelsElem.setAttribute("valueAngle", axisStyle.getValuesAngle().toString() );
Format format = axisStyle.getValuesFormat();
if ( format instanceof DateFormat )
valueLabelsElem.setAttribute("dateFormat", format.toString());
More information about the Schmitzm-commits
mailing list