KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > style > DynamicStyleSheetResource


1 /*
2  * $Id: DynamicStyleSheetResource.java,v 1.18 2005/05/27 12:51:29 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.HashSet JavaDoc;
34
35 /**
36  * Traverses the component hierarchy of a frame and gathers the dynamic styles
37  * (attributes) of all components in a style sheet.
38  *
39  * @author <a HREF="mailto:hengels@mercatis.de">Holger Engels</a>
40  * @version $Revision: 1.18 $
41  */

42 public class DynamicStyleSheetResource
43         extends DynamicResource {
44
45     /**
46      * These CSS properties will not be inherited over tables in IE.
47      * Circumvents using quirks mode.
48      */

49     private final static List JavaDoc 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 JavaDoc {
61         try {
62             StyleSheetWriter visitor = new StyleSheetWriter(out);
63             getFrame().invite(visitor);
64         } catch (IOException JavaDoc e) {
65             throw e;
66         } catch (Exception JavaDoc e) {
67             e.printStackTrace();
68             throw new IOException JavaDoc(e.getMessage()); // UndeclaredThrowable
69
}
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 JavaDoc {
82             Collection JavaDoc dynamicStyles = component.getDynamicStyles();
83             if (dynamicStyles != null) {
84                 ComponentCG cg = component.getCG();
85                 for (Iterator JavaDoc iterator = dynamicStyles.iterator(); iterator.hasNext();) {
86                     CSSStyle style = (CSSStyle) iterator.next();
87                     // Map pseudo css selectors to real selectors
88
CSSSelector selector = cg.mapSelector(component, (CSSSelector) style.getSelector());
89                     String JavaDoc selectorString = selector.getSelectorString();
90                     out.print(selectorString);
91
92                     final BrowserType currentBrowser = SessionManager.getSession().getUserAgent().getBrowserType();
93                     if (BrowserType.IE.equals(currentBrowser)) {
94                         // IE Workaround: We need to operate IE in quirks mode.
95
// Hence we have to inherit some props manually over TABLE elements.
96
final Set JavaDoc tableBlockedProperties = new HashSet JavaDoc(style.properties());
97                         tableBlockedProperties.retainAll(tableResistantProperties);
98                         // If this style containes a table blocked CSS property then add table as additional selector.
99
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 JavaDoc {
118             writeAttributesFrom(component);
119         }
120
121         public void visit(SContainer container) throws Exception JavaDoc {
122             writeAttributesFrom(container);
123             container.inviteEachComponent(this);
124         }
125     }
126 }
127
Popular Tags