org.icepdf.ri.util.jxlayer
Class JXLayer<V extends java.awt.Component>

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by org.icepdf.ri.util.jxlayer.JXLayer<V>
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.beans.PropertyChangeListener, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.Scrollable

public final class JXLayer<V extends java.awt.Component>
extends javax.swing.JComponent
implements javax.swing.Scrollable, java.beans.PropertyChangeListener, javax.accessibility.Accessible

The universal decorator for Swing components with which you can implement various advanced painting effects as well as receive notification of all MouseEvents, KeyEvents and FocusEvents which generated within its borders.

JXLayer delegates its painting and input events handling to its LayerUI object which performs the actual decoration.

The custom painting and events notification automatically work for JXLayer itself and all its subcomponents. This powerful combination makes it possible to enrich existing components with new advanced functionality such as temporary locking of a hierarchy, data tips for compound components, enhanced mouse scrolling etc...

JXLayer is a great solution if you just need to do custom painting over compound component or catch input events of its subcomponents.

         // create a component to be decorated with the layer
        JPanel panel = new JPanel();
        panel.add(new JButton("JButton"));

        // This custom layerUI will fill the layer with translucent green
        // and print out all mouseMotion events generated within its borders
        AbstractLayerUI<JPanel> layerUI = new AbstractLayerUI<JPanel>() {

            protected void paintLayer(Graphics2D g2, JXLayer<JPanel> l) {
                // this paints the layer as is
                super.paintLayer(g2, l);
                // fill it with the translucent green
                g2.setColor(new Color(0, 128, 0, 128));
                g2.fillRect(0, 0, l.getWidth(), l.getHeight());
            }

            // overridden method which catches MouseMotion events
            protected void processMouseMotionEvent(MouseEvent e) {
                System.out.println("MouseMotionEvent detected: "
                        + e.getX() + " " + e.getY());
            }
        };

        // create the layer for the panel using our custom layerUI
        JXLayer<JPanel> layer = new JXLayer<JPanel>(panel, layerUI);

        // work with the layer as with any other Swing component
        frame.add(layer);
 

Note: When a LayerUI instance is disabled or not set, its JXLayers temporary lose all their decorations. Note: JXLayer is very friendly to your application, it uses only public Swing API and doesn't rely on any global settings like custom RepaintManager or AWTEventListener. It neither change the opaque state of its subcomponents nor use the glassPane of its parent frame.

JXLayer can be used under restricted environment (e.g. unsigned applets)

See Also:
setUI(LayerUI), LayerUI, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
accessibleContext, 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
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
JXLayer()
          Creates a new JXLayer object with empty view component and empty LayerUI.
JXLayer(V view)
          Creates a new JXLayer object with empty LayerUI.
JXLayer(V view, LayerUI<V> ui)
          Creates a new JXLayer object with provided view component and LayerUI object.
 
Method Summary
protected  void addImpl(java.awt.Component comp, java.lang.Object constraints, int index)
          This method is not supported by JXLayer and always throws UnsupportedOperationException
 javax.swing.JPanel createGlassPane()
          Called by the constructor methods to create the default glassPane.
 javax.accessibility.AccessibleContext getAccessibleContext()
           
 javax.swing.JPanel getGlassPane()
          Returns the JXLayer's glassPane component or null.
 long getLayerEventMask()
          Returns the bitmap of event mask to receive by this JXLayer and its LayerUI.
 java.awt.Dimension getPreferredScrollableViewportSize()
          Returns the preferred size of the viewport for a view component.
 int getScrollableBlockIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
          Components that display logical rows or columns should compute the scroll increment that will completely expose one block of rows or columns, depending on the value of orientation.
 boolean getScrollableTracksViewportHeight()
          Returns false to indicate that the height of the viewport does not determine the height of the layer, unless the preferred height of the layer is smaller than the viewports height.
 boolean getScrollableTracksViewportWidth()
          Returns false to indicate that the width of the viewport does not determine the width of the layer, unless the preferred width of the layer is smaller than the viewports width.
 int getScrollableUnitIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
          Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation.
 LayerUI<? super V> getUI()
          Returns the LayerUI for this JXLayer.
 V getView()
          Returns the JXLayer's view component or null.
 boolean isOptimizedDrawingEnabled()
          To enable the correct painting of the glassPane and view component, the JXLayer overrides the default implementation of this method to return false when the glassPane is visible.
 void paint(java.awt.Graphics g)
          Delegates all painting to the LayerUI object.
protected  void paintComponent(java.awt.Graphics g)
          This method is empty, because all painting is done by paint(Graphics) and ComponentUI.update(Graphics, JComponent) methods
 void propertyChange(java.beans.PropertyChangeEvent evt)
          
 void remove(java.awt.Component comp)
          
 void removeAll()
          
 void setGlassPane(javax.swing.JPanel glassPane)
          Sets the JXLayer's glassPane component, which can be null.
 void setLayerEventMask(long layerEventMask)
          Sets the bitmask of event types to receive by this JXLayer.
 void setUI(LayerUI<? super V> ui)
          Sets the LayerUI which will perform painting and receive input events for this JXLayer.
 void setView(V view)
          Sets the JXLayer's view component, which can be null.
 void updateUI()
          Delegates its functionality to the LayerUI.updateUI(JXLayer) method, if LayerUI is set.
 
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, 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, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, 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, 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, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, 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, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JXLayer

public JXLayer()
Creates a new JXLayer object with empty view component and empty LayerUI.

See Also:
setView(V), setUI(org.icepdf.ri.util.jxlayer.plaf.LayerUI)

JXLayer

public JXLayer(V view)
Creates a new JXLayer object with empty LayerUI.

Parameters:
view - the component to be decorated with this JXLayer
See Also:
setUI(org.icepdf.ri.util.jxlayer.plaf.LayerUI)

JXLayer

public JXLayer(V view,
               LayerUI<V> ui)
Creates a new JXLayer object with provided view component and LayerUI object.

Parameters:
view - the component to be decorated
ui - the LayerUI delegate to be used by this JXLayer
Method Detail

getView

public V getView()
Returns the JXLayer's view component or null.
This is a bound property.

Returns:
the JXLayer's view component or null if none exists
See Also:
setView(Component)

setView

public void setView(V view)
Sets the JXLayer's view component, which can be null.
This is a bound property.

Parameters:
view - the view component for this JXLayer
See Also:
getView()

setUI

public void setUI(LayerUI<? super V> ui)
Sets the LayerUI which will perform painting and receive input events for this JXLayer.

Parameters:
ui - the LayerUI for this JXLayer

getUI

public LayerUI<? super V> getUI()
Returns the LayerUI for this JXLayer.

Returns:
the LayerUI for this JXLayer

getGlassPane

public javax.swing.JPanel getGlassPane()
Returns the JXLayer's glassPane component or null.
This is a bound property.

Returns:
the JXLayer's glassPane component or null if none exists
See Also:
setGlassPane(JPanel)

setGlassPane

public void setGlassPane(javax.swing.JPanel glassPane)
Sets the JXLayer's glassPane component, which can be null.
This is a bound property.

Parameters:
glassPane - the glassPane component of this JXLayer
See Also:
getGlassPane()

createGlassPane

public javax.swing.JPanel createGlassPane()
Called by the constructor methods to create the default glassPane. By default this method creates a new JPanel with visibility set to true and opacity set to false.

Returns:
the default glassPane

addImpl

protected void addImpl(java.awt.Component comp,
                       java.lang.Object constraints,
                       int index)
This method is not supported by JXLayer and always throws UnsupportedOperationException

Overrides:
addImpl in class java.awt.Container
Throws:
java.lang.UnsupportedOperationException - this method is not supported
See Also:
setView(Component), setGlassPane(JPanel)

remove

public void remove(java.awt.Component comp)

Overrides:
remove in class java.awt.Container

removeAll

public void removeAll()

Overrides:
removeAll in class java.awt.Container

paint

public void paint(java.awt.Graphics g)
Delegates all painting to the LayerUI object.

Overrides:
paint in class javax.swing.JComponent
Parameters:
g - the Graphics to render to

paintComponent

protected void paintComponent(java.awt.Graphics g)
This method is empty, because all painting is done by paint(Graphics) and ComponentUI.update(Graphics, JComponent) methods

Overrides:
paintComponent in class javax.swing.JComponent

isOptimizedDrawingEnabled

public boolean isOptimizedDrawingEnabled()
To enable the correct painting of the glassPane and view component, the JXLayer overrides the default implementation of this method to return false when the glassPane is visible.

Overrides:
isOptimizedDrawingEnabled in class javax.swing.JComponent
Returns:
false if JXLayer's glassPane is visible

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent evt)

Specified by:
propertyChange in interface java.beans.PropertyChangeListener

setLayerEventMask

public void setLayerEventMask(long layerEventMask)
Sets the bitmask of event types to receive by this JXLayer. Here is the list of the supported event types:

If LayerUI is installed, LayerUI.eventDispatched(AWTEvent, JXLayer) method will only receive events that match the event mask.

Here is an example how to correclty use this method in the LayerUI implementations:

    public void installUI(JComponent c) {
       super.installUI(c);
       JXLayer l = (JXLayer) c;
       // this LayerUI will receive only key and focus events
       l.setLayerEventMask(AWTEvent.KEY_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
    }

    public void uninstallUI(JComponent c) {
       super.uninstallUI(c);
       JXLayer l = (JXLayer) c;
       // JXLayer must be returned to its initial state
       l.setLayerEventMask(0);
    }
 

By default JXLayer receives no events.

Parameters:
layerEventMask - the bitmask of event types to receive
Throws:
java.lang.IllegalArgumentException - if the layerEventMask parameter contains unsupported event types
See Also:
getLayerEventMask()

getLayerEventMask

public long getLayerEventMask()
Returns the bitmap of event mask to receive by this JXLayer and its LayerUI.

It means that LayerUI.eventDispatched(AWTEvent, JXLayer) method will only receive events that match the event mask.

By default JXLayer receives no events.

Returns:
the bitmask of event types to receive for this JXLayer

updateUI

public void updateUI()
Delegates its functionality to the LayerUI.updateUI(JXLayer) method, if LayerUI is set.

Overrides:
updateUI in class javax.swing.JComponent

getPreferredScrollableViewportSize

public java.awt.Dimension getPreferredScrollableViewportSize()
Returns the preferred size of the viewport for a view component.

If the ui delegate of this layer is not null, this method delegates its implementation to the LayerUI.getPreferredScrollableViewportSize(JXLayer)

Specified by:
getPreferredScrollableViewportSize in interface javax.swing.Scrollable
Returns:
the preferred size of the viewport for a view component
See Also:
Scrollable, LayerUI.getPreferredScrollableViewportSize(JXLayer)

getScrollableBlockIncrement

public int getScrollableBlockIncrement(java.awt.Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Components that display logical rows or columns should compute the scroll increment that will completely expose one block of rows or columns, depending on the value of orientation.

If the ui delegate of this layer is not null, this method delegates its implementation to the LayerUI.getScrollableBlockIncrement(JXLayer, Rectangle, int, int)

Specified by:
getScrollableBlockIncrement in interface javax.swing.Scrollable
Returns:
the "block" increment for scrolling in the specified direction
See Also:
Scrollable, LayerUI.getScrollableBlockIncrement(JXLayer, Rectangle, int, int)

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Returns false to indicate that the height of the viewport does not determine the height of the layer, unless the preferred height of the layer is smaller than the viewports height.

If the ui delegate of this layer is not null, this method delegates its implementation to the LayerUI.getScrollableTracksViewportHeight(JXLayer)

Specified by:
getScrollableTracksViewportHeight in interface javax.swing.Scrollable
Returns:
whether the layer should track the height of the viewport
See Also:
Scrollable, LayerUI.getScrollableTracksViewportHeight(JXLayer)

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Returns false to indicate that the width of the viewport does not determine the width of the layer, unless the preferred width of the layer is smaller than the viewports width.

If the ui delegate of this layer is not null, this method delegates its implementation to the LayerUI.getScrollableTracksViewportWidth(JXLayer)

Specified by:
getScrollableTracksViewportWidth in interface javax.swing.Scrollable
Returns:
whether the layer should track the width of the viewport
See Also:
Scrollable, LayerUI.getScrollableTracksViewportWidth(JXLayer)

getScrollableUnitIncrement

public int getScrollableUnitIncrement(java.awt.Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation. Ideally, components should handle a partially exposed row or column by returning the distance required to completely expose the item.

Scrolling containers, like JScrollPane, will use this method each time the user requests a unit scroll.

If the ui delegate of this layer is not null, this method delegates its implementation to the LayerUI.getScrollableUnitIncrement(JXLayer, Rectangle, int, int)

Specified by:
getScrollableUnitIncrement in interface javax.swing.Scrollable
Returns:
The "unit" increment for scrolling in the specified direction. This value should always be positive.
See Also:
Scrollable, LayerUI.getScrollableUnitIncrement(JXLayer, Rectangle, int, int)

getAccessibleContext

public javax.accessibility.AccessibleContext getAccessibleContext()
Specified by:
getAccessibleContext in interface javax.accessibility.Accessible
Overrides:
getAccessibleContext in class javax.swing.JComponent