LayerUI.java revision 1561
1561N/A/*
1561N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
1561N/A * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
1561N/A */
1561N/A
1561N/Apackage javax.swing.plaf;
1561N/A
1561N/Aimport javax.accessibility.Accessible;
1561N/Aimport javax.swing.*;
1561N/Aimport javax.swing.plaf.ComponentUI;
1561N/Aimport java.awt.*;
1561N/Aimport java.awt.event.*;
1561N/Aimport java.beans.PropertyChangeEvent;
1561N/Aimport java.beans.PropertyChangeSupport;
1561N/Aimport java.beans.PropertyChangeListener;
1561N/Aimport java.io.Serializable;
1561N/A
1561N/A/**
1561N/A * The base class for all {@link javax.swing.JLayer}'s UI delegates.
1561N/A * <p/>
1561N/A * {@link #paint(java.awt.Graphics, javax.swing.JComponent)} method performes the
1561N/A * painting of the {@code JLayer}
1561N/A * and {@link #eventDispatched(AWTEvent, JLayer)} method is notified
1561N/A * about any {@code AWTEvent}s which have been generated by a {@code JLayer}
1561N/A * or any of its subcomponents.
1561N/A * <p/>
1561N/A * The {@code LayerUI} differs from the UI delegates of the other components,
1561N/A * because it is LookAndFeel independent and is not updated by default when
1561N/A * the system LookAndFeel is changed.
1561N/A * <p/>
1561N/A * The subclasses of {@code LayerUI} can either be stateless and shareable
1561N/A * by multiple {@code JLayer}s or not shareable.
1561N/A *
1561N/A * @param <V> one of the super types of {@code JLayer}'s view component
1561N/A *
1561N/A * @see JLayer#setUI(LayerUI)
1561N/A * @see JLayer#setView(Component)
1561N/A * @see JLayer#getView()
1561N/A * @since 1.7
1561N/A *
1561N/A * @author Alexander Potochkin
1561N/A */
1561N/Apublic class LayerUI<V extends Component>
1561N/A extends ComponentUI implements Serializable {
1561N/A
1561N/A private final PropertyChangeSupport propertyChangeSupport =
1561N/A new PropertyChangeSupport(this);
1561N/A
1561N/A /**
1561N/A * Paints the specified component.
1561N/A * Subclasses should override this method and use
1561N/A * the specified {@code Graphics} object to
1561N/A * render the content of the component.
1561N/A *
1561N/A * @param g the {@code Graphics} context in which to paint;
1561N/A * @param c the component being painted;
1561N/A * it can be safely cast to the {@code JLayer<V>}
1561N/A */
1561N/A @Override
1561N/A public void paint(Graphics g, JComponent c) {
1561N/A c.paint(g);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Dispatches {@code AWTEvent}s for {@code JLayer}
1561N/A * and <b>all it subcomponents</b> to this {@code LayerUI} instance.
1561N/A * <p>
1561N/A * To enable the {@code AWTEvent} of the particular type,
1561N/A * you call {@link javax.swing.JLayer#setLayerEventMask}
1561N/A * in {@link #installUI(javax.swing.JComponent)}
1561N/A * and set the layer event mask to {@code 0}
1561N/A * in {@link #uninstallUI(javax.swing.JComponent)} after that
1561N/A *
1561N/A * @param e the event to be dispatched
1561N/A * @param l the layer this LayerUI is set to
1561N/A *
1561N/A * @see JLayer#setLayerEventMask(long)
1561N/A * @see javax.swing.JLayer#getLayerEventMask()
1561N/A */
1561N/A public void eventDispatched(AWTEvent e, JLayer<? extends V> l){
1561N/A }
1561N/A
1561N/A /**
1561N/A * Invoked when {@link javax.swing.JLayer#updateUI()} is called
1561N/A * by the {@code JLayer} this {@code LayerUI} is set to.
1561N/A *
1561N/A * @param l the {@code JLayer} which UI is updated
1561N/A */
1561N/A public void updateUI(JLayer<? extends V> l){
1561N/A }
1561N/A
1561N/A /**
1561N/A * Configures the {@code JLayer} this {@code LayerUI} is set to.
1561N/A * The default implementation registers the {@code LayerUI}
1561N/A * as a property change listener for the passed {@code JLayer} component.
1561N/A *
1561N/A * @param c the {@code JLayer} component where this UI delegate is being installed
1561N/A */
1561N/A public void installUI(JComponent c) {
1561N/A addPropertyChangeListener((JLayer) c);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Reverses the configuration which was previously set
1561N/A * in the {@link #installUI(JComponent)} method.
1561N/A * The default implementation unregisters the property change listener
1561N/A * for the passed JLayer component.
1561N/A *
1561N/A * @param c the component from which this UI delegate is being removed.
1561N/A */
1561N/A public void uninstallUI(JComponent c) {
1561N/A removePropertyChangeListener((JLayer) c);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Adds a PropertyChangeListener to the listener list. The listener is
1561N/A * registered for all bound properties of this class.
1561N/A * <p/>
1561N/A * If {@code listener} is {@code null},
1561N/A * no exception is thrown and no action is performed.
1561N/A *
1561N/A * @param listener the property change listener to be added
1561N/A * @see #removePropertyChangeListener
1561N/A * @see #getPropertyChangeListeners
1561N/A * @see #addPropertyChangeListener(String, java.beans.PropertyChangeListener)
1561N/A */
1561N/A public void addPropertyChangeListener(PropertyChangeListener listener) {
1561N/A propertyChangeSupport.addPropertyChangeListener(listener);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Removes a PropertyChangeListener from the listener list. This method
1561N/A * should be used to remove PropertyChangeListeners that were registered
1561N/A * for all bound properties of this class.
1561N/A * <p/>
1561N/A * If {@code listener} is {@code null},
1561N/A * no exception is thrown and no action is performed.
1561N/A *
1561N/A * @param listener the PropertyChangeListener to be removed
1561N/A * @see #addPropertyChangeListener
1561N/A * @see #getPropertyChangeListeners
1561N/A * @see #removePropertyChangeListener(String, PropertyChangeListener)
1561N/A */
1561N/A public void removePropertyChangeListener(PropertyChangeListener listener) {
1561N/A propertyChangeSupport.removePropertyChangeListener(listener);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Returns an array of all the property change listeners
1561N/A * registered on this component.
1561N/A *
1561N/A * @return all of this ui's {@code PropertyChangeListener}s
1561N/A * or an empty array if no property change
1561N/A * listeners are currently registered
1561N/A * @see #addPropertyChangeListener
1561N/A * @see #removePropertyChangeListener
1561N/A * @see #getPropertyChangeListeners(String)
1561N/A */
1561N/A public PropertyChangeListener[] getPropertyChangeListeners() {
1561N/A return propertyChangeSupport.getPropertyChangeListeners();
1561N/A }
1561N/A
1561N/A /**
1561N/A * Adds a PropertyChangeListener to the listener list for a specific
1561N/A * property.
1561N/A * <p/>
1561N/A * If {@code propertyName} or {@code listener} is {@code null},
1561N/A * no exception is thrown and no action is taken.
1561N/A *
1561N/A * @param propertyName one of the property names listed above
1561N/A * @param listener the property change listener to be added
1561N/A * @see #removePropertyChangeListener(String, PropertyChangeListener)
1561N/A * @see #getPropertyChangeListeners(String)
1561N/A * @see #addPropertyChangeListener(String, PropertyChangeListener)
1561N/A */
1561N/A public void addPropertyChangeListener(String propertyName,
1561N/A PropertyChangeListener listener) {
1561N/A propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Removes a {@code PropertyChangeListener} from the listener
1561N/A * list for a specific property. This method should be used to remove
1561N/A * {@code PropertyChangeListener}s
1561N/A * that were registered for a specific bound property.
1561N/A * <p/>
1561N/A * If {@code propertyName} or {@code listener} is {@code null},
1561N/A * no exception is thrown and no action is taken.
1561N/A *
1561N/A * @param propertyName a valid property name
1561N/A * @param listener the PropertyChangeListener to be removed
1561N/A * @see #addPropertyChangeListener(String, PropertyChangeListener)
1561N/A * @see #getPropertyChangeListeners(String)
1561N/A * @see #removePropertyChangeListener(PropertyChangeListener)
1561N/A */
1561N/A public void removePropertyChangeListener(String propertyName,
1561N/A PropertyChangeListener listener) {
1561N/A propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Returns an array of all the listeners which have been associated
1561N/A * with the named property.
1561N/A *
1561N/A * @return all of the {@code PropertyChangeListener}s associated with
1561N/A * the named property; if no such listeners have been added or
1561N/A * if {@code propertyName} is {@code null}, an empty
1561N/A * array is returned
1561N/A * @see #addPropertyChangeListener(String, PropertyChangeListener)
1561N/A * @see #removePropertyChangeListener(String, PropertyChangeListener)
1561N/A * @see #getPropertyChangeListeners
1561N/A */
1561N/A public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
1561N/A return propertyChangeSupport.getPropertyChangeListeners(propertyName);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Support for reporting bound property changes for Object properties.
1561N/A * This method can be called when a bound property has changed and it will
1561N/A * send the appropriate PropertyChangeEvent to any registered
1561N/A * PropertyChangeListeners.
1561N/A *
1561N/A * @param propertyName the property whose value has changed
1561N/A * @param oldValue the property's previous value
1561N/A * @param newValue the property's new value
1561N/A */
1561N/A protected void firePropertyChange(String propertyName,
1561N/A Object oldValue, Object newValue) {
1561N/A propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
1561N/A }
1561N/A
1561N/A /**
1561N/A * Notifies the {@code LayerUI} when any of its property are changed
1561N/A * and enables updating every {@code JLayer} this {@code LayerUI} instance is set to.
1561N/A *
1561N/A * @param evt the PropertyChangeEvent generated by this {@code LayerUI}
1561N/A * @param l the {@code JLayer} this LayerUI is set to
1561N/A */
1561N/A public void applyPropertyChange(PropertyChangeEvent evt, JLayer<? extends V> l) {
1561N/A }
1561N/A
1561N/A /**
1561N/A * Returns the preferred size of the viewport for a view component.
1561N/A *
1561N/A * @return the preferred size of the viewport for a view component
1561N/A * @see Scrollable#getPreferredScrollableViewportSize()
1561N/A */
1561N/A public Dimension getPreferredScrollableViewportSize(JLayer<? extends V> l) {
1561N/A if (l.getView() instanceof Scrollable) {
1561N/A return ((Scrollable)l.getView()).getPreferredScrollableViewportSize();
1561N/A }
1561N/A return l.getPreferredSize();
1561N/A }
1561N/A
1561N/A /**
1561N/A * Returns a scroll increment, which is required for components
1561N/A * that display logical rows or columns in order to completely expose
1561N/A * one block of rows or columns, depending on the value of orientation.
1561N/A *
1561N/A * @return the "block" increment for scrolling in the specified direction
1561N/A * @see Scrollable#getScrollableBlockIncrement(Rectangle, int, int)
1561N/A */
1561N/A public int getScrollableBlockIncrement(JLayer<? extends V> l,
1561N/A Rectangle visibleRect,
1561N/A int orientation, int direction) {
1561N/A if (l.getView() instanceof Scrollable) {
1561N/A return ((Scrollable)l.getView()).getScrollableBlockIncrement(
1561N/A visibleRect,orientation, direction);
1561N/A }
1561N/A return (orientation == SwingConstants.VERTICAL) ? visibleRect.height :
1561N/A visibleRect.width;
1561N/A }
1561N/A
1561N/A /**
1561N/A * Returns {@code false} to indicate that the height of the viewport does not
1561N/A * determine the height of the layer, unless the preferred height
1561N/A * of the layer is smaller than the height of the viewport.
1561N/A *
1561N/A * @return whether the layer should track the height of the viewport
1561N/A * @see Scrollable#getScrollableTracksViewportHeight()
1561N/A */
1561N/A public boolean getScrollableTracksViewportHeight(JLayer<? extends V> l) {
1561N/A if (l.getView() instanceof Scrollable) {
1561N/A return ((Scrollable)l.getView()).getScrollableTracksViewportHeight();
1561N/A }
1561N/A if (l.getParent() instanceof JViewport) {
1561N/A return (((JViewport)l.getParent()).getHeight() > l.getPreferredSize().height);
1561N/A }
1561N/A return false;
1561N/A }
1561N/A
1561N/A /**
1561N/A * Returns {@code false} to indicate that the width of the viewport does not
1561N/A * determine the width of the layer, unless the preferred width
1561N/A * of the layer is smaller than the width of the viewport.
1561N/A *
1561N/A * @return whether the layer should track the width of the viewport
1561N/A * @see Scrollable
1561N/A * @see LayerUI#getScrollableTracksViewportWidth(JLayer)
1561N/A */
1561N/A public boolean getScrollableTracksViewportWidth(JLayer<? extends V> l) {
1561N/A if (l.getView() instanceof Scrollable) {
1561N/A return ((Scrollable)l.getView()).getScrollableTracksViewportWidth();
1561N/A }
1561N/A if (l.getParent() instanceof JViewport) {
1561N/A return (((JViewport)l.getParent()).getWidth() > l.getPreferredSize().width);
1561N/A }
1561N/A return false;
1561N/A }
1561N/A
1561N/A /**
1561N/A * Returns a scroll increment, which is required for components
1561N/A * that display logical rows or columns in order to completely expose
1561N/A * one new row or column, depending on the value of orientation.
1561N/A * Ideally, components should handle a partially exposed row or column
1561N/A * by returning the distance required to completely expose the item.
1561N/A * <p>
1561N/A * Scrolling containers, like JScrollPane, will use this method
1561N/A * each time the user requests a unit scroll.
1561N/A *
1561N/A * @return The "unit" increment for scrolling in the specified direction.
1561N/A * This value should always be positive.
1561N/A * @see Scrollable#getScrollableUnitIncrement(Rectangle, int, int)
1561N/A */
1561N/A public int getScrollableUnitIncrement(JLayer<? extends V> l,
1561N/A Rectangle visibleRect,
1561N/A int orientation, int direction) {
1561N/A if (l.getView() instanceof Scrollable) {
1561N/A return ((Scrollable)l.getView()).getScrollableUnitIncrement(
1561N/A visibleRect, orientation, direction);
1561N/A }
1561N/A return 1;
1561N/A }
1561N/A
1561N/A /**
1561N/A * If the {@code JLayer}'s view component is not {@code null},
1561N/A * this calls the view's {@code getBaseline()} method.
1561N/A * Otherwise, the default implementation is called.
1561N/A *
1561N/A * @param c {@code JLayer} to return baseline resize behavior for
1561N/A * @param width the width to get the baseline for
1561N/A * @param height the height to get the baseline for
1561N/A * @return baseline or a value &lt; 0 indicating there is no reasonable
1561N/A * baseline
1561N/A */
1561N/A public int getBaseline(JComponent c, int width, int height) {
1561N/A JLayer l = (JLayer) c;
1561N/A if (l.getView() != null) {
1561N/A return l.getView().getBaseline(width, height);
1561N/A }
1561N/A return super.getBaseline(c, width, height);
1561N/A }
1561N/A
1561N/A /**
1561N/A * If the {@code JLayer}'s view component is not {@code null},
1561N/A * this calls the view's {@code getBaselineResizeBehavior()} method.
1561N/A * Otherwise, the default implementation is called.
1561N/A *
1561N/A * @param c {@code JLayer} to return baseline resize behavior for
1561N/A * @return an enum indicating how the baseline changes as the component
1561N/A * size changes
1561N/A */
1561N/A public Component.BaselineResizeBehavior getBaselineResizeBehavior(JComponent c) {
1561N/A JLayer l = (JLayer) c;
1561N/A if (l.getView() != null) {
1561N/A return l.getView().getBaselineResizeBehavior();
1561N/A }
1561N/A return super.getBaselineResizeBehavior(c);
1561N/A }
1561N/A}