James Moger
2012-10-22 eba89539a29deba954035056437279088c3e047b
commit | author | age
3b6904 1 /*
JM 2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.gitblit.wicket.panels;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.wicket.Response;
26 import org.apache.wicket.markup.ComponentTag;
27 import org.apache.wicket.markup.MarkupStream;
28 import org.apache.wicket.util.value.IValueMap;
29
30 /**
31  * https://cwiki.apache.org/WICKET/object-container-adding-flash-to-a-wicket-application.html
32  * 
33  * @author Jan Kriesten
34  * @author manuelbarzi
35  * @author James Moger
36  * 
37  */
38 public class ShockWaveComponent extends ObjectContainer {
39     private static final long serialVersionUID = 1L;
40
41     private static final String CONTENTTYPE = "application/x-shockwave-flash";
42     private static final String CLSID = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
43     private static final String CODEBASE = "http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0";
44
45     // valid attributes
46     private static final List<String> attributeNames = Arrays.asList(new String[] { "classid",
47             "width", "height", "codebase", "align", "base", "data", "flashvars" });
48     // valid parameters
49     private static final List<String> parameterNames = Arrays.asList(new String[] { "devicefont",
50             "movie", "play", "loop", "quality", "bgcolor", "scale", "salign", "menu", "wmode",
51             "allowscriptaccess", "seamlesstabbing", "flashvars" });
52
53     // combined options (to iterate over them)
54     private static final List<String> optionNames = new ArrayList<String>(attributeNames.size()
55             + parameterNames.size());
56     static {
57         optionNames.addAll(attributeNames);
58         optionNames.addAll(parameterNames);
59     }
60
61     private Map<String, String> attributes;
62     private Map<String, String> parameters;
63
64     public ShockWaveComponent(String id) {
65         super(id);
66
67         attributes = new HashMap<String, String>();
68         parameters = new HashMap<String, String>();
69     }
70     
71     public ShockWaveComponent(String id, String movie) {
72         this(id);
73         setValue("movie", movie);
74     }
75
76     public ShockWaveComponent(String id, String movie, String width, String height) {
77         this(id);
78
79         setValue("movie", movie);
80         setValue("width", width);
81         setValue("height", height);
82     }
83
84     public void setValue(String name, String value) {
85         // IE and other browsers handle movie/data differently. So movie is used
86         // for IE, whereas
87         // data is used for all other browsers. The class uses movie parameter
88         // to handle url and
89         // puts the values to the maps depending on the browser information
90         String parameter = name.toLowerCase();
91         if ("data".equals(parameter))
92             parameter = "movie";
93
94         if ("movie".equals(parameter) && !getClientProperties().isBrowserInternetExplorer())
95             attributes.put("data", value);
96
97         if (attributeNames.contains(parameter))
98             attributes.put(parameter, value);
99         else if (parameterNames.contains(parameter))
100             parameters.put(parameter, value);
101     }
102
103     public String getValue(String name) {
104         String parameter = name.toLowerCase();
105         String value = null;
106
107         if ("data".equals(parameter)) {
108             if (getClientProperties().isBrowserInternetExplorer())
109                 return null;
110             parameter = "movie";
111         }
112
113         if (attributeNames.contains(parameter))
114             value = attributes.get(parameter);
115         else if (parameterNames.contains(parameter))
116             value = parameters.get(parameter);
117
118         // special treatment of movie to resolve to the url
119         if (value != null && parameter.equals("movie"))
120             value = resolveResource(value);
121
122         return value;
123     }
124
125     public void onComponentTag(ComponentTag tag) {
126         // get options from the markup
127         IValueMap valueMap = tag.getAttributes();
128
129         // Iterate over valid options
130         for (String s : optionNames) {
131             if (valueMap.containsKey(s)) {
132                 // if option isn't set programmatically, set value from markup
133                 if (!attributes.containsKey(s) && !parameters.containsKey(s))
134                     setValue(s, valueMap.getString(s));
135                 // remove attribute - they are added in super.onComponentTag()
136                 // to
137                 // the right place as attribute or param
138                 valueMap.remove(s);
139             }
140         }
141
142         super.onComponentTag(tag);
143     }
144
145     public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
146
147         super.onComponentTagBody(markupStream, openTag);
148
149         Response response = getResponse();
150
151         // add all object's parameters in embed tag too:
152         response.write("<embed");
153         addParameter(response, "type", CONTENTTYPE);
154         for (String name : getParameterNames()) {
155             String value = getValue(name);
156             if (value != null) {
157                 name = "movie".equals(name) ? "src" : name;
158                 addParameter(response, name, value);
159             }
160         }
161         for (String name : getAttributeNames()) {
162             if ("width".equals(name) || "height".equals(name)) {
163                 String value = getValue(name);
164                 if (value != null) {
165                     addParameter(response, name, value);
166                 }
167             }
168         }
169         response.write(" />\n");
170
171     }
172
173     private void addParameter(Response response, String name, String value) {
174         response.write(" ");
175         response.write(name);
176         response.write("=\"");
177         response.write(value);
178         response.write("\"");
179     }
180
181     @Override
182     protected String getClsid() {
183         return CLSID;
184     }
185
186     @Override
187     protected String getCodebase() {
188         return CODEBASE;
189     }
190
191     @Override
192     protected String getContentType() {
193         return CONTENTTYPE;
194     }
195
196     @Override
197     protected List<String> getAttributeNames() {
198         return attributeNames;
199     }
200
201     @Override
202     protected List<String> getParameterNames() {
203         return parameterNames;
204     }
205 }