1 50 package org.apache.avalon.fortress.examples.swing; 51 52 60 public final class SwingContainer extends org.apache.avalon.fortress.impl.DefaultContainer 61 implements org.apache.avalon.framework.activity.Startable, java.awt.event.ActionListener , Runnable 62 { 63 private org.apache.avalon.fortress.examples.components.Translator m_translator; 65 66 private javax.swing.JFrame m_frame; 68 private javax.swing.JLabel m_label; 69 70 private String m_key = "hello-world"; 72 73 79 public void initialize() 80 throws Exception 81 { 82 super.initialize(); 83 84 m_translator = (org.apache.avalon.fortress.examples.components.Translator)m_serviceManager.lookup( org.apache.avalon.fortress.examples.components.Translator.ROLE ); 86 87 javax.swing.JComboBox cb = new javax.swing.JComboBox ( m_translator.getSupportedLanguages( m_key ) ); 89 cb.addActionListener( this ); 90 91 m_label = new javax.swing.JLabel ( "Select your language" ); 93 m_label.setPreferredSize( new java.awt.Dimension ( 150, 30 ) ); 94 95 javax.swing.JPanel panel = new javax.swing.JPanel (); 97 panel.add( cb ); 98 panel.add( m_label ); 99 100 m_frame = new javax.swing.JFrame ( "Hello World!" ); 102 m_frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE ); 103 m_frame.setContentPane( panel ); 104 m_frame.pack(); 105 106 if( getLogger().isDebugEnabled() ) 108 { 109 getLogger().debug( "Initialized" ); 110 } 111 } 112 113 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 ie ) 135 { 136 m_frame.setVisible( false ); 137 } 138 } 139 } 140 141 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 160 public void actionPerformed( java.awt.event.ActionEvent evt ) 161 { 162 javax.swing.JComboBox cb = (javax.swing.JComboBox )evt.getSource(); 163 String selected = (String )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 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 |