001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import javax.servlet.jsp.JspException;
024: import javax.servlet.jsp.tagext.TagSupport;
025: import java.util.Map;
026: import java.io.IOException;
027: import java.util.HashMap;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import org.apache.struts.util.MessageResources;
031: import org.apache.struts.Globals;
032: import com.methodhead.sitecontext.SiteContext;
033:
034: public class PanelTag extends TagSupport {
035:
036: // constructors /////////////////////////////////////////////////////////////
037:
038: // constants ////////////////////////////////////////////////////////////////
039:
040: // classes //////////////////////////////////////////////////////////////////
041:
042: // methods //////////////////////////////////////////////////////////////////
043:
044: public int doStartTag() throws JspException {
045:
046: try {
047:
048: //
049: // define template?
050: //
051: Map map = (Map) pageContext.getRequest().getAttribute(
052: ShimGlobals.PANELMAP_KEY);
053:
054: if (map != null) {
055:
056: //
057: // ignore the panel if it's already defined
058: //
059: if (map.containsKey(getName()))
060: return SKIP_BODY;
061:
062: //
063: // create a panel config and add it to the map
064: //
065: PanelConfig panelConfig = new PanelConfig();
066: panelConfig.setName(getName());
067:
068: if (getDefaultModule().equals(""))
069: panelConfig
070: .setDefaultModule(ShimGlobals.DEFAULT_MODULE);
071: else
072: panelConfig.setDefaultModule(getDefaultModule());
073:
074: map.put(panelConfig.getName(), panelConfig);
075:
076: return SKIP_BODY;
077: }
078:
079: //
080: // get some things
081: //
082: String mode = (String) pageContext.getSession()
083: .getAttribute(ShimGlobals.MODE_KEY);
084:
085: Page page = (Page) pageContext.getRequest().getAttribute(
086: ShimGlobals.PAGE_KEY);
087:
088: Module module = null;
089: Panel panel = (Panel) page.getPanels().get(name_);
090: if (panel != null)
091: module = panel.getModule();
092:
093: MessageResources messageResources = (MessageResources) pageContext
094: .getServletContext().getAttribute(
095: Globals.MESSAGES_KEY);
096:
097: //
098: // richedit mode?
099: //
100: if (pageContext.getRequest().getAttribute(
101: ShimGlobals.EDITPANEL_KEY) != null) {
102:
103: //
104: // display the module in edit mode if it's the one being edited
105: //
106: if (name_.equals(pageContext.getRequest().getAttribute(
107: ShimGlobals.EDITPANEL_KEY))) {
108:
109: pageContext.getOut().println(
110: "<div id=\"editableDiv\">");
111:
112: if (module == null)
113: pageContext
114: .getOut()
115: .println(
116: messageResources
117: .getMessage("shim.panelNotSet"));
118: else
119: module.display((HttpServletRequest) pageContext
120: .getRequest(),
121: (HttpServletResponse) pageContext
122: .getResponse(), pageContext
123: .getOut());
124:
125: pageContext.getOut().println("</div>");
126: } else {
127:
128: //
129: // otherwise just display the panel
130: //
131: if (module == null)
132: pageContext
133: .getOut()
134: .println(
135: messageResources
136: .getMessage("shim.panelNotSet"));
137: else
138: module.display((HttpServletRequest) pageContext
139: .getRequest(),
140: (HttpServletResponse) pageContext
141: .getResponse(), pageContext
142: .getOut());
143: }
144: }
145:
146: //
147: // edit mode?
148: //
149: else if (ShimGlobals.MODE_EDIT.equals(mode)
150: && (pageContext.getRequest().getAttribute(
151: ShimGlobals.PREVIEW_KEY) == null)) {
152:
153: String headerResource = null;
154: String footerResource = null;
155:
156: if (ShimUtils
157: .displayEditElements((HttpServletRequest) pageContext
158: .getRequest())) {
159: //
160: // figure out which header to use
161: //
162: if (module == null) {
163: headerResource = "shim.editPage.panelHeaderPanel";
164: footerResource = "shim.editPage.panelFooterPanel";
165: } else if (module.isConfigurable()
166: && !module.isEditable()) {
167: headerResource = "shim.editPage.panelHeaderPanelConfigure";
168: footerResource = "shim.editPage.panelFooterPanelConfigure";
169: } else if (!module.isConfigurable()
170: && module.isEditable()) {
171: headerResource = "shim.editPage.panelHeaderPanelEdit";
172: footerResource = "shim.editPage.panelFooterPanelEdit";
173: } else if (module.isConfigurable()
174: && module.isEditable()) {
175: headerResource = "shim.editPage.panelHeaderPanelConfigureEdit";
176: footerResource = "shim.editPage.panelFooterPanelConfigureEdit";
177: }
178:
179: //
180: // print the header if there is one
181: //
182: if (headerResource != null) {
183:
184: //
185: // add edit controls if we're in edit mode, but not previewing
186: //
187: pageContext.getOut().println(
188: messageResources.getMessage(
189: headerResource, ""
190: + page.getInt("id"),
191: name_));
192: }
193: }
194:
195: //
196: // display the module
197: //
198: if (module == null)
199: pageContext.getOut().println(
200: messageResources
201: .getMessage("shim.panelNotSet"));
202: else
203: module.display((HttpServletRequest) pageContext
204: .getRequest(),
205: (HttpServletResponse) pageContext
206: .getResponse(), pageContext
207: .getOut());
208:
209: if (ShimUtils
210: .displayEditElements((HttpServletRequest) pageContext
211: .getRequest())) {
212: //
213: // print the footer if there is one
214: //
215: if (footerResource != null) {
216: pageContext.getOut().println(
217: messageResources.getMessage(
218: footerResource, ""
219: + page.getInt("id"),
220: name_));
221: }
222: }
223: }
224:
225: //
226: // otherwise, just display the module
227: //
228: else {
229: if (module == null)
230: pageContext.getOut().println(
231: messageResources
232: .getMessage("shim.panelNotSet"));
233: else
234: module.display((HttpServletRequest) pageContext
235: .getRequest(),
236: (HttpServletResponse) pageContext
237: .getResponse(), pageContext
238: .getOut());
239: }
240: } catch (IOException e) {
241: throw new JspException(
242: "Unexpected IOException when displaying panel \""
243: + name_ + "\": " + e.toString());
244: }
245:
246: return SKIP_BODY;
247: }
248:
249: // properties ///////////////////////////////////////////////////////////////
250:
251: public String getName() {
252: return name_;
253: }
254:
255: public void setName(String name) {
256: name_ = name;
257: }
258:
259: public String getDefaultModule() {
260: return defaultModule_;
261: }
262:
263: public void setDefaultModule(String defaultModule) {
264: defaultModule_ = defaultModule;
265: }
266:
267: // attributes ///////////////////////////////////////////////////////////////
268:
269: private String name_ = "";
270:
271: private String defaultModule_ = "";
272: }
|