001// Copyright 2010, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.services.javascript;
016
017import org.apache.tapestry5.internal.TapestryInternalUtils;
018
019/**
020 * Provides options to describe options associated with importing a stylesheet onto a page.  Stylesheet options
021 * are immutable.
022 *
023 * @since 5.2.0
024 */
025public final class StylesheetOptions
026{
027    /**
028     * The media associated with this stylesheet, i.e., "print". Becomes the media attribute
029     * of the <link> tag. May be null.
030     */
031    public final String media;
032
033    /**
034     * The Internet Explorer condition associated with the link. When non-blank, the
035     * <link> element will be written inside a specially formatted comment interpreted
036     * by Internet Explorer. Usually null, and only used for full page renders (not partial page renders).
037     *
038     * @see <a href="http://en.wikipedia.org/wiki/Conditional_comment">http://en.wikipedia.org/wiki/Conditional_comment</a>
039     */
040    public final String condition;
041
042    /**
043     * If true, then this stylesheet is the insertion point for Ajax operations; any added CSS links will be inserted before this link. Only at most
044     * one CSS link should be the insertion point.     Only used for full page renders (not partial page renders). When this is true, the {@code <link>} element's
045     * ref attribute is wrtten out as "stylesheet t-ajax-insertion-point".
046     */
047    public boolean ajaxInsertionPoint;
048
049    /**
050     * Returns a new options object with media as null (that is, unspecified), no condition, and not the Ajax insertion point.
051     */
052    public StylesheetOptions()
053    {
054        this(null);
055    }
056
057    public StylesheetOptions(String media)
058    {
059        this(media, null);
060    }
061
062    /**
063     * @deprecated In 5.3, may be removed in a later release. Use {@link #StylesheetOptions(String)} and {@link #withCondition(String)}} instead.
064     */
065    public StylesheetOptions(String media, String condition)
066    {
067        this(media, condition, false);
068    }
069
070    private StylesheetOptions(String media, String condition, boolean ajaxInsertionPoint)
071    {
072        this.media = media;
073        this.condition = condition;
074        this.ajaxInsertionPoint = ajaxInsertionPoint;
075    }
076
077    /**
078     * Returns a new options object with the indicated {@linkplain #condition Internet Explorer condition}. @since 5.3
079     */
080    public StylesheetOptions withCondition(String condition)
081    {
082        return new StylesheetOptions(media, condition, ajaxInsertionPoint);
083    }
084
085    /**
086     * Returns a new options object with the {@link #ajaxInsertionPoint} flag set to true.
087     */
088    public StylesheetOptions asAjaxInsertionPoint()
089    {
090        return new StylesheetOptions(media, condition, true);
091    }
092
093    /**
094     * The media associated with this stylesheet, i.e., "print". Becomes the media attribute
095     * of the &lt;link&gt; tag. May be null.
096     *
097     * @deprecated in 5.3, may be removed in a later release, use the {@link #media} field instead
098     */
099    public String getMedia()
100    {
101        return media;
102    }
103
104    /**
105     * The Internet Explorer condition associated with the link. When non-blank, the
106     * &lt;link&gt; element will be written inside a specially formatted comment interpreted
107     * by Internet Explorer. Usually null.
108     *
109     * @see <a href="http://en.wikipedia.org/wiki/Conditional_comment">http://en.wikipedia.org/wiki/Conditional_comment</a>
110     * @deprecated in 5.3, may be removed in a later release, use the {@link #condition} field instead
111     */
112    public String getCondition()
113    {
114        return condition;
115    }
116
117    @Override
118    public String toString()
119    {
120        StringBuilder builder = new StringBuilder("StyleSheetOptions[");
121
122        String sep = "";
123
124        if (media != null)
125        {
126            builder.append("media=").append(media);
127            sep = " ";
128        }
129
130        if (condition != null)
131        {
132            builder.append(sep).append("condition=").append(condition);
133            sep = " ";
134        }
135
136        if (ajaxInsertionPoint)
137        {
138            builder.append(sep).append("ajaxInsertionPoint=true");
139            sep = " ";
140        }
141
142        return builder.append("]").toString();
143    }
144
145    @Override
146    public boolean equals(Object obj)
147    {
148        if (obj == this)
149            return true;
150
151        if (obj == null || !(obj instanceof StylesheetOptions))
152            return false;
153
154        StylesheetOptions sso = (StylesheetOptions) obj;
155
156        return ajaxInsertionPoint == sso.ajaxInsertionPoint && TapestryInternalUtils.isEqual(media, sso.media)
157                && TapestryInternalUtils.isEqual(condition, sso.condition);
158    }
159}