Method from javax.swing.JViewport Detail: |
public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, 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. |
protected void addImpl(Component child,
Object constraints,
int index) {
setView(child);
}
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.) |
protected boolean computeBlit(int dx,
int dy,
Point blitFrom,
Point blitTo,
Dimension blitSize,
Rectangle blitPaint) {
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;
}
}
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. |
protected LayoutManager createLayoutManager() {
return ViewportLayout.SHARED_INSTANCE;
}
Subclassers can override this to install a different
layout manager (or null ) in the constructor. Returns
the LayoutManager to install on the JViewport . |
protected ViewListener createViewListener() {
return new ViewListener();
}
Creates a listener for the view. |
protected void firePropertyChange(String propertyName,
Object oldValue,
Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
if (propertyName.equals(EnableWindowBlit)) {
if (newValue != null) {
setScrollMode(BLIT_SCROLL_MODE);
} else {
setScrollMode(SIMPLE_SCROLL_MODE);
}
}
}
Notifies listeners of a property change. This is subclassed to update
the windowBlit property.
(The putClientProperty property is final). |
protected void fireStateChanged() {
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);
}
}
}
Notifies all ChangeListeners when the views
size, position, or the viewports extent size has changed. |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJViewport();
}
return accessibleContext;
}
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. |
public ChangeListener[] getChangeListeners() {
return listenerList.getListeners(ChangeListener.class);
}
Returns an array of all the ChangeListener s added
to this JViewport with addChangeListener(). |
public Dimension getExtentSize() {
return getSize();
}
Returns the size of the visible part of the view in view coordinates. |
public final Insets getInsets() {
return new Insets(0, 0, 0, 0);
}
Returns the insets (border) dimensions as (0,0,0,0), since borders
are not supported on a JViewport . |
public final Insets getInsets(Insets insets) {
insets.left = insets.top = insets.right = insets.bottom = 0;
return 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. |
public int getScrollMode() {
return scrollMode;
}
Returns the current scrolling mode. |
public ViewportUI getUI() {
return (ViewportUI)ui;
}
Returns the L&F object that renders this component. |
public String getUIClassID() {
return uiClassID;
}
Returns a string that specifies the name of the L&F class
that renders this component. |
public Component getView() {
return (getComponentCount() > 0) ? getComponent(0) : null;
}
Returns the JViewport 's one child or null . |
public Point getViewPosition() {
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);
}
}
Returns the view coordinates that appear in the upper left
hand corner of the viewport, or 0,0 if there's no view. |
public Rectangle getViewRect() {
return new Rectangle(getViewPosition(), getExtentSize());
}
Returns a rectangle whose origin is getViewPosition
and size is getExtentSize .
This is the visible part of the view, in view coordinates. |
public Dimension getViewSize() {
Component view = getView();
if (view == null) {
return new Dimension(0,0);
}
else if (isViewSizeSet) {
return view.getSize();
}
else {
return view.getPreferredSize();
}
}
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. |
public boolean isBackingStoreEnabled() {
return scrollMode == BACKINGSTORE_SCROLL_MODE;
} Deprecated! As - of Java 2 platform v1.3, replaced by
getScrollMode() .
Returns true if this viewport is maintaining
an offscreen image of its contents. |
public boolean isOptimizedDrawingEnabled() {
return false;
}
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. |
protected boolean isPaintingOrigin() {
return scrollMode == BACKINGSTORE_SCROLL_MODE;
}
Returns true if scroll mode is a {@code BACKINGSTORE_SCROLL_MODE} to cause
painting to originate from {@code JViewport}, or one of its
ancestors. Otherwise returns {@code false}. |
public void paint(Graphics g) {
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;
}
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.)
|
protected String paramString() {
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;
}
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 . |
public void remove(Component child) {
child.removeComponentListener(viewListener);
super.remove(child);
}
Removes the Viewport s one lightweight child. |
public void removeChangeListener(ChangeListener l) {
listenerList.remove(ChangeListener.class, l);
}
Removes a ChangeListener from the list that's notified each
time the views size, position, or the viewports extent size
has changed. |
public void repaint(long tm,
int x,
int y,
int w,
int h) {
Container parent = getParent();
if(parent != null)
parent.repaint(tm,x+getX(),y+getY(),w,h);
else
super.repaint(tm,x,y,w,h);
}
Always repaint in the parents coordinate system to make sure
only one paint is performed by the RepaintManager . |
public void reshape(int x,
int y,
int w,
int h) {
boolean sizeChanged = (getWidth() != w) || (getHeight() != h);
if (sizeChanged) {
backingStoreImage = null;
}
super.reshape(x, y, w, h);
if (sizeChanged) {
fireStateChanged();
}
}
Sets the bounds of this viewport. If the viewport's width
or height has changed, fire a StateChanged event. |
public void scrollRectToVisible(Rectangle contentRect) {
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, dy;
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;
}
}
}
}
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. |
public void setBackingStoreEnabled(boolean enabled) {
if (enabled) {
setScrollMode(BACKINGSTORE_SCROLL_MODE);
} else {
setScrollMode(BLIT_SCROLL_MODE);
}
} Deprecated! As - of Java 2 platform v1.3, replaced by
setScrollMode() .
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. |
public final void setBorder(Border border) {
if (border != null) {
throw new IllegalArgumentException("JViewport.setBorder() not supported");
}
}
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 . |
public void setExtentSize(Dimension newExtent) {
Dimension oldExtent = getExtentSize();
if (!newExtent.equals(oldExtent)) {
setSize(newExtent);
fireStateChanged();
}
}
Sets the size of the visible part of the view using view coordinates. |
public void setScrollMode(int mode) {
scrollMode = mode;
backingStore = mode == BACKINGSTORE_SCROLL_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. |
public void setUI(ViewportUI ui) {
super.setUI(ui);
}
Sets the L&F object that renders this component. |
public void setView(Component view) {
/* 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();
}
Sets the JViewport 's one lightweight child
(view ), which can be null . |
public void setViewPosition(Point p) {
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;
}
// we must validate the hierarchy to not break the hw/lw mixing
revalidate();
fireStateChanged();
}
}
Sets the view coordinates that appear in the upper left
hand corner of the viewport, does nothing if there's no view. |
public void setViewSize(Dimension newSize) {
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();
}
}
}
Sets the size of the view. A state changed event will be fired. |
public Dimension toViewCoordinates(Dimension size) {
return new Dimension(size);
}
Converts a size in pixel coordinates to view coordinates.
Subclasses of viewport that support "logical coordinates"
will override this method. |
public Point toViewCoordinates(Point p) {
return new Point(p);
}
Converts a point in pixel coordinates to view coordinates.
Subclasses of viewport that support "logical coordinates"
will override this method. |
public void updateUI() {
setUI((ViewportUI)UIManager.getUI(this));
}
Resets the UI property to a value from the current look and feel. |