[Schmitzm-commits] r990 - in trunk: src/schmitzm/jfree/chart/style src/schmitzm/swing src/schmitzm/xml src_junit/schmitzm/jfree/feature/style

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Aug 26 14:08:36 CEST 2010


Author: mojays
Date: 2010-08-26 14:08:35 +0200 (Thu, 26 Aug 2010)
New Revision: 990

Modified:
   trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java
   trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
   trunk/src/schmitzm/swing/SwingUtil.java
   trunk/src/schmitzm/xml/XMLUtil.java
   trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java
Log:
- setting color for axis values
- axis title font included in XML definition
- axis values font and color included in XML definition


Modified: trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java	2010-08-26 11:03:37 UTC (rev 989)
+++ trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java	2010-08-26 12:08:35 UTC (rev 990)
@@ -62,6 +62,8 @@
   protected Format valuesFormat = null;
   /** Holds the font the axis values are formated with. */
   protected Font valuesFont = null;
+  /** Holds the color the axis values are painted with. */
+  protected Color valuesPaint = null;
   /** Holds a String that reflects the values units */
   protected String unitString = null;
   /** Indicates whether the unit is visible in axis label (Default is {@code true}). */
@@ -123,6 +125,7 @@
     dest.setLabelFont( getLabelFont() );
     dest.setValuesAngle( getValuesAngle() );
     dest.setValuesFont( getValuesFont() );
+    dest.setValuesPaint( getValuesPaint() );
     dest.setVisible( isVisible() );
     dest.setValuesFormat( getValuesFormat() );
     dest.setUnitString( getUnitString() );
@@ -204,6 +207,21 @@
   }
 
   /**
+   * Returns the {@link Color} the axis values are painted with.
+   */
+  public Color getValuesPaint() {
+    return valuesPaint;
+  }
+  
+  /**
+   * Sets the {@link Color} the axis values are painted with.
+   * @param color color for the axis values
+   */
+  public void setValuesPaint(Color color) {
+    this.valuesPaint = color;
+  }
+
+  /**
    * Returns the {@link Font} the axis values are formated with.
    */
   public Font getValuesFont() {
@@ -259,6 +277,8 @@
       axis.setLabelFont( getLabelFont() );
     if (getValuesFont() != null)
       axis.setTickLabelFont( getValuesFont() );
+    if (getValuesPaint() != null)
+      axis.setTickLabelPaint(getValuesPaint());
     axis.setVisible( isVisible() );
     
     

Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java	2010-08-26 11:03:37 UTC (rev 989)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java	2010-08-26 12:08:35 UTC (rev 990)
@@ -30,6 +30,7 @@
 package schmitzm.jfree.chart.style;
 
 import java.awt.Color;
+import java.awt.Font;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -239,14 +240,23 @@
     // read the axis title from child <title>
     Element axisTitleElem = element.getChild("title");
     applyLabelStyleFromXML(axisTitleElem,style);
-    // read the axis title angle from child <title>
+    // read the axis title angle and font from child <title>
     Double labelAngle = XMLUtil.getDoubleAttribute(axisTitleElem, "angle");
     if ( labelAngle != null )
       style.setLabelAngle( labelAngle );
+    Font labelFont = XMLUtil.getFontAttribute(axisTitleElem, "font");
+    if ( labelFont != null )
+      style.setLabelFont( labelFont );
 
-    // values format and angle from child element <valueLabels>
+    // values font, color, format and angle from child element <valueLabels>
     Element valuesElem = element.getChild("valueLabels");
     if ( valuesElem != null ) {
+      Color valuesColor = XMLUtil.getColorAttribute(valuesElem, "paint");
+      if ( valuesColor != null )
+        style.setValuesPaint( valuesColor );
+      Font valuesFont = XMLUtil.getFontAttribute(valuesElem, "font");
+      if ( valuesFont != null )
+        style.setValuesFont( valuesFont );
       Double valuesAngle = XMLUtil.getDoubleAttribute(valuesElem, "valueAngle");
       if ( valuesAngle != null )
         style.setValuesAngle( valuesAngle );
@@ -656,11 +666,14 @@
     
     // Create and add child <title>
     Element titleElem = addLabelChildToElement(axisElem, "title", axisStyle);
-    // Add angle attribute to <title> element
+    // Add angle and font attribute to <title> element
+    XMLUtil.setNotNullAttribute(titleElem, "font", axisStyle.getLabelFont());
     XMLUtil.setNotNullAttribute(titleElem, "angle", axisStyle.getLabelAngle());
     
     // Create and add child <valueLabels>
     Element valueLabelsElem = addChildToElement(axisElem, "valueLabels");
+    XMLUtil.setNotNullAttribute(valueLabelsElem, "paint", axisStyle.getValuesPaint());
+    XMLUtil.setNotNullAttribute(valueLabelsElem, "font", axisStyle.getValuesFont());
     XMLUtil.setNotNullAttribute(valueLabelsElem, "valueAngle", axisStyle.getValuesAngle());
 
     Format format = axisStyle.getValuesFormat();

Modified: trunk/src/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/src/schmitzm/swing/SwingUtil.java	2010-08-26 11:03:37 UTC (rev 989)
+++ trunk/src/schmitzm/swing/SwingUtil.java	2010-08-26 12:08:35 UTC (rev 990)
@@ -957,8 +957,45 @@
 		// combine RGB to Hex string
 		return result.toString();
 	}
+	
+	/**
+	 * Converts a {@link Font} to a string, which vise-versa can be
+	 * decoded by {@link Font#decode(String)}.
+	 * @param font a font
+     * @return {@code null} if {@code font} is {@code null}
+	 */
+	public static String convertFontToString(Font font) {
+	  if ( font == null )
+	    return null;
+	  StringBuffer sb = new StringBuffer();
+	  // Format: fontname-style-size
+	  sb.append( font.getName() )
+	    .append("-")
+	    .append( convertFontStyleToString(font) )
+	    .append("-")
+	    .append( font.getSize() );
+	  
+	  return sb.toString();
+	}
 
 	/**
+	 * Converts the style of a {@link Font} to string according to
+	 * {@link Font#decode(String)}.
+	 * @param font a font
+	 * @return {@code "PLAIN"}, {@code "BOLD"}, {@code "ITALIC"} or
+	 *         {@code "BOLDITALIC"}
+	 */
+	public static String convertFontStyleToString(Font font) {
+	  if ( font.isBold() && font.isItalic() )
+	    return "BOLDITALIC";
+      if ( font.isBold() )
+        return "BOLD";
+      if ( font.isItalic() )
+        return "ITALIC";
+      return "PLAIN";
+	}
+
+	/**
 	 * Prueft, ob eine Datei existiert und fordert gegenfalls zum Bestaetigen
 	 * des Ueberschreiben auf.
 	 * 

Modified: trunk/src/schmitzm/xml/XMLUtil.java
===================================================================
--- trunk/src/schmitzm/xml/XMLUtil.java	2010-08-26 11:03:37 UTC (rev 989)
+++ trunk/src/schmitzm/xml/XMLUtil.java	2010-08-26 12:08:35 UTC (rev 990)
@@ -30,6 +30,7 @@
 package schmitzm.xml;
 
 import java.awt.Color;
+import java.awt.Font;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.StringTokenizer;
@@ -164,6 +165,23 @@
   }
   
   /**
+   * Gets the attribute value from element as {@link Font}.
+   * @param element  element where the attribute is determined from
+   * @param attrName name of the attribute
+   * @param defValue optional default value returned if attribute is not found (or empty)
+   * @return {@code null} if {@code element} is {@code null} or attribute is not
+   *         specified in element
+   */
+  public static Font getFontAttribute(Element element, String attrName, Font... defValue) {
+    String value = getAttribute(element, attrName);
+    if ( value != null )
+      return Font.decode(value);
+    if ( defValue.length > 0 )
+      return defValue[0];
+    return null;
+  }
+
+  /**
    * Gets a semicolon-separated attribute value from element as {@code Set<String>}.
    * @param element  element where the attribute is determined from
    * @param attrName name of the attribute
@@ -299,6 +317,9 @@
     // Special: convert Translation to one line
     if ( value instanceof Translation )
       valueStr = ((Translation)value).toOneLine();
+    // Special: convert Font to string
+    if ( value instanceof Font )
+      valueStr = SwingUtil.convertFontToString((Font)value);
     
     element.setAttribute(attrName, valueStr);
   

Modified: trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java
===================================================================
--- trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java	2010-08-26 11:03:37 UTC (rev 989)
+++ trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java	2010-08-26 12:08:35 UTC (rev 990)
@@ -316,8 +316,10 @@
 		ChartAxisStyle xAxisStyle = new ChartAxisStyle();
 		ChartAxisStyle yAxisStyle = new ChartAxisStyle();
 		ChartAxisStyle yAxisStyle2 = new ChartAxisStyle();
-        yAxisStyle.setValuesFont( SwingUtil.DEFAULT_FONT );
-        yAxisStyle2.setValuesFont( SwingUtil.DEFAULT_FONT );
+        yAxisStyle.setValuesFont( SwingUtil.DEFAULT_FONT.deriveFont(20f) );
+        yAxisStyle.setValuesPaint( Color.RED );
+        yAxisStyle2.setValuesFont( SwingUtil.DEFAULT_FONT.deriveFont(8f).deriveFont(Font.BOLD+Font.ITALIC) );
+        yAxisStyle2.setValuesPaint( Color.BLUE );
 
 		FeatureBasicChartStyle chartStyle = new FeatureBasicChartStyle(
 				"keckBarChart", ChartType.BAR);



More information about the Schmitzm-commits mailing list