001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.widgets; 003 004import java.awt.Dimension; 005import java.awt.LayoutManager; 006import java.awt.Rectangle; 007 008import javax.swing.JPanel; 009import javax.swing.JScrollPane; 010import javax.swing.Scrollable; 011 012import org.openstreetmap.josm.gui.util.GuiHelper; 013 014/** 015 * A panel that can be scrolled vertically. It enhances the normal {@link JPanel} to allow for better scrolling. 016 * Scroll pane contents may extend this. 017 * Use {@link #getVerticalScrollPane()} once to embed it into a scroll pane. 018 */ 019public class VerticallyScrollablePanel extends JPanel implements Scrollable { 020 021 /** 022 * Constructs a new {@code VerticallyScrollablePanel}. 023 */ 024 public VerticallyScrollablePanel() { 025 super(); 026 } 027 028 /** 029 * Constructs a new {@code VerticallyScrollablePanel}. 030 * @param isDoubleBuffered a boolean, true for double-buffering, which 031 * uses additional memory space to achieve fast, flicker-free updates 032 */ 033 public VerticallyScrollablePanel(boolean isDoubleBuffered) { 034 super(isDoubleBuffered); 035 } 036 037 /** 038 * Constructs a new {@code VerticallyScrollablePanel}. 039 * @param layout the LayoutManager to use 040 * @param isDoubleBuffered a boolean, true for double-buffering, which 041 * uses additional memory space to achieve fast, flicker-free updates 042 */ 043 public VerticallyScrollablePanel(LayoutManager layout, boolean isDoubleBuffered) { 044 super(layout, isDoubleBuffered); 045 } 046 047 /** 048 * Constructs a new {@code VerticallyScrollablePanel}. 049 * @param layout the LayoutManager to use 050 */ 051 public VerticallyScrollablePanel(LayoutManager layout) { 052 super(layout); 053 } 054 055 /** 056 * Returns a vertical scrollable {@code JScrollPane} containing this panel. 057 * @return the vertical scrollable {@code JScrollPane} 058 * @since 6666 059 */ 060 public final JScrollPane getVerticalScrollPane() { 061 return GuiHelper.embedInVerticalScrollPane(this); 062 } 063 064 @Override 065 public Dimension getPreferredScrollableViewportSize() { 066 return getPreferredSize(); 067 } 068 069 @Override 070 public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { 071 return 20; 072 } 073 074 @Override 075 public boolean getScrollableTracksViewportHeight() { 076 return false; 077 } 078 079 @Override 080 public boolean getScrollableTracksViewportWidth() { 081 return true; 082 } 083 084 @Override 085 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { 086 return 10; 087 } 088}