KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > render > EditModeRenderStrategy


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.render;
25
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.riotfamily.common.web.util.ServletUtils;
36 import org.riotfamily.components.ComponentList;
37 import org.riotfamily.components.ComponentRepository;
38 import org.riotfamily.components.ComponentVersion;
39 import org.riotfamily.components.Location;
40 import org.riotfamily.components.VersionContainer;
41 import org.riotfamily.components.config.ComponentListConfiguration;
42 import org.riotfamily.components.context.PageRequestUtils;
43 import org.riotfamily.components.dao.ComponentDao;
44
45 public class EditModeRenderStrategy extends PreviewModeRenderStrategy {
46
47     private static final Log log = LogFactory.getLog(EditModeRenderStrategy.class);
48
49     public EditModeRenderStrategy(ComponentDao dao,
50             ComponentRepository repository, ComponentListConfiguration config,
51             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
52             throws IOException JavaDoc {
53
54         super(dao, repository, config, request, response);
55     }
56
57     public void renderComponentVersion(ComponentVersion version)
58             throws IOException JavaDoc {
59
60         VersionContainer c = version.getContainer();
61         List JavaDoc components = getComponentsToRender(c.getList());
62         int position = components.indexOf(c);
63         boolean last = position == components.size() - 1;
64         renderComponentVersion(version, getPositionalClassName(position, last));
65     }
66
67     /**
68      * Overrides the default implementation to render a DIV tag around the
69      * actual list. The DIV has attributes that are required for the
70      * Riot-Toolbar JavaScript.
71      */

72     protected void renderComponentList(ComponentList list) throws IOException JavaDoc {
73         boolean renderOuterDiv = PageRequestUtils.createAndStoreContext(
74                 request, list.getId(), 120000);
75
76         if (renderOuterDiv) {
77             out.print("<div riot:listId=\"");
78             out.print(list.getId());
79             out.print("\" riot:controllerId=\"");
80             String JavaDoc uri = ServletUtils.getIncludeUri(request);
81             uri = uri.substring(request.getContextPath().length());
82             out.print(uri);
83             out.print('"');
84             if (config.getMinComponents() != null) {
85                 out.print(" riot:minComponents=\"");
86                 out.print(config.getMinComponents());
87                 out.print('"');
88             }
89             if (config.getMaxComponents() != null) {
90                 out.print(" riot:maxComponents=\"");
91                 out.print(config.getMaxComponents());
92                 out.print('"');
93             }
94             if (list.isDirty()) {
95                 out.print(" riot:dirty=\"true\"");
96             }
97             out.print(" class=\"riot-components riot-component-list\">");
98             super.renderComponentList(list);
99             out.print("</div>");
100         }
101         else {
102             super.renderComponentList(list);
103         }
104     }
105
106     /**
107      * Overrides the default implementation to create a new list if no existing
108      * list is found.
109      *
110      * @see #createNewList(Location)
111      */

112     protected ComponentList getComponentList(Location location) {
113         ComponentList list = super.getComponentList(location);
114         if (list == null) {
115             list = createNewList(location);
116         }
117         return list;
118     }
119
120     /**
121      * Creates a new ComponentList with an initial component set as defined by
122      * the controller.
123      */

124     protected ComponentList createNewList(Location location) {
125         ComponentList list = new ComponentList();
126         list.setLocation(new Location(location));
127         list.setParent(parent);
128         String JavaDoc[] initialTypes = config.getInitialComponentTypes();
129         if (initialTypes != null) {
130             List JavaDoc containers = new ArrayList JavaDoc();
131             for (int i = 0; i < initialTypes.length; i++) {
132                 VersionContainer container = new VersionContainer();
133                 ComponentVersion live = new ComponentVersion(initialTypes[i]);
134                 live.setContainer(container);
135                 container.setList(list);
136                 container.setLiveVersion(live);
137                 containers.add(container);
138             }
139             list.setLiveContainers(containers);
140         }
141         dao.saveComponentList(list);
142         log.debug("New ComponentList created: " + list);
143         return list;
144     }
145
146     /**
147      * Overrides the default implementation to render a DIV tag around the
148      * actual component. The DIV has attributes that are required for the
149      * Riot-Toolbar JavaScript.
150      * @throws IOException
151      */

152     protected void renderContainer(VersionContainer container,
153             String JavaDoc positionClassName) throws IOException JavaDoc {
154
155         ComponentVersion version = getVersionToRender(container);
156         out.print("<div riot:containerId=\"");
157         out.print(container.getId());
158         out.print("\" riot:componentType=\"");
159         out.print(version.getType());
160         out.print('"');
161
162         String JavaDoc type = version.getType();
163         String JavaDoc formUrl = repository.getFormUrl(type, container.getId());
164         if (formUrl != null) {
165             out.print(" riot:form=\"");
166             out.print(formUrl);
167             out.print('"');
168         }
169
170         out.print(" class=\"riot-component riot-component-");
171         out.print(version.getType());
172         out.print("\">");
173         renderComponentVersion(version, positionClassName);
174         out.print("</div>");
175     }
176
177     protected RenderStrategy getStrategyForParentList() throws IOException JavaDoc {
178         return new InheritingRenderStrategy(dao, repository, config,
179                 request, response);
180     }
181
182 }
183
Popular Tags