📄 tonicscrollpanelayout.java
字号:
package com.digitprop.tonic;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/** Custom layout for JScrollPanes.
*
* @author Markus Fischer
*
* <p>This software is under the <a href="http://www.gnu.org/copyleft/lesser.html" target="_blank">GNU Lesser General Public License</a>
*/
/*
* ------------------------------------------------------------------------
* Copyright (C) 2004 Markus Fischer
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* You can contact the author at:
* Markus Fischer
* www.digitprop.com
* info@digitprop.com
* ------------------------------------------------------------------------
*/
public class TonicScrollPaneLayout extends ScrollPaneLayout
{
/** The scrollpane's viewport child. Default is an empty <code>JViewport</code>.
*
* @see JScrollPane#setViewport
*/
protected JViewport viewport;
/** The scrollpane's vertical scrollbar child. Default is a <code>JScrollBar</code>.
*
* @see JScrollPane#setVerticalScrollBar
*/
protected JScrollBar vsb;
/** The scrollpane's horizontal scrollbar child. Default is a <code>JScrollBar</code>.
*
* @see JScrollPane#setHorizontalScrollBar
*/
protected JScrollBar hsb;
/** The row header child. Default is <code>null</code>.
*
* @see JScrollPane#setRowHeader
*/
protected JViewport rowHead;
/** The column header child. Default is <code>null</code>.
*
* @see JScrollPane#setColumnHeader
*/
protected JViewport colHead;
/** The component to display in the lower left corner. Default is <code>null</code>.
*
* @see JScrollPane#setCorner
*/
protected Component lowerLeft;
/** The component to display in the lower right corner. Default is <code>null</code>.
*
* @see JScrollPane#setCorner
*/
protected Component lowerRight;
/** The component to display in the upper left corner. Default is <code>null</code>.
*
* @see JScrollPane#setCorner
*/
protected Component upperLeft;
/** The component to display in the upper right corner. Default is <code>null</code>.
*
* @see JScrollPane#setCorner
*/
protected Component upperRight;
/** The display policy for the vertical scrollbar. The default is
* <code>JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED</code>.<p>
*
* This field is obsolete, please use the <code>JScrollPane</code> field instead.
*
* @see JScrollPane#setVerticalScrollBarPolicy
*/
protected int vsbPolicy = VERTICAL_SCROLLBAR_AS_NEEDED;
/**
* The display policy for the horizontal scrollbar.
* The default is <code>JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED</code>.
* <p>
* This field is obsolete, please use the <code>JScrollPane</code> field instead.
*
* @see JScrollPane#setHorizontalScrollBarPolicy
*/
protected int hsbPolicy = HORIZONTAL_SCROLLBAR_AS_NEEDED;
/**
* This method is invoked after the ScrollPaneLayout is set as the
* LayoutManager of a <code>JScrollPane</code>.
* It initializes all of the internal fields that
* are ordinarily set by <code>addLayoutComponent</code>. For example:
* <pre>
* ScrollPaneLayout mySPLayout = new ScrollPanelLayout() {
* public void layoutContainer(Container p) {
* super.layoutContainer(p);
* // do some extra work here ...
* }
* };
* scrollpane.setLayout(mySPLayout):
* </pre>
*/
public void syncWithScrollPane(JScrollPane sp) {
viewport = sp.getViewport();
vsb = sp.getVerticalScrollBar();
hsb = sp.getHorizontalScrollBar();
rowHead = sp.getRowHeader();
colHead = sp.getColumnHeader();
lowerLeft = sp.getCorner(LOWER_LEFT_CORNER);
lowerRight = sp.getCorner(LOWER_RIGHT_CORNER);
upperLeft = sp.getCorner(UPPER_LEFT_CORNER);
upperRight = sp.getCorner(UPPER_RIGHT_CORNER);
vsbPolicy = sp.getVerticalScrollBarPolicy();
hsbPolicy = sp.getHorizontalScrollBarPolicy();
}
/**
* Removes an existing component. When a new component, such as
* the left corner, or vertical scrollbar, is added, the old one,
* if it exists, must be removed.
* <p>
* This method returns <code>newC</code>. If <code>oldC</code> is
* not equal to <code>newC</code> and is non-<code>null</code>,
* it will be removed from its parent.
*
* @param oldC the <code>Component</code> to replace
* @param newC the <code>Component</code> to add
* @return the <code>newC</code>
*/
protected Component addSingletonComponent(Component oldC, Component newC)
{
if ((oldC != null) && (oldC != newC)) {
oldC.getParent().remove(oldC);
}
return newC;
}
/**
* Adds the specified component to the layout. The layout is
* identified using one of:
* <ul>
* <li>JScrollPane.VIEWPORT
* <li>JScrollPane.VERTICAL_SCROLLBAR
* <li>JScrollPane.HORIZONTAL_SCROLLBAR
* <li>JScrollPane.ROW_HEADER
* <li>JScrollPane.COLUMN_HEADER
* <li>JScrollPane.LOWER_LEFT_CORNER
* <li>JScrollPane.LOWER_RIGHT_CORNER
* <li>JScrollPane.UPPER_LEFT_CORNER
* <li>JScrollPane.UPPER_RIGHT_CORNER
* </ul>
*
* @param s the component identifier
* @param c the the component to be added
* @exception IllegalArgumentException if <code>s</code> is an invalid key
*/
public void addLayoutComponent(String s, Component c)
{
if (s.equals(VIEWPORT)) {
viewport = (JViewport)addSingletonComponent(viewport, c);
}
else if (s.equals(VERTICAL_SCROLLBAR)) {
vsb = (JScrollBar)addSingletonComponent(vsb, c);
}
else if (s.equals(HORIZONTAL_SCROLLBAR)) {
hsb = (JScrollBar)addSingletonComponent(hsb, c);
}
else if (s.equals(ROW_HEADER)) {
rowHead = (JViewport)addSingletonComponent(rowHead, c);
}
else if (s.equals(COLUMN_HEADER)) {
colHead = (JViewport)addSingletonComponent(colHead, c);
}
else if (s.equals(LOWER_LEFT_CORNER)) {
lowerLeft = addSingletonComponent(lowerLeft, c);
}
else if (s.equals(LOWER_RIGHT_CORNER)) {
lowerRight = addSingletonComponent(lowerRight, c);
}
else if (s.equals(UPPER_LEFT_CORNER)) {
upperLeft = addSingletonComponent(upperLeft, c);
}
else if (s.equals(UPPER_RIGHT_CORNER)) {
upperRight = addSingletonComponent(upperRight, c);
}
else {
throw new IllegalArgumentException("invalid layout key " + s);
}
}
/**
* Removes the specified component from the layout.
*
* @param c the component to remove
*/
public void removeLayoutComponent(Component c)
{
if (c == viewport) {
viewport = null;
}
else if (c == vsb) {
vsb = null;
}
else if (c == hsb) {
hsb = null;
}
else if (c == rowHead) {
rowHead = null;
}
else if (c == colHead) {
colHead = null;
}
else if (c == lowerLeft) {
lowerLeft = null;
}
else if (c == lowerRight) {
lowerRight = null;
}
else if (c == upperLeft) {
upperLeft = null;
}
else if (c == upperRight) {
upperRight = null;
}
}
/**
* Returns the vertical scrollbar-display policy.
*
* @return an integer giving the display policy
* @see #setVerticalScrollBarPolicy
*/
public int getVerticalScrollBarPolicy() {
return vsbPolicy;
}
/**
* Sets the vertical scrollbar-display policy. The options
* are:
* <ul>
* <li>JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
* <li>JScrollPane.VERTICAL_SCROLLBAR_NEVER
* <li>JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
* </ul>
* Note: Applications should use the <code>JScrollPane</code> version
* of this method. It only exists for backwards compatibility
* with the Swing 1.0.2 (and earlier) versions of this class.
*
* @param x an integer giving the display policy
* @exception IllegalArgumentException if <code>x</code> is an invalid
* vertical scroll bar policy, as listed above
*/
public void setVerticalScrollBarPolicy(int x) {
switch (x) {
case VERTICAL_SCROLLBAR_AS_NEEDED:
case VERTICAL_SCROLLBAR_NEVER:
case VERTICAL_SCROLLBAR_ALWAYS:
vsbPolicy = x;
break;
default:
throw new IllegalArgumentException("invalid verticalScrollBarPolicy");
}
}
/**
* Returns the horizontal scrollbar-display policy.
*
* @return an integer giving the display policy
* @see #setHorizontalScrollBarPolicy
*/
public int getHorizontalScrollBarPolicy() {
return hsbPolicy;
}
/**
* Sets the horizontal scrollbar-display policy.
* The options are:<ul>
* <li>JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
* <li>JScrollPane.HOTRIZONTAL_SCROLLBAR_NEVER
* <li>JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
* </ul>
* Note: Applications should use the <code>JScrollPane</code> version
* of this method. It only exists for backwards compatibility
* with the Swing 1.0.2 (and earlier) versions of this class.
*
* @param x an int giving the display policy
* @exception IllegalArgumentException if <code>x</code> is not a valid
* horizontal scrollbar policy, as listed above
*/
public void setHorizontalScrollBarPolicy(int x) {
switch (x) {
case HORIZONTAL_SCROLLBAR_AS_NEEDED:
case HORIZONTAL_SCROLLBAR_NEVER:
case HORIZONTAL_SCROLLBAR_ALWAYS:
hsbPolicy = x;
break;
default:
throw new IllegalArgumentException("invalid horizontalScrollBarPolicy");
}
}
/**
* Returns the <code>JViewport</code> object that displays the
* scrollable contents.
* @return the <code>JViewport</code> object that displays the scrollable contents
* @see JScrollPane#getViewport
*/
public JViewport getViewport() {
return viewport;
}
/**
* Returns the <code>JScrollBar</code> object that handles horizontal scrolling.
* @return the <code>JScrollBar</code> object that handles horizontal scrolling
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -