1 19 20 package org.apache.excalibur.instrument.client; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dimension ; 24 25 import javax.swing.JDesktopPane ; 26 import javax.swing.JInternalFrame ; 27 import javax.swing.JPanel ; 28 import javax.swing.event.InternalFrameEvent ; 29 import javax.swing.event.InternalFrameListener ; 30 31 import org.apache.avalon.framework.configuration.Configuration; 32 import org.apache.avalon.framework.configuration.DefaultConfiguration; 33 import org.apache.avalon.framework.logger.LogEnabled; 34 import org.apache.avalon.framework.logger.Logger; 35 36 37 43 abstract class AbstractInternalFrame 44 extends JInternalFrame 45 implements InternalFrameListener , LogEnabled 46 { 47 private InstrumentClientFrame m_frame; 48 private Logger m_logger; 49 50 private JInternalFrame m_nextFrame; 51 private boolean m_loaded; 52 private boolean m_active; 53 54 57 AbstractInternalFrame( Configuration stateConfig, 58 boolean resizable, 59 boolean closable, 60 boolean maximizable, 61 boolean iconifiable, 62 InstrumentClientFrame frame ) 63 { 64 super( "", resizable, closable, maximizable, iconifiable ); 65 66 m_frame = frame; 67 68 int x = stateConfig.getAttributeAsInteger( "x", getX() ); 70 int y = stateConfig.getAttributeAsInteger( "y", getY() ); 71 int width = stateConfig.getAttributeAsInteger( "width", getWidth() ); 72 int height = stateConfig.getAttributeAsInteger( "height", getHeight() ); 73 setLocation( x, y ); 74 setSize( width, height ); 75 76 try 78 { 79 if( stateConfig.getAttributeAsBoolean( "iconized", false ) ) 80 { 81 setIcon( true ); 82 } 83 else if( stateConfig.getAttributeAsBoolean( "maximized", false ) ) 84 { 85 this.setMaximum( true ); 86 } 87 } 88 catch( java.beans.PropertyVetoException e ) 89 { 90 } 91 92 JPanel contentPane = new JPanel (); 94 contentPane.setLayout( new BorderLayout () ); 95 setContentPane( contentPane ); 96 97 addInternalFrameListener( this ); 98 99 m_loaded = true; 100 } 101 102 AbstractInternalFrame( String title, 103 boolean resizable, 104 boolean closable, 105 boolean maximizable, 106 boolean iconifiable, 107 InstrumentClientFrame frame ) 108 { 109 super( title, resizable, closable, maximizable, iconifiable ); 110 111 m_frame = frame; 112 113 JPanel contentPane = new JPanel (); 115 contentPane.setLayout( new BorderLayout () ); 116 setContentPane( contentPane ); 117 118 addInternalFrameListener( this ); 119 120 m_loaded = false; 121 } 122 123 126 131 public void enableLogging( Logger logger ) 132 { 133 m_logger = logger; 134 } 135 136 139 144 protected Logger getLogger() 145 { 146 return m_logger; 147 } 148 149 152 void addToDesktop( JDesktopPane desktop ) 153 { 154 Dimension maxSize = desktop.getSize(); 156 Dimension size = getSize(); 157 if( ( maxSize.width < size.width ) || ( maxSize.height < size.height ) ) 158 { 159 setSize( new Dimension ( Math.min( maxSize.width, size.width ), 160 Math.min( maxSize.height, size.height ) ) ); 161 size = getSize(); 162 } 163 164 if( !m_loaded ) 165 { 166 int max = (int)Math.min( Math.ceil( ( maxSize.width - size.width ) / 20.0 ), 168 Math.ceil( ( maxSize.height - size.height ) / 20.0 ) ); 169 170 JInternalFrame [] frames = desktop.getAllFrames(); 171 int pos; 172 if( max > 0 ) 173 { 174 pos = ( frames.length % max ) * 20; 175 } 176 else 177 { 178 pos = 0; 179 } 180 setLocation( pos, pos ); 181 } 182 183 desktop.add( this ); 184 } 185 186 void hideFrame() 187 { 188 if ( !Thread.currentThread().getName().equals( InstrumentClientFrame.SHUTDOWN_HOOK_NAME ) ) 190 { 191 setVisible( false ); 192 dispose(); 193 } 194 } 195 196 public void updateUI() 197 { 198 super.updateUI(); 199 200 pack(); 201 setMinimumSize( getSize() ); 202 } 203 204 208 abstract protected void getState( DefaultConfiguration stateConfig ); 209 210 final Configuration getState() 211 { 212 DefaultConfiguration stateConfig = new DefaultConfiguration( "inner-frame", "-" ); 213 214 stateConfig.setAttribute( "x", Integer.toString( getX() ) ); 216 stateConfig.setAttribute( "y", Integer.toString( getY() ) ); 217 stateConfig.setAttribute( "width", Integer.toString( getWidth() ) ); 218 stateConfig.setAttribute( "height", Integer.toString( getHeight() ) ); 219 220 if( isIcon() ) 222 { 223 stateConfig.setAttribute( "iconized", "true" ); 224 } 225 else if( isMaximum() ) 226 { 227 stateConfig.setAttribute( "maximized", "true" ); 228 } 229 230 getState( stateConfig ); 231 232 return stateConfig; 233 } 234 235 protected InstrumentClientFrame getFrame() 236 { 237 return m_frame; 238 } 239 240 public void setTitle( String title ) 241 { 242 super.setTitle( title ); 243 if ( m_active ) 244 { 245 m_frame.setStatusMessage( getTitle() ); 246 } 247 } 248 249 252 public void internalFrameOpened( InternalFrameEvent event ) 253 { 254 } 255 256 public void internalFrameClosing( InternalFrameEvent event ) 257 { 258 JDesktopPane desktop = m_frame.getDesktopPane(); 260 JInternalFrame [] frames = desktop.getAllFrames(); 261 m_nextFrame = null; 263 for( int i = 0; i < frames.length; i++ ) 264 { 265 JInternalFrame frame = frames[ i ]; 266 if( frame != this ) 267 { 268 m_nextFrame = frame; 269 270 i = frames.length; 272 } 273 } 274 } 275 276 public void internalFrameClosed( InternalFrameEvent event ) 277 { 278 if( m_nextFrame != null ) 281 { 282 DesktopPane desktop = (DesktopPane)m_frame.getDesktopPane(); 285 JInternalFrame top = desktop.getSelectedFrame(); 286 287 if( top != null ) 288 { 289 if( top != m_nextFrame ) 290 { 291 try 292 { 293 m_nextFrame.setSelected( true ); 294 desktop.moveToFront( m_nextFrame ); 295 desktop.moveToBack( top ); 296 } 297 catch( java.beans.PropertyVetoException e ) 298 { 299 } 300 } 301 } 302 } 303 } 304 305 public void internalFrameIconified( InternalFrameEvent event ) 306 { 307 } 308 309 public void internalFrameDeiconified( InternalFrameEvent event ) 310 { 311 JDesktopPane desktop = m_frame.getDesktopPane(); 314 desktop.moveToFront( this ); 315 } 316 317 public void internalFrameActivated( InternalFrameEvent event ) 318 { 319 m_active = true; 320 m_frame.setStatusMessage( getTitle() ); 321 } 322 323 public void internalFrameDeactivated( InternalFrameEvent event ) 324 { 325 m_active = false; 326 m_frame.setStatusMessage( "" ); 327 } 328 } 329 330 | Popular Tags |