View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.render.bean;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  
24  import org.springframework.extensions.surf.WebFrameworkConstants;
25  import org.springframework.extensions.surf.render.RenderContext;
26  import org.springframework.extensions.surf.render.bean.RegionRenderer;
27  import org.springframework.extensions.surf.studio.WebStudioService;
28  
29  /**
30   * Modifies the way that components are rendered when Surf is running in preview mode.
31   * This allows Web Studio to pull "pulled in" to any Surf Preview application.
32   * 
33   * Primarily, this enables the regions to output additional Web Studio
34   * specific JavaScript to bind client-side DOM elements together.
35   * 
36   * @author muzquiano
37   */
38  public class WebStudioRegionRenderer extends RegionRenderer
39  {
40      private WebStudioService webStudioService = null;
41      
42      /**
43       * Sets the web studio service.
44       * 
45       * @param webStudioService the new web studio service
46       */
47      public void setWebStudioService(WebStudioService webStudioService)
48      {
49          this.webStudioService = webStudioService;
50      }
51      
52      /**
53       * Gets the web studio service.
54       * 
55       * @return the web studio service
56       */
57      protected WebStudioService getWebStudioService()
58      {
59          return this.webStudioService;
60      }
61      
62      /*
63       * (non-Javadoc)
64       * 
65       * @see org.alfresco.web.framework.render.bean.RegionRenderer#postProcess(org.alfresco.web.framework.render.RenderContext)
66       */
67      public void postProcess(RenderContext context) throws IOException
68      {
69          // TODO: check whether web studio is enabled
70          boolean webStudioEnabled = true;
71          if (webStudioEnabled)
72          {
73              // if we are not in passive mode
74              if (!context.isPassiveMode())
75              {
76                  // html binding id
77                  String htmlId = (String) context
78                          .getValue(WebFrameworkConstants.RENDER_DATA_HTMLID);
79      
80                  // region properties
81                  String regionId = (String) context
82                          .getValue(WebFrameworkConstants.RENDER_DATA_REGION_ID);
83                  String regionScopeId = (String) context
84                          .getValue(WebFrameworkConstants.RENDER_DATA_REGION_SCOPE_ID);
85                  String regionSourceId = (String) context
86                          .getValue(WebFrameworkConstants.RENDER_DATA_REGION_SOURCE_ID);
87      
88                  // commit to output
89                  PrintWriter writer = context.getResponse().getWriter();
90                  writer
91                          .println("<script type='text/javascript'>");
92      
93                  if (regionId != null && regionScopeId != null
94                          && regionSourceId != null)
95                  {
96                      writer.println("if(typeof WebStudio != \"undefined\"){");
97                      writer.println("WebStudio.configureRegion('" + htmlId + "', '"
98                              + regionId + "', '" + regionScopeId + "', '"
99                              + regionSourceId + "');");
100                     writer.println("}");
101                 }
102     
103                 writer.println("</script>");
104                 writer.flush();
105             }
106         }
107     }
108 }