001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: KFrame.java,v $
023   Revision 1.12  2004/05/12 18:35:18  markl
024   added setFont() method.
025
026   Revision 1.11  2003/04/30 04:34:41  markl
027   get frame icon from UIChangeManager
028
029   Revision 1.10  2003/01/19 09:50:53  markl
030   Javadoc & comment header updates.
031
032   Revision 1.9  2002/03/08 21:36:16  markl
033   Handle menubar/statusbar layout automatically
034
035   Revision 1.8  2001/03/12 09:27:56  markl
036   Source code and Javadoc cleanup.
037
038   Revision 1.7  1999/07/19 03:59:18  markl
039   Fixed listener problem, renamed dispose() to destroy().
040
041   Revision 1.6  1999/07/12 08:50:41  markl
042   Listen for texture change events.
043
044   Revision 1.5  1999/07/06 09:17:13  markl
045   Added cursor method.
046
047   Revision 1.4  1999/05/10 08:57:29  markl
048   Added canClose() method.
049
050   Revision 1.3  1999/04/25 04:12:41  markl
051   Changes to support solid backgrounds in addition to textures.
052
053   Revision 1.2  1999/01/10 02:25:57  markl
054   added GPL header & RCS tag
055   ----------------------------------------------------------------------------
056*/
057
058package kiwi.ui;
059
060import java.awt.*;
061import java.awt.event.*;
062import java.beans.*;
063import javax.swing.*;
064
065import kiwi.util.*;
066
067/** <code>KFrame</code> is a trivial extension of <code>JFrame</code>
068  * that provides support for tiling the background of the frame with an image.
069  * <p>
070  * The method <code>getMainContainer()</code> will return the frame's
071  * <code>KPanel</code>. Add child components to the frame by adding
072  * them to this <code>KPanel</code>.
073  *
074  * <p><center>
075  * <img src="snapshot/KFrame.gif"><br>
076  * <i>An example KFrame.</i>
077  * </center>
078  *
079  * @see kiwi.ui.KPanel
080  *
081  * @author Mark Lindner
082  */
083
084public class KFrame extends JFrame
085  {
086  private KPanel _main, _content;
087  private _PropertyChangeListener propListener;
088
089  /** Construct a new <code>KFrame</code>. */
090
091  public KFrame()
092    {
093    this("");
094    }
095
096  /** Construct a new <code>KFrame</code>.
097    *
098    * @param title The title for the frame.
099    */
100
101  public KFrame(String title)
102    {
103    super(title);
104
105    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
106    addWindowListener(new _WindowListener());
107    
108    ResourceManager rm = KiwiUtils.getResourceManager();
109    getContentPane().setLayout(new GridLayout(1, 0));
110    _main = new KPanel(UIChangeManager.getInstance()
111                              .getDefaultTexture());
112    _main.setOpaque(true);
113    _main.setLayout(new BorderLayout(0, 0));
114    getContentPane().add(_main);
115    Image frameIcon = UIChangeManager.getDefaultFrameIcon();
116    setIconImage((frameIcon != null) ? frameIcon
117                 : rm.getImage("kiwi-icon.gif"));
118
119    _content = new KPanel();
120    _main.add("Center", _content);
121
122    UIChangeManager.getInstance().registerComponent(getRootPane());
123    propListener = new _PropertyChangeListener();
124    }
125
126  /** Set the menu bar for the frame. Using this method rather than
127   * <code>setJMenuBar()</code> allows for the installation of a transparent
128   * menubar.
129   *
130   * @param menuBar The new menu bar.
131   * @since Kiwi 1.3.3
132   */
133
134  public void setMenuBar(JMenuBar menuBar)
135    {
136    menuBar.setOpaque(false);
137    _main.add("North", menuBar);
138    }
139
140  /** Set the status bar for the frame.
141   *
142   * @param statusBar the new status bar.
143   * @since Kiwi 1.3.3
144   */
145
146  public void setStatusBar(StatusBar statusBar)
147    {
148    _main.add("South", statusBar);
149    }
150
151  /** Get a reference to the main container (in this case, the
152    * <code>KPanel</code> that is the child of the frame's content pane).
153    */
154
155  public KPanel getMainContainer()
156    {
157    return(_content);
158    }
159
160  /** Set the background texture.
161    *
162    * @param image The image to use as the background texture for the frame.
163    */
164
165  public void setTexture(Image image)
166    {
167    _main.setTexture(image);
168    invalidate();
169    repaint();
170    }
171
172  /** Set the background color.
173   *
174   * @param color The new background color.
175   */
176  
177  public void setColor(Color color)
178    {
179    _main.setBackground(color);
180    }
181
182  /** Called in response to a frame close event to determine if this frame
183   * may be closed.
184   *
185   * @return <code>true</code> if the frame is allowed to close, and
186   * <code>false</code> otherwise. The default implementation returns
187   * <code>true</code>.
188   */
189  
190  protected boolean canClose()
191    {
192    return(true);
193    }
194
195  /** Show or hide the frame.
196   *
197   * @param flag A flag specifying whether the frame should be shown or
198   * hidden. If <code>true</code>, the <code>startFocus()</code> method is
199   * called to allow the subclasser to request focus for a given child
200   * component.
201   *
202   * @see #startFocus
203   */
204  
205  public void setVisible(boolean flag)
206    {
207    if(flag)
208      {
209      // attach listeners
210
211      UIChangeManager.getInstance().addPropertyChangeListener(propListener);
212      
213      startFocus();
214      }
215    else
216      {
217      // remove liseteners
218      
219      UIChangeManager.getInstance().removePropertyChangeListener(propListener);
220      }
221
222    super.setVisible(flag);
223    }
224  
225  /** This method is called when the frame is made visible; it should
226   * transfer focus to the appropriate child component. The default
227   * implementation does nothing.
228   */
229  
230  protected void startFocus()
231    {
232    }
233  
234  /** Turn the busy cursor on or off for this window.
235   *
236   * @param flag If <code>true</code>, the wait cursor will be set for this
237   * window, otherwise the default cursor will be set.
238   */
239  
240  public void setBusyCursor(boolean flag)
241    {
242    setCursor(Cursor.getPredefinedCursor(flag ? Cursor.WAIT_CURSOR
243                                         : Cursor.DEFAULT_CURSOR));
244    }
245  
246  /** Destroy this frame. Call this method when the frame is no longer needed.
247   * The frame will detach its listeners from the
248   * <code>UIChanageManager</code>.
249   */
250
251  public void destroy()
252    {
253    UIChangeManager.getInstance().unregisterComponent(getRootPane());
254    UIChangeManager.getInstance().removePropertyChangeListener(propListener);
255    }
256
257  /* WindowListener */
258  
259  private class _WindowListener extends WindowAdapter
260    {
261    public void windowClosing(WindowEvent evt)
262      {
263      if(canClose())
264        {
265        setVisible(false);
266        dispose();
267        }
268      }
269    }
270
271  /* PropertyChangeListener */
272
273  private class _PropertyChangeListener implements PropertyChangeListener
274    {
275    public void propertyChange(PropertyChangeEvent evt)
276      {
277      if(evt.getPropertyName().equals(UIChangeManager.TEXTURE_PROPERTY))
278        setTexture((Image)evt.getNewValue());
279      }
280    }
281
282  /** Set the font for this frame window. This method sets the font for
283   * each component in the window's component hierarchy.
284   *
285   * @param font The new font.
286   *
287   * @since Kiwi 2.0
288   */
289
290  public void setFont(Font font)
291    {
292    KiwiUtils.setFonts(getMainContainer(), font);
293    }
294  
295  }
296
297/* end of source file */