001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
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: */
016:
017: package org.romaframework.aspect.view.echo2.component;
018:
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import nextapp.echo2.app.Column;
024: import nextapp.echo2.app.Component;
025: import nextapp.echo2.app.Label;
026: import nextapp.echo2.app.Row;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.romaframework.aspect.view.ViewConstants;
031: import org.romaframework.aspect.view.echo2.form.EntityForm;
032: import org.romaframework.aspect.view.form.ContentComponent;
033: import org.romaframework.aspect.view.form.FormPool;
034: import org.romaframework.core.config.RomaApplicationContext;
035: import org.romaframework.core.flow.ObjectContext;
036:
037: public class DynaComponentSet extends AbstractContentComponent {
038:
039: public DynaComponentSet(ContentComponent iParent) {
040: super (iParent);
041: setOrientation(ViewConstants.ORIENTATION_VERTICAL);
042: }
043:
044: public void setOrientation(String iOrientation) {
045: if (getComponentCount() > 0)
046: remove(0);
047:
048: if (iOrientation == ViewConstants.ORIENTATION_HORIZONTAL)
049: frame = new Row();
050: else
051: frame = new Column();
052: add(frame);
053: }
054:
055: public void addElement(Component iComponent) {
056: frame.add(iComponent);
057: }
058:
059: public void addElement(Object iValue) {
060: Component childComponent;
061:
062: if (iValue.getClass().equals(String.class)) {
063: // STRING VALUE
064: Label label = new Label();
065: label.setText(iValue.toString());
066: childComponent = label;
067: } else if (iValue instanceof Map.Entry) {
068: Map.Entry entry = (Map.Entry) iValue;
069:
070: childComponent = new Row();
071: childComponent.add(new Label(entry.getKey().toString()));
072: childComponent.add(getComponentForObject(entry.getValue()));
073: } else {
074: // COMPLEX TYPE VALUE
075: childComponent = getComponentForObject(iValue);
076: }
077:
078: frame.add(childComponent);
079: }
080:
081: private Component getComponentForObject(Object iValue) {
082: try {
083: if (iValue instanceof String || iValue instanceof Integer
084: || iValue instanceof Short
085: || iValue instanceof Float
086: || iValue instanceof Double
087: || iValue instanceof Long)
088: return ObjectContext.getInstance().getComponent(
089: ComponentFactory.class).createTextComponent(
090: (EntityForm) getContainerComponent(),
091: schemaField);
092: else {
093: EntityForm childForm = (EntityForm) RomaApplicationContext
094: .getInstance().getBean(FormPool.class).getForm(
095: schemaInstance, schemaField, iValue);
096: childForm.renderContent();
097: return childForm;
098: }
099: } catch (Exception e) {
100: log.error("[DynaComponentSet.getComponent]", e);
101: }
102: return null;
103: }
104:
105: public void addElements(Object value) {
106: // FILL VALUES
107: if (value instanceof List) {
108: for (Iterator it = ((List) value).iterator(); it.hasNext();)
109: addElement(it.next());
110: } else if (value instanceof Map) {
111: for (Iterator it = ((Map) value).entrySet().iterator(); it
112: .hasNext();)
113: addElement(it.next());
114: }
115: }
116:
117: public void render(ContentComponent iFormToRender) {
118: }
119:
120: private Component frame;
121:
122: private static Log log = LogFactory.getLog(DynaComponentSet.class);
123: }
|