001: /*
002: * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.widget;
030:
031: import java.util.AbstractMap;
032: import java.util.Collections;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.Set;
036:
037: /**
038: * The state for a widget is restored from and saved to a String[].
039: */
040: abstract public class WidgetState extends
041: AbstractMap<String, WidgetState> {
042: private Widget _widget;
043: private WidgetState _parent;
044: WidgetMode _widgetMode; // package access for Widget;
045:
046: protected WidgetState() {
047: }
048:
049: void setWidget(Widget widget) {
050: _widget = widget;
051: }
052:
053: public Widget getWidget() {
054: return _widget;
055: }
056:
057: void setParent(WidgetState parent) {
058: _parent = parent;
059: }
060:
061: public WidgetState getParent() {
062: return _parent;
063: }
064:
065: public Set<Map.Entry<String, WidgetState>> entrySet() {
066: return (Set<Map.Entry<String, WidgetState>>) Collections.EMPTY_SET;
067: }
068:
069: public WidgetState put(String id, WidgetState value) {
070: throw new UnsupportedOperationException();
071: }
072:
073: public Object getValue() {
074: throw new UnsupportedOperationException();
075: }
076:
077: /**
078: * Called once for each request to restore the state of the widget
079: * for the request.
080: */
081: abstract public void decode(String[] data) throws WidgetException;
082:
083: abstract public String[] encode() throws WidgetException;
084:
085: public List<WidgetWarning> getWarnings() {
086: return null;
087: }
088:
089: public List<WidgetError> getErrors() {
090: return null;
091: }
092:
093: /**
094: * Set the mode for this widget. The default is to use the mode set for the
095: * widget, or if that has not been set to inherit the mode from the parent
096: * state.
097: */
098: public void setWidgetMode(WidgetMode widgetMode) {
099: if (_widget.isWidgetModeAllowed(widgetMode))
100: _widgetMode = widgetMode;
101: }
102:
103: /**
104: * If the widgetMode has not been set, then the parent's mode is returned, if
105: * there is no parent then WidgetMode.VIEW is returned.
106: */
107: final public WidgetMode getWidgetMode() {
108: WidgetMode widgetMode = _widgetMode;
109:
110: if (widgetMode == null) {
111: widgetMode = getWidget().getWidgetMode();
112:
113: if (widgetMode == null) {
114: if (_parent != null)
115: widgetMode = _parent.getWidgetMode();
116:
117: if (widgetMode == null)
118: widgetMode = WidgetMode.VIEW;
119: }
120: }
121:
122: return widgetMode;
123: }
124:
125: void resetAll() {
126: reset();
127:
128: _widget = null;
129: _parent = null;
130: _widgetMode = null;
131: }
132:
133: /**
134: * Reset all member variables to the state immediately following
135: * construction.
136: */
137: abstract public void reset();
138: }
|