Example usage for javax.swing JViewport getParent

List of usage examples for javax.swing JViewport getParent

Introduction

In this page you can find the example usage for javax.swing JViewport getParent.

Prototype

public Container getParent() 

Source Link

Document

Gets the parent of this component.

Usage

From source file:Main.java

public static boolean canVScroll(JViewport viewport) {
    JScrollPane scrollPane = (JScrollPane) viewport.getParent();
    Rectangle availR = scrollPane.getBounds();

    Component view = viewport.getView();
    Dimension viewPrefSize = view != null ? view.getPreferredSize() : new Dimension(0, 0);
    Dimension extentSize = viewport.toViewCoordinates(availR.getSize());

    boolean canVScroll = true;
    if (view instanceof Scrollable)
        canVScroll = !((Scrollable) view).getScrollableTracksViewportHeight();
    if (canVScroll && (viewPrefSize.height <= extentSize.height))
        canVScroll = false;/*from   w w w .j a  va2s.  c  o m*/

    return canVScroll;
}

From source file:Main.java

public static boolean canHScroll(JViewport viewport) {
    JScrollPane scrollPane = (JScrollPane) viewport.getParent();
    Rectangle availR = scrollPane.getBounds();

    Component view = viewport.getView();
    Dimension viewPrefSize = view != null ? view.getPreferredSize() : new Dimension(0, 0);
    Dimension extentSize = viewport.toViewCoordinates(availR.getSize());

    boolean canHScroll = true;
    if (view instanceof Scrollable)
        canHScroll = !((Scrollable) view).getScrollableTracksViewportWidth();
    if (canHScroll && (viewPrefSize.width <= extentSize.width))
        canHScroll = false;// w w w. j  av a2 s  .  c  o  m

    return canHScroll;
}

From source file:Main.java

/**
 * @return the visible size of a component in its viewport (including
 *         scrollbars)//ww w. ja va2  s . c o  m
 */
public static Dimension getVisibleSizeInViewport(Component c) {
    if (c.getParent() instanceof JViewport) {
        JViewport vp = (JViewport) c.getParent();
        Dimension d = vp.getExtentSize();
        if (vp.getParent() instanceof JScrollPane) {
            JScrollPane sp = (JScrollPane) vp.getParent();
            if (sp.getVerticalScrollBar() != null && sp.getVerticalScrollBar().isVisible()) {
                d.width += sp.getVerticalScrollBar().getWidth();
            }
            if (sp.getHorizontalScrollBar() != null && sp.getHorizontalScrollBar().isVisible()) {
                d.height += sp.getHorizontalScrollBar().getHeight();
            }
        }
        return d;
    } else {
        return null;
    }
}

From source file:KjellDirdalNotepad.java

private JScrollPane getScrollPane() {
    if (desktop.getParent() instanceof JViewport) {
        JViewport viewPort = (JViewport) desktop.getParent();
        if (viewPort.getParent() instanceof JScrollPane)
            return (JScrollPane) viewPort.getParent();
    }//  ww w .j  av  a  2 s  .  c  o m
    return null;
}

From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java

private void doSynchronizeTreeWithHighlitedPath() {
    String highlitedPath = myMessages.getHighlitedPath();
    if (highlitedPath == null) {
        return;/*w w  w. ja v  a  2s.c o m*/
    }

    final AbstractLayoutCache layout = ((OutlineModel) getModel()).getLayout();
    int lastSegmentRow = -1;
    int currentSegmentRow = -1;
    int currentSelectedRow = -1;
    int currentMessageIndex = -1;
    for (int row = 0; row < layout.getRowCount(); row++) {

        TreePath path = layout.getPathForRow(row);
        Object component = path.getLastPathComponent();
        if (component instanceof TreeNodeMessage) {
            currentMessageIndex = ((TreeNodeMessage) component).getMessageIndex();
            if (highlitedPath.startsWith(currentMessageIndex + "/")) {
                expandPath(path);
            } else {
                // collapsePath(path);
            }
            continue;
        }

        if (component instanceof TreeNodeUnknown) {
            continue;
        }

        if (component instanceof TreeNodeSegment) {
            lastSegmentRow = row;
        }

        TreeNodeBase node = (TreeNodeBase) component;

        String terserPath = (currentMessageIndex) + node.getTerserPath();
        if (highlitedPath != null && highlitedPath.startsWith(terserPath)
                && !highlitedPath.startsWith(terserPath + "(")) {
            expandPath(path);
            if (highlitedPath.equals(terserPath)) {
                currentSelectedRow = row;
                getSelectionModel().setSelectionInterval(row, row);
                currentSegmentRow = lastSegmentRow;
            }
        } else {
            // collapsePath(path);
        }

    }

    // Adjust the tree scrollpane's scroll position so that the newly
    // selected row is visible
    if (currentSegmentRow != -1 && currentSelectedRow != -1 && !myRespondingToManualRangeChange) {
        JViewport viewPort = (JViewport) getParent();
        final JScrollPane scrollPane = (JScrollPane) viewPort.getParent();

        int tableHeaderHeight = getTableHeader().getHeight();

        int numRowsVisible = ((scrollPane.getHeight() - tableHeaderHeight) / layout.getRowHeight()) - 1;
        int segmentDelta = currentSelectedRow - currentSegmentRow;
        if (segmentDelta > numRowsVisible) {
            currentSegmentRow = currentSegmentRow + (segmentDelta - numRowsVisible);
        }

        final int scrollToRow = currentSegmentRow;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                scrollPane.getVerticalScrollBar().setValue(layout.getRowHeight() * scrollToRow);
            }
        });

    }
}

From source file:pipeline.GUI_utils.ListOfPointsView.java

private static JViewport getTableViewPort(JTable table) {
    Container parent = SwingUtilities.getUnwrappedParent(table);
    if (parent instanceof JViewport) {
        JViewport port = (JViewport) parent;
        Container gp = port.getParent();
        if (gp instanceof JScrollPane) {
            JScrollPane scrollPane = (JScrollPane) gp;
            // Make certain we are the viewPort's view and not, for
            // example, the rowHeaderView of the scrollPane -
            // an implementor of fixed columns might do this.
            JViewport viewPort = scrollPane.getViewport();
            if (viewPort == null || SwingUtilities.getUnwrappedView(viewPort) != table) {
                return null;
            }/*w w  w  .ja  va2s.c o m*/
            return viewPort;
        }
    }
    return null;
}