Package org.faceless.pdf2.viewer2
Class DocumentViewport
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JPanel
-
- org.faceless.pdf2.viewer2.DocumentViewport
-
- All Implemented Interfaces:
ImageObserver
,MenuContainer
,PropertyChangeListener
,Serializable
,EventListener
,Accessible
- Direct Known Subclasses:
DualPageDocumentViewport
,MultiPageDocumentViewport
,NullDocumentViewport
,SinglePageDocumentViewport
public abstract class DocumentViewport extends JPanel implements PropertyChangeListener
ADocumentViewport
displays a view of a PDF inside aDocumentPanel
, although it can also be instantiated on it's own if required. It typically will contain one or morePagePanel
objects along with scrollbars and whatever else is required to display the document in limited screen space. See the viewer tutorial for more detail on how to use this class and the "viewer" package.This code is copyright the Big Faceless Organization. You're welcome to use, modify and distribute it in any form in your own projects, provided those projects continue to make use of the Big Faceless PDF library.
- Since:
- 2.8
- See Also:
PagePanel
,DocumentPanel
, Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
-
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
-
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
-
Field Summary
Fields Modifier and Type Field Description protected static Cursor
CURSOR_GRAB
protected static Cursor
CURSOR_GRABBING
protected int
interpagemargin
protected int
margin
protected int
mouseWheelUnit
static int
ZOOM_FIT
A value that can be passed in tosetZoomMode(int)
to specify that the document should be zoomed to fit the entire page in the Viewport.static int
ZOOM_FITHEIGHT
A value that can be passed in tosetZoomMode(int)
to specify that the document should be zoomed to ensure the height fits the height of the Viewport.static int
ZOOM_FITWIDTH
A value that can be passed in tosetZoomMode(int)
to specify that the document shuold be zoomed to ensure the width fits the width of the Viewport.static int
ZOOM_NONE
A value that can be passed tosetZoomMode(int)
to specify that no re-zooming of the page should be performed when the Viewport is resized; this is the default.-
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
-
Constructor Summary
Constructors Constructor Description DocumentViewport()
DocumentViewport(LayoutManager manager)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract void
addPagePanelInteractionListener(PagePanelInteractionListener listener)
Add aPagePanelInteractionListener
to anyPagePanel
objects that have been or will be created by this DocumentViewportabstract void
addPagePanelListener(PagePanelListener listener)
Add aPagePanelListener
to anyPagePanel
objects that have been or will be created by this DocumentViewportvoid
ensureVisible(PDFPage page, double x, double y)
Ensure the specified point on the page is visible.abstract Adjustable
getAdjustable(int position)
Return the "Adjustable" object for the specified position - typically this is the horizontal or verticalJScrollBar
, although this method may returnnull
or accept other parameters.Color
getBorderColor()
Return the Color to be used to paint the border of each page in this viewportprotected RenderingHints
getDefaultRenderingHints()
DocumentPanel
getDocumentPanel()
Return theDocumentPanel
containing this DocumentViewport.int
getNextSelectablePageIndex(PDFPage page)
Return the index of the page that should be displayed when the specified page is selected in this viewport and the "next page" is requested, via a button or other action.PDFPage
getPage()
Return the PDFPage in use bygetPagePanel()
abstract PagePanel
getPagePanel()
Get the currently displayed PagePanel.Collection<PagePanel>
getPagePanels()
Returns a read only Collection containing all the PagePanels currently displayed in this Viewport.int
getPreviousSelectablePageIndex(PDFPage page)
Return the index of the page that should be displayed when the specified page is selected in this viewport and the "previous page" is requested, via a button or other action.abstract PDFPage
getRenderingPage()
Return the page that is currently in the process of rendering.Color
getShadowColor()
Return the Color to be used to paint the shadow for each page in this viewportfloat
getTargetZoom(int zoommode, PDFPage page)
Return the appropriate zoom level when switching to the specified page.abstract JComponent
getView()
Get the JComponent that directly contains the PagePanel objects, not including scrollbars or other similar items.abstract Dimension
getViewportSize()
Return the size in pixels of the space available to display pages in this viewport, not including scrollbars or other decorationabstract float
getZoom()
Get the current zoom levelint
getZoomMode()
Return the current value of the zoom-mode, as set bysetZoomMode(int)
.abstract boolean
isDraggable()
Indicates whether the contents of this viewport can be dragged with the mouse to position them.boolean
isPageVisible(PDFPage page, double x, double y)
Indicates whether the specified point in the specified page is visible in the viewport.protected boolean
isSmoothScrolling()
Return true if this viewport is smooth scrollingprotected void
paintPageBorder(Graphics g, Rectangle r)
Paint the border of the pagevoid
propertyChange(PropertyChangeEvent event)
The viewport implements PropertyChangeListener, and will be called whenever the PDF has been updated.abstract void
removePagePanelInteractionListener(PagePanelInteractionListener listener)
Remove aPagePanelInteractionListener
from anyPagePanel
objects that have been created by this DocumentViewportabstract void
removePagePanelListener(PagePanelListener listener)
Remove aPagePanelListener
from anyPagePanel
objects that have been created by this DocumentViewportabstract void
setAdjustableValues(int horizontal, int vertical)
Set the values of the adjustables returned bygetAdjustable(int)
.void
setDocumentPanel(DocumentPanel panel)
Set the DocumentPanel that contains this DocumentViewport.abstract void
setDraggable(boolean draggable)
Sets whether to allow the contents of this viewport to be dragged with the mouse.abstract void
setPage(PDFPage page, double x, double y, double zoom)
Set the currently displayed page.abstract void
setRenderingHints(RenderingHints hints)
Set theRenderingHints
that should be used when rendering the pagesabstract void
setZoom(float zoom)
Set the zoom level of this DocumentViewport.void
setZoomMode(int mode)
Control how the document in the viewport is redrawn when the Viewport is resized.protected void
smoothScroll(int x, int y, JScrollBar hsb, JScrollBar vsb)
Smoothly adjust the supplied scrollbars from their current position to the specified position, or (if hsb or vsb are null) cancel any running adjustment.-
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
-
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Field Detail
-
CURSOR_GRAB
protected static final Cursor CURSOR_GRAB
-
CURSOR_GRABBING
protected static final Cursor CURSOR_GRABBING
-
mouseWheelUnit
protected int mouseWheelUnit
-
margin
protected int margin
-
interpagemargin
protected int interpagemargin
-
ZOOM_NONE
public static final int ZOOM_NONE
A value that can be passed tosetZoomMode(int)
to specify that no re-zooming of the page should be performed when the Viewport is resized; this is the default.- Since:
- 2.10.3
- See Also:
- Constant Field Values
-
ZOOM_FIT
public static final int ZOOM_FIT
A value that can be passed in tosetZoomMode(int)
to specify that the document should be zoomed to fit the entire page in the Viewport.- Since:
- 2.10.3
- See Also:
- Constant Field Values
-
ZOOM_FITWIDTH
public static final int ZOOM_FITWIDTH
A value that can be passed in tosetZoomMode(int)
to specify that the document shuold be zoomed to ensure the width fits the width of the Viewport.- Since:
- 2.10.3
- See Also:
- Constant Field Values
-
ZOOM_FITHEIGHT
public static final int ZOOM_FITHEIGHT
A value that can be passed in tosetZoomMode(int)
to specify that the document should be zoomed to ensure the height fits the height of the Viewport.- Since:
- 2.10.3
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DocumentViewport
public DocumentViewport()
-
DocumentViewport
public DocumentViewport(LayoutManager manager)
-
-
Method Detail
-
getDefaultRenderingHints
protected final RenderingHints getDefaultRenderingHints()
-
setRenderingHints
public abstract void setRenderingHints(RenderingHints hints)
Set theRenderingHints
that should be used when rendering the pages
-
setZoomMode
public void setZoomMode(int mode)
Control how the document in the viewport is redrawn when the Viewport is resized. The default isZOOM_NONE
, which means no action is performed, but other actions includeZOOM_FIT
,ZOOM_FITWIDTH
andZOOM_FITHEIGHT
, which will ensure that the document is always zoomed to fit completely, fit the width or fit the height of the viewport respectively- Since:
- 2.10.3
-
getZoomMode
public int getZoomMode()
Return the current value of the zoom-mode, as set bysetZoomMode(int)
. The returnd value is one ofZOOM_FIT
,ZOOM_FITWIDTH
,ZOOM_FITHEIGHT
orZOOM_NONE
(the default).- Since:
- 2.10.3
-
getTargetZoom
public float getTargetZoom(int zoommode, PDFPage page)
Return the appropriate zoom level when switching to the specified page. If the value ofzoommode
isZOOM_NONE
then this just returns the value ofgetZoom()
, otherwise the returned zoom level will correctly fit the page to the Viewport.- Parameters:
page
- the PDFPage that we are calculating the zoom for.- Since:
- 2.10.3
- See Also:
getZoomMode()
-
setPage
public abstract void setPage(PDFPage page, double x, double y, double zoom)
Set the currently displayed page. The exact implementation of this depends on the type of viewport, but the idea is that the specified page and position becomes the primary focus of this viewport.- Parameters:
page
- the page to displayx
- the left-most X position of the page, relative toPagePanel.getFullPageView(org.faceless.pdf2.PDFPage)
. A value of NaN means keep the current value. 0 means the left edgey
- the top-most Y position of the page, relative toPagePanel.getFullPageView(org.faceless.pdf2.PDFPage)
. A value of NaN means keep the current value. 0 means the top edgezoom
- the zoom level. A value of <= 0 or NaN means keep the current zoom. A value of one means 72dpi
-
ensureVisible
public void ensureVisible(PDFPage page, double x, double y)
Ensure the specified point on the page is visible. The zoom level of the page is not changed, but the page itself may be changed or repositioned to ensure the specified point is visible in the centre area.- Parameters:
x
- the X position of the page in absolute PDF points (ie measured from the bottom left)y
- the Y position of the page in absolute PDF points (ie measured from the bottom left)- Since:
- 2.10.3
-
isPageVisible
public boolean isPageVisible(PDFPage page, double x, double y)
Indicates whether the specified point in the specified page is visible in the viewport. Ifx
ory
isNaN
, indicates whether any part of the page is visible.- Parameters:
x
- the X position of the page in pointsy
- the Y position of the page in points- Since:
- 2.10.4
-
setZoom
public abstract void setZoom(float zoom)
Set the zoom level of this DocumentViewport. The page and position should remain unchanged if possible.
-
getZoom
public abstract float getZoom()
Get the current zoom level
-
getPagePanel
public abstract PagePanel getPagePanel()
Get the currently displayed PagePanel. As forsetPage()
, the exact implementation of this method depends on the type of viewport, but the idea is it returns the PagePanel that is the primary focus of this viewport.
-
getView
public abstract JComponent getView()
Get the JComponent that directly contains the PagePanel objects, not including scrollbars or other similar items.- Since:
- 2.10.4
-
getPagePanels
public Collection<PagePanel> getPagePanels()
Returns a read only Collection containing all the PagePanels currently displayed in this Viewport.- Since:
- 2.10.4
-
getPage
public PDFPage getPage()
Return the PDFPage in use bygetPagePanel()
-
getRenderingPage
public abstract PDFPage getRenderingPage()
Return the page that is currently in the process of rendering. If the page has rendered this method returns the same asgetPage()
-
getNextSelectablePageIndex
public int getNextSelectablePageIndex(PDFPage page)
Return the index of the page that should be displayed when the specified page is selected in this viewport and the "next page" is requested, via a button or other action. By default this is just the next page in the PDF- Returns:
- the index of the next page to display, or -1 if no such page exists.
- Since:
- 2.14
-
getPreviousSelectablePageIndex
public int getPreviousSelectablePageIndex(PDFPage page)
Return the index of the page that should be displayed when the specified page is selected in this viewport and the "previous page" is requested, via a button or other action. By default this is just the previous page in the PDF- Returns:
- the index of the previous page to display, or -1 if no such page exists.
- Since:
- 2.14
-
getViewportSize
public abstract Dimension getViewportSize()
Return the size in pixels of the space available to display pages in this viewport, not including scrollbars or other decoration
-
getAdjustable
public abstract Adjustable getAdjustable(int position)
Return the "Adjustable" object for the specified position - typically this is the horizontal or verticalJScrollBar
, although this method may returnnull
or accept other parameters. The adjustable is measured in AWT space (pixels from the top left). This method may return null if there is no adjustable.- Parameters:
position
- one ofAdjustable.HORIZONTAL
orAdjustable.VERTICAL
- Since:
- 2.10
-
setAdjustableValues
public abstract void setAdjustableValues(int horizontal, int vertical)
Set the values of the adjustables returned bygetAdjustable(int)
. Calling this method is preferable to setting the values of each adjustable individually when both are to be set- Parameters:
horizontal
- the value for theAdjustable.HORIZONTAL
adjustable.vertical
- the value for theAdjustable.VERTICAL
adjustable.- Since:
- 2.10.4
-
getDocumentPanel
public DocumentPanel getDocumentPanel()
Return theDocumentPanel
containing this DocumentViewport.
-
setDocumentPanel
public void setDocumentPanel(DocumentPanel panel)
Set the DocumentPanel that contains this DocumentViewport. If overriding this method you must first callsuper.setDocumentPanel(panel)
- Since:
- 2.11.4 (when it was made public)
-
addPagePanelListener
public abstract void addPagePanelListener(PagePanelListener listener)
Add aPagePanelListener
to anyPagePanel
objects that have been or will be created by this DocumentViewport- Parameters:
listener
- the listener
-
removePagePanelListener
public abstract void removePagePanelListener(PagePanelListener listener)
Remove aPagePanelListener
from anyPagePanel
objects that have been created by this DocumentViewport- Parameters:
listener
- the listener
-
addPagePanelInteractionListener
public abstract void addPagePanelInteractionListener(PagePanelInteractionListener listener)
Add aPagePanelInteractionListener
to anyPagePanel
objects that have been or will be created by this DocumentViewport- Parameters:
listener
- the listener
-
removePagePanelInteractionListener
public abstract void removePagePanelInteractionListener(PagePanelInteractionListener listener)
Remove aPagePanelInteractionListener
from anyPagePanel
objects that have been created by this DocumentViewport- Parameters:
listener
- the listener
-
isDraggable
public abstract boolean isDraggable()
Indicates whether the contents of this viewport can be dragged with the mouse to position them.- Since:
- 2.11.15
-
setDraggable
public abstract void setDraggable(boolean draggable)
Sets whether to allow the contents of this viewport to be dragged with the mouse.- Parameters:
draggable
- if true, contents may be dragged- Since:
- 2.11.15
-
propertyChange
public void propertyChange(PropertyChangeEvent event)
The viewport implements PropertyChangeListener, and will be called whenever the PDF has been updated. By default it's a no-op.- Specified by:
propertyChange
in interfacePropertyChangeListener
- Since:
- 2.11.25
-
smoothScroll
protected void smoothScroll(int x, int y, JScrollBar hsb, JScrollBar vsb)
Smoothly adjust the supplied scrollbars from their current position to the specified position, or (if hsb or vsb are null) cancel any running adjustment. The timing in ms and max distance in pixels for a smooth scroll are set by thesmoothScrollTime
andsmoothScrollDistance
properties on the DocumentPanel, and both default to 500- Parameters:
x
- the desired position of the horizontal scrollbary
- the desired position of the vertical scrollbarhsb
- the horizontal scrollbarvsb
- the vertical scrollbar- Since:
- 2.11.25
-
isSmoothScrolling
protected boolean isSmoothScrolling()
Return true if this viewport is smooth scrolling- Since:
- 2.11.25
-
getBorderColor
public Color getBorderColor()
Return the Color to be used to paint the border of each page in this viewport- Since:
- 2.16.1
-
getShadowColor
public Color getShadowColor()
Return the Color to be used to paint the shadow for each page in this viewport- Since:
- 2.16.1
-
-