001: package org.araneaframework.uilib.tab;
002:
003: import org.araneaframework.Component;
004: import org.araneaframework.Environment;
005: import org.araneaframework.Scope;
006: import org.araneaframework.Widget;
007: import org.araneaframework.core.Assert;
008: import org.araneaframework.core.BaseApplicationWidget;
009: import org.araneaframework.core.WidgetFactory;
010: import org.araneaframework.http.util.EnvironmentUtil;
011:
012: /**
013: * Represents a tab managed by {@link TabContainerContext} implementation {@link TabContainerWidget}.
014: * Tab consists of <i>label</i> and <i>content</i>, represented either by {@link Widget}s (for stateful tabs)
015: * or {@link WidgetFactory}ies (for stateless tabs). Difference between stateful and stateless tabs is that
016: * stateless tabs forget the state when they become inactive (deselected).
017: *
018: * @author Taimo Peelo (taimo@araneaframework.org)
019: * @since 1.1
020: */
021: public class TabWidget extends BaseApplicationWidget {
022: private static final long serialVersionUID = 1L;
023:
024: /** Child key for tab's label widget. */
025: public static final String LABEL_WIDGET_KEY = "tlw";
026: /** Child key for tab's content widget. */
027: public static final String CONTENT_WIDGET_KEY = "tcw";
028:
029: protected String labelId;
030: protected Widget labelWidget;
031: protected Widget tabContentWidget;
032: protected WidgetFactory tabContentWidgetFactory;
033: protected boolean disabled = false;
034:
035: /* CONSTRUCTORS of all kinds */
036: protected TabWidget(Widget tabContentWidget) {
037: this .tabContentWidget = tabContentWidget;
038: }
039:
040: protected TabWidget(WidgetFactory tabContentWidgetFactory) {
041: this .tabContentWidgetFactory = tabContentWidgetFactory;
042: }
043:
044: public TabWidget(String labelId, Widget tabContentWidget) {
045: this (tabContentWidget);
046: this .labelId = labelId;
047: }
048:
049: public TabWidget(Widget labelWidget, Widget tabContentWidget) {
050: this (tabContentWidget);
051: this .labelWidget = labelWidget;
052: }
053:
054: public TabWidget(String labelId,
055: WidgetFactory tabContentWidgetFactory) {
056: this (tabContentWidgetFactory);
057: this .labelId = labelId;
058: }
059:
060: public TabWidget(Widget labelWidget,
061: WidgetFactory tabContentWidgetFactory) {
062: this (tabContentWidgetFactory);
063: this .labelWidget = labelWidget;
064: }
065:
066: /* enabling/disabling/deselecting */
067: public void enableTab() {
068: disabled = false;
069: if (_getDisabledChildren().containsKey(CONTENT_WIDGET_KEY)) {
070: enableWidget(CONTENT_WIDGET_KEY);
071: } else if (!_getChildren().containsKey(CONTENT_WIDGET_KEY)) {
072: if (isStateless())
073: addWidget(CONTENT_WIDGET_KEY, tabContentWidgetFactory
074: .buildWidget(getEnvironment()));
075: else
076: addWidget(CONTENT_WIDGET_KEY, tabContentWidget);
077: }
078: }
079:
080: public void disableTab() {
081: disabled = true;
082: if (_getDisabledChildren().containsKey(CONTENT_WIDGET_KEY))
083: disableWidget(CONTENT_WIDGET_KEY);
084: }
085:
086: public void deleselectTab() {
087: if (isStateless()) {
088: removeWidget(CONTENT_WIDGET_KEY);
089: }
090: }
091:
092: /* PUBLIC GETTERS */
093: public String getLabel() {
094: if (labelId != null)
095: return EnvironmentUtil.requireLocalizationContext(
096: getEnvironment()).localize(labelId);
097: return null;
098: }
099:
100: public Widget getLabelWidget() {
101: return labelWidget;
102: }
103:
104: public Widget getTabContentWidget() {
105: return isStateless() ? getWidget(CONTENT_WIDGET_KEY)
106: : tabContentWidget;
107: }
108:
109: public boolean isTabDisabled() {
110: return disabled;
111: }
112:
113: public boolean isSelected() {
114: if (!isInitialized())
115: return false;
116: return getTabContainerContext().isTabSelected(
117: getScope().getId().toString());
118: }
119:
120: public boolean isStateless() {
121: return tabContentWidgetFactory != null;
122: }
123:
124: /* ****************** COMPONENT LIFECYCLE METHODS ************************** */
125: public Component.Interface _getComponent() {
126: return new ComponentImpl();
127: }
128:
129: protected class ComponentImpl extends
130: BaseApplicationWidget.ComponentImpl {
131: public synchronized void init(Scope scope, Environment env) {
132: super .init(scope, env);
133: TabContainerContext tabContainer = getTabContainerContext();
134: Assert
135: .notNull(
136: this ,
137: tabContainer,
138: "TabWidget initialization failed due to TabContainerContext missing from Environment. Make sure that TabWidget is child of TabContainerWidget");
139:
140: TabRegistrationContext tabRegistrationContext = getTabRegistrationContext();
141: Assert
142: .notNull(
143: this ,
144: tabRegistrationContext,
145: "TabWidget initialization failed due to TabRegistrationContext missing from Environment. Make sure that TabWidget is child of TabRegistrationContext");
146: tabRegistrationContext.registerTab(TabWidget.this );
147:
148: if (labelWidget != null)
149: addWidget(LABEL_WIDGET_KEY, labelWidget);
150: }
151: }
152:
153: protected void destroy() throws Exception {
154: TabRegistrationContext tabRegistrationContext = (TabRegistrationContext) getEnvironment()
155: .requireEntry(TabRegistrationContext.class);
156: tabRegistrationContext.unregisterTab(TabWidget.this );
157:
158: super .destroy();
159: }
160:
161: /* ****************** PROTECTED METHODS ************************** */
162: protected TabContainerContext getTabContainerContext() {
163: return (TabContainerContext) getEnvironment().getEntry(
164: TabContainerContext.class);
165: }
166:
167: protected TabRegistrationContext getTabRegistrationContext() {
168: return (TabRegistrationContext) getEnvironment().getEntry(
169: TabRegistrationContext.class);
170: }
171: }
|