ingo@1986: package de.intevation.flys.exports;
ingo@1986: 
ingo@1994: import javax.xml.xpath.XPathConstants;
ingo@1994: 
ingo@1994: import org.w3c.dom.Node;
ingo@1994: import org.w3c.dom.NodeList;
ingo@1994: 
ingo@1994: import org.apache.log4j.Logger;
ingo@1994: 
ingo@1994: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@1994: 
ingo@1986: import de.intevation.artifactdatabase.state.DefaultSection;
ingo@1986: import de.intevation.artifactdatabase.state.DefaultSettings;
ingo@1986: import de.intevation.artifactdatabase.state.Section;
ingo@1986: 
ingo@1986: 
ingo@1986: /**
ingo@1986:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@1986:  */
ingo@1986: public class ChartSettings extends DefaultSettings {
ingo@1986: 
ingo@1994:     private static final Logger logger = Logger.getLogger(ChartSettings.class);
ingo@1994: 
ingo@2046:     protected ChartSection  chartSection;
ingo@2046:     protected LegendSection legendSection;
ingo@2056:     protected ExportSection exportSection;
ingo@2046:     protected Section       axesSection;
ingo@1986: 
ingo@1986: 
ingo@1986:     public ChartSettings() {
ingo@1986:         super();
ingo@1986: 
ingo@1986:         axesSection = new DefaultSection("axes");
ingo@1986:         addSection(axesSection);
ingo@1986:     }
ingo@1986: 
ingo@1986: 
ingo@1986:     /**
ingo@1986:      * Sets the chart section. Old chart sections are removed.
ingo@1986:      *
ingo@1986:      * @param chartSection A new Section that stores chart specific attributes.
ingo@1986:      */
ingo@2046:     public void setChartSection(ChartSection chartSection) {
ingo@2046:         ChartSection oldChartSection = getChartSection();
ingo@1986: 
ingo@1986:         if (oldChartSection != null) {
ingo@1986:             removeSection(oldChartSection);
ingo@1986:         }
ingo@1986: 
ingo@1986:         this.chartSection = chartSection;
ingo@1986:         addSection(chartSection);
ingo@1986:     }
ingo@1986: 
ingo@1986: 
ingo@1986:     /**
ingo@1986:      * Returns the Section that stores chart specific attributes.
ingo@1986:      *
ingo@1986:      * @return the Section that stores chart specific attributes.
ingo@1986:      */
ingo@2046:     public ChartSection getChartSection() {
ingo@1986:         return chartSection;
ingo@1986:     }
ingo@1986: 
ingo@1986: 
ingo@1986:     /**
ingo@1990:      * Sets the legend section. Old legend sections are removed.
ingo@1990:      *
ingo@1990:      * @param legendSection A new Section that stores legend specific
ingo@1990:      * attributes.
ingo@1990:      */
ingo@2046:     public void setLegendSection(LegendSection legendSection) {
ingo@2046:         LegendSection oldLegendSection = getLegendSection();
ingo@1990: 
ingo@1990:         if (oldLegendSection != null) {
ingo@1990:             removeSection(oldLegendSection);
ingo@1990:         }
ingo@1990: 
ingo@1990:         this.legendSection = legendSection;
ingo@1990:         addSection(legendSection);
ingo@1990:     }
ingo@1990: 
ingo@1990: 
ingo@1990:     /**
ingo@1990:      * Returns the Section that stores legend specific attributes.
ingo@1990:      *
ingo@1990:      * @return the Section that stores legend specific attributes.
ingo@1990:      */
ingo@2046:     public LegendSection getLegendSection() {
ingo@1990:         return legendSection;
ingo@1990:     }
ingo@1990: 
ingo@1990: 
ingo@1990:     /**
ingo@2056:      * Sets the export section. Old export sections are removed.
ingo@2056:      *
ingo@2056:      * @param exportSection A new Section that stores export specific
ingo@2056:      * attributes.
ingo@2056:      */
ingo@2056:     public void setExportSection(ExportSection exportSection) {
ingo@2056:         ExportSection oldExportSection = getExportSection();
ingo@2056: 
ingo@2056:         if (oldExportSection != null) {
ingo@2056:             removeSection(oldExportSection);
ingo@2056:         }
ingo@2056: 
ingo@2056:         this.exportSection = exportSection;
ingo@2056:         addSection(exportSection);
ingo@2056:     }
ingo@2056: 
ingo@2056: 
ingo@2056:     /**
ingo@2056:      * Returns the Section that stores export specific attributes.
ingo@2056:      *
ingo@2056:      * @return the Section that stores export specific attributes.
ingo@2056:      */
ingo@2056:     public ExportSection getExportSection() {
ingo@2056:         return exportSection;
ingo@2056:     }
ingo@2056: 
ingo@2056: 
ingo@2056:     /**
ingo@1986:      * Adds a Section for a new axis of the chart.
ingo@1986:      *
ingo@1986:      * @param axisSection The Section specific for a chart axis.
ingo@1986:      */
ingo@2050:     public void addAxisSection(AxisSection axisSection) {
ingo@1986:         if (axisSection != null) {
ingo@1986:             axesSection.addSubsection(axisSection);
ingo@1986:         }
ingo@1986:     }
ingo@1994: 
ingo@1994: 
ingo@1994:     /**
ingo@2050:      * This method returns an AxisSection specified by <i>axisId</i> or null if
ingo@2050:      * no AxisSection is existing with identifier <i>axisId</i>.
ingo@2050:      *
ingo@2050:      * @param axisId The identifier of the wanted AxisSection.
ingo@2050:      *
ingo@2050:      * @return the AxisSection specified by <i>axisId</i> or null.
ingo@2050:      */
ingo@2050:     public AxisSection getAxisSection(String axisId) {
ingo@2050:         for (int i = 0, n = axesSection.getSubsectionCount(); i < n; i++) {
ingo@2050:             AxisSection as = (AxisSection) axesSection.getSubsection(i);
ingo@2050:             String      id = as.getIdentifier();
ingo@2050: 
ingo@2050:             if (id != null && id.equals(axisId)) {
ingo@2050:                 return as;
ingo@2050:             }
ingo@2050:         }
ingo@2050: 
ingo@2050:         return null;
ingo@2050:     }
ingo@2050: 
ingo@2050: 
ingo@2050:     /**
ingo@1994:      * Parses the settings from <i>settings</i>. The result is a new
ingo@1994:      * ChartSettings instance.
ingo@1994:      *
ingo@1994:      * @param settings A <i>settings</i> node.
ingo@1994:      *
ingo@1994:      * @return a new <i>ChartSettings</i> instance.
ingo@1994:      */
ingo@1994:     public static ChartSettings parse(Node settings) {
ingo@1994:         if (settings == null) {
ingo@1994:             logger.warn("Tried to parse ChartSettings from empty Node!");
ingo@1994:             return null;
ingo@1994:         }
ingo@1994: 
ingo@1994:         ChartSettings chartSettings = new ChartSettings();
ingo@1994: 
ingo@1994:         parseAxes(chartSettings, settings);
ingo@1994:         parseChart(chartSettings, settings);
ingo@1994:         parseLegend(chartSettings, settings);
ingo@2056:         parseExport(chartSettings, settings);
ingo@1994: 
ingo@1994:         return chartSettings;
ingo@1994:     }
ingo@1994: 
ingo@1994: 
ingo@1994:     protected static void parseAxes(ChartSettings target, Node settings) {
ingo@1994:         NodeList axesList = (NodeList) XMLUtils.xpath(
ingo@1994:             settings, "axes/axis", XPathConstants.NODESET, null);
ingo@1994: 
ingo@1994:         int num = axesList != null ? axesList.getLength() : 0;
ingo@1994: 
ingo@1994:         if (num <= 0) {
ingo@1994:             logger.debug("No axis sections found.");
ingo@1994:             return;
ingo@1994:         }
ingo@1994: 
ingo@1994:         for (int i = 0; i < num; i++) {
ingo@1994:             parseAxis(target, axesList.item(i));
ingo@1994:         }
ingo@1994:     }
ingo@1994: 
ingo@1994: 
ingo@1994:     protected static void parseAxis(ChartSettings target, Node axis) {
ingo@1994:         AxisSection section = new AxisSection();
ingo@1994: 
ingo@1994:         String id       = XMLUtils.xpathString(axis, "id", null);
ingo@1994:         String label    = XMLUtils.xpathString(axis, "label", null);
ingo@1994:         String fSize    = XMLUtils.xpathString(axis, "font-size", null);
ingo@1994:         String fixation = XMLUtils.xpathString(axis, "fixation", null);
ingo@1994:         String low      = XMLUtils.xpathString(axis, "lower", null);
ingo@1994:         String up       = XMLUtils.xpathString(axis, "upper", null);
ingo@1994: 
ingo@1994:         if (logger.isDebugEnabled()) {
ingo@1994:             logger.debug("Fount axis id:        '" + id + "'");
ingo@1994:             logger.debug("Fount axis label:     '" + label + "'");
ingo@1994:             logger.debug("Fount axis font size: '" + fSize + "'");
ingo@1994:             logger.debug("Fount axis fixation:  '" + fixation + "'");
ingo@1994:             logger.debug("Fount axis lower:     '" + low + "'");
ingo@1994:             logger.debug("Fount axis upper:     '" + up + "'");
ingo@1994:         }
ingo@1994: 
ingo@1994:         section.setIdentifier(id);
ingo@1994:         section.setLabel(label);
sascha@3405:         section.setFontSize(Integer.parseInt(fSize.length() > 0 ? fSize : "-1"));
ingo@1994:         section.setFixed(Boolean.valueOf(fixation));
sascha@3405:         section.setLowerRange(Double.parseDouble(low.length() > 0 ? low : "0"));
sascha@3405:         section.setUpperRange(Double.parseDouble(up.length() > 0 ? up : "0"));
ingo@1994: 
ingo@1994:         target.addAxisSection(section);
ingo@1994:     }
ingo@1994: 
ingo@1994: 
ingo@1994:     protected static void parseChart(ChartSettings target, Node chart) {
ingo@1994:         ChartSection chartSection = new ChartSection();
ingo@1994: 
ingo@1994:         String title = XMLUtils.xpathString(chart, "chart/title", null);
ingo@1994:         String sub   = XMLUtils.xpathString(chart, "chart/subtitle", null);
ingo@1994:         String grid  = XMLUtils.xpathString(chart, "chart/display-grid", null);
ingo@1994: 
ingo@1994:         if (logger.isDebugEnabled()) {
ingo@1994:             logger.debug("Found chart title:    '" + title + "'");
ingo@1994:             logger.debug("Found chart subtitle: '" + sub + "'");
ingo@1994:             logger.debug("Found chart grid:     '" + grid + "'");
ingo@1994:         }
ingo@1994: 
ingo@1994:         chartSection.setTitle(title);
ingo@1994:         chartSection.setSubtitle(sub);
ingo@1994:         chartSection.setDisplayGird(Boolean.valueOf(grid));
ingo@1994: 
ingo@1994:         target.setChartSection(chartSection);
ingo@1994:     }
ingo@1994: 
ingo@1994: 
ingo@1994:     protected static void parseLegend(ChartSettings target, Node legend) {
ingo@1994:         LegendSection section = new LegendSection();
ingo@1994: 
ingo@1994:         String vis   = XMLUtils.xpathString(legend, "legend/visibility", null);
ingo@1994:         String fSize = XMLUtils.xpathString(legend, "legend/font-size", null);
felix@3153:         String lthre = XMLUtils.xpathString(legend, "legend/aggregation-threshold", null);
ingo@1994: 
ingo@1994:         if (logger.isDebugEnabled()) {
ingo@1994:             logger.debug("Found legend visibility: '" + vis + "'");
ingo@1994:             logger.debug("Found legend font size : '" + fSize + "'");
felix@3153:             logger.debug("Found legend aggregation threshold : '" + lthre + "'");
ingo@1994:         }
ingo@1994: 
ingo@1994:         section.setVisibility(Boolean.valueOf(vis));
ingo@1994:         section.setFontSize(Integer.valueOf(fSize.length() > 0 ? fSize : "-1"));
felix@3153:         section.setAggregationThreshold(Integer.valueOf(lthre.length() >0 ? lthre : "-1"));
ingo@1994: 
ingo@1994:         target.setLegendSection(section);
ingo@1994:     }
ingo@2056: 
ingo@2056: 
ingo@2056:     protected static void parseExport(ChartSettings target, Node export) {
ingo@2056:         ExportSection section = new ExportSection();
ingo@2056: 
ingo@2056:         String width  = XMLUtils.xpathString(export, "export/width", null);
ingo@2056:         String height = XMLUtils.xpathString(export, "export/height", null);
ingo@2056: 
ingo@2056:         if (logger.isDebugEnabled()) {
ingo@2056:             logger.debug("Found export width : '" + width + "'");
ingo@2056:             logger.debug("Found export height: '" + height + "'");
ingo@2056:         }
ingo@2056: 
ingo@2056:         section.setWidth(Integer.valueOf(width.length() > 0 ? width : "-1"));
ingo@2056:         section.setHeight(Integer.valueOf(height.length() > 0 ? height : "-1"));
ingo@2056: 
ingo@2056:         target.setExportSection(section);
ingo@2056:     }
ingo@1986: }
ingo@1986: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :