gnu.java.awt.peer.swing

Class SwingComponentPeer

Implemented Interfaces:
ComponentPeer
Known Direct Subclasses:
SwingButtonPeer, SwingCanvasPeer, SwingCheckboxPeer, SwingContainerPeer, SwingLabelPeer, SwingListPeer, SwingTextAreaPeer, SwingTextFieldPeer

public class SwingComponentPeer
extends Object
implements ComponentPeer

The base class for Swing based component peers. This provides the basic functionality needed for Swing based component peers. Many methods are implemented to forward to the Swing component. Others however forward to the component's parent and expect the toplevel component peer to provide a real implementation of it. These are for example the key methods getGraphics() and createImage(int,int), as well as getLocationOnScreen(). This class also provides the necesary hooks into the Swing painting and event handling system. In order to achieve this, it traps paint, mouse and key events in handleEvent(AWTEvent) and calls some special methods (peerPaint(Graphics), handleKeyEvent(KeyEvent), handleMouseEvent(MouseEvent) and handleMouseMotionEvent(MouseEvent)) that call the corresponding Swing methods.

Field Summary

protected Component
awtComponent
The AWT component for this peer.
protected Rectangle
paintArea
The current repaint area.
protected Font
peerFont
The font that is set for this peer.
protected SwingComponent
swingComponent
The Swing component for this peer.

Constructor Summary

SwingComponentPeer()
Creates a SwingComponentPeer instance.

Method Summary

boolean
canDetermineObscurity()
Returns true if this component peer can determine if the component has been obscured, false otherwise.
int
checkImage(Image img, int width, int height, ImageObserver ob)
Returns the construction status of the specified image.
void
coalescePaintEvent(PaintEvent e)
Coalesces the specified paint event.
void
createBuffers(int numBuffers, BufferCapabilities caps)
Create a number of image buffers that implement a buffering strategy according to the given capabilities.
Image
createImage(int width, int height)
Creates an empty image with the specified width and height.
Image
createImage(ImageProducer prod)
Creates an image by starting the specified image producer.
VolatileImage
createVolatileImage(int width, int height)
A convenience method that creates a volatile image.
void
destroyBuffers()
Destroy the resources created by createBuffers.
void
disable()
Disables the component.
void
dispose()
Disposes the component peer.
void
enable()
Enables the component.
void
flip(BufferCapabilities.FlipContents contents)
Perform a page flip, leaving the contents of the back buffer in the specified state.
Image
getBackBuffer()
Return the back buffer of this component.
Rectangle
getBounds()
Get the bounds of this component peer.
ColorModel
getColorModel()
Returns the color model of the component.
Component
getComponent()
Returns the AWT component for this peer.
FontMetrics
getFontMetrics(Font f)
Returns the font metrics for the specified font.
Graphics
getGraphics()
Returns a Graphics object suitable for drawing on this component.
GraphicsConfiguration
getGraphicsConfiguration()
Get the graphics configuration of the component.
Point
getLocationOnScreen()
Returns the location of this component in screen coordinates.
Dimension
getMinimumSize()
Returns the minimum size for the component.
Dimension
getPreferredSize()
Returns the preferred size for the component.
Toolkit
getToolkit()
Returns the toolkit that created this peer.
void
handleEvent(AWTEvent e)
Handles the given event.
protected void
handleFocusEvent(FocusEvent e)
Handles focus events on the component.
protected void
handleKeyEvent(KeyEvent e)
Handles key events on the component.
protected void
handleMouseEvent(MouseEvent e)
Handles mouse events on the component.
protected void
handleMouseMotionEvent(MouseEvent e)
Handles mouse motion events on the component.
boolean
handlesWheelScrolling()
Returns true, if this component can handle wheel scrolling, false otherwise.
void
hide()
Makes the component invisible.
protected void
init(Component awtComp, SwingComponent swingComp)
Initializes the AWT and Swing component for this peer.
boolean
isFocusTraversable()
Returns true if the component can receive keyboard input focus.
boolean
isFocusable()
Returns true if the component can receive keyboard input focus.
boolean
isObscured()
Returns true if this component has been obscured, false otherwise.
boolean
isReparentSupported()
Check if this component supports being reparented.
void
layout()
Layout this component peer.
Dimension
minimumSize()
Returns the minimum size for the component.
void
paint(Graphics graphics)
Paints the component.
protected void
peerPaint(Graphics g, boolean update)
Triggers 'heavyweight' painting of the components.
protected void
peerPaintComponent(Graphics g)
Paints the actual 'heavyweight' swing component, if there is one associated to this peer.
Dimension
preferredSize()
Returns the preferred size for the component.
boolean
prepareImage(Image img, int width, int height, ImageObserver ob)
Prepares an image for rendering on this component.
void
print(Graphics graphics)
void
repaint(long tm, int x, int y, int width, int height)
Repaints the specified rectangle of this component.
void
reparent(ContainerPeer parent)
Reparent this component under another container.
void
requestFocus()
Requests that this component receives the focus.
boolean
requestFocus(Component source, boolean tmp, boolean allowWindowFocus, long tm)
Requests that this component receives the focus.
boolean
requestFocus(Component lightweightChild, boolean temporary, boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause)
void
reshape(int x, int y, int width, int height)
Notifies the peer that the bounds of this component have changed.
void
setBackground(Color color)
Sets the background color of the component.
void
setBounds(int x, int y, int width, int height)
Notifies the peer that the bounds of this component have changed.
void
setBounds(int x, int y, int width, int height, int z)
Set the bounds of this component peer.
void
setCursor(Cursor cursor)
Sets the cursor of the component.
void
setEnabled(boolean enabled)
Sets the enabled/disabled state of this component.
void
setEventMask(long mask)
Part of an older API, no longer needed.
void
setFont(Font font)
Sets the font of the component.
void
setForeground(Color color)
Sets the foreground color of the component.
void
setVisible(boolean visible)
Sets the visibility state of the component.
void
show()
Makes the component visible.
void
updateCursorImmediately()
Updates the cursor.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

awtComponent

protected Component awtComponent
The AWT component for this peer.

paintArea

protected Rectangle paintArea
The current repaint area.

peerFont

protected Font peerFont
The font that is set for this peer.

swingComponent

protected SwingComponent swingComponent
The Swing component for this peer.

Constructor Details

SwingComponentPeer

protected SwingComponentPeer()
Creates a SwingComponentPeer instance. Subclasses are expected to call this constructor and thereafter call init(Component, JComponent) in order to setup the AWT and Swing components properly.

Method Details

canDetermineObscurity

public boolean canDetermineObscurity()
Returns true if this component peer can determine if the component has been obscured, false otherwise. This is not yet implemented.
Specified by:
canDetermineObscurity in interface ComponentPeer
Returns:
true if this component peer can determine if the component has been obscured, false otherwise

checkImage

public int checkImage(Image img,
                      int width,
                      int height,
                      ImageObserver ob)
Returns the construction status of the specified image. This is called by Component.checkImage(Image,int,int,ImageObserver).
Specified by:
checkImage in interface ComponentPeer
Parameters:
img - the image
width - the width of the image
height - the height of the image
ob - the image observer to be notified of updates of the status
Returns:
a bitwise ORed set of ImageObserver flags

coalescePaintEvent

public void coalescePaintEvent(PaintEvent e)
Coalesces the specified paint event.
Specified by:
coalescePaintEvent in interface ComponentPeer
Parameters:
e - the paint event

createBuffers

public void createBuffers(int numBuffers,
                          BufferCapabilities caps)
            throws AWTException
Create a number of image buffers that implement a buffering strategy according to the given capabilities. This is implemented to forward to the parent component peer. Eventually this ends up in the top level component peer, which is then responsible for doing the real work.
Specified by:
createBuffers in interface ComponentPeer
Parameters:
numBuffers - the number of buffers
caps - the buffering capabilities
Throws:
AWTException - if the specified buffering strategy is not implemented
Since:
1.2

createImage

public Image createImage(int width,
                         int height)
Creates an empty image with the specified width and height. This is implemented to let the parent component create the image. This eventually goes up to the top-level component peer, which is then expected to deliver the image.
Specified by:
createImage in interface ComponentPeer
Parameters:
width - the width of the image to be created
height - the height of the image to be created
Returns:
the created image

createImage

public Image createImage(ImageProducer prod)
Creates an image by starting the specified image producer. This is called by Component.createImage(ImageProducer).
Specified by:
createImage in interface ComponentPeer
Parameters:
prod - the image producer to be used to create the image
Returns:
the created image

createVolatileImage

public VolatileImage createVolatileImage(int width,
                                         int height)
A convenience method that creates a volatile image. The volatile image is created on the screen device on which this component is displayed, in the device's current graphics configuration. This is implemented to let the parent component peer create an image. This eventually ends up in the toplevel component peer, which is then responsible for creating the real image.
Specified by:
createVolatileImage in interface ComponentPeer
Parameters:
width - width of the image
height - height of the image
Since:
1.2
See Also:
VolatileImage

destroyBuffers

public void destroyBuffers()
Destroy the resources created by createBuffers. This is implemented to forward to the parent component peer. Eventually this ends up in the top level component peer, which is then responsible for doing the real work.
Specified by:
destroyBuffers in interface ComponentPeer
Since:
1.2

disable

public void disable()
Disables the component. This is called by Component.disable().
Specified by:
disable in interface ComponentPeer

dispose

public void dispose()
Disposes the component peer. This should release all resources held by the peer. This is called when the component is no longer in use.
Specified by:
dispose in interface ComponentPeer

enable

public void enable()
Enables the component. This is called by Component.enable().
Specified by:
enable in interface ComponentPeer

flip

public void flip(BufferCapabilities.FlipContents contents)
Perform a page flip, leaving the contents of the back buffer in the specified state. This is implemented to forward to the parent. Eventually this ends up in the toplevel component, which is then responsible for doing the real work.
Specified by:
flip in interface ComponentPeer
Parameters:
contents - the state in which to leave the back buffer
Since:
1.2

getBackBuffer

public Image getBackBuffer()
Return the back buffer of this component. This is implemented to forward to the parent. Eventually this ends up in the toplevel component, which is then responsible for providing a back buffer.
Specified by:
getBackBuffer in interface ComponentPeer
Returns:
the back buffer of this component.
Since:
1.2

getBounds

public Rectangle getBounds()
Get the bounds of this component peer. This is implemented to forward to the Swing component.
Specified by:
getBounds in interface ComponentPeer
Returns:
component peer bounds
Since:
1.5

getColorModel

public ColorModel getColorModel()
Returns the color model of the component. This is currently not used.
Specified by:
getColorModel in interface ComponentPeer
Returns:
the color model of the component

getComponent

public Component getComponent()
Returns the AWT component for this peer.
Returns:
the AWT component for this peer

getFontMetrics

public FontMetrics getFontMetrics(Font f)
Returns the font metrics for the specified font. This is called by Component.getFontMetrics(Font). This is implemented to query the font metrics from the parent component. This will eventually call the top-level component peer, which is then expected to deliver a font metrics object.
Specified by:
getFontMetrics in interface ComponentPeer
Parameters:
f - the font for which to query the font metrics
Returns:
the font metrics for the specified font

getGraphics

public Graphics getGraphics()
Returns a Graphics object suitable for drawing on this component. This is called by Component.getGraphics(). This is implemented to query the graphics from the parent component and adjust the clip and translation to match this component. This will eventually call the top-level component peer, which is then expected to deliver a graphics object.
Specified by:
getGraphics in interface ComponentPeer
Returns:
a graphics object suitable for drawing on this component

getGraphicsConfiguration

public GraphicsConfiguration getGraphicsConfiguration()
Get the graphics configuration of the component. The color model of the component can be derived from the configuration. This is implemented to return the GraphicsConfiguration of the parent component. This will eventually call the toplevel component peer, which is expected to provide a real implementation.
Specified by:
getGraphicsConfiguration in interface ComponentPeer
Returns:
the graphics configuration of the component

getLocationOnScreen

public Point getLocationOnScreen()
Returns the location of this component in screen coordinates. This is called by Component.getLocationOnScreen(). This is implemented to query the parent component peer for its screen location and adds the offset of this component to it. This will eventually call the top-level component's peer, which is then expected to provide it's screen location.
Specified by:
getLocationOnScreen in interface ComponentPeer
Returns:
the location of this component in screen coordinates

getMinimumSize

public Dimension getMinimumSize()
Returns the minimum size for the component. This is called by Component.getMinimumSize(). This is implemented to return the Swing component's minimum size.
Specified by:
getMinimumSize in interface ComponentPeer
Returns:
the minimum size for the component

getPreferredSize

public Dimension getPreferredSize()
Returns the preferred size for the component. This is called by Component.getPreferredSize(). This is implemented to return the Swing component's preferred size.
Specified by:
getPreferredSize in interface ComponentPeer
Returns:
the preferred size for the component

getToolkit

public Toolkit getToolkit()
Returns the toolkit that created this peer.
Specified by:
getToolkit in interface ComponentPeer
Returns:
the toolkit that created this peer

handleEvent

public void handleEvent(AWTEvent e)
Handles the given event. This is called from Component.dispatchEvent(AWTEvent) to give the peer a chance to react to events for the component.
Specified by:
handleEvent in interface ComponentPeer
Parameters:
e - the event

handleFocusEvent

protected void handleFocusEvent(FocusEvent e)
Handles focus events on the component. This is usually forwarded to the SwingComponent's processFocusEvent() method.
Parameters:
e - the key event

handleKeyEvent

protected void handleKeyEvent(KeyEvent e)
Handles key events on the component. This is usually forwarded to the SwingComponent's processKeyEvent() method.
Parameters:
e - the key event

handleMouseEvent

protected void handleMouseEvent(MouseEvent e)
Handles mouse events on the component. This is usually forwarded to the SwingComponent's processMouseEvent() method.
Parameters:
e - the mouse event

handleMouseMotionEvent

protected void handleMouseMotionEvent(MouseEvent e)
Handles mouse motion events on the component. This is usually forwarded to the SwingComponent's processMouseMotionEvent() method.
Parameters:
e - the mouse motion event

handlesWheelScrolling

public boolean handlesWheelScrolling()
Returns true, if this component can handle wheel scrolling, false otherwise. This is not yet implemented and returns false.
Specified by:
handlesWheelScrolling in interface ComponentPeer
Returns:
true, if this component can handle wheel scrolling, false otherwise

hide

public void hide()
Makes the component invisible. This is called from Component.hide(). This is implemented to call setVisible(false) on the Swing component.
Specified by:
hide in interface ComponentPeer

init

protected void init(Component awtComp,
                    SwingComponent swingComp)
Initializes the AWT and Swing component for this peer. It is expected that subclasses call this from within their constructor.
Parameters:
awtComp - the AWT component for this peer
swingComp - the Swing component for this peer

isFocusTraversable

public boolean isFocusTraversable()
Returns true if the component can receive keyboard input focus. This is called from Component.isFocusTraversable(). This is implemented to return isFocusable() from the Swing component.
Specified by:
isFocusTraversable in interface ComponentPeer

isFocusable

public boolean isFocusable()
Returns true if the component can receive keyboard input focus. This is called from Component.isFocusable(). This is implemented to return isFocusable() from the Swing component.
Specified by:
isFocusable in interface ComponentPeer

isObscured

public boolean isObscured()
Returns true if this component has been obscured, false otherwise. This will only work if canDetermineObscurity() also returns true. This is not yet implemented.
Specified by:
isObscured in interface ComponentPeer
Returns:
true if this component has been obscured, false otherwise.

isReparentSupported

public boolean isReparentSupported()
Check if this component supports being reparented.
Specified by:
isReparentSupported in interface ComponentPeer
Returns:
true if this component can be reparented, false otherwise.
Since:
1.5

layout

public void layout()
Layout this component peer.
Specified by:
layout in interface ComponentPeer
Since:
1.5

minimumSize

public Dimension minimumSize()
Returns the minimum size for the component. This is called by Component.minimumSize(). This is implemented to return the Swing component's minimum size.
Specified by:
minimumSize in interface ComponentPeer
Returns:
the minimum size for the component

paint

public void paint(Graphics graphics)
Paints the component. This is triggered by Component.paintAll(Graphics).
Specified by:
paint in interface ComponentPeer
Parameters:
graphics - the graphics to paint with

peerPaint

protected void peerPaint(Graphics g,
                         boolean update)
Triggers 'heavyweight' painting of the components. This usually calls paint() on the Swing component.
Parameters:
g - the graphics context to use for painting
update - wether we need to call update or paint on the AWT component

peerPaintComponent

protected void peerPaintComponent(Graphics g)
Paints the actual 'heavyweight' swing component, if there is one associated to this peer.
Parameters:
g - the graphics to paint the component with

preferredSize

public Dimension preferredSize()
Returns the preferred size for the component. This is called by Component.getPreferredSize(). This is implemented to return the Swing component's preferred size.
Specified by:
preferredSize in interface ComponentPeer
Returns:
the preferred size for the component

prepareImage

public boolean prepareImage(Image img,
                            int width,
                            int height,
                            ImageObserver ob)
Prepares an image for rendering on this component. This is called by Component.prepareImage(Image,int,int,ImageObserver).
Specified by:
prepareImage in interface ComponentPeer
Parameters:
img - the image to prepare
width - the desired width of the rendered image
height - the desired height of the rendered image
ob - the image observer to be notified of updates in the preparation process
Returns:
true if the image has been fully prepared, false otherwise (in which case the image observer receives updates)

print

public void print(Graphics graphics)
Specified by:
print in interface ComponentPeer

repaint

public void repaint(long tm,
                    int x,
                    int y,
                    int width,
                    int height)
Repaints the specified rectangle of this component. This is called from Component.repaint(long,int,int,int,int). This is implemented to call repaint() on the Swing component.
Specified by:
repaint in interface ComponentPeer
Parameters:
tm - number of milliseconds to wait with repainting
x - the X coordinate of the upper left corner of the damaged rectangle
y - the Y coordinate of the upper left corner of the damaged rectangle
width - the width of the damaged rectangle
height - the height of the damaged rectangle

reparent

public void reparent(ContainerPeer parent)
Reparent this component under another container.
Specified by:
reparent in interface ComponentPeer
Parameters:
parent -
Since:
1.5

requestFocus

public void requestFocus()
Requests that this component receives the focus. This is called from Component.requestFocus(). This calls requestFocus() on the Swing component.
Specified by:
requestFocus in interface ComponentPeer

requestFocus

public boolean requestFocus(Component source,
                            boolean tmp,
                            boolean allowWindowFocus,
                            long tm)
Requests that this component receives the focus. This is called from Component.requestFocus(). This calls requestFocus() on the Swing component.
Specified by:
requestFocus in interface ComponentPeer
Parameters:
source - the actual component that requests focus (may be a lightweight descendant of the heavyweight container)
tmp - true when the change is temporary
allowWindowFocus -
tm - the timestamp of the focus change
Returns:
true when the focus change is guaranteed to be granted, false otherwise

requestFocus

public boolean requestFocus(Component lightweightChild,
                            boolean temporary,
                            boolean focusedWindowChangeAllowed,
                            long time,
                            CausedFocusEvent.Cause cause)
Specified by:
requestFocus in interface ComponentPeer

reshape

public void reshape(int x,
                    int y,
                    int width,
                    int height)
Notifies the peer that the bounds of this component have changed. This is called by Component.reshape(int,int,int,int). This is implemented to call setBounds() on the Swing component.
Specified by:
reshape in interface ComponentPeer
Parameters:
x - the X coordinate of the upper left corner of the component
y - the Y coordinate of the upper left corner of the component
width - the width of the component
height - the height of the component

setBackground

public void setBackground(Color color)
Sets the background color of the component. This is called by Component.setBackground(Color). This is implemented to call setBackground() on the Swing component.
Specified by:
setBackground in interface ComponentPeer
Parameters:
color - the background color to set

setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height)
Notifies the peer that the bounds of this component have changed. This is called by Component.setBounds(int,int,int,int). This is implemented to call setBounds() on the Swing component.
Specified by:
setBounds in interface ComponentPeer
Parameters:
x - the X coordinate of the upper left corner of the component
y - the Y coordinate of the upper left corner of the component
width - the width of the component
height - the height of the component

setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height,
                      int z)
Set the bounds of this component peer. This is implemented to forward to the swing component.
Specified by:
setBounds in interface ComponentPeer
Parameters:
x - the new x co-ordinate
y - the new y co-ordinate
width - the new width
height - the new height
z - the new stacking level
Since:
1.5

setCursor

public void setCursor(Cursor cursor)
Sets the cursor of the component. This is called by Component.setCursor(Cursor). This is implemented to call setCursor() on the Swing component.
Specified by:
setCursor in interface ComponentPeer

setEnabled

public void setEnabled(boolean enabled)
Sets the enabled/disabled state of this component. This is called by Component.setEnabled(boolean). This is implemented to call setEnabled() on the Swing component.
Specified by:
setEnabled in interface ComponentPeer
Parameters:
enabled - true to enable the component, false to disable it

setEventMask

public void setEventMask(long mask)
Part of an older API, no longer needed.
Specified by:
setEventMask in interface ComponentPeer

setFont

public void setFont(Font font)
Sets the font of the component. This is called by Component.setFont(Font). This is implemented to call setFont() on the Swing component.
Specified by:
setFont in interface ComponentPeer
Parameters:
font - the font to set

setForeground

public void setForeground(Color color)
Sets the foreground color of the component. This is called by Component.setForeground(Color). This is implemented to call setForeground() on the Swing component.
Specified by:
setForeground in interface ComponentPeer
Parameters:
color - the foreground color to set

setVisible

public void setVisible(boolean visible)
Sets the visibility state of the component. This is called by Component.setVisible(boolean). This is implemented to call setVisible() on the Swing component.
Specified by:
setVisible in interface ComponentPeer
Parameters:
visible - true to make the component visible, false to make it invisible

show

public void show()
Makes the component visible. This is called by Component.show(). This is implemented to call setVisible(true) on the Swing component.
Specified by:
show in interface ComponentPeer

updateCursorImmediately

public void updateCursorImmediately()
Updates the cursor. This is not yet implemented.
Specified by:
updateCursorImmediately in interface ComponentPeer

SwingComponentPeer.java -- An abstract base class for Swing based peers Copyright (C) 2006, 2007 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath 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 General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.