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 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    @Override
095    public String toString()
096    {
097        StringBuilder builder = new StringBuilder("StyleSheetOptions[");
098
099        String sep = "";
100
101        if (media != null)
102        {
103            builder.append("media=").append(media);
104            sep = " ";
105        }
106
107        if (condition != null)
108        {
109            builder.append(sep).append("condition=").append(condition);
110            sep = " ";
111        }
112
113        if (ajaxInsertionPoint)
114        {
115            builder.append(sep).append("ajaxInsertionPoint=true");
116            sep = " ";
117        }
118
119        return builder.append(']').toString();
120    }
121
122    @Override
123    public boolean equals(Object obj)
124    {
125        if (obj == this)
126            return true;
127
128        if (obj == null || !(obj instanceof StylesheetOptions))
129            return false;
130
131        StylesheetOptions sso = (StylesheetOptions) obj;
132
133        return ajaxInsertionPoint == sso.ajaxInsertionPoint && TapestryInternalUtils.isEqual(media, sso.media)
134                && TapestryInternalUtils.isEqual(condition, sso.condition);
135    }
136}