001    /*
002     * Copyright 2005 Stephen J. McConnell.
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
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.test.composite;
020    
021    import java.awt.Color;
022    import java.util.Map;
023    
024    import net.dpml.logging.Logger;
025    
026    import net.dpml.test.ColorManager;
027    
028    /**
029     * This component declares an inner Parts interface through which 
030     * it accesses a map of the context infomation of a subsidiary component
031     * named 'child'. The subsidiary component is defined and established 
032     * within the component types definition (see build file for details).
033     * 
034     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
035     * @version 1.2.0 
036     */
037    public class CompositeComponent implements ColorManager
038    {
039        //------------------------------------------------------------------
040        // concerns
041        //------------------------------------------------------------------
042    
043       /**
044        * The construction criteria.
045        */
046        public interface Context
047        {
048           /**
049            * Return the assigned color.
050            * @return the color value
051            */
052            Color getColor();
053        }
054    
055       /**
056        * Internal part managmeent interface.
057        */
058        public interface Parts
059        {
060           /**
061            * Get the context map of the child component.
062            * @return the context map
063            */
064            Map getChildMap();
065            
066           /**
067            * Return the child componet as a service instance.
068            * @return the color manager
069            */
070            ChildComponent getChild();
071        }
072    
073        //------------------------------------------------------------------
074        // state
075        //------------------------------------------------------------------
076    
077       /**
078        * The logging channel.
079        */
080        private final Logger m_logger;
081    
082       /**
083        * The assigned context instance.
084        */
085        private final Context m_context;
086    
087       /**
088        * The assigned part manager.
089        */
090        private final Parts m_parts;
091    
092        //------------------------------------------------------------------
093        // constructor
094        //------------------------------------------------------------------
095    
096       /**
097        * Creation of a new composite component instance.
098        * @param logger the assingned logging channel
099        * @param context a context implementation
100        * @param parts the parts manager
101        */
102        public CompositeComponent( final Logger logger, final Context context, final Parts parts )
103        {
104            logger.debug( "instantiation" );
105            
106            m_context = context;
107            m_logger = logger;
108            m_parts = parts;
109            Map map = parts.getChildMap();
110            map.put( "color", context.getColor() );
111        }
112    
113        //------------------------------------------------------------------
114        // Example
115        //------------------------------------------------------------------
116       
117       /**
118        * Return the color value assigned to the component context.
119        * @return the color value
120        */
121        public Color getColor()
122        {
123            return m_context.getColor();
124        }
125        
126        //------------------------------------------------------------------
127        // Validation
128        //------------------------------------------------------------------
129        
130       /**
131        * Return the child component for evaluation by the testcase.
132        * @return ythe child
133        */
134        public ChildComponent getChild()
135        {
136            return m_parts.getChild();
137        }
138        
139        //------------------------------------------------------------------
140        // internal
141        //------------------------------------------------------------------
142    
143       /**
144        * Return the assigned logging channel.
145        * @return the logging channel
146        */
147        private Logger getLogger()
148        {
149            return m_logger;
150        }
151    }