1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package com.sun.swingset3.demos.scrollpane;
24
25import java.awt.*;
26import javax.swing.*;
27
28import com.sun.swingset3.DemoProperties;
29import com.sun.swingset3.demos.ResourceManager;
30
31/**
32 * Scroll Pane Demo
33 *
34 * @version 1.9 11/17/05
35 * @author Jeff Dinkins
36 */
37@DemoProperties(
38        value = "JScrollPane Demo",
39        category = "Containers",
40        description = "Demonstrates JScrollPane, a container for scrolling contents within a view port",
41        sourceFiles = {
42            "com/sun/swingset3/demos/scrollpane/ScrollPaneDemo.java",
43            "com/sun/swingset3/demos/ResourceManager.java",
44            "com/sun/swingset3/demos/scrollpane/resources/ScrollPaneDemo.properties",
45            "com/sun/swingset3/demos/scrollpane/resources/images/colheader.jpg",
46            "com/sun/swingset3/demos/scrollpane/resources/images/COPYRIGHT",
47            "com/sun/swingset3/demos/scrollpane/resources/images/crayons.jpg",
48            "com/sun/swingset3/demos/scrollpane/resources/images/lowerleft.jpg",
49            "com/sun/swingset3/demos/scrollpane/resources/images/rowheader.jpg",
50            "com/sun/swingset3/demos/scrollpane/resources/images/ScrollPaneDemo.gif",
51            "com/sun/swingset3/demos/scrollpane/resources/images/upperleft.jpg",
52            "com/sun/swingset3/demos/scrollpane/resources/images/upperright.jpg"}
53)
54public class ScrollPaneDemo extends JPanel {
55
56    private final ResourceManager resourceManager = new ResourceManager(this.getClass());
57    public static final String DEMO_TITLE = ScrollPaneDemo.class.getAnnotation(DemoProperties.class).value();
58
59    /**
60     * main method allows us to run as a standalone demo.
61     *
62     * @param args
63     */
64    public static void main(String[] args) {
65        JFrame frame = new JFrame(DEMO_TITLE);
66
67        frame.getContentPane().add(new ScrollPaneDemo());
68        frame.setPreferredSize(new Dimension(800, 600));
69        frame.pack();
70        frame.setLocationRelativeTo(null);
71        frame.setVisible(true);
72    }
73
74    /**
75     * ScrollPaneDemo Constructor
76     */
77    public ScrollPaneDemo() {
78        setLayout(new BorderLayout());
79
80        ImageIcon crayons = resourceManager.createImageIcon("crayons.jpg",
81                resourceManager.getString("ScrollPaneDemo.crayons"));
82        add(new ImageScroller(crayons), BorderLayout.CENTER);
83    }
84
85    /**
86     * ScrollPane class that demonstrates how to set the various column and row
87     * headers and corners.
88     */
89    private class ImageScroller extends JScrollPane {
90
91        public ImageScroller(Icon icon) {
92            super();
93
94            // Panel to hold the icon image
95            JPanel p = new JPanel(new BorderLayout());
96            p.add(new JLabel(icon), BorderLayout.CENTER);
97            getViewport().add(p);
98
99            // Create and add a column header to the scrollpane
100            JLabel colHeader = new JLabel(
101                    resourceManager.createImageIcon("colheader.jpg", resourceManager.getString("ScrollPaneDemo.colheader")));
102            setColumnHeaderView(colHeader);
103
104            // Create and add a row header to the scrollpane
105            JLabel rowHeaderLabel = new JLabel(
106                    resourceManager.createImageIcon("rowheader.jpg", resourceManager.getString("ScrollPaneDemo.rowheader")));
107            setRowHeaderView(rowHeaderLabel);
108
109            // Create and add the upper left corner
110            JLabel cornerUL = new JLabel(
111                    resourceManager.createImageIcon("upperleft.jpg", resourceManager.getString("ScrollPaneDemo.upperleft")));
112            setCorner(UPPER_LEFT_CORNER, cornerUL);
113
114            // Create and add the upper right corner
115            JLabel cornerUR = new JLabel(
116                    resourceManager.createImageIcon("upperright.jpg", resourceManager.getString("ScrollPaneDemo.upperright")));
117            setCorner(UPPER_RIGHT_CORNER, cornerUR);
118
119            // Create and add the lower left corner
120            JLabel cornerLL = new JLabel(
121                    resourceManager.createImageIcon("lowerleft.jpg", resourceManager.getString("ScrollPaneDemo.lowerleft")));
122            setCorner(LOWER_LEFT_CORNER, cornerLL);
123
124            JScrollBar vsb = getVerticalScrollBar();
125            JScrollBar hsb = getHorizontalScrollBar();
126
127            vsb.setValue(icon.getIconHeight());
128            hsb.setValue(icon.getIconWidth() / 10);
129        }
130    }
131
132}
133