KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > client > AbstractInternalFrame


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.client;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Dimension JavaDoc;
24
25 import javax.swing.JDesktopPane JavaDoc;
26 import javax.swing.JInternalFrame JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.event.InternalFrameEvent JavaDoc;
29 import javax.swing.event.InternalFrameListener JavaDoc;
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 /**
38  *
39  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
40  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:23 $
41  * @since 4.1
42  */

43 abstract class AbstractInternalFrame
44     extends JInternalFrame JavaDoc
45     implements InternalFrameListener JavaDoc, LogEnabled
46 {
47     private InstrumentClientFrame m_frame;
48     private Logger m_logger;
49     
50     private JInternalFrame JavaDoc m_nextFrame;
51     private boolean m_loaded;
52     private boolean m_active;
53
54     /*---------------------------------------------------------------
55      * Constructors
56      *-------------------------------------------------------------*/

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         // Look for the location and size of the frame.
69
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         // Look for the window state.
77
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 JavaDoc e )
89         {
90         }
91
92         // Set the content pane so that it is the right color
93
JPanel JavaDoc contentPane = new JPanel JavaDoc();
94         contentPane.setLayout( new BorderLayout JavaDoc() );
95         setContentPane( contentPane );
96
97         addInternalFrameListener( this );
98
99         m_loaded = true;
100     }
101
102     AbstractInternalFrame( String JavaDoc 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         // Set the content pane so that it is the right color
114
JPanel JavaDoc contentPane = new JPanel JavaDoc();
115         contentPane.setLayout( new BorderLayout JavaDoc() );
116         setContentPane( contentPane );
117
118         addInternalFrameListener( this );
119
120         m_loaded = false;
121     }
122     
123     /*---------------------------------------------------------------
124      * LogEnabled Methods
125      *-------------------------------------------------------------*/

126     /**
127      * Sets the Logger to be used by the component.
128      *
129      * @param logger The Logger.
130      */

131     public void enableLogging( Logger logger )
132     {
133         m_logger = logger;
134     }
135
136     /*---------------------------------------------------------------
137      * Methods
138      *-------------------------------------------------------------*/

139     /**
140      * Returns the logger to be used by the component.
141      *
142      * @return The Logger.
143      */

144     protected Logger getLogger()
145     {
146         return m_logger;
147     }
148     
149     /**
150      * Adds the frame to the desktop in a simple and dumb cascading format.
151      */

152     void addToDesktop( JDesktopPane JavaDoc desktop )
153     {
154         // Make sure that the size is valid
155
Dimension JavaDoc maxSize = desktop.getSize();
156         Dimension JavaDoc size = getSize();
157         if( ( maxSize.width < size.width ) || ( maxSize.height < size.height ) )
158         {
159             setSize( new Dimension JavaDoc( Math.min( maxSize.width, size.width ),
160                                     Math.min( maxSize.height, size.height ) ) );
161             size = getSize();
162         }
163
164         if( !m_loaded )
165         {
166             // Position the frame
167
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 JavaDoc[] 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         // calling setVisible in the shutdown hook will cause the thread to deadlock.
189
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     /**
205      * Allows subclasses to fill in configuration information. At the least, they must set
206      * a type attribute.
207      */

208     abstract protected void getState( DefaultConfiguration stateConfig );
209
210     final Configuration getState()
211     {
212         DefaultConfiguration stateConfig = new DefaultConfiguration( "inner-frame", "-" );
213
214         // Save the location and size of the frame.
215
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         // Save the window state.
221
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 JavaDoc title )
241     {
242         super.setTitle( title );
243         if ( m_active )
244         {
245             m_frame.setStatusMessage( getTitle() );
246         }
247     }
248
249     /*---------------------------------------------------------------
250      * InternalFrameListener Methods
251      *-------------------------------------------------------------*/

252     public void internalFrameOpened( InternalFrameEvent JavaDoc event )
253     {
254     }
255
256     public void internalFrameClosing( InternalFrameEvent JavaDoc event )
257     {
258         // Select the new top frame
259
JDesktopPane JavaDoc desktop = m_frame.getDesktopPane();
260         JInternalFrame JavaDoc[] frames = desktop.getAllFrames();
261         // Find the first frame other than the one being hidden and select and move it to the front
262
m_nextFrame = null;
263         for( int i = 0; i < frames.length; i++ )
264         {
265             JInternalFrame JavaDoc frame = frames[ i ];
266             if( frame != this )
267             {
268                 m_nextFrame = frame;
269
270                 // Break out
271
i = frames.length;
272             }
273         }
274     }
275
276     public void internalFrameClosed( InternalFrameEvent JavaDoc event )
277     {
278         // On closing Swing will bring forward the window at the bottom,
279
// rather than the next window. So we need to move it back and show the correct one.
280
if( m_nextFrame != null )
281         {
282             // The getSelectedFrame method was added in JDK1.3, so it may not yet exist.
283
// Cast this to our workaround DesktopPane to work around this.
284
DesktopPane desktop = (DesktopPane)m_frame.getDesktopPane();
285             JInternalFrame JavaDoc 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 JavaDoc e )
298                     {
299                     }
300                 }
301             }
302         }
303     }
304
305     public void internalFrameIconified( InternalFrameEvent JavaDoc event )
306     {
307     }
308
309     public void internalFrameDeiconified( InternalFrameEvent JavaDoc event )
310     {
311         // Swing always activates a frame when it is deiconified, but it down't
312
// always move it to the front
313
JDesktopPane JavaDoc desktop = m_frame.getDesktopPane();
314         desktop.moveToFront( this );
315     }
316
317     public void internalFrameActivated( InternalFrameEvent JavaDoc event )
318     {
319         m_active = true;
320         m_frame.setStatusMessage( getTitle() );
321     }
322
323     public void internalFrameDeactivated( InternalFrameEvent JavaDoc event )
324     {
325         m_active = false;
326         m_frame.setStatusMessage( "" );
327     }
328 }
329
330
Popular Tags