JViewportpublic class JViewport extends JComponent implements AccessibleThe "viewport" or "porthole" through which you see the underlying
information. When you scroll, what moves is the viewport. It is like
peering through a camera's viewfinder. Moving the viewfinder upwards
brings new things into view at the top of the picture and loses
things that were at the bottom.
By default, JViewport is opaque. To change this, use the
setOpaque method.
NOTE:We have implemented a faster scrolling algorithm that
does not require a buffer to draw in. The algorithm works as follows:
- The view and parent view and checked to see if they are
JComponents ,
if they aren't, stop and repaint the whole viewport.
- If the viewport is obscured by an ancestor, stop and repaint the whole
viewport.
- Compute the region that will become visible, if it is as big as
the viewport, stop and repaint the whole view region.
- Obtain the ancestor
Window 's graphics and
do a copyArea on the scrolled region.
- Message the view to repaint the newly visible region.
- The next time paint is invoked on the viewport, if the clip region
is smaller than the viewport size a timer is kicked off to repaint the
whole region.
In general this approach is much faster. Compared to the backing store
approach this avoids the overhead of maintaining an offscreen buffer and
having to do two copyArea s.
Compared to the non backing store case this
approach will greatly reduce the painted region.
This approach can cause slower times than the backing store approach
when the viewport is obscured by another window, or partially offscreen.
When another window
obscures the viewport the copyArea will copy garbage and a
paint event will be generated by the system to inform us we need to
paint the newly exposed region. The only way to handle this is to
repaint the whole viewport, which can cause slower performance than the
backing store case. In most applications very rarely will the user be
scrolling while the viewport is obscured by another window or offscreen,
so this optimization is usually worth the performance hit when obscured.
Warning: Swing is not thread safe. For more
information see Swing's Threading
Policy.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see {@link java.beans.XMLEncoder}. |
Fields Summary |
---|
private static final String | uiClassID | static final Object | EnableWindowBlitProperty used to indicate window blitting should not be done. | protected boolean | isViewSizeSetTrue when the viewport dimensions have been determined.
The default is false. | protected Point | lastPaintPositionThe last viewPosition that we've painted, so we know how
much of the backing store image is valid. | protected boolean | backingStoreTrue when this viewport is maintaining an offscreen image of its
contents, so that some scrolling can take place using fast "bit-blit"
operations instead of by accessing the view object to construct the
display. The default is false . | protected transient Image | backingStoreImageThe view image used for a backing store. | protected boolean | scrollUnderwayThe scrollUnderway flag is used for components like
JList . When the downarrow key is pressed on a
JList and the selected
cell is the last in the list, the scrollpane autoscrolls.
Here, the old selected cell needs repainting and so we need
a flag to make the viewport do the optimized painting
only when there is an explicit call to
setViewPosition(Point) .
When setBounds is called through other routes,
the flag is off and the view repaints normally. Another approach
would be to remove this from the JViewport
class and have the JList manage this case by using
setBackingStoreEnabled . The default is
false . | private ComponentListener | viewListener | private transient ChangeEvent | changeEvent | public static final int | BLIT_SCROLL_MODEUse graphics.copyArea to implement scrolling.
This is the fastest for most applications. | public static final int | BACKINGSTORE_SCROLL_MODEDraws viewport contents into an offscreen image.
This was previously the default mode for JTable .
This mode may offer advantages over "blit mode"
in some cases, but it requires a large chunk of extra RAM. | public static final int | SIMPLE_SCROLL_MODEThis mode uses the very simple method of redrawing the entire
contents of the scrollpane each time it is scrolled.
This was the default behavior in Swing 1.0 and Swing 1.1.
Either of the other two options will provide better performance
in most cases. | private int | scrollMode | private transient boolean | repaintAllThis is set to true in setViewPosition
if doing a window blit and the viewport is obscured. | private transient boolean | waitingForRepaintThis is set to true in paint, if repaintAll
is true and the clip rectangle does not match the bounds.
If true, and scrolling happens the
repaint manager is not cleared which then allows for the repaint
previously invoked to succeed. | private transient Timer | repaintTimerInstead of directly invoking repaint, a Timer
is started and when it fires, repaint is invoked. | private transient boolean | inBlitPaintSet to true in paintView when paint is invoked. | private boolean | hasHadValidViewWhether or not a valid view has been installed. |
Constructors Summary |
---|
public JViewport()Creates a JViewport .
super();
setLayout(createLayoutManager());
setOpaque(true);
updateUI();
setInheritsPopupMenu(true);
|
Methods Summary |
---|
public void | addChangeListener(javax.swing.event.ChangeListener l)Adds a ChangeListener to the list that is
notified each time the view's
size, position, or the viewport's extent size has changed.
listenerList.add(ChangeListener.class, l);
| protected void | addImpl(java.awt.Component child, java.lang.Object constraints, int index)Sets the JViewport 's one lightweight child,
which can be null .
(Since there is only one child which occupies the entire viewport,
the constraints and index
arguments are ignored.)
setView(child);
| private void | blitDoubleBuffered(javax.swing.JComponent view, java.awt.Graphics g, int clipX, int clipY, int clipW, int clipH, int blitFromX, int blitFromY, int blitToX, int blitToY, int blitW, int blitH)
// NOTE:
// blitFrom/blitTo are in JViewport coordinates system
// not the views coordinate space.
// clip* are in the views coordinate space.
RepaintManager rm = RepaintManager.currentManager(this);
int bdx = blitToX - blitFromX;
int bdy = blitToY - blitFromY;
// Shift the scrolled region
rm.copyArea(this, g, blitFromX, blitFromY, blitW, blitH, bdx, bdy,
false);
// Paint the newly exposed region.
int x = view.getX();
int y = view.getY();
g.translate(x, y);
g.setClip(clipX, clipY, clipW, clipH);
view.paintForceDoubleBuffered(g);
g.translate(-x, -y);
| private boolean | canUseWindowBlitter()Returns true if the viewport is not obscured by one of its ancestors,
or its ancestors children and if the viewport is showing. Blitting
when the view isn't showing will work,
or rather copyArea will work,
but will not produce the expected behavior.
if (!isShowing() || (!(getParent() instanceof JComponent) &&
!(getView() instanceof JComponent))) {
return false;
}
if (isPainting()) {
// We're in the process of painting, don't blit. If we were
// to blit we would draw on top of what we're already drawing,
// so bail.
return false;
}
Rectangle dirtyRegion = RepaintManager.currentManager(this).
getDirtyRegion((JComponent)getParent());
if (dirtyRegion != null && dirtyRegion.width > 0 &&
dirtyRegion.height > 0) {
// Part of the scrollpane needs to be repainted too, don't blit.
return false;
}
Rectangle clip = new Rectangle(0,0,getWidth(),getHeight());
Rectangle oldClip = new Rectangle();
Rectangle tmp2 = null;
Container parent;
Component lastParent = null;
int x, y, w, h;
for(parent = this; parent != null && isLightweightComponent(parent); parent = parent.getParent()) {
x = parent.getX();
y = parent.getY();
w = parent.getWidth();
h = parent.getHeight();
oldClip.setBounds(clip);
SwingUtilities.computeIntersection(0, 0, w, h, clip);
if(!clip.equals(oldClip))
return false;
if(lastParent != null && parent instanceof JComponent &&
!((JComponent)parent).isOptimizedDrawingEnabled()) {
Component comps[] = parent.getComponents();
int index = 0;
for(int i = comps.length - 1 ;i >= 0; i--) {
if(comps[i] == lastParent) {
index = i - 1;
break;
}
}
while(index >= 0) {
tmp2 = comps[index].getBounds(tmp2);
if(tmp2.intersects(clip))
return false;
index--;
}
}
clip.x += x;
clip.y += y;
lastParent = parent;
}
if (parent == null) {
// No Window parent.
return false;
}
return true;
| protected boolean | computeBlit(int dx, int dy, java.awt.Point blitFrom, java.awt.Point blitTo, java.awt.Dimension blitSize, java.awt.Rectangle blitPaint)Computes the parameters for a blit where the backing store image
currently contains oldLoc in the upper left hand corner
and we're scrolling to newLoc .
The parameters are modified
to return the values required for the blit.
int dxAbs = Math.abs(dx);
int dyAbs = Math.abs(dy);
Dimension extentSize = getExtentSize();
if ((dx == 0) && (dy != 0) && (dyAbs < extentSize.height)) {
if (dy < 0) {
blitFrom.y = -dy;
blitTo.y = 0;
blitPaint.y = extentSize.height + dy;
}
else {
blitFrom.y = 0;
blitTo.y = dy;
blitPaint.y = 0;
}
blitPaint.x = blitFrom.x = blitTo.x = 0;
blitSize.width = extentSize.width;
blitSize.height = extentSize.height - dyAbs;
blitPaint.width = extentSize.width;
blitPaint.height = dyAbs;
return true;
}
else if ((dy == 0) && (dx != 0) && (dxAbs < extentSize.width)) {
if (dx < 0) {
blitFrom.x = -dx;
blitTo.x = 0;
blitPaint.x = extentSize.width + dx;
}
else {
blitFrom.x = 0;
blitTo.x = dx;
blitPaint.x = 0;
}
blitPaint.y = blitFrom.y = blitTo.y = 0;
blitSize.width = extentSize.width - dxAbs;
blitSize.height = extentSize.height;
blitPaint.width = dxAbs;
blitPaint.height = extentSize.height;
return true;
}
else {
return false;
}
| protected java.awt.LayoutManager | createLayoutManager()Subclassers can override this to install a different
layout manager (or null ) in the constructor. Returns
the LayoutManager to install on the JViewport .
return ViewportLayout.SHARED_INSTANCE;
| private javax.swing.Timer | createRepaintTimer()
Timer timer = new Timer(300, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// waitingForRepaint will be false if a paint came down
// with the complete clip rect, in which case we don't
// have to cause a repaint.
if (waitingForRepaint) {
repaint();
}
}
});
timer.setRepeats(false);
return timer;
| protected javax.swing.JViewport$ViewListener | createViewListener()Creates a listener for the view.
return new ViewListener();
| protected void | firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)Notifies listeners of a property change. This is subclassed to update
the windowBlit property.
(The putClientProperty property is final).
super.firePropertyChange(propertyName, oldValue, newValue);
if (propertyName.equals(EnableWindowBlit)) {
if (newValue != null) {
setScrollMode(BLIT_SCROLL_MODE);
} else {
setScrollMode(SIMPLE_SCROLL_MODE);
}
}
| protected void | fireStateChanged()Notifies all ChangeListeners when the views
size, position, or the viewports extent size has changed.
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((ChangeListener)listeners[i + 1]).stateChanged(changeEvent);
}
}
| private void | flushViewDirtyRegion(java.awt.Graphics g, java.awt.Rectangle dirty)If the repaint manager has a dirty region for the view, the view is
asked to paint.
JComponent view = (JComponent) getView();
if(dirty != null && dirty.width > 0 && dirty.height > 0) {
dirty.x += view.getX();
dirty.y += view.getY();
Rectangle clip = g.getClipBounds();
if (clip == null) {
// Only happens in 1.2
g.setClip(0, 0, getWidth(), getHeight());
}
g.clipRect(dirty.x, dirty.y, dirty.width, dirty.height);
clip = g.getClipBounds();
// Only paint the dirty region if it is visible.
if (clip.width > 0 && clip.height > 0) {
paintView(g);
}
}
| public javax.accessibility.AccessibleContext | getAccessibleContext()Gets the AccessibleContext associated with this JViewport.
For viewports, the AccessibleContext takes the form of an
AccessibleJViewport.
A new AccessibleJViewport instance is created if necessary.
if (accessibleContext == null) {
accessibleContext = new AccessibleJViewport();
}
return accessibleContext;
| private java.awt.Graphics | getBackingStoreGraphics(java.awt.Graphics g)
Graphics bsg = backingStoreImage.getGraphics();
bsg.setColor(g.getColor());
bsg.setFont(g.getFont());
bsg.setClip(g.getClipBounds());
return bsg;
| public javax.swing.event.ChangeListener[] | getChangeListeners()Returns an array of all the ChangeListener s added
to this JViewport with addChangeListener().
return (ChangeListener[])listenerList.getListeners(
ChangeListener.class);
| public java.awt.Dimension | getExtentSize()Returns the size of the visible part of the view in view coordinates.
return getSize();
| public final java.awt.Insets | getInsets()Returns the insets (border) dimensions as (0,0,0,0), since borders
are not supported on a JViewport .
return new Insets(0, 0, 0, 0);
| public final java.awt.Insets | getInsets(java.awt.Insets insets)Returns an Insets object containing this
JViewport s inset values. The passed-in
Insets object will be reinitialized, and
all existing values within this object are overwritten.
insets.left = insets.top = insets.right = insets.bottom = 0;
return insets;
| public int | getScrollMode()Returns the current scrolling mode.
return scrollMode;
| public javax.swing.plaf.ViewportUI | getUI()Returns the L&F object that renders this component.
return (ViewportUI)ui;
| public java.lang.String | getUIClassID()Returns a string that specifies the name of the L&F class
that renders this component.
return uiClassID;
| public java.awt.Component | getView()Returns the JViewport 's one child or null .
return (getComponentCount() > 0) ? getComponent(0) : null;
| private java.awt.Point | getViewLocation()Only used by the paint method below.
Component view = getView();
if (view != null) {
return view.getLocation();
}
else {
return new Point(0,0);
}
| public java.awt.Point | getViewPosition()Returns the view coordinates that appear in the upper left
hand corner of the viewport, or 0,0 if there's no view.
Component view = getView();
if (view != null) {
Point p = view.getLocation();
p.x = -p.x;
p.y = -p.y;
return p;
}
else {
return new Point(0,0);
}
| public java.awt.Rectangle | getViewRect()Returns a rectangle whose origin is getViewPosition
and size is getExtentSize .
This is the visible part of the view, in view coordinates.
return new Rectangle(getViewPosition(), getExtentSize());
| public java.awt.Dimension | getViewSize()If the view's size hasn't been explicitly set, return the
preferred size, otherwise return the view's current size.
If there is no view, return 0,0.
Component view = getView();
if (view == null) {
return new Dimension(0,0);
}
else if (isViewSizeSet) {
return view.getSize();
}
else {
return view.getPreferredSize();
}
| public boolean | isBackingStoreEnabled()Returns true if this viewport is maintaining
an offscreen image of its contents.
return scrollMode == BACKINGSTORE_SCROLL_MODE;
| private final boolean | isBlitting()
Component view = getView();
return (scrollMode == BLIT_SCROLL_MODE) &&
(view instanceof JComponent) && ((JComponent)view).isOpaque();
| public boolean | isOptimizedDrawingEnabled()The JViewport overrides the default implementation of
this method (in JComponent ) to return false.
This ensures
that the drawing machinery will call the Viewport 's
paint
implementation rather than messaging the JViewport 's
children directly.
return false;
| boolean | isPaintingOrigin()Returns true if scroll mode is a BACKINGSTORE_SCROLL_MODE to cause
painting to originate from JViewport , or one of its
ancestors. Otherwise returns false.
if (scrollMode == BACKINGSTORE_SCROLL_MODE) {
return true;
}
return false;
| private boolean | needsRepaintAfterBlit()Returns true if the component needs to be completely repainted after
a blit and a paint is received.
// Find the first heavy weight ancestor. isObscured and
// canDetermineObscurity are only appropriate for heavy weights.
Component heavyParent = getParent();
while (heavyParent != null && heavyParent.isLightweight()) {
heavyParent = heavyParent.getParent();
}
if (heavyParent != null) {
ComponentPeer peer = heavyParent.getPeer();
if (peer != null && peer.canDetermineObscurity() &&
!peer.isObscured()) {
// The peer says we aren't obscured, therefore we can assume
// that we won't later be messaged to paint a portion that
// we tried to blit that wasn't valid.
// It is certainly possible that when we blited we were
// obscured, and by the time this is invoked we aren't, but the
// chances of that happening are pretty slim.
return false;
}
}
return true;
| public void | paint(java.awt.Graphics g)Depending on whether the backingStore is enabled,
either paint the image through the backing store or paint
just the recently exposed part, using the backing store
to "blit" the remainder.
The term "blit" is the pronounced version of the PDP-10
BLT (BLock Transfer) instruction, which copied a block of
bits. (In case you were curious.)
int width = getWidth();
int height = getHeight();
if ((width <= 0) || (height <= 0)) {
return;
}
if (inBlitPaint) {
// We invoked paint as part of copyArea cleanup, let it through.
super.paint(g);
return;
}
if (repaintAll) {
repaintAll = false;
Rectangle clipB = g.getClipBounds();
if (clipB.width < getWidth() ||
clipB.height < getHeight()) {
waitingForRepaint = true;
if (repaintTimer == null) {
repaintTimer = createRepaintTimer();
}
repaintTimer.stop();
repaintTimer.start();
// We really don't need to paint, a future repaint will
// take care of it, but if we don't we get an ugly flicker.
}
else {
if (repaintTimer != null) {
repaintTimer.stop();
}
waitingForRepaint = false;
}
}
else if (waitingForRepaint) {
// Need a complete repaint before resetting waitingForRepaint
Rectangle clipB = g.getClipBounds();
if (clipB.width >= getWidth() &&
clipB.height >= getHeight()) {
waitingForRepaint = false;
repaintTimer.stop();
}
}
if (!backingStore || isBlitting() || getView() == null) {
super.paint(g);
lastPaintPosition = getViewLocation();
return;
}
// If the view is smaller than the viewport and we are not opaque
// (that is, we won't paint our background), we should set the
// clip. Otherwise, as the bounds of the view vary, we will
// blit garbage into the exposed areas.
Rectangle viewBounds = getView().getBounds();
if (!isOpaque()) {
g.clipRect(0, 0, viewBounds.width, viewBounds.height);
}
if (backingStoreImage == null) {
// Backing store is enabled but this is the first call to paint.
// Create the backing store, paint it and then copy to g.
// The backing store image will be created with the size of
// the viewport. We must make sure the clip region is the
// same size, otherwise when scrolling the backing image
// the region outside of the clipped region will not be painted,
// and result in empty areas.
backingStoreImage = createImage(width, height);
Rectangle clip = g.getClipBounds();
if (clip.width != width || clip.height != height) {
if (!isOpaque()) {
g.setClip(0, 0, Math.min(viewBounds.width, width),
Math.min(viewBounds.height, height));
}
else {
g.setClip(0, 0, width, height);
}
paintViaBackingStore(g, clip);
}
else {
paintViaBackingStore(g);
}
}
else {
if (!scrollUnderway || lastPaintPosition.equals(getViewLocation())) {
// No scrolling happened: repaint required area via backing store.
paintViaBackingStore(g);
} else {
// The image was scrolled. Manipulate the backing store and flush it to g.
Point blitFrom = new Point();
Point blitTo = new Point();
Dimension blitSize = new Dimension();
Rectangle blitPaint = new Rectangle();
Point newLocation = getViewLocation();
int dx = newLocation.x - lastPaintPosition.x;
int dy = newLocation.y - lastPaintPosition.y;
boolean canBlit = computeBlit(dx, dy, blitFrom, blitTo, blitSize, blitPaint);
if (!canBlit) {
// The image was either moved diagonally or
// moved by more than the image size: paint normally.
paintViaBackingStore(g);
} else {
int bdx = blitTo.x - blitFrom.x;
int bdy = blitTo.y - blitFrom.y;
// Move the relevant part of the backing store.
Rectangle clip = g.getClipBounds();
// We don't want to inherit the clip region when copying
// bits, if it is inherited it will result in not moving
// all of the image resulting in garbage appearing on
// the screen.
g.setClip(0, 0, width, height);
Graphics bsg = getBackingStoreGraphics(g);
try {
bsg.copyArea(blitFrom.x, blitFrom.y, blitSize.width, blitSize.height, bdx, bdy);
g.setClip(clip.x, clip.y, clip.width, clip.height);
// Paint the rest of the view; the part that has just been exposed.
Rectangle r = viewBounds.intersection(blitPaint);
bsg.setClip(r);
super.paint(bsg);
// Copy whole of the backing store to g.
g.drawImage(backingStoreImage, 0, 0, this);
} finally {
bsg.dispose();
}
}
}
}
lastPaintPosition = getViewLocation();
scrollUnderway = false;
| private void | paintViaBackingStore(java.awt.Graphics g)
Graphics bsg = getBackingStoreGraphics(g);
try {
super.paint(bsg);
g.drawImage(backingStoreImage, 0, 0, this);
} finally {
bsg.dispose();
}
| private void | paintViaBackingStore(java.awt.Graphics g, java.awt.Rectangle oClip)
Graphics bsg = getBackingStoreGraphics(g);
try {
super.paint(bsg);
g.setClip(oClip);
g.drawImage(backingStoreImage, 0, 0, this);
} finally {
bsg.dispose();
}
| private void | paintView(java.awt.Graphics g)Called to paint the view, usually when blitPaint
can not blit.
Rectangle clip = g.getClipBounds();
JComponent view = (JComponent)getView();
if (view.getWidth() >= getWidth()) {
// Graphics is relative to JViewport, need to map to view's
// coordinates space.
int x = view.getX();
int y = view.getY();
g.translate(x, y);
g.setClip(clip.x - x, clip.y - y, clip.width, clip.height);
view.paintForceDoubleBuffered(g);
g.translate(-x, -y);
g.setClip(clip.x, clip.y, clip.width, clip.height);
}
else {
// To avoid any problems that may result from the viewport being
// bigger than the view we start painting from the viewport.
try {
inBlitPaint = true;
paintForceDoubleBuffered(g);
} finally {
inBlitPaint = false;
}
}
| protected java.lang.String | paramString()Returns a string representation of this JViewport .
This method
is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null .
String isViewSizeSetString = (isViewSizeSet ?
"true" : "false");
String lastPaintPositionString = (lastPaintPosition != null ?
lastPaintPosition.toString() : "");
String scrollUnderwayString = (scrollUnderway ?
"true" : "false");
return super.paramString() +
",isViewSizeSet=" + isViewSizeSetString +
",lastPaintPosition=" + lastPaintPositionString +
",scrollUnderway=" + scrollUnderwayString;
| private int | positionAdjustment(int parentWidth, int childWidth, int childAt)
// +-----+
// | --- | No Change
// +-----+
if (childAt >= 0 && childWidth + childAt <= parentWidth) {
return 0;
}
// +-----+
// --------- No Change
// +-----+
if (childAt <= 0 && childWidth + childAt >= parentWidth) {
return 0;
}
// +-----+ +-----+
// | ---- -> | ----|
// +-----+ +-----+
if (childAt > 0 && childWidth <= parentWidth) {
return -childAt + parentWidth - childWidth;
}
// +-----+ +-----+
// | -------- -> |--------
// +-----+ +-----+
if (childAt >= 0 && childWidth >= parentWidth) {
return -childAt;
}
// +-----+ +-----+
// ---- | -> |---- |
// +-----+ +-----+
if (childAt <= 0 && childWidth <= parentWidth) {
return -childAt;
}
// +-----+ +-----+
//-------- | -> --------|
// +-----+ +-----+
if (childAt < 0 && childWidth >= parentWidth) {
return -childAt + parentWidth - childWidth;
}
return 0;
| public void | remove(java.awt.Component child)Removes the Viewport s one lightweight child.
child.removeComponentListener(viewListener);
super.remove(child);
| public void | removeChangeListener(javax.swing.event.ChangeListener l)Removes a ChangeListener from the list that's notified each
time the views size, position, or the viewports extent size
has changed.
listenerList.remove(ChangeListener.class, l);
| public void | repaint(long tm, int x, int y, int w, int h)Always repaint in the parents coordinate system to make sure
only one paint is performed by the RepaintManager .
Container parent = getParent();
if(parent != null)
parent.repaint(tm,x+getX(),y+getY(),w,h);
else
super.repaint(tm,x,y,w,h);
| public void | reshape(int x, int y, int w, int h)Sets the bounds of this viewport. If the viewport's width
or height has changed, fire a StateChanged event.
boolean sizeChanged = (getWidth() != w) || (getHeight() != h);
if (sizeChanged) {
backingStoreImage = null;
}
super.reshape(x, y, w, h);
if (sizeChanged) {
fireStateChanged();
}
| public void | scrollRectToVisible(java.awt.Rectangle contentRect)Scrolls the view so that Rectangle
within the view becomes visible.
This attempts to validate the view before scrolling if the
view is currently not valid - isValid returns false.
To avoid excessive validation when the containment hierarchy is
being created this will not validate if one of the ancestors does not
have a peer, or there is no validate root ancestor, or one of the
ancestors is not a Window or Applet .
Note that this method will not scroll outside of the
valid viewport; for example, if contentRect is larger
than the viewport, scrolling will be confined to the viewport's
bounds.
Component view = getView();
if (view == null) {
return;
} else {
if (!view.isValid()) {
// If the view is not valid, validate. scrollRectToVisible
// may fail if the view is not valid first, contentRect
// could be bigger than invalid size.
validateView();
}
int dx = 0, dy = 0;
dx = positionAdjustment(getWidth(), contentRect.width, contentRect.x);
dy = positionAdjustment(getHeight(), contentRect.height, contentRect.y);
if (dx != 0 || dy != 0) {
Point viewPosition = getViewPosition();
Dimension viewSize = view.getSize();
int startX = viewPosition.x;
int startY = viewPosition.y;
Dimension extent = getExtentSize();
viewPosition.x -= dx;
viewPosition.y -= dy;
// Only constrain the location if the view is valid. If the
// the view isn't valid, it typically indicates the view
// isn't visible yet and most likely has a bogus size as will
// we, and therefore we shouldn't constrain the scrolling
if (view.isValid()) {
if (getParent().getComponentOrientation().isLeftToRight()) {
if (viewPosition.x + extent.width > viewSize.width) {
viewPosition.x = Math.max(0, viewSize.width - extent.width);
} else if (viewPosition.x < 0) {
viewPosition.x = 0;
}
} else {
if (extent.width > viewSize.width) {
viewPosition.x = viewSize.width - extent.width;
} else {
viewPosition.x = Math.max(0, Math.min(viewSize.width - extent.width, viewPosition.x));
}
}
if (viewPosition.y + extent.height > viewSize.height) {
viewPosition.y = Math.max(0, viewSize.height -
extent.height);
}
else if (viewPosition.y < 0) {
viewPosition.y = 0;
}
}
if (viewPosition.x != startX || viewPosition.y != startY) {
setViewPosition(viewPosition);
// NOTE: How JViewport currently works with the
// backing store is not foolproof. The sequence of
// events when setViewPosition
// (scrollRectToVisible) is called is to reset the
// views bounds, which causes a repaint on the
// visible region and sets an ivar indicating
// scrolling (scrollUnderway). When
// JViewport.paint is invoked if scrollUnderway is
// true, the backing store is blitted. This fails
// if between the time setViewPosition is invoked
// and paint is received another repaint is queued
// indicating part of the view is invalid. There
// is no way for JViewport to notice another
// repaint has occured and it ends up blitting
// what is now a dirty region and the repaint is
// never delivered.
// It just so happens JTable encounters this
// behavior by way of scrollRectToVisible, for
// this reason scrollUnderway is set to false
// here, which effectively disables the backing
// store.
scrollUnderway = false;
}
}
}
| public void | setBackingStoreEnabled(boolean enabled)If true if this viewport will maintain an offscreen
image of its contents. The image is used to reduce the cost
of small one dimensional changes to the viewPosition .
Rather than repainting the entire viewport we use
Graphics.copyArea to effect some of the scroll.
if (enabled) {
setScrollMode(BACKINGSTORE_SCROLL_MODE);
} else {
setScrollMode(BLIT_SCROLL_MODE);
}
| public final void | setBorder(javax.swing.border.Border border)The viewport "scrolls" its child (called the "view") by the
normal parent/child clipping (typically the view is moved in
the opposite direction of the scroll). A non-null border,
or non-zero insets, isn't supported, to prevent the geometry
of this component from becoming complex enough to inhibit
subclassing. To create a JViewport with a border,
add it to a JPanel that has a border.
Note: If border is non-null , this
method will throw an exception as borders are not supported on
a JViewPort .
if (border != null) {
throw new IllegalArgumentException("JViewport.setBorder() not supported");
}
| public void | setExtentSize(java.awt.Dimension newExtent)Sets the size of the visible part of the view using view coordinates.
Dimension oldExtent = getExtentSize();
if (!newExtent.equals(oldExtent)) {
setSize(newExtent);
fireStateChanged();
}
| public void | setScrollMode(int mode)Used to control the method of scrolling the viewport contents.
You may want to change this mode to get maximum performance for your
use case.
scrollMode = mode;
if (mode == BACKINGSTORE_SCROLL_MODE) {
backingStore = true;
} else {
backingStore = false;
}
| public void | setUI(javax.swing.plaf.ViewportUI ui)Sets the L&F object that renders this component.
super.setUI(ui);
| public void | setView(java.awt.Component view)Sets the JViewport 's one lightweight child
(view ), which can be null .
/* Remove the viewport's existing children, if any.
* Note that removeAll() isn't used here because it
* doesn't call remove() (which JViewport overrides).
*/
int n = getComponentCount();
for(int i = n - 1; i >= 0; i--) {
remove(getComponent(i));
}
isViewSizeSet = false;
if (view != null) {
super.addImpl(view, null, -1);
viewListener = createViewListener();
view.addComponentListener(viewListener);
}
if (hasHadValidView) {
// Only fire a change if a view has been installed.
fireStateChanged();
}
else if (view != null) {
hasHadValidView = true;
}
revalidate();
repaint();
| public void | setViewPosition(java.awt.Point p)Sets the view coordinates that appear in the upper left
hand corner of the viewport, does nothing if there's no view.
Component view = getView();
if (view == null) {
return;
}
int oldX, oldY, x = p.x, y = p.y;
/* Collect the old x,y values for the views location
* and do the song and dance to avoid allocating
* a Rectangle object if we don't have to.
*/
if (view instanceof JComponent) {
JComponent c = (JComponent)view;
oldX = c.getX();
oldY = c.getY();
}
else {
Rectangle r = view.getBounds();
oldX = r.x;
oldY = r.y;
}
/* The view scrolls in the opposite direction to mouse
* movement.
*/
int newX = -x;
int newY = -y;
if ((oldX != newX) || (oldY != newY)) {
if (!waitingForRepaint && isBlitting() && canUseWindowBlitter()) {
RepaintManager rm = RepaintManager.currentManager(this);
// The cast to JComponent will work, if view is not
// a JComponent, isBlitting will return false.
JComponent jview = (JComponent)view;
Rectangle dirty = rm.getDirtyRegion(jview);
if (dirty == null || !dirty.contains(jview.getVisibleRect())) {
rm.beginPaint();
try {
Graphics g = JComponent.safelyGetGraphics(this);
flushViewDirtyRegion(g, dirty);
view.setLocation(newX, newY);
g.setClip(0,0,getWidth(), Math.min(getHeight(),
jview.getHeight()));
// Repaint the complete component if the blit succeeded
// and needsRepaintAfterBlit returns true.
repaintAll = (windowBlitPaint(g) &&
needsRepaintAfterBlit());
g.dispose();
rm.markCompletelyClean((JComponent)getParent());
rm.markCompletelyClean(this);
rm.markCompletelyClean(jview);
} finally {
rm.endPaint();
}
}
else {
// The visible region is dirty, no point in doing copyArea
view.setLocation(newX, newY);
repaintAll = false;
}
}
else {
scrollUnderway = true;
// This calls setBounds(), and then repaint().
view.setLocation(newX, newY);
repaintAll = false;
}
fireStateChanged();
}
| public void | setViewSize(java.awt.Dimension newSize)Sets the size of the view. A state changed event will be fired.
Component view = getView();
if (view != null) {
Dimension oldSize = view.getSize();
if (!newSize.equals(oldSize)) {
// scrollUnderway will be true if this is invoked as the
// result of a validate and setViewPosition was previously
// invoked.
scrollUnderway = false;
view.setSize(newSize);
isViewSizeSet = true;
fireStateChanged();
}
}
| public java.awt.Dimension | toViewCoordinates(java.awt.Dimension size)Converts a size in pixel coordinates to view coordinates.
Subclasses of viewport that support "logical coordinates"
will override this method.
return new Dimension(size);
| public java.awt.Point | toViewCoordinates(java.awt.Point p)Converts a point in pixel coordinates to view coordinates.
Subclasses of viewport that support "logical coordinates"
will override this method.
return new Point(p);
| public void | updateUI()Resets the UI property to a value from the current look and feel.
setUI((ViewportUI)UIManager.getUI(this));
| private void | validateView()Ascends the Viewport 's parents stopping when
a component is found that returns
true to isValidateRoot .
If all the Component 's parents are visible,
validate will then be invoked on it. The
RepaintManager is then invoked with
removeInvalidComponent . This
is the synchronous version of a revalidate .
Component validateRoot = null;
/* Find the first JComponent ancestor of this component whose
* isValidateRoot() method returns true.
*/
for(Component c = this; c != null; c = c.getParent()) {
if ((c instanceof CellRendererPane) || (c.getPeer() == null)) {
return;
}
if ((c instanceof JComponent) &&
(((JComponent)c).isValidateRoot())) {
validateRoot = c;
break;
}
}
// If no validateRoot, nothing to validate from.
if (validateRoot == null) {
return;
}
// Make sure all ancestors are visible.
Component root = null;
for(Component c = validateRoot; c != null; c = c.getParent()) {
// We don't check isVisible here, otherwise if the component
// is contained in something like a JTabbedPane when the
// component is made visible again it won't have scrolled
// to the correct location.
if (c.getPeer() == null) {
return;
}
if ((c instanceof Window) || (c instanceof Applet)) {
root = c;
break;
}
}
// Make sure there is a Window ancestor.
if (root == null) {
return;
}
// Validate the root.
validateRoot.validate();
// And let the RepaintManager it does not have to validate from
// validateRoot anymore.
RepaintManager rm = RepaintManager.currentManager(this);
if (rm != null) {
rm.removeInvalidComponent((JComponent)validateRoot);
}
| private boolean | windowBlitPaint(java.awt.Graphics g)Used when blitting.
int width = getWidth();
int height = getHeight();
if ((width == 0) || (height == 0)) {
return false;
}
boolean retValue;
RepaintManager rm = RepaintManager.currentManager(this);
JComponent view = (JComponent) getView();
if (lastPaintPosition == null ||
lastPaintPosition.equals(getViewLocation())) {
paintView(g);
retValue = false;
} else {
// The image was scrolled. Manipulate the backing store and flush
// it to g.
Point blitFrom = new Point();
Point blitTo = new Point();
Dimension blitSize = new Dimension();
Rectangle blitPaint = new Rectangle();
Point newLocation = getViewLocation();
int dx = newLocation.x - lastPaintPosition.x;
int dy = newLocation.y - lastPaintPosition.y;
boolean canBlit = computeBlit(dx, dy, blitFrom, blitTo, blitSize,
blitPaint);
if (!canBlit) {
paintView(g);
retValue = false;
} else {
// Prepare the rest of the view; the part that has just been
// exposed.
Rectangle r = view.getBounds().intersection(blitPaint);
r.x -= view.getX();
r.y -= view.getY();
blitDoubleBuffered(view, g, r.x, r.y, r.width, r.height,
blitFrom.x, blitFrom.y, blitTo.x, blitTo.y,
blitSize.width, blitSize.height);
retValue = true;
}
}
lastPaintPosition = getViewLocation();
return retValue;
|
|