1 50 package org.apache.avalon.fortress.examples.viewer; 51 52 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 , Runnable 73 { 74 private javax.swing.JFrame m_frame; 76 private javax.swing.JComboBox m_components; 77 78 84 public void initialize() 85 throws Exception 86 { 87 super.initialize(); 88 89 m_frame = new javax.swing.JFrame ( "Component Viewer" ); 91 m_frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE ); 92 93 m_components = new javax.swing.JComboBox ( getRoles() ); 94 95 javax.swing.JButton button = new javax.swing.JButton ( "Lookup!" ); 96 button.addActionListener( this ); 97 98 103 104 javax.swing.JPanel selectionPanel = new javax.swing.JPanel (); 105 selectionPanel.add( m_components ); 106 selectionPanel.add( button ); 107 108 javax.swing.JPanel mainPanel = new javax.swing.JPanel (); 109 mainPanel.setLayout( new javax.swing.BoxLayout ( mainPanel, javax.swing.BoxLayout.Y_AXIS ) ); 110 mainPanel.add( selectionPanel ); 111 112 m_frame.setContentPane( mainPanel ); 113 m_frame.pack(); 114 115 if( getLogger().isDebugEnabled() ) 117 { 118 getLogger().debug( "Initialized" ); 119 } 120 } 121 122 127 private Object [] getRoles() 128 { 129 java.util.Set keys = m_mapper.keySet(); 130 Object [] roles = new Object [ keys.size() ]; 131 int j = 0; 132 133 for( java.util.Iterator i = keys.iterator(); i.hasNext(); ) 134 { 135 roles[ j++ ] = i.next(); 136 } 137 138 return roles; 139 } 140 141 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 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 173 public void actionPerformed( java.awt.event.ActionEvent evt ) 174 { 175 String selected = (String )m_components.getSelectedItem(); 176 177 if( getLogger().isDebugEnabled() ) 178 { 179 getLogger().debug( "Looking up component " + selected ); 180 } 181 182 Object 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 ie ) 210 { 211 m_frame.setVisible( false ); 212 } 213 } 214 } 215 } 216 217 | Popular Tags |