001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008-2010 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.tapestry.core.base;
016    
017    import org.apache.tapestry5.Asset;
018    import org.apache.tapestry5.BindingConstants;
019    import org.apache.tapestry5.ClientElement;
020    import org.apache.tapestry5.ComponentResources;
021    import org.apache.tapestry5.MarkupWriter;
022    import org.apache.tapestry5.annotations.Environmental;
023    import org.apache.tapestry5.annotations.Import;
024    import org.apache.tapestry5.annotations.Parameter;
025    import org.apache.tapestry5.ioc.annotations.Inject;
026    import org.apache.tapestry5.ioc.services.SymbolSource;
027    import org.apache.tapestry5.json.JSONObject;
028    import org.apache.tapestry5.services.AssetSource;
029    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
030    
031    /**
032     * creates a window based on jvascript <a href="http://prototype-window.xilinus.com/">window</a> library.
033     *
034     * @version $Id: AbstractWindow.java 724 2010-11-03 19:34:53Z homburgs $
035     */
036    @Import(library = {"${tapestry.scriptaculous}/effects.js", "../components/window/window.js",
037                    "../components/window/window_effects.js", "../components/Window.js"}, stylesheet = {"../components/window/themes/default.css"})
038    abstract public class AbstractWindow implements ClientElement
039    {
040            /**
041             * The id used to generate a page-unique client-side identifier for the component. If a component renders multiple
042             * times, a suffix will be appended to the to id to ensure uniqueness. The uniqued value may be accessed via the
043             * {@link #getClientId() clientId property}.
044             */
045            @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
046            private String clientId;
047    
048            /**
049             * style name for the window.
050             * <p/>
051             * possible values are:
052             * <br/>
053             * <ul>
054             * <li>dialog</li>
055             * <li>alert (attention! has no window buttons)</li>
056             * <li>alert_lite (attention! has no window buttons)</li>
057             * <li>alphacube</li>
058             * <li>mac_os_x</li>
059             * <li>blur_os_x</li>
060             * <li>mac_os_x_dialog</li>
061             * <li>nuncio</li>
062             * <li>spread</li>
063             * <li>darkX</li>
064             * <li>greenlighting</li>
065             * <li>bluelighting</li>
066             * <li>greylighting</li>
067             * <li>darkbluelighting</li>
068             * </ul>
069             */
070            @Parameter(value = "alphacube", defaultPrefix = BindingConstants.LITERAL, name = "style")
071            private String className;
072    
073            /**
074             * initial width of the window.
075             */
076            @Parameter(value = "0", defaultPrefix = BindingConstants.PROP)
077            private int width;
078    
079            /**
080             * initial height of the window.
081             */
082            @Parameter(value = "0", defaultPrefix = BindingConstants.PROP)
083            private int height;
084    
085            /**
086             * Shows window at its current position.
087             */
088            @Parameter(value = "true", defaultPrefix = BindingConstants.PROP)
089            private boolean show;
090    
091            /**
092             * Shows window centered (only if parameter "show" is true).
093             */
094            @Parameter(value = "true", defaultPrefix = BindingConstants.PROP)
095            private boolean center;
096    
097            /**
098             * Shows window in modal mode (only if parameter "show" is true).
099             */
100            @Parameter(value = "true", defaultPrefix = BindingConstants.PROP)
101            private boolean modal;
102    
103            /**
104             * set the window title.
105             */
106            @Parameter(value = "", defaultPrefix = BindingConstants.PROP)
107            private String title;
108    
109            private String assignedClientId;
110    
111            @Environmental
112            private JavaScriptSupport javascriptSupport;
113    
114            @Inject
115            private JavaScriptSupport javaScriptSupport;
116    
117            @Inject
118            private ComponentResources resources;
119    
120            @Inject
121            private AssetSource assetSource;
122    
123            @Inject
124            private SymbolSource symbolSource;
125    
126            /**
127             * Tapestry render phase method.
128             * Initialize temporary instance variables here.
129             */
130            void setupRender()
131            {
132                    // By default, use the component id as the (base) client id. If the clientid
133                    // parameter is bound, then that is the value to use.
134                    // Often, these controlName and clientId will end up as the same value. There are many
135                    // exceptions, including a form that renders inside a loop, or a form inside a component
136                    // that is used multiple times.
137                    assignedClientId = javascriptSupport.allocateClientId(clientId);
138            }
139    
140            /**
141             * Tapestry render phase method.
142             * Start a tag here, end it in afterRender
143             */
144            void beginRender(MarkupWriter writer)
145            {
146                    String cssStyleFile;
147    
148                    String scriptPathSymbolValue = symbolSource.expandSymbols("${ck.components}") + "/window/themes";
149    
150                    if (className.endsWith("lighting"))
151                            cssStyleFile = "lighting.css";
152                    else if (className.equals("dialog"))
153                            cssStyleFile = "default.css";
154                    else if (className.endsWith("_os_x"))
155                            cssStyleFile = "mac_os_x.css";
156                    else
157                            cssStyleFile = className + ".css";
158    
159                    Asset cssAsset = assetSource.getClasspathAsset(scriptPathSymbolValue + "/" + cssStyleFile);
160                    javaScriptSupport.importStylesheet(cssAsset);
161            }
162    
163            /**
164             * Invoked to allow subclasses to further configure the parameters passed to this component's javascript
165             * options. Subclasses may override this method to configure additional features of the Window.
166             * <p/>
167             * This implementation does nothing. For more information about window options look at
168             * this <a href="http://prototype-window.xilinus.com/documentation.html#initialize">page</a>.
169             *
170             * @param options windows option object
171             */
172            protected void configure(JSONObject options)
173            {
174            }
175    
176            /**
177             * Returns a unique id for the element. This value will be unique for any given rendering of a
178             * page. This value is intended for use as the id attribute of the client-side element, and will
179             * be used with any DHTML/Ajax related JavaScript.
180             */
181            public String getClientId()
182            {
183                    return assignedClientId;
184            }
185    
186            public boolean isShow()
187            {
188                    return show;
189            }
190    
191            public boolean isCenter()
192            {
193                    return center;
194            }
195    
196            public boolean isModal()
197            {
198                    return modal;
199            }
200    
201            public String getClassName()
202            {
203                    return className;
204            }
205    
206            public int getWidth()
207            {
208                    return width;
209            }
210    
211            public int getHeight()
212            {
213                    return height;
214            }
215    
216            public String getTitle()
217            {
218                    return title;
219            }
220    }