KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CSSSelector.java,v 1.6 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.SFrame;
18
19 import java.io.Serializable JavaDoc;
20
21 /**
22  * A CSS selector string. A CSS selector my address one of the following
23  * <ul>
24  * <li>One specific visual SComponent</li>
25   * <li>A virtual area of an visual components. (Instances of {@link Pseudo} found as constants).</li>
26  * <li>some free user-defined CSS selection string</li>
27  * </ul>
28  * On this selection you can define specific value fo CSS attributes.
29  * <p/>
30  * You use CSS selectors ({@link CSSSelector}) to define <b>which</b> elements you want to modify and define
31  * with CSS properties {@link CSSProperty} <b>what</b> visual property you want to modify.
32  * <p/>
33  * {@see http://www.w3.org/TR/REC-CSS2/selector.html}
34  *
35  * @author bschmid
36  */

37 public class CSSSelector implements Serializable JavaDoc {
38
39     /**
40      * We address a free CSS selector string.
41      */

42     protected String JavaDoc selectorString;
43     /**
44      * We address a specific component.
45      */

46     protected SComponent addressedComponent;
47
48     /**
49      * Constructs a CSS selector with an free CSS Selector expression.
50      * In the dynamic style sheet these selectors are rendered exactly as the passed argument.
51      *
52      * @param selectorString A css selector expression. i.e. <code>DIV.SLabel &gt; table &gt; tr</code>
53      */

54     public CSSSelector(String JavaDoc selectorString) {
55         this.selectorString = selectorString;
56     }
57
58     /**
59      * Constructs a CSS selector which exactly addresses the passed component.
60      * In the dynamic style sheet these selectors appear as <code>#id</code>
61      *
62      * @param addressedComponent The SComponent to address via a dynamically generated CSS selector expression
63      */

64     public CSSSelector(SComponent addressedComponent) {
65         this.addressedComponent = addressedComponent;
66     }
67     
68     /**
69      * @return An valid CSS selection expression.
70      */

71     public String JavaDoc getSelectorString() {
72         if (selectorString != null)
73             return selectorString;
74         else
75             return getSelectorString(addressedComponent);
76     }
77
78     /**
79      * Creates the CSS selector string to addressed the passed SComponent
80      * @param addressedComponent Adress this component. SFrame are applied globally.
81      * @return A valid CSS selector string
82      */

83     public static String JavaDoc getSelectorString(SComponent addressedComponent) {
84         if (addressedComponent == null)
85             return "";
86         else if (addressedComponent instanceof SFrame)
87             return "body";
88         else
89             return "#" + addressedComponent.getName();
90     }
91
92     public boolean equals(Object JavaDoc o) {
93         if (this == o) return true;
94         if (!(o instanceof CSSSelector)) return false;
95
96         final CSSSelector cssSelector = (CSSSelector) o;
97         final String JavaDoc selectorString = getSelectorString();
98         return (selectorString.equals(cssSelector.getSelectorString()));
99     }
100
101     public int hashCode() {
102         if (selectorString != null)
103             return selectorString.hashCode();
104         else if (addressedComponent != null)
105             return addressedComponent.hashCode();
106         else
107             return 0;
108     }
109
110     /**
111      * A CSS Pseudo-Selector. This refers to a specific area of a component.
112      * This pseudo selecteor get mapped by the according CG to a real CSS selector.
113      */

114     public static class Pseudo extends CSSSelector {
115
116         /**
117          * Address an area of the specific component. Instances of this classes are converted to real
118          * CSS selector by the according component CG's
119          *
120          * @param pseudoSelectorName A unique name for the area.
121          */

122         public Pseudo(String JavaDoc pseudoSelectorName) {
123             super(pseudoSelectorName);
124             this.selectorString = pseudoSelectorName;
125         }
126     }
127 }
128
Popular Tags