001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.core;
016:
017: import org.araneaframework.Component;
018: import org.araneaframework.Environment;
019: import org.araneaframework.OutputData;
020: import org.araneaframework.Scope;
021: import org.araneaframework.core.BaseApplicationWidget;
022: import org.araneaframework.core.ProxyEventListener;
023: import org.araneaframework.framework.FlowContext;
024: import org.araneaframework.framework.LocalizationContext;
025: import org.araneaframework.framework.MessageContext;
026: import org.araneaframework.framework.MountContext;
027: import org.araneaframework.framework.OverlayContext;
028: import org.araneaframework.framework.OverlayContext.OverlayActivityMarkerContext;
029: import org.araneaframework.http.JspContext;
030: import org.araneaframework.http.util.ServletUtil;
031: import org.araneaframework.uilib.ConfigurationContext;
032: import org.springframework.beans.factory.BeanFactory;
033:
034: /**
035: * This widget represents the usual custom application widget that is rendered using
036: * JSP tags. It assumes to be connected with a JSP page and allows setting its view selector.
037: *
038: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
039: */
040: public class BaseUIWidget extends BaseApplicationWidget {
041: protected String viewSelector;
042:
043: /**
044: * Sets the view selector for this widget, should be path to <code>jsp</code> file
045: * without <code>jsp</code> extension.
046: *
047: * @param viewSelector path to <code>jsp</code> file, without file extension
048: */
049: protected void setViewSelector(String viewSelector) {
050: this .viewSelector = viewSelector;
051: }
052:
053: protected ConfigurationContext getConfiguration() {
054: return (ConfigurationContext) getEnvironment().requireEntry(
055: ConfigurationContext.class);
056: }
057:
058: protected FlowContext getFlowCtx() {
059: return (FlowContext) getEnvironment().requireEntry(
060: FlowContext.class);
061: }
062:
063: protected MessageContext getMessageCtx() {
064: return (MessageContext) getEnvironment().requireEntry(
065: MessageContext.class);
066: }
067:
068: protected LocalizationContext getL10nCtx() {
069: return (LocalizationContext) getEnvironment().requireEntry(
070: LocalizationContext.class);
071: }
072:
073: protected MountContext getMountCtx() {
074: return (MountContext) getEnvironment().requireEntry(
075: MountContext.class);
076: }
077:
078: protected BeanFactory getBeanFactory() {
079: return (BeanFactory) getEnvironment().requireEntry(
080: BeanFactory.class);
081: }
082:
083: protected OverlayContext getOverlayCtx() {
084: return (OverlayContext) getEnvironment().getEntry(
085: OverlayContext.class);
086: }
087:
088: /** @since 1.1 */
089: protected boolean isRunningInOverlay() {
090: return (getEnvironment()
091: .getEntry(OverlayActivityMarkerContext.class)) != null;
092: }
093:
094: /**
095: * Translates the message under the given key,
096: * with help from widget's current {@link org.araneaframework.framework.LocalizationContext}.
097: *
098: * @param key
099: * @return message under the key translated into language corresponding to current <code>Locale</code>
100: */
101: protected String t(String key) {
102: return getL10nCtx().localize(key);
103: }
104:
105: /**
106: * Renders widget to <code>output</code> using the defined <code>viewSelector</code>.
107: */
108: protected void render(OutputData output) throws Exception {
109: if (viewSelector == null)
110: throw new RuntimeException("Widget '"
111: + getClass().getName()
112: + "' does not have a view selector!");
113:
114: JspContext jspCtx = (JspContext) getEnvironment().requireEntry(
115: JspContext.class);
116:
117: String jsp = resolveJspName(jspCtx, viewSelector);
118: ServletUtil.include(jsp, this , output);
119: }
120:
121: protected String resolveJspName(JspContext jspCtx,
122: String viewSelector) {
123: return jspCtx.getJspPath() + "/" + viewSelector
124: + jspCtx.getJspExtension();
125: }
126:
127: public Component.Interface _getComponent() {
128: return new ComponentImpl();
129: }
130:
131: protected class ComponentImpl extends
132: BaseApplicationWidget.ComponentImpl {
133: public synchronized void init(Scope scope, Environment env) {
134: setGlobalEventListener(new ProxyEventListener(
135: BaseUIWidget.this));
136:
137: super.init(scope, env);
138: }
139: }
140: }
|