Some further FrameManager wrangling and a proper bridge for managed applets.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@32 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2006-08-25 00:34:18 +00:00
parent 22b867bb61
commit 9ed3f62af7
8 changed files with 140 additions and 34 deletions
+27 -22
View File
@@ -42,7 +42,6 @@ import java.awt.EventQueue;
import javax.swing.JLayeredPane;
import javax.swing.RepaintManager;
import javax.swing.JRootPane;
import javax.swing.RootPaneContainer;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
@@ -122,15 +121,30 @@ public abstract class FrameManager
{
}
/**
* Provides a bridge between either {@link ManagedJFrame} or {@link
* ManagedJApplet} and the frame manager.
*/
public static interface ManagedRoot
{
/** Configures the root with a reference to its frame manager. */
public void init (FrameManager fmgr);
/** Returns the window at the root of the UI hierarchy. */
public Window getWindow ();
/** Returns the top-level Swing pane. */
public JRootPane getRootPane();
}
/**
* Creates a frame manager that will use a {@link SystemMediaTimer} to
* obtain timing information, which is available on every platform, but
* returns inaccurate time stamps on many platforms.
*
* @see #newInstance(Window, RootPaneContainer, MediaTimer)
* @see #newInstance(ManagedRoot, MediaTimer)
*/
public static FrameManager newInstance (
Window window, RootPaneContainer root)
public static FrameManager newInstance (ManagedRoot root)
{
// first try creating a PerfTimer which is the best if we're using
// JDK1.4.2
@@ -142,20 +156,14 @@ public abstract class FrameManager
"System.currentTimeMillis() based timer.");
timer = new SystemMediaTimer();
}
return newInstance(window, root, timer);
return newInstance(root, timer);
}
/**
* Constructs a frame manager that will do its rendering to the
* supplied frame. It is likely that the caller will want to have put
* the frame into full-screen exclusive mode prior to providing it to
* the frame manager so that the frame manager can take advantage of
* optimizations available in that mode.
*
* @see GraphicsDevice#setFullScreenWindow
* Constructs a frame manager that will do its rendering to the supplied
* root.
*/
public static FrameManager newInstance (
Window window, RootPaneContainer root, MediaTimer timer)
public static FrameManager newInstance (ManagedRoot root, MediaTimer timer)
{
FrameManager fmgr;
if (_useFlip.getValue()) {
@@ -165,21 +173,18 @@ public abstract class FrameManager
Log.info("Creating back frame manager.");
fmgr = new BackFrameManager();
}
fmgr.init(window, root, timer);
fmgr.init(root, timer);
return fmgr;
}
/**
* Initializes this frame manager and prepares it for operation.
*/
protected void init (
Window window, RootPaneContainer root, MediaTimer timer)
protected void init (ManagedRoot root, MediaTimer timer)
{
_window = window;
_window = root.getWindow();
_root = root;
if (window instanceof ManagedJFrame) {
((ManagedJFrame)window).init(this);
}
_root.init(this);
_timer = timer;
// set up our custom repaint manager
@@ -773,7 +778,7 @@ public abstract class FrameManager
protected Window _window;
/** Provides access to our Swing bits. */
protected RootPaneContainer _root;
protected ManagedRoot _root;
/** Used to obtain timing measurements. */
protected MediaTimer _timer;
@@ -0,0 +1,95 @@
//
// $Id$
//
// Nenya library - tools for developing networked games
// Copyright (C) 2002-2006 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/nenya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.media;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Window;
import javax.swing.JApplet;
import com.threerings.media.Log;
/**
* When using the {@link FrameManager} in an Applet, one must use this
* top-level class.
*/
public class ManagedJApplet extends JApplet
{
// from interface FrameManager.ManagedRoot
public void init (FrameManager fmgr)
{
_fmgr = fmgr;
}
// from interface FrameManager.ManagedRoot
public Window getWindow ()
{
Component parent = getParent();
while (!(parent instanceof Window) && parent != null) {
parent = parent.getParent();
}
return (Window)parent;
}
/**
* Returns the frame manager managing this frame.
*/
public FrameManager getFrameManager ()
{
return _fmgr;
}
@Override // from Component
public void paint (Graphics g)
{
// we catch paint requests and forward them on to the repaint
// infrastructure
update(g);
}
@Override // from Component
public void update (Graphics g)
{
Shape clip = g.getClip();
Rectangle dirty;
if (clip != null) {
dirty = clip.getBounds();
} else {
dirty = getRootPane().getBounds();
// account for our frame insets
Insets insets = getInsets();
dirty.x += insets.left;
dirty.y += insets.top;
}
if (_fmgr != null) {
_fmgr.restoreFromBack(dirty);
} else {
Log.info("Dropping AWT dirty rect " + dirty + " (" + clip + ").");
}
}
protected FrameManager _fmgr;
}
@@ -26,10 +26,12 @@ import java.awt.GraphicsConfiguration;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Window;
import javax.swing.JFrame;
import com.samskivert.util.StringUtil;
import com.threerings.media.Log;
/**
@@ -37,6 +39,7 @@ import com.threerings.media.Log;
* class.
*/
public class ManagedJFrame extends JFrame
implements FrameManager.ManagedRoot
{
/**
* Constructs a managed frame with no title.
@@ -62,14 +65,18 @@ public class ManagedJFrame extends JFrame
super(gc);
}
/**
* Called by our frame manager when it's ready to go.
*/
// from interface FrameManager.ManagedRoot
public void init (FrameManager fmgr)
{
_fmgr = fmgr;
}
// from interface FrameManager.ManagedRoot
public Window getWindow ()
{
return this;
}
/**
* Returns the frame manager managing this frame.
*/
@@ -107,9 +114,6 @@ public class ManagedJFrame extends JFrame
if (_fmgr != null) {
_fmgr.restoreFromBack(dirty);
} else {
Log.info("Dropping AWT dirty rect " + StringUtil.toString(dirty) +
" [clip=" + StringUtil.toString(clip) + "].");
}
}