1/*
2 * Copyright (c) 2012, 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 */
23
24import javax.swing.*;
25import java.awt.*;
26import java.awt.dnd.Autoscroll;
27
28public class JListWithScroll<E> extends JList<E> implements Autoscroll {
29    private Insets scrollInsets;
30
31    public JListWithScroll(E[] listData) {
32        super(listData);
33        scrollInsets = new Insets(50, 50, 50, 50);
34    }
35
36    @Override
37    public Insets getAutoscrollInsets() {
38        return scrollInsets;
39    }
40
41    @Override
42    public void autoscroll(Point cursorLoc) {
43        JViewport viewport = getViewport();
44
45        if (viewport == null) {
46            return;
47        }
48
49        Point viewPos = viewport.getViewPosition();
50        int viewHeight = viewport.getExtentSize().height;
51        int viewWidth = viewport.getExtentSize().width;
52
53        if ((cursorLoc.y - viewPos.y) < scrollInsets.top) {
54            viewport.setViewPosition(new Point(viewPos.x, Math.max(viewPos.y - scrollInsets.top, 0)));
55        } else if (((viewPos.y + viewHeight) - cursorLoc.y) < scrollInsets.bottom) {
56            viewport.setViewPosition(
57                    new Point(viewPos.x, Math.min(viewPos.y + scrollInsets.bottom, this.getHeight() - viewHeight))
58            );
59        } else if ((cursorLoc.x - viewPos.x) < scrollInsets.left) {
60            viewport.setViewPosition(new Point(Math.max(viewPos.x - scrollInsets.left, 0), viewPos.y));
61        } else if (((viewPos.x + viewWidth) - cursorLoc.x) < scrollInsets.right) {
62            viewport.setViewPosition(
63                    new Point(Math.min(viewPos.x + scrollInsets.right, this.getWidth() - viewWidth), viewPos.y)
64            );
65        }
66
67    }
68
69    public JViewport getViewport() {
70        Component curComp = this;
71
72        while (!(curComp instanceof JViewport) && (curComp != null)) {
73            curComp = curComp.getParent();
74        }
75        if(curComp instanceof JViewport) {
76            return (JViewport) curComp;
77        } else {
78            return null;
79        }
80    }
81}
82