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.core;
016:
017: import org.araneaframework.InputData;
018: import org.araneaframework.OutputData;
019: import org.araneaframework.Path;
020: import org.araneaframework.Widget;
021: import org.araneaframework.core.util.ExceptionUtil;
022:
023: /**
024: * Non-composite widget component.
025: * <p>
026: * Compared to BaseService has the following extra
027: * functionality:
028: * <ul>
029: * <li>update(InputData) - update the state of the widget with the data in InputData</li>
030: * <li>event(Path, InputData) - handling a event</li>
031: * <li>render(OutputData) - rendering this Widget to OutputData</li>
032: * </ul>
033: * </p>
034: * @author "Toomas Römer" <toomas@webmedia.ee>
035: */
036: public abstract class BaseWidget extends BaseService implements Widget {
037:
038: /**
039: * Returns a widget's internal implementation.
040: * @return the widget's implementation
041: */
042: public Widget.Interface _getWidget() {
043: return new WidgetImpl();
044: }
045:
046: protected class WidgetImpl implements Widget.Interface {
047:
048: public void update(InputData input) {
049: Assert.notNullParam(this , input, "input");
050:
051: _startCall();
052: currentInputData = input;
053: try {
054: BaseWidget.this .update(input);
055: } catch (Exception e) {
056: try {
057: handleWidgetException(e);
058: } catch (Exception e2) {
059: ExceptionUtil.uncheckException(e2);
060: }
061: } finally {
062: currentInputData = null;
063: _endCall();
064: }
065: }
066:
067: public void event(Path path, InputData input) {
068: Assert.notNullParam(this , input, "input");
069:
070: _startCall();
071: currentInputData = input;
072: try {
073: BaseWidget.this .event(path, input);
074: } catch (Exception e) {
075: try {
076: handleWidgetException(e);
077: } catch (Exception e2) {
078: ExceptionUtil.uncheckException(e2);
079: }
080: } finally {
081: currentInputData = null;
082: _endCall();
083: }
084: }
085:
086: public void render(OutputData output) {
087: Assert.notNullParam(this , output, "output");
088:
089: _startCall();
090: currentOutputData = output;
091: try {
092: BaseWidget.this .render(output);
093: } catch (Exception e) {
094: try {
095: handleWidgetException(e);
096: } catch (Exception e2) {
097: ExceptionUtil.uncheckException(e2);
098: }
099: } finally {
100: currentOutputData = null;
101: _endCall();
102: }
103: }
104: }
105:
106: protected void handleWidgetException(Exception e) throws Exception {
107: handleException(e);
108: }
109:
110: // Callbacks
111: protected void update(InputData input) throws Exception {
112: }
113:
114: protected void event(Path path, InputData input) throws Exception {
115: }
116:
117: protected void render(OutputData output) throws Exception {
118: }
119:
120: protected InputData getInputData() {
121: InputData input = super .getInputData();
122: OutputData output = super .getOutputData();
123:
124: // lets try to give a not null answer to the user
125: if (input == null && output != null) {
126: return output.getInputData();
127: }
128:
129: // as last resort, look in the Environment -- when result cannot be found be
130: // before, this probably means that we are still in widgets init() method.
131: if (input == null)
132: input = (InputData) getEnvironment().getEntry(
133: InputData.class);
134:
135: return input;
136: }
137:
138: protected OutputData getOutputData() {
139: OutputData output = super .getOutputData();
140: InputData input = super .getInputData();
141: if (output == null && input != null) {
142: return input.getOutputData();
143: }
144:
145: // as last resort, look in the Environment -- when result cannot be found be
146: // before, this probably means that we are still in widgets init() method.
147: if (output == null)
148: output = (OutputData) getEnvironment().getEntry(
149: OutputData.class);
150:
151: return output;
152: }
153: }
|