001: /*
002: * Copyright 2007 Raffaele Specia (raffaele.specia@assetdata.it)
003: *
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License.
007: *
008: * You may obtain a copy of the License at
009: * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
010: * or agreed to in writing, software distributed under the License is
011: * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
012: * KIND, either express or implied. See the License for the specific language
013: * governing permissions and limitations under the License.
014: */
015: package org.romaframework.module.designer.view.domain.radpage.aspect;
016:
017: import java.util.Collection;
018:
019: import nextapp.echo2.app.Alignment;
020: import nextapp.echo2.app.Component;
021: import nextapp.echo2.app.Extent;
022: import nextapp.echo2.app.Grid;
023: import nextapp.echo2.app.Insets;
024: import nextapp.echo2.app.layout.GridLayoutData;
025:
026: import org.romaframework.aspect.core.annotation.AnnotationConstants;
027: import org.romaframework.aspect.view.annotation.ViewField;
028: import org.romaframework.aspect.view.echo2.area.AreaInstance;
029: import org.romaframework.core.domain.type.TreeNode;
030: import org.romaframework.module.designer.view.domain.dnd.component.DnDAreaComponent;
031: import org.romaframework.module.designer.view.domain.dnd.component.PlaceholderArea;
032: import org.romaframework.module.designer.view.domain.dnd.component.TrashComponent;
033: import org.romaframework.module.designer.view.domain.dnd.controller.DnDController;
034: import org.romaframework.module.designer.view.domain.dnd.util.AreaWrapperFactory;
035: import org.romaframework.module.designer.view.domain.dnd.util.DnDUtils;
036: import org.romaframework.module.designer.view.domain.dnd.util.SourceManager;
037:
038: import echopointng.ContainerEx;
039:
040: public class FormAreaEditingPage extends ContainerEx {
041: private DnDController controller;
042: private SourceManager sourceManager;
043: private DnDUtils utils;
044: private Grid panel;
045: private DnDAreaComponent rootArea;
046:
047: public FormAreaEditingPage(AreaInstance root) {
048: controller = new DnDController(this );
049: sourceManager = new SourceManager();
050: utils = new DnDUtils();
051: create(root);
052: }
053:
054: private void create(AreaInstance iRootArea) {
055: panel = new Grid();
056: panel.setInsets(new Insets(10));
057: panel.setWidth(new Extent(100, Extent.PERCENT));
058: panel.setSize(3);
059: add(panel);
060: ContainerEx menu = sourceManager.addSources();
061: menu.setHeight(new Extent(300));
062: GridLayoutData menuLayout = new GridLayoutData();
063: menuLayout.setAlignment(Alignment.ALIGN_TOP);
064: menu.setLayoutData(menuLayout);
065: TrashComponent trash = new TrashComponent(this );
066:
067: GridLayoutData trashLayout = new GridLayoutData();
068: trashLayout.setAlignment(Alignment.ALIGN_CENTER);
069: trash.setLayoutData(trashLayout);
070:
071: controller.registerTrashComponentTarget(trash);
072: // utils.clearAreaNames();
073: rootArea = createFromInstance(null, iRootArea, 0);
074:
075: panel.add(menu);
076: panel.add(rootArea);
077: panel.add(trash);
078: }
079:
080: public DnDController getController() {
081: return controller;
082: }
083:
084: public DnDUtils getUtils() {
085: return utils;
086: }
087:
088: public SourceManager getSourceManager() {
089: return sourceManager;
090: }
091:
092: private AreaInstance insideWalker(Component iComponent,
093: AreaInstance parent) {
094: AreaInstance instance = null;
095: if (iComponent instanceof DnDAreaComponent) {
096: String areaName = ((DnDAreaComponent) iComponent)
097: .getAreaName();
098: if (areaName == null || areaName.trim().equals(""))
099: areaName = utils.getFakeAreaName();
100: instance = new AreaInstance(parent, parent, areaName,
101: ((DnDAreaComponent) iComponent).getAreaType(),
102: ((DnDAreaComponent) iComponent).getAreaSize());
103: if (parent == null)
104: parent = instance;
105: }
106: Component[] children = iComponent.getComponents();
107: for (Component child : children) {
108: if (parent != null) {
109: AreaInstance tmpInstance = insideWalker(child, instance);
110: if (tmpInstance != null)
111: parent.addChild(tmpInstance);
112: }
113: }
114: return instance;
115: }
116:
117: private DnDAreaComponent createFromInstance(
118: DnDAreaComponent iParent, AreaInstance iAreaInstance,
119: int iPosition) {
120: if (iAreaInstance != null) {
121: DnDAreaComponent component = AreaWrapperFactory
122: .getComponentArea(iAreaInstance, this );
123: Collection<? extends TreeNode> children = iAreaInstance
124: .getChildren();
125: if (children != null) {
126: int counter = 0;
127: for (TreeNode treeNode : children) {
128: DnDAreaComponent tmpComponent = createFromInstance(
129: component, (AreaInstance) treeNode, counter);
130: if (tmpComponent != null)
131: component.addInnerComponent(tmpComponent,
132: counter);
133: counter++;
134: }
135: }
136: return component;
137: } else {
138: DnDAreaComponent main = new PlaceholderArea(this );
139: controller.registerComponentAsTarget(main);
140: return main;
141: }
142: }
143:
144: /**
145: * Return the root of the edited area
146: *
147: * @return
148: */
149: @ViewField(visible=AnnotationConstants.FALSE)
150: public AreaInstance getRootArea() {
151: AreaInstance result = insideWalker(rootArea, null);
152: return result;
153: }
154:
155: @Override
156: public String toString() {
157: return "Layout";
158: }
159: }
|