KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > examples > viewer > ComponentViewer


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48
49 */

50 package org.apache.avalon.fortress.examples.viewer;
51
52 /**
53  * Simple Fortress container containing a Swing based viewer for performing
54  * lookups on registered components.
55  *
56  * <p>
57  * The intention of the viewer is to allow you to perform a lookup of a component
58  * manually, (currently) so you can check the see the effect of lazy vs startup
59  * initialization.
60  * </p>
61  *
62  * <p>
63  * REVISIT: add a text component which tracks the log file to make it easier to
64  * see a component being initialized upon first lookup.
65  * </p>
66  *
67  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
68  * @version CVS $Revision: 1.6 $ $Date: 2003/04/11 07:36:21 $
69  */

70 public final class ComponentViewer
71     extends org.apache.avalon.fortress.impl.DefaultContainer
72     implements org.apache.avalon.framework.activity.Startable, java.awt.event.ActionListener JavaDoc, Runnable JavaDoc
73 {
74     // GUI references
75
private javax.swing.JFrame JavaDoc m_frame;
76     private javax.swing.JComboBox JavaDoc m_components;
77
78     /**
79      * Initializes this component. Creates simple Swing GUI containing
80      * available translations for the key 'hello-world'.
81      *
82      * @exception java.lang.Exception if an error occurs
83      */

84     public void initialize()
85         throws Exception JavaDoc
86     {
87         super.initialize();
88
89         // create main frame
90
m_frame = new javax.swing.JFrame JavaDoc( "Component Viewer" );
91         m_frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
92
93         m_components = new javax.swing.JComboBox JavaDoc( getRoles() );
94
95         javax.swing.JButton JavaDoc button = new javax.swing.JButton JavaDoc( "Lookup!" );
96         button.addActionListener( this );
97
98         /*
99         // can we output the log data into this text area somehow ?
100         JTextArea logData = new JTextArea();
101         logData.setEditable( false );
102         */

103
104         javax.swing.JPanel JavaDoc selectionPanel = new javax.swing.JPanel JavaDoc();
105         selectionPanel.add( m_components );
106         selectionPanel.add( button );
107
108         javax.swing.JPanel JavaDoc mainPanel = new javax.swing.JPanel JavaDoc();
109         mainPanel.setLayout( new javax.swing.BoxLayout JavaDoc( mainPanel, javax.swing.BoxLayout.Y_AXIS ) );
110         mainPanel.add( selectionPanel );
111
112         m_frame.setContentPane( mainPanel );
113         m_frame.pack();
114
115         // all done
116
if( getLogger().isDebugEnabled() )
117         {
118             getLogger().debug( "Initialized" );
119         }
120     }
121
122     /**
123      * Helper method to obtain a list of all Roles registered with this container
124      *
125      * @return an array of roles
126      */

127     private Object JavaDoc[] getRoles()
128     {
129         java.util.Set JavaDoc keys = m_mapper.keySet();
130         Object JavaDoc[] roles = new Object JavaDoc[ keys.size() ];
131         int j = 0;
132
133         for( java.util.Iterator JavaDoc i = keys.iterator(); i.hasNext(); )
134         {
135             roles[ j++ ] = i.next();
136         }
137
138         return roles;
139     }
140
141     /**
142      * Starts the component, makes GUI visible, ready for use.
143      */

144     public void start()
145     {
146         m_frame.setVisible( true );
147
148         if( getLogger().isDebugEnabled() )
149         {
150             getLogger().debug( "GUI Activated" );
151         }
152     }
153
154     /**
155      * Stops component, make GUI invisible, ready for decomissioning.
156      */

157     public void stop()
158     {
159         m_frame.setVisible( false );
160
161         if( getLogger().isDebugEnabled() )
162         {
163             getLogger().debug( "GUI Disactivated" );
164         }
165     }
166
167     /**
168      * Handles the <i>lookup</i> event. Finds out which component
169      * was selected and performs a lookup and release on this component.
170      *
171      * @param evt an <code>ActionEvent</code> value
172      */

173     public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt )
174     {
175         String JavaDoc selected = (String JavaDoc)m_components.getSelectedItem();
176
177         if( getLogger().isDebugEnabled() )
178         {
179             getLogger().debug( "Looking up component " + selected );
180         }
181
182         Object JavaDoc component = null;
183
184         try
185         {
186             component = m_serviceManager.lookup( selected );
187         }
188         catch( org.apache.avalon.framework.service.ServiceException e )
189         {
190             if( getLogger().isWarnEnabled() )
191             {
192                 getLogger().warn( "Error looking up component: " + e.getKey(), e );
193             }
194         }
195         finally
196         {
197             if( component != null ) m_serviceManager.release( component );
198         }
199     }
200
201     public void run()
202     {
203         while( m_frame.isVisible() )
204         {
205             try
206             {
207                 Thread.sleep( 1000 );
208             }
209             catch( InterruptedException JavaDoc ie )
210             {
211                 m_frame.setVisible( false );
212             }
213         }
214     }
215 }
216
217
Popular Tags