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 java.util.Collections;
018: import java.util.Map;
019: import org.apache.commons.collections.map.LinkedMap;
020: import org.araneaframework.Component;
021: import org.araneaframework.Composite;
022: import org.araneaframework.Environment;
023: import org.araneaframework.Message;
024: import org.araneaframework.Scope;
025: import org.araneaframework.Viewable;
026: import org.araneaframework.core.util.ExceptionUtil;
027:
028: /**
029: * A component with support for composite.
030: *
031: * @author "Toomas Römer" <toomas@webmedia.ee>
032: */
033: public abstract class BaseApplicationComponent extends BaseComponent
034: implements ApplicationComponent {
035: //*******************************************************************
036: // PROTECTED CLASSES
037: //*******************************************************************
038: protected class ViewModel implements
039: ApplicationComponent.ComponentViewModel {
040: /**
041: * @see ApplicationComponent.ComponentViewModel#getScope()
042: * @since 1.1 */
043: public Scope getScope() {
044: return BaseApplicationComponent.this .getScope();
045: }
046:
047: public Map getChildren() {
048: return BaseApplicationComponent.this .getChildren();
049: }
050: }
051:
052: protected class ViewableImpl implements Viewable.Interface {
053: public Object getViewModel() {
054: try {
055: return BaseApplicationComponent.this .getViewModel();
056: } catch (Exception e) {
057: throw ExceptionUtil.uncheckException(e);
058: }
059: }
060: }
061:
062: protected class CompositeComponentImpl implements
063: Composite.Interface {
064: public Map getChildren() {
065: return BaseApplicationComponent.this .getChildren();
066: }
067:
068: public void attach(Object key, Component comp) {
069: _getChildren().put(key, comp);
070: }
071:
072: public Component detach(Object key) {
073: return (Component) _getChildren().remove(key);
074: }
075: }
076:
077: //*******************************************************************
078: // PUBLIC METHODS
079: //*******************************************************************
080: /**
081: * Returns a unmodifiable map of all the child components under this Component.
082: * @return a map of child components
083: */
084: public Map getChildren() {
085: return Collections
086: .unmodifiableMap(new LinkedMap(_getChildren()));
087: }
088:
089: public Viewable.Interface _getViewable() {
090: return new ViewableImpl();
091: }
092:
093: public Composite.Interface _getComposite() {
094: return new CompositeComponentImpl();
095: }
096:
097: /**
098: * Adds a component with the specified key. Allready initilized component cannot be added.
099: * Duplicate keys not allowed. The child is initialized with the Environment env.
100: */
101: public void addComponent(Object key, Component child,
102: Environment env) {
103: _addComponent(key, child, env);
104: }
105:
106: /**
107: * Adds a component with the specified key. Allready initilized components cannot be added.
108: * Duplicate keys not allowed. The child is initialized with the Environment from
109: * <code>getChildComponentEnvironment()</code>.
110: * @throws Exception
111: */
112: public void addComponent(Object key, Component child) {
113: _addComponent(key, child, getChildComponentEnvironment());
114: }
115:
116: /**
117: * Relocates parent's child with keyFrom to this component with a new key keyTo. The child
118: * will get the Environment specified by newEnv.
119: * @param parent is the current parent of the child to be relocated.
120: * @param newEnv the new Environment of the child.
121: * @param keyFrom is the key of the child to be relocated.
122: * @param keyTo is the the key, with which the child will be added to this StandardService.
123: */
124: public void relocateComponent(Composite parent, Environment newEnv,
125: Object keyFrom, Object keyTo) {
126: _relocateComponent(parent, newEnv, keyFrom, keyTo);
127: }
128:
129: /**
130: * Relocates parent's child with keyFrom to this service with a new key keyTo. The child
131: * will get the Environment of this StandardService.
132: * @param parent is the current parent of the child to be relocated.
133: * @param keyFrom is the key of the child to be relocated.
134: * @param keyTo is the the key, with which the child will be added to this StandardService.
135: */
136: public void relocateComponent(Composite parent, Object keyFrom,
137: Object keyTo) {
138: _relocateComponent(parent, getChildComponentEnvironment(),
139: keyFrom, keyTo);
140: }
141:
142: /**
143: * Enables the component with the specified key. Only a disabled componet can be enabled.
144: */
145: public void enableComponent(Object key) {
146: _enableComponent(key);
147: }
148:
149: /**
150: * Disables the component with the specified key. Only a enabled component can be disabled.
151: */
152: public void disableComponent(Object key) {
153: _disableComponent(key);
154: }
155:
156: /**
157: * Removes the component with the specified key.
158: */
159: public void removeComponent(Object key) {
160: _removeComponent(key);
161: }
162:
163: protected void propagate(Message message) {
164: _propagate(message);
165: }
166:
167: public Environment getEnvironment() {
168: return super .getEnvironment();
169: }
170:
171: public Environment getChildEnvironment() {
172: return getChildComponentEnvironment();
173: }
174:
175: //*******************************************************************
176: // PROTECTED METHODS
177: //*******************************************************************
178: /**
179: * Returns the view model. Usually overridden.
180: */
181: protected Object getViewModel() throws Exception {
182: return new ViewModel();
183: }
184:
185: /**
186: * Returns the the Environment of this Component by default. Usually overridden.
187: */
188: protected Environment getChildComponentEnvironment() {
189: return getEnvironment();
190: }
191: }
|