JList ViewportLayout and scrollRectToVisible

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

JList ViewportLayout and scrollRectToVisible

i30817
I did my own layout for the JScrollPane viewport, but replacing it seems to make a (core ?) bug visible in JList. Whenever the user uses the keyboard to move the selected cell, the viewport jumps from the left-origin to the center (the layout is centered). I "solved" it by disabling keyboard navigation by no-oping scrollRectToVisible, but if you can help me find the bug i would be grateful. Here is my layout, it is made to be used with HORIZONTAL_WRAP orientation.
public static final class CenteredViewPortLayout implements LayoutManager {

        public void addLayoutComponent(String name, Component comp) {/*nop*/

        }

        public void removeLayoutComponent(Component comp) {/*nop*/

        }

        public Dimension maximumLayoutSize(Container target) {
            if (target == null) {
                return new Dimension(0, 0);
            }
            return target.getPreferredSize();
        }

        public Dimension preferredLayoutSize(Container parent) {
            Component view = ((JViewport) parent).getView();
            if (view == null) {
                return new Dimension(0, 0);
            } else if (view instanceof Scrollable) {
                return ((Scrollable) view).getPreferredScrollableViewportSize();
            } else {
                return view.getPreferredSize();
            }
        }

        public Dimension minimumLayoutSize(Container parent) {
            return new Dimension(4, 4);
        }

        public void layoutContainer(Container parent) {
            JViewport vp = (JViewport) parent;
            Component view = vp.getView();
            if (view == null) {
                return;
            }
            /**
             * All of the dimensions below are in view coordinates.
             */
            Dimension newViewSize = view.getPreferredSize();
            Dimension maximumViewSize = vp.toViewCoordinates(vp.getSize());
            /**
             * If a JList the prefered size is the prefered size of the
             * sum of cells fitting in a row.
             * Also, the limit is the number of cells.
             */
            if (view instanceof JList) {
                int fixedCellWidth = ((JList) view).getFixedCellWidth();
                int expandLimit = ((JList) view).getModel().getSize();
                if (fixedCellWidth != -1) {
                    newViewSize.width = fixedCellWidth;
                }
                /**
                 * Multiply the cell width until it matches the
                 * n * viewPreferredSize.width + z = viewPort.width
                 * for a z < preferredSize and n <= expandLimit
                 */
                int n = 0;
                while (((n + 1) * newViewSize.width) <= maximumViewSize.width && (n + 1) <= expandLimit) {
                    n++;
                }
                newViewSize.width = n * newViewSize.width;
            }
            //fill to maximum size if it doesn't fit or nothing there
            if (newViewSize.width == 0) {
                newViewSize.width = maximumViewSize.width;
            }
            /**_________     __________
             * |X|XYX|Y|  -> |X|YYYY|X|
             * |X|XYX|Y|     |X|YYYY|X|
             */
            Point viewPosition = vp.getViewPosition();
            int centerJustifiedX = (maximumViewSize.width - newViewSize.width) / 2;
            viewPosition.x = -Math.max(0, centerJustifiedX);
            vp.setViewPosition(viewPosition);
            vp.setViewSize(newViewSize);
        }
    }
Reply | Threaded
Open this post in threaded view
|

Re: JList ViewportLayout and scrollRectToVisible

James Lemieux
This isn't a Glazed Lists bug, so it's best posted to some other Swing-specific forum, such as SUN's Java forums.

On Tue, Aug 11, 2009 at 3:48 PM, i30817 <[hidden email]> wrote:
I did my own layout for the JScrollPane viewport, but replacing it seems to make a (core ?) bug visible in JList. Whenever the user uses the keyboard to move the selected cell, the viewport jumps from the left-origin to the center (the layout is centered). I "solved" it by disabling keyboard navigation by no-oping scrollRectToVisible, but if you can help me find the bug i would be grateful. Here is my layout, it is made to be used with HORIZONTAL_WRAP orientation.
public static final class CenteredViewPortLayout implements LayoutManager {

        public void addLayoutComponent(String name, Component comp) {/*nop*/

        }

        public void removeLayoutComponent(Component comp) {/*nop*/

        }

        public Dimension maximumLayoutSize(Container target) {
            if (target == null) {
                return new Dimension(0, 0);
            }
            return target.getPreferredSize();
        }

        public Dimension preferredLayoutSize(Container parent) {
            Component view = ((JViewport) parent).getView();
            if (view == null) {
                return new Dimension(0, 0);
            } else if (view instanceof Scrollable) {
                return ((Scrollable) view).getPreferredScrollableViewportSize();
            } else {
                return view.getPreferredSize();
            }
        }

        public Dimension minimumLayoutSize(Container parent) {
            return new Dimension(4, 4);
        }

        public void layoutContainer(Container parent) {
            JViewport vp = (JViewport) parent;
            Component view = vp.getView();
            if (view == null) {
                return;
            }
            /**
             * All of the dimensions below are in view coordinates.
             */
            Dimension newViewSize = view.getPreferredSize();
            Dimension maximumViewSize = vp.toViewCoordinates(vp.getSize());
            /**
             * If a JList the prefered size is the prefered size of the
             * sum of cells fitting in a row.
             * Also, the limit is the number of cells.
             */
            if (view instanceof JList) {
                int fixedCellWidth = ((JList) view).getFixedCellWidth();
                int expandLimit = ((JList) view).getModel().getSize();
                if (fixedCellWidth != -1) {
                    newViewSize.width = fixedCellWidth;
                }
                /**
                 * Multiply the cell width until it matches the
                 * n * viewPreferredSize.width + z = viewPort.width
                 * for a z < preferredSize and n <= expandLimit
                 */
                int n = 0;
                while (((n + 1) * newViewSize.width) <= maximumViewSize.width && (n + 1) <= expandLimit) {
                    n++;
                }
                newViewSize.width = n * newViewSize.width;
            }
            //fill to maximum size if it doesn't fit or nothing there
            if (newViewSize.width == 0) {
                newViewSize.width = maximumViewSize.width;
            }
            /**_________     __________
             * |X|XYX|Y|  -> |X|YYYY|X|
             * |X|XYX|Y|     |X|YYYY|X|
             */
            Point viewPosition = vp.getViewPosition();
            int centerJustifiedX = (maximumViewSize.width - newViewSize.width) / 2;
            viewPosition.x = -Math.max(0, centerJustifiedX);
            vp.setViewPosition(viewPosition);
            vp.setViewSize(newViewSize);
        }
    }


View this message in context: JList ViewportLayout and scrollRectToVisible
Sent from the GlazedLists - Dev mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: JList ViewportLayout and scrollRectToVisible

i30817
You obviously never posted anything in those pits of newbie developers.
Besides this is where the List experts hangout.