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.List;
20
21 import org.apache.wicket.Component;
22 import org.apache.wicket.ResourceReference;
23 import org.apache.wicket.Response;
24 import org.apache.wicket.markup.ComponentTag;
25 import org.apache.wicket.markup.MarkupStream;
26 import org.apache.wicket.markup.html.WebMarkupContainer;
27 import org.apache.wicket.markup.html.panel.Fragment;
28 import org.apache.wicket.protocol.http.ClientProperties;
29 import org.apache.wicket.protocol.http.WebRequestCycle;
30 import org.apache.wicket.protocol.http.WebSession;
31 import org.apache.wicket.protocol.http.request.WebClientInfo;
32 import org.apache.wicket.request.ClientInfo;
33 import org.apache.wicket.util.value.IValueMap;
34
35 /**
36  * https://cwiki.apache.org/WICKET/object-container-adding-flash-to-a-wicket-application.html
37  */
38 public abstract class ObjectContainer extends WebMarkupContainer {
39
40     private static final long serialVersionUID = 1L;
41
42     // Some general attributes for the object tag:
43     private static final String ATTRIBUTE_CONTENTTYPE = "type";
44     private static final String ATTRIBUTE_CLASSID = "classid";
45     private static final String ATTRIBUTE_CODEBASE = "codebase";
46
47     // This is used for browser specific adjustments
48     private ClientProperties clientProperties = null;
49
50     public ObjectContainer(String id) {
51         super(id);
52     }
53
54     // Set an attribute/property
55     public abstract void setValue(String name, String value);
56
57     // Get an attribute/property
58     public abstract String getValue(String name);
59
60     // Set the object's content type
61     protected abstract String getContentType();
62
63     // Set the object's clsid (for IE)
64     protected abstract String getClsid();
65
66     // Where to get the browser plugin (for IE)
67     protected abstract String getCodebase();
68
69     // Object's valid attribute names
70     protected abstract List<String> getAttributeNames();
71
72     // Object's valid parameter names
73     protected abstract List<String> getParameterNames();
74
75     // Utility function to get the URL for the object's data
76     protected String resolveResource(String src) {
77         // if it's an absolute path, return it:
78         if (src.startsWith("/") || src.startsWith("http://") || src.startsWith("https://"))
79             return (src);
80
81         // use the parent container class to resolve the resource reference
82         Component parent = getParent();
83         if (parent instanceof Fragment) {
84             // must check for fragment, otherwise we end up in Wicket namespace
85             parent = parent.getParent();
86         }        
87         if (parent != null) {
88             ResourceReference resRef = new ResourceReference(parent.getClass(), src, false);
89             return (urlFor(resRef).toString());
90         }
91
92         return (src);
93     }
94
95     public void onComponentTag(ComponentTag tag) {
96         super.onComponentTag(tag);
97
98         // get the attributes from the html-source
99         IValueMap attributeMap = tag.getAttributes();
100
101         // set the content type
102         String contentType = getContentType();
103         if (contentType != null && !"".equals(contentType))
104             attributeMap.put(ATTRIBUTE_CONTENTTYPE, contentType);
105
106         // set clsid and codebase for IE
107         if (getClientProperties().isBrowserInternetExplorer()) {
108             String clsid = getClsid();
109             String codeBase = getCodebase();
110
111             if (clsid != null && !"".equals(clsid))
112                 attributeMap.put(ATTRIBUTE_CLASSID, clsid);
113             if (codeBase != null && !"".equals(codeBase))
114                 attributeMap.put(ATTRIBUTE_CODEBASE, codeBase);
115         }
116
117         // add all attributes
118         for (String name : getAttributeNames()) {
119             String value = getValue(name);
120             if (value != null)
121                 attributeMap.put(name, value);
122         }
123     }
124
125     public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
126         Response response = getResponse();
127         response.write("\n");
128
129         // add all object's parameters:
130         for (String name : getParameterNames()) {
131             String value = getValue(name);
132             if (value != null) {
133                 response.write("<param name=\"");
134                 response.write(name);
135                 response.write("\" value=\"");
136                 response.write(value);
137                 response.write("\"/>\n");
138             }
139         }
140
141         super.onComponentTagBody(markupStream, openTag);
142     }
143
144     // shortcut to the client properties:
145     protected ClientProperties getClientProperties() {
146         if (clientProperties == null) {
147             ClientInfo clientInfo = WebSession.get().getClientInfo();
148
149             if (clientInfo == null || !(clientInfo instanceof WebClientInfo)) {
150                 clientInfo = new WebClientInfo((WebRequestCycle) getRequestCycle());
151                 WebSession.get().setClientInfo(clientInfo);
152             }
153
154             clientProperties = ((WebClientInfo) clientInfo).getProperties();
155         }
156         return (clientProperties);
157     }
158 }