# HG changeset patch # User Ingo Weinzierl # Date 1319118345 0 # Node ID d2a17e990c707872c6ddb561f4aeb094a44d6fb0 # Parent 94faf08d143cda597d29bbf93e4e9376f5c833fe Improved the Themes: we now support special themes for facets which need to match a given pattern string. flys-artifacts/trunk@3047 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 94faf08d143c -r d2a17e990c70 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Oct 20 13:29:54 2011 +0000 +++ b/flys-artifacts/ChangeLog Thu Oct 20 13:45:45 2011 +0000 @@ -1,3 +1,28 @@ +2011-10-20 Ingo Weinzierl + + * doc/conf/themes.xml: Added main value themes for longitudinal section + charts. + + * src/main/java/de/intevation/flys/themes/ThemeMapping.java: New. This + class stores the name of a facet, the related theme and a pattern + string. + + * src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java: + Read the pattern string and store a list of ThemeMapping objects in the + FLYSContext. + + * src/main/java/de/intevation/flys/themes/ThemeFactory.java: Modified + getTheme() which now takes the FLYSContext, the name of a facet and an + optional pattern string. Now, we can have specialized Themes for each + chart type. E.g. the facet "longitudinal_section.w" maps the default + Theme for W lines in longitudinal section charts. If the optional + pattern string matches the pattern ".*(HQ1000)(\D.*)*", the ThemeFactory + will return the Theme "LongitudinalSectionW_HQ1000". + + * src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java: + Use the description of a facet as pattern string to get the relevant + Theme from ThemeFactory. + 2011-10-20 Raimund Renkert * doc/conf/themes.xml: diff -r 94faf08d143c -r d2a17e990c70 flys-artifacts/doc/conf/themes.xml --- a/flys-artifacts/doc/conf/themes.xml Thu Oct 20 13:29:54 2011 +0000 +++ b/flys-artifacts/doc/conf/themes.xml Thu Oct 20 13:45:45 2011 +0000 @@ -23,6 +23,168 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -32,6 +194,168 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 94faf08d143c -r d2a17e990c70 flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java Thu Oct 20 13:29:54 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/context/FLYSContextFactory.java Thu Oct 20 13:45:45 2011 +0000 @@ -30,6 +30,7 @@ import de.intevation.flys.artifacts.transitions.TransitionFactory; import de.intevation.flys.themes.Theme; import de.intevation.flys.themes.ThemeFactory; +import de.intevation.flys.themes.ThemeMapping; /** @@ -344,7 +345,8 @@ return; } - Map mapping = new HashMap(); + Map> mapping = + new HashMap>(); for (int i = 0; i < num; i++) { Node node = mappings.item(i); @@ -355,12 +357,22 @@ String to = (String) XMLUtils.xpath( node, "@to", XPathConstants.STRING); + String pattern = (String) XMLUtils.xpath( + node, "@pattern", XPathConstants.STRING); + if (from != null && to != null) { - mapping.put(from, to); + List tm = mapping.get(from); + + if (tm == null) { + tm = new ArrayList(); + mapping.put(from, tm); + } + + tm.add(new ThemeMapping(from, to, pattern)); } } - logger.debug("Found " + mapping.size() + " theme/facet mappings."); + logger.debug("Found " + mapping.size() + " theme mappings."); context.put(FLYSContext.THEME_MAPPING, mapping); } diff -r 94faf08d143c -r d2a17e990c70 flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java --- a/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java Thu Oct 20 13:29:54 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java Thu Oct 20 13:45:45 2011 +0000 @@ -302,6 +302,7 @@ art, outName, facetName, + theme.getDescription(), theme.getIndex(), context), theme.getActive() == 1); @@ -473,6 +474,7 @@ String uuid, String outName, String facet, + String pattern, int index, CallContext context) throws ArtifactDatabaseException @@ -486,15 +488,10 @@ ? (FLYSContext) context : (FLYSContext) context.globalContext(); - Map mappings = (Map) - flysContext.get(FLYSContext.THEME_MAPPING); - - String themeName = mappings.get(facet); - Document attr = db.getCollectionItemAttribute(identifier(), uuid, meta); if (attr == null) { - attr = initItemAttribute(uuid, facet, index, context); + attr = initItemAttribute(uuid, facet, pattern, index, context); if (attr == null) { return null; @@ -514,7 +511,7 @@ return null; } - log.debug("Search theme '" + themeName + "' in attribute."); + log.debug("Search theme for facet '" + facet + "' in attribute."); Node theme = (Node) XMLUtils.xpath( tmp, @@ -526,7 +523,7 @@ if (theme == null) { log.warn("Could not find the theme in attribute of: " + uuid); - Theme t = getThemeForFacet(uuid, facet, index, context); + Theme t = getThemeForFacet(uuid, facet, pattern, index, context); if (t == null) { log.warn("No theme found for facet: " + facet); @@ -619,12 +616,13 @@ protected Document initItemAttribute( String uuid, String facet, + String pattern, int index, CallContext context) { log.info("FLYSArtifactCollection.initItemAttribute"); - Theme t = getThemeForFacet(uuid, facet, index, context); + Theme t = getThemeForFacet(uuid, facet, pattern, index, context); if (t == null) { log.info("Could not find theme for facet. Cancel initialization."); @@ -681,6 +679,7 @@ protected Theme getThemeForFacet( String uuid, String facet, + String pattern, int index, CallContext context) { @@ -690,7 +689,7 @@ ? (FLYSContext) context : (FLYSContext) context.globalContext(); - Theme t = ThemeFactory.getTheme(flysContext, facet); + Theme t = ThemeFactory.getTheme(flysContext, facet, pattern); if (t != null) { t.setFacet(facet); diff -r 94faf08d143c -r d2a17e990c70 flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java --- a/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java Thu Oct 20 13:29:54 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeFactory.java Thu Oct 20 13:45:45 2011 +0000 @@ -1,5 +1,6 @@ package de.intevation.flys.themes; +import java.util.List; import java.util.Map; import javax.xml.xpath.XPathConstants; @@ -61,12 +62,18 @@ * @return a theme. */ public static Theme getTheme(FLYSContext c, String name) { + return getTheme(c, name, null); + } + + + public static Theme getTheme(FLYSContext c, String name, String pattern) { + logger.debug("Search theme for: " + name + " - " + pattern); if (c == null || name == null) { logger.warn("Cannot search for theme."); return null; } - Map map = (Map) + Map> map = (Map>) c.get(FLYSContext.THEME_MAPPING); Map t = (Map) @@ -77,13 +84,50 @@ return null; } - String themeName = map.get(name); + List mapping = map.get(name); - if (themeName == null) { - logger.warn("No theme found for mapping: " + name); + if (mapping == null) { + logger.warn("No theme fount for mapping: " + name); + return null; } - return t.get(themeName); + int patternLen = pattern != null ? pattern.length() : 0; + + Theme defaultTheme = null; + + for (ThemeMapping tm: mapping) { + if (name.equals(tm.getFrom())) { + String tmPattern = tm.getPatternStr(); + + int tmLen = tmPattern != null ? tmPattern.length() : 0; + + if (tmLen == 0 && patternLen == 0) { + // we want and get the default theme + return t.get(tm.getTo()); + } + else if (tmLen == 0 && patternLen > 0) { + // found a defaultTheme, lets save it + defaultTheme = t.get(tm.getTo()); + } + else if (tm.applyPattern(pattern)) { + // found the special theme we have searched for + return t.get(tm.getTo()); + } + } + } + + String msg = + "No theme found for '" + name + + "' with pattern '" + pattern + "'."; + + if (defaultTheme != null) { + logger.info(msg + " Use the default instead."); + return defaultTheme; + } + + logger.warn(msg); + + return null; } diff -r 94faf08d143c -r d2a17e990c70 flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeMapping.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeMapping.java Thu Oct 20 13:45:45 2011 +0000 @@ -0,0 +1,51 @@ +package de.intevation.flys.themes; + +import java.io.Serializable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + +public class ThemeMapping implements Serializable { + + protected String from; + protected String to; + protected String patternStr; + + protected Pattern pattern; + + + public ThemeMapping(String from, String to) { + this(from, to, null); + } + + + public ThemeMapping(String from, String to, String patternStr) { + this.from = from; + this.to = to; + this.patternStr = patternStr; + + this.pattern = Pattern.compile(patternStr); + } + + + public String getFrom() { + return from; + } + + + public String getTo() { + return to; + } + + + public String getPatternStr() { + return patternStr; + } + + + public boolean applyPattern(String text) { + Matcher m = pattern.matcher(text); + return m.matches(); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :