KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > examples > swing > SwingContainer


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.swing;
51
52 /**
53  * Simple Fortress based container containing a Swing implementation of Hello World.
54  * This container creates a small Swing based GUI displaying a combobox of available
55  * languages from the translator component.
56  *
57  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
58  * @version CVS $Revision: 1.6 $ $Date: 2003/04/11 07:36:21 $
59  */

60 public final class SwingContainer extends org.apache.avalon.fortress.impl.DefaultContainer
61     implements org.apache.avalon.framework.activity.Startable, java.awt.event.ActionListener JavaDoc, Runnable JavaDoc
62 {
63     // Component references
64
private org.apache.avalon.fortress.examples.components.Translator m_translator;
65
66     // GUI references
67
private javax.swing.JFrame JavaDoc m_frame;
68     private javax.swing.JLabel JavaDoc m_label;
69
70     // Dictionary key
71
private String JavaDoc m_key = "hello-world";
72
73     /**
74      * Initializes this component. Creates simple Swing GUI containing
75      * available translations for the key 'hello-world'.
76      *
77      * @exception java.lang.Exception if an error occurs
78      */

79     public void initialize()
80         throws Exception JavaDoc
81     {
82         super.initialize();
83
84         // obtain translator component
85
m_translator = (org.apache.avalon.fortress.examples.components.Translator)m_serviceManager.lookup( org.apache.avalon.fortress.examples.components.Translator.ROLE );
86
87         // create combo box
88
javax.swing.JComboBox JavaDoc cb = new javax.swing.JComboBox JavaDoc( m_translator.getSupportedLanguages( m_key ) );
89         cb.addActionListener( this );
90
91         // create label
92
m_label = new javax.swing.JLabel JavaDoc( "Select your language" );
93         m_label.setPreferredSize( new java.awt.Dimension JavaDoc( 150, 30 ) );
94
95         // create panel holding box and label
96
javax.swing.JPanel JavaDoc panel = new javax.swing.JPanel JavaDoc();
97         panel.add( cb );
98         panel.add( m_label );
99
100         // create main frame
101
m_frame = new javax.swing.JFrame JavaDoc( "Hello World!" );
102         m_frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
103         m_frame.setContentPane( panel );
104         m_frame.pack();
105
106         // all done
107
if( getLogger().isDebugEnabled() )
108         {
109             getLogger().debug( "Initialized" );
110         }
111     }
112
113     /**
114      * Starts the component, makes GUI visible, ready for use.
115      */

116     public void start()
117     {
118         m_frame.setVisible( true );
119
120         if( getLogger().isDebugEnabled() )
121         {
122             getLogger().debug( "GUI Activated" );
123         }
124     }
125
126     public void run()
127     {
128         while( m_frame.isVisible() )
129         {
130             try
131             {
132                 Thread.sleep( 1000 );
133             }
134             catch( InterruptedException JavaDoc ie )
135             {
136                 m_frame.setVisible( false );
137             }
138         }
139     }
140
141     /**
142      * Stops component, make GUI invisible, ready for decomissioning.
143      */

144     public void stop()
145     {
146         m_frame.setVisible( false );
147
148         if( getLogger().isDebugEnabled() )
149         {
150             getLogger().debug( "GUI Disactivated" );
151         }
152     }
153
154     /**
155      * Method called when the user changes the selected item in the
156      * combobox.
157      *
158      * @param evt an <code>ActionEvent</code> instance
159      */

160     public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt )
161     {
162         javax.swing.JComboBox JavaDoc cb = (javax.swing.JComboBox JavaDoc)evt.getSource();
163         String JavaDoc selected = (String JavaDoc)cb.getSelectedItem();
164
165         m_label.setText( m_translator.getTranslation( m_key, selected ) );
166
167         if( getLogger().isDebugEnabled() )
168         {
169             getLogger().debug( "Language changed to " + selected );
170         }
171     }
172
173     /**
174      * Cleans up references to retrieved components.
175      */

176     public void dispose()
177     {
178         if( m_translator != null )
179             m_serviceManager.release( m_translator );
180
181         m_frame.dispose();
182
183         super.dispose();
184     }
185 }
186
187
Popular Tags