001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: BaseComponentTag.java 6385 2007-10-25 14:02:26Z wehrens $
004: */
005: package org.gridsphere.provider.portletui.tags;
006:
007: import org.gridsphere.portlet.impl.SportletProperties;
008: import org.gridsphere.provider.portletui.beans.BaseComponentBean;
009:
010: import javax.portlet.RenderRequest;
011: import javax.portlet.RenderResponse;
012: import javax.servlet.jsp.JspException;
013: import javax.servlet.jsp.JspWriter;
014: import javax.servlet.jsp.PageContext;
015: import javax.servlet.jsp.tagext.Tag;
016: import java.util.Locale;
017: import java.util.ResourceBundle;
018:
019: /**
020: * The abstract <code>BaseComponentTag</code> is used by all UI tags to provide CSS support and general
021: * name, value attributes
022: */
023: public abstract class BaseComponentTag extends BaseBeanTag {
024:
025: protected String name = null;
026: protected String value = null;
027: protected String key = null;
028: protected boolean readonly = false;
029: protected boolean disabled = false;
030: protected String cssStyle = null;
031: protected String cssClass = null;
032: protected Locale locale = null;
033: protected String id = null;
034:
035: /**
036: * Sets the id of the bean (not to be confused with beanId)
037: *
038: * @param id the id of the bean
039: */
040: public void setId(String id) {
041: this .id = id;
042: }
043:
044: /**
045: * Gets the id of the bean
046: *
047: * @return id of the bean
048: */
049: public String getId() {
050: return this .id;
051: }
052:
053: /**
054: * Sets the name of the bean
055: *
056: * @param name the name of the bean
057: */
058: public void setName(String name) {
059: this .name = name;
060: }
061:
062: /**
063: * Gets the name of the bean
064: *
065: * @return name of the bean
066: */
067: public String getName() {
068: return this .name;
069: }
070:
071: /**
072: * Sets the tag value
073: *
074: * @param value the tag value
075: */
076: public void setValue(String value) {
077: this .value = value;
078: }
079:
080: /**
081: * Returns the tag value
082: *
083: * @return the tag value
084: */
085: public String getValue() {
086: return value;
087: }
088:
089: /**
090: * Return the key used to identify the value in the properties file
091: *
092: * @return the key
093: */
094: public String getKey() {
095: return key;
096: }
097:
098: /**
099: * Sets the key used to identify the value in the properties file
100: *
101: * @param key the property key
102: */
103: public void setKey(String key) {
104: this .key = key;
105: }
106:
107: /**
108: * Returns true if bean is in disabled state
109: *
110: * @return the tag state
111: */
112: public boolean isDisabled() {
113: return disabled;
114: }
115:
116: /**
117: * Sets the disabled attribute of the bean to be in the 'flag' state.
118: *
119: * @param flag is true if this tag/bean is disabled
120: */
121: public void setDisabled(boolean flag) {
122: this .disabled = flag;
123: }
124:
125: /**
126: * Returns disabled String if bean is disabled
127: *
128: * @return a String depending if bean is disabled
129: */
130: protected String checkDisabled() {
131: if (disabled) {
132: return " disabled='disabled' ";
133: } else {
134: return "";
135: }
136: }
137:
138: /**
139: * Sets the bean to read-only
140: *
141: * @param flag is true if bean is read-only, false otherwise
142: */
143: public void setReadonly(boolean flag) {
144: this .readonly = flag;
145: }
146:
147: /**
148: * Returns the read-only status of the bean
149: *
150: * @return read-only status of the bean
151: */
152: public boolean isReadonly() {
153: return readonly;
154: }
155:
156: /**
157: * Returns a String to use in markup if the bean is read-only
158: *
159: * @return a String to use in markup if the bean is read-only
160: */
161: protected String checkReadonly() {
162: if (readonly) {
163: return " readonly='readonly' ";
164: } else {
165: return "";
166: }
167: }
168:
169: /**
170: * Returns the CSS style name of the beans
171: *
172: * @return the name of the css style
173: */
174: public String getCssStyle() {
175: return cssStyle;
176: }
177:
178: /**
179: * Sets the CSS style of the beans
180: *
181: * @param style css style name to set for the beans
182: */
183: public void setCssStyle(String style) {
184: this .cssStyle = style;
185: }
186:
187: /**
188: * Returns the CSS class name of the beans
189: *
190: * @return the name of the css class
191: */
192: public String getCssClass() {
193: return cssClass;
194: }
195:
196: /**
197: * Sets the CSS class of the beans
198: *
199: * @param cssClass the class name to set for the beans
200: */
201: public void setCssClass(String cssClass) {
202: this .cssClass = cssClass;
203: }
204:
205: public void addCssClass(String cssClass) {
206: if (this .cssClass == null)
207: this .cssClass = "";
208: if (cssClass != null)
209: this .cssClass += " " + cssClass;
210: }
211:
212: /**
213: * Sets the base component properties of the bean
214: *
215: * @param componentBean a ui bean
216: */
217: protected void setBaseComponentBean(BaseComponentBean componentBean) {
218: componentBean.setBeanId(beanId);
219: if (id != null)
220: componentBean.setId(id);
221: componentBean.setLocale(getLocale());
222: componentBean
223: .addParam(
224: SportletProperties.COMPONENT_ID,
225: (String) pageContext
226: .findAttribute(SportletProperties.COMPONENT_ID));
227: componentBean
228: .addParam(
229: SportletProperties.GP_COMPONENT_ID,
230: (String) pageContext
231: .findAttribute(SportletProperties.GP_COMPONENT_ID));
232: if (cssStyle != null)
233: componentBean.setCssStyle(cssStyle);
234: if (cssClass != null)
235: componentBean.setCssClass(cssClass);
236: componentBean.setDisabled(disabled);
237: componentBean.setReadOnly(readonly);
238: if (name != null)
239: componentBean.setName(name);
240: if (value != null)
241: componentBean.setValue(value);
242:
243: componentBean.setReadOnly(readonly);
244: RenderResponse res = (RenderResponse) pageContext
245: .getAttribute("renderResponse");
246: componentBean.setRenderResponse(res);
247: RenderRequest req = (RenderRequest) pageContext
248: .getAttribute("renderRequest");
249: componentBean.setRenderRequest(req);
250: }
251:
252: /**
253: * Updates the base component bean properties
254: *
255: * @param componentBean a ui bean
256: */
257: protected void updateBaseComponentBean(
258: BaseComponentBean componentBean) {
259: componentBean.setLocale(getLocale());
260: if (id != null)
261: componentBean.setId(id);
262: componentBean
263: .addParam(
264: SportletProperties.COMPONENT_ID,
265: (String) pageContext
266: .findAttribute(SportletProperties.COMPONENT_ID));
267: componentBean
268: .addParam(
269: SportletProperties.GP_COMPONENT_ID,
270: (String) pageContext
271: .findAttribute(SportletProperties.GP_COMPONENT_ID));
272: if ((cssClass != null) && componentBean.getCssClass() == null) {
273: componentBean.setCssClass(cssClass);
274: }
275: if ((cssStyle != null) && componentBean.getCssStyle() == null) {
276: componentBean.setCssStyle(cssStyle);
277: }
278: if ((name != null) && (componentBean.getName() == null)) {
279: componentBean.setName(name);
280: }
281: if ((value != null) && (componentBean.getValue() == null)) {
282: componentBean.setValue(value);
283: }
284: RenderResponse res = (RenderResponse) pageContext
285: .getAttribute("renderResponse");
286: componentBean.setRenderResponse(res);
287: RenderRequest req = (RenderRequest) pageContext
288: .getAttribute("renderRequest");
289: componentBean.setRenderRequest(req);
290: }
291:
292: protected String getLocalizedText(String key) {
293: return getLocalizedText(key, "Portlet");
294: }
295:
296: protected Locale getLocale() {
297: Locale locale = Locale.ENGLISH;
298: RenderRequest renderReq = (RenderRequest) pageContext
299: .getAttribute(SportletProperties.RENDER_REQUEST,
300: PageContext.REQUEST_SCOPE);
301: locale = renderReq.getLocale();
302: return locale;
303: }
304:
305: protected String getLocalizedText(String key, String base) {
306: Locale locale = getLocale();
307: try {
308: ResourceBundle bundle = ResourceBundle.getBundle(base,
309: locale);
310: return bundle.getString(key);
311: } catch (Exception e) {
312: try {
313: ResourceBundle bundle = ResourceBundle.getBundle(base,
314: Locale.ENGLISH);
315: return bundle.getString(key);
316: } catch (Exception ex) {
317: return key;
318: }
319: }
320: }
321:
322: public int doStartTag() throws JspException {
323: Tag parent = getParent();
324: if (parent instanceof PanelTag) {
325: PanelTag panelTag = (PanelTag) parent;
326:
327: int numCols = panelTag.getNumCols();
328:
329: int this col = panelTag.getColumnCounter();
330: try {
331: JspWriter out = pageContext.getOut();
332: if ((this col % numCols) == 0) {
333: out.println("<tr>");
334: }
335: // Attribute 'width' replaced by 'style="width:"' for XHTML 1.0 Strict compliance
336: out.println("<td style=\"width:" + "100%" + "\">");
337: } catch (Exception e) {
338: throw new JspException(e.getMessage());
339: }
340: }
341: return EVAL_BODY_INCLUDE;
342: }
343:
344: public void release() {
345: name = null;
346: value = null;
347: key = null;
348: readonly = false;
349: disabled = false;
350: cssStyle = null;
351: cssClass = null;
352: locale = null;
353: id = null;
354: }
355:
356: public int doEndTag() throws JspException {
357: Tag parent = getParent();
358: if (parent instanceof PanelTag) {
359: PanelTag panelTag = (PanelTag) parent;
360:
361: int numCols = panelTag.getNumCols();
362: int this col = panelTag.getColumnCounter();
363: this col++;
364: panelTag.setColumnCounter(this col);
365: try {
366: JspWriter out = pageContext.getOut();
367: out.println("</td>");
368: if ((this col % numCols) == 0) {
369: out.println("</tr>");
370: }
371: } catch (Exception e) {
372: throw new JspException(e.getMessage());
373: }
374: }
375: return super .doEndTag();
376: }
377:
378: /*
379: public void updatePageEndBuffer(StringBuffer buffer) {
380: RenderRequest req = (RenderRequest)pageContext.getAttribute(SportletProperties.RENDER_REQUEST, PageContext.REQUEST_SCOPE);
381: StringBuffer pagebuffer = (StringBuffer)req.getAttribute(SportletProperties.PAGE_BUFFER);
382: if (pagebuffer == null) pagebuffer = new StringBuffer();
383: pagebuffer.append(buffer);
384: req.setAttribute(SportletProperties.PAGE_BUFFER, pagebuffer);
385: }
386: */
387: }
|