KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > PanelTag


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import org.apache.struts.util.MessageResources;
31 import org.apache.struts.Globals;
32 import com.methodhead.sitecontext.SiteContext;
33
34 public class PanelTag
35 extends
36   TagSupport JavaDoc {
37
38   // constructors /////////////////////////////////////////////////////////////
39

40   // constants ////////////////////////////////////////////////////////////////
41

42   // classes //////////////////////////////////////////////////////////////////
43

44   // methods //////////////////////////////////////////////////////////////////
45

46   public int doStartTag()
47   throws
48     JspException JavaDoc {
49
50     try {
51
52       //
53
// define template?
54
//
55
Map JavaDoc map =
56         ( Map JavaDoc )pageContext.getRequest().getAttribute(
57           ShimGlobals.PANELMAP_KEY );
58
59       if ( map != null ) {
60
61         //
62
// ignore the panel if it's already defined
63
//
64
if ( map.containsKey( getName() ) )
65           return SKIP_BODY;
66
67         //
68
// create a panel config and add it to the map
69
//
70
PanelConfig panelConfig = new PanelConfig();
71         panelConfig.setName( getName() );
72
73         if ( getDefaultModule().equals( "" ) )
74           panelConfig.setDefaultModule( ShimGlobals.DEFAULT_MODULE );
75         else
76           panelConfig.setDefaultModule( getDefaultModule() );
77
78         map.put( panelConfig.getName(), panelConfig );
79
80         return SKIP_BODY;
81       }
82
83       //
84
// get some things
85
//
86
String JavaDoc mode =
87         ( String JavaDoc )pageContext.getSession().getAttribute( ShimGlobals.MODE_KEY );
88
89       Page page =
90         ( Page )pageContext.getRequest().getAttribute( ShimGlobals.PAGE_KEY );
91
92       Module module = null;
93       Panel panel = ( Panel )page.getPanels().get( name_ );
94       if ( panel != null )
95         module = panel.getModule();
96
97       MessageResources messageResources =
98         ( MessageResources )pageContext.getServletContext().getAttribute(
99           Globals.MESSAGES_KEY );
100
101       //
102
// richedit mode?
103
//
104
if ( pageContext.getRequest().getAttribute(
105                   ShimGlobals.EDITPANEL_KEY ) != null ) {
106
107         //
108
// display the module in edit mode if it's the one being edited
109
//
110
if ( name_.equals(
111                pageContext.getRequest().getAttribute(
112                   ShimGlobals.EDITPANEL_KEY ) ) ) {
113
114           pageContext.getOut().println( "<div id=\"editableDiv\">" );
115
116           if ( module == null )
117             pageContext.getOut().println(
118               messageResources.getMessage( "shim.panelNotSet" ) );
119           else
120             module.display(
121               ( HttpServletRequest JavaDoc )pageContext.getRequest(),
122               ( HttpServletResponse JavaDoc )pageContext.getResponse(),
123               pageContext.getOut() );
124
125           pageContext.getOut().println( "</div>" );
126         }
127         else {
128
129           //
130
// otherwise just display the panel
131
//
132
if ( module == null )
133             pageContext.getOut().println(
134               messageResources.getMessage( "shim.panelNotSet" ) );
135           else
136             module.display(
137               ( HttpServletRequest JavaDoc )pageContext.getRequest(),
138               ( HttpServletResponse JavaDoc )pageContext.getResponse(),
139               pageContext.getOut() );
140         }
141       }
142
143       //
144
// edit mode?
145
//
146
else if ( ShimGlobals.MODE_EDIT.equals( mode ) &&
147            ( pageContext.getRequest().getAttribute(
148                ShimGlobals.PREVIEW_KEY ) == null ) ) {
149
150         String JavaDoc headerResource = null;
151         String JavaDoc footerResource = null;
152
153         if ( ShimUtils.displayEditElements(
154                ( HttpServletRequest JavaDoc )pageContext.getRequest() ) ) {
155           //
156
// figure out which header to use
157
//
158
if ( module == null ) {
159             headerResource = "shim.editPage.panelHeaderPanel";
160             footerResource = "shim.editPage.panelFooterPanel";
161           }
162           else if ( module.isConfigurable() && !module.isEditable() ) {
163             headerResource = "shim.editPage.panelHeaderPanelConfigure";
164             footerResource = "shim.editPage.panelFooterPanelConfigure";
165           }
166           else if ( !module.isConfigurable() && module.isEditable() ) {
167             headerResource = "shim.editPage.panelHeaderPanelEdit";
168             footerResource = "shim.editPage.panelFooterPanelEdit";
169           }
170           else if ( module.isConfigurable() && module.isEditable() ) {
171             headerResource = "shim.editPage.panelHeaderPanelConfigureEdit";
172             footerResource = "shim.editPage.panelFooterPanelConfigureEdit";
173           }
174
175           //
176
// print the header if there is one
177
//
178
if ( headerResource != null ) {
179
180             //
181
// add edit controls if we're in edit mode, but not previewing
182
//
183
pageContext.getOut().println(
184               messageResources.getMessage(
185                 headerResource,
186                 "" + page.getInt( "id" ),
187                 name_ ) );
188           }
189         }
190
191         //
192
// display the module
193
//
194
if ( module == null )
195           pageContext.getOut().println(
196             messageResources.getMessage( "shim.panelNotSet" ) );
197         else
198           module.display(
199             ( HttpServletRequest JavaDoc )pageContext.getRequest(),
200             ( HttpServletResponse JavaDoc )pageContext.getResponse(),
201             pageContext.getOut() );
202
203         if ( ShimUtils.displayEditElements(
204                ( HttpServletRequest JavaDoc )pageContext.getRequest() ) ) {
205           //
206
// print the footer if there is one
207
//
208
if ( footerResource != null ) {
209             pageContext.getOut().println(
210               messageResources.getMessage(
211                 footerResource,
212                 "" + page.getInt( "id" ),
213                 name_ ) );
214           }
215         }
216       }
217
218       //
219
// otherwise, just display the module
220
//
221
else {
222         if ( module == null )
223           pageContext.getOut().println(
224             messageResources.getMessage( "shim.panelNotSet" ) );
225         else
226           module.display(
227             ( HttpServletRequest JavaDoc )pageContext.getRequest(),
228             ( HttpServletResponse JavaDoc )pageContext.getResponse(),
229             pageContext.getOut() );
230       }
231     }
232     catch ( IOException JavaDoc e ) {
233       throw new JspException JavaDoc(
234         "Unexpected IOException when displaying panel \"" + name_ +
235         "\": " + e.toString() );
236     }
237
238     return SKIP_BODY;
239   }
240
241   // properties ///////////////////////////////////////////////////////////////
242

243   public String JavaDoc getName() {
244     return name_;
245   }
246
247   public void setName( String JavaDoc name ) {
248     name_ = name;
249   }
250
251   public String JavaDoc getDefaultModule() {
252     return defaultModule_;
253   }
254
255   public void setDefaultModule( String JavaDoc defaultModule ) {
256     defaultModule_ = defaultModule;
257   }
258
259   // attributes ///////////////////////////////////////////////////////////////
260

261   private String JavaDoc name_ = "";
262
263   private String JavaDoc defaultModule_ = "";
264 }
265
Popular Tags