1 14 package org.wings.style; 15 16 import org.wings.SComponent; 17 import org.wings.SContainer; 18 import org.wings.SFrame; 19 import org.wings.border.SBorder; 20 import org.wings.io.Device; 21 import org.wings.plaf.ComponentCG; 22 import org.wings.resource.DynamicResource; 23 import org.wings.session.BrowserType; 24 import org.wings.session.SessionManager; 25 import org.wings.util.ComponentVisitor; 26 27 import java.io.IOException ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Set ; 33 import java.util.HashSet ; 34 35 42 public class DynamicStyleSheetResource 43 extends DynamicResource { 44 45 49 private final static List tableResistantProperties = Arrays.asList(new CSSProperty[]{ 50 CSSProperty.COLOR, CSSProperty.FONT, CSSProperty.FONT_FAMILY, CSSProperty.FONT_SIZE, 51 CSSProperty.FONT_STYLE, CSSProperty.FONT_VARIANT, CSSProperty.FONT_WEIGHT, 52 CSSProperty.TEXT_DECORATION, CSSProperty.TEXT_TRANSFORM, CSSProperty.LETTER_SPACING, 53 CSSProperty.LINE_HEIGHT, CSSProperty.BACKGROUND_COLOR}); 54 55 public DynamicStyleSheetResource(SFrame frame) { 56 super(frame, "css", "text/css"); 57 } 58 59 public void write(Device out) 60 throws IOException { 61 try { 62 StyleSheetWriter visitor = new StyleSheetWriter(out); 63 getFrame().invite(visitor); 64 } catch (IOException e) { 65 throw e; 66 } catch (Exception e) { 67 e.printStackTrace(); 68 throw new IOException (e.getMessage()); } 70 } 71 72 protected static class StyleSheetWriter 73 implements ComponentVisitor { 74 Device out; 75 76 public StyleSheetWriter(Device out) { 77 this.out = out; 78 } 79 80 private void writeAttributesFrom(SComponent component) 81 throws IOException { 82 Collection dynamicStyles = component.getDynamicStyles(); 83 if (dynamicStyles != null) { 84 ComponentCG cg = component.getCG(); 85 for (Iterator iterator = dynamicStyles.iterator(); iterator.hasNext();) { 86 CSSStyle style = (CSSStyle) iterator.next(); 87 CSSSelector selector = cg.mapSelector(component, (CSSSelector) style.getSelector()); 89 String selectorString = selector.getSelectorString(); 90 out.print(selectorString); 91 92 final BrowserType currentBrowser = SessionManager.getSession().getUserAgent().getBrowserType(); 93 if (BrowserType.IE.equals(currentBrowser)) { 94 final Set tableBlockedProperties = new HashSet (style.properties()); 97 tableBlockedProperties.retainAll(tableResistantProperties); 98 if (tableBlockedProperties.size() > 0) 100 out.print(", ").print(selectorString).print(" table "); 101 } 102 103 out.print("{"); 104 style.write(out); 105 out.print("}\n"); 106 } 107 } 108 109 SBorder border = component.getBorder(); 110 if (border != null) { 111 out.print(CSSSelector.getSelectorString(component)).print("{"); 112 border.getAttributes().write(out); 113 out.print("}\n"); 114 } 115 } 116 117 public void visit(SComponent component) throws Exception { 118 writeAttributesFrom(component); 119 } 120 121 public void visit(SContainer container) throws Exception { 122 writeAttributesFrom(container); 123 container.inviteEachComponent(this); 124 } 125 } 126 } 127 | Popular Tags |