001    /*
002     * Copyright 2004 Chris Nelson
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 http://www.apache.org/licenses/LICENSE-2.0
007     * Unless required by applicable law or agreed to in writing,
008     * software distributed under the License is distributed on an "AS IS" BASIS,
009     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010     * See the License for the specific language governing permissions and limitations under the License.
011     */
012    package org.trails.component;
013    
014    import org.apache.tapestry.AbstractComponent;
015    import org.apache.tapestry.IMarkupWriter;
016    import org.apache.tapestry.IRequestCycle;
017    import org.apache.tapestry.annotations.Component;
018    import org.apache.tapestry.annotations.ComponentClass;
019    import org.apache.tapestry.annotations.Parameter;
020    import org.apache.tapestry.components.Block;
021    import org.apache.tapestry.components.RenderBlock;
022    import org.apache.tapestry.util.ComponentAddress;
023    import org.trails.finder.BlockFinder;
024    import org.trails.descriptor.IPropertyDescriptor;
025    import org.trails.descriptor.IClassDescriptor;
026    import org.trails.page.IEditorBlockPage;
027    
028    @ComponentClass(allowBody = true, allowInformalParameters = true)
029    public abstract class PropertyEditor extends AbstractComponent
030    {
031    
032            @Parameter(defaultValue = "container.classDescriptor")
033            public abstract IClassDescriptor getClassDescriptor();
034    
035            @Parameter(defaultValue = "container.property")
036            public abstract IPropertyDescriptor getDescriptor();
037    
038            @Parameter(defaultValue = "container.model")
039            public abstract Object getModel();
040    
041            @Parameter(defaultValue = "container.modelNew")
042            public abstract boolean isModelNew();
043    
044            @Parameter(defaultValue = "container.blockFinder")
045            public abstract BlockFinder getBlockFinder();
046    
047            protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
048            {
049                    getRenderBlock().render(writer, cycle);
050            }
051    
052            public Block getBlock()
053            {
054    
055                    Block editorBlock = (Block)
056                                    getEditorAddress().findComponent(getPage().getRequestCycle());
057    
058                    ((IEditorBlockPage) editorBlock.getPage()).setModel(getModel());
059                    ((IEditorBlockPage) editorBlock.getPage()).setModelNew(isModelNew());
060                    ((IEditorBlockPage) editorBlock.getPage()).setDescriptor(getDescriptor());
061                    ((IEditorBlockPage) editorBlock.getPage()).setClassDescriptor(getClassDescriptor());
062    
063                    return editorBlock;
064            }
065    
066            public ComponentAddress getEditorAddress()
067            {
068    
069                    return getBlockFinder().findBlockAddress(getDescriptor());
070            }
071    
072            @Component(bindings = "block=ognl:block")
073            public abstract RenderBlock getRenderBlock();
074    
075    }