Example usage for javax.swing JViewport getViewRect

List of usage examples for javax.swing JViewport getViewRect

Introduction

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

Prototype

public Rectangle getViewRect() 

Source Link

Document

Returns a rectangle whose origin is getViewPosition and size is getExtentSize.

Usage

From source file:Main.java

public static void scrollToCenter(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;/*from   w w  w . ja  v  a  2  s.  c o m*/
    }
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Rectangle viewRect = viewport.getViewRect();
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;
    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);
    viewport.scrollRectToVisible(rect);
}

From source file:JTableHelper.java

public static void scrollToCenter(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;/*from   w  w  w .j  a  v a2 s .  co m*/
    }
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Rectangle viewRect = viewport.getViewRect();
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;

    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);
    viewport.scrollRectToVisible(rect);
}

From source file:com.igormaznitsa.sciareto.ui.editors.MMDEditor.java

@Override
public void onEnsureVisibilityOfTopic(@Nonnull final MindMapPanel source, @Nullable final Topic topic) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override/*from   www.j  a  v a  2s  .  co m*/
        public void run() {
            if (topic == null) {
                return;
            }

            final AbstractElement element = (AbstractElement) topic.getPayload();
            if (element == null) {
                return;
            }

            final Rectangle2D orig = element.getBounds();
            final int GAP = 30;

            final Rectangle bounds = orig.getBounds();
            bounds.setLocation(Math.max(0, bounds.x - GAP), Math.max(0, bounds.y - GAP));
            bounds.setSize(bounds.width + GAP * 2, bounds.height + GAP * 2);

            final JViewport viewport = getViewport();
            final Rectangle visible = viewport.getViewRect();

            if (visible.contains(bounds)) {
                return;
            }

            bounds.setLocation(bounds.x - visible.x, bounds.y - visible.y);

            viewport.scrollRectToVisible(bounds);
        }

    });
}

From source file:forge.itemmanager.views.ItemListView.java

@Override
protected void onScrollSelectionIntoView(final JViewport viewport) {
    // compute where we're going and where we are
    final Rectangle targetRect = this.table.getCellRect(this.getSelectedIndex(), 0, true);
    final Rectangle curViewRect = viewport.getViewRect();

    // if the target cell is not visible, attempt to jump to a location where it is
    // visible but not on the edge of the viewport
    if (targetRect.y + targetRect.height > curViewRect.y + curViewRect.height) {
        // target is below us, move to position 3 rows below target
        targetRect.setLocation(targetRect.x, targetRect.y + (targetRect.height * 3));
    } else if (targetRect.y < curViewRect.y) {
        // target is above is, move to position 3 rows above target
        targetRect.setLocation(targetRect.x, targetRect.y - (targetRect.height * 3));
    }/*from  ww  w .ja  va 2  s  . co  m*/

    this.table.scrollRectToVisible(targetRect);
}

From source file:net.sf.jabref.gui.maintable.MainTable.java

public void scrollToCenter(int rowIndex, int vColIndex) {
    if (!(this.getParent() instanceof JViewport)) {
        return;/*from   w ww. j  a v a  2  s.  co  m*/
    }

    JViewport viewport = (JViewport) this.getParent();

    // This rectangle is relative to the table where the
    // northwest corner of cell (0,0) is always (0,0).
    Rectangle rect = this.getCellRect(rowIndex, vColIndex, true);

    // The location of the view relative to the table
    Rectangle viewRect = viewport.getViewRect();

    // Translate the cell location so that it is relative
    // to the view, assuming the northwest corner of the
    // view is (0,0).
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    // Calculate location of rect if it were at the center of view
    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;

    // Fake the location of the cell so that scrollRectToVisible
    // will move the cell to the center
    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);

    // Scroll the area into view.
    viewport.scrollRectToVisible(rect);

    revalidate();
    repaint();
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

private void dragAndScroll(MouseEvent e) {
    // move view if near borders                
    Point point = e.getPoint();//from w  w  w  . j  ava2s  .  co m
    JViewport view = theScrollPane.getViewport();
    Rectangle inner = view.getViewRect();
    inner.grow(-10, -10);

    if (!inner.contains(point)) {
        Point orig = view.getViewPosition();
        if (point.x < inner.x)
            orig.x -= 10;
        else if (point.x > (inner.x + inner.width))
            orig.x += 10;
        if (point.y < inner.y)
            orig.y -= 10;
        else if (point.y > (inner.y + inner.height))
            orig.y += 10;

        int maxx = getBounds().width - view.getViewRect().width;
        int maxy = getBounds().height - view.getViewRect().height;
        if (orig.x < 0)
            orig.x = 0;
        if (orig.x > maxx)
            orig.x = maxx;
        if (orig.y < 0)
            orig.y = 0;
        if (orig.y > maxy)
            orig.y = maxy;

        view.setViewPosition(orig);
    }
}

From source file:de.codesourcery.jasm16.ide.ui.views.SourceCodeView.java

protected final ITextRegion getVisibleTextRegion() {
    JViewport viewport = editorScrollPane.getViewport();
    Rectangle viewRect = viewport.getViewRect();

    Point p1 = viewRect.getLocation();
    int startIndex = editorPane.viewToModel(p1);
    if (startIndex < 0) {
        return null;
    }/*from   w w w . j  a va 2 s .co  m*/
    Point p2 = new Point(p1.x + viewRect.width - 10, p1.y + viewRect.height - 10); // -10 is some arbitrary offset to fix an issue with viewToModel() returning a position at the end of the input text 
    int endIndex = editorPane.viewToModel(p2);
    if (endIndex < 0) {
        return null;
    }

    int len = endIndex - startIndex;
    if (len < 0) {
        return null;
    }
    System.out.println("getVisibleTextRegion( " + p1 + " , " + p2 + " => " + startIndex + "," + endIndex);
    return new TextRegion(startIndex, len);
}

From source file:net.sf.dvstar.transmission.TransmissionView.java

private List<Integer> buildViewModelList() {
    ArrayList ret = new ArrayList();

    int firstRow = 0, lastRow = 0;
    JViewport viewport = (JViewport) spTorrentList.getViewport();
    Rectangle viewRect = viewport.getViewRect();
    firstRow = tblTorrentList.rowAtPoint(new Point(0, viewRect.y));
    lastRow = tblTorrentList.rowAtPoint(new Point(0, viewRect.y + viewRect.height - 1));
    if (lastRow < 0) {
        lastRow = firstRow + tblTorrentList.getRowSorter().getViewRowCount() - 1;
    }// ww  w.  ja  v a 2s  .  com
    /**
     * @todo   if rows < height - not update last row
     */
    tracePrint(true, "=-=-=-=-=-=-= tblTorrentList first=" + firstRow + " last=" + lastRow);

    for (int i = firstRow; i <= lastRow; i++) {
        ret.add(new Integer(tblTorrentList.convertRowIndexToModel(i)/* realConvertRowIndexToModel(i)*/));
    }

    return ret;
}

From source file:org.jimcat.gui.wheellist.WheelList.java

/**
 * does layout before printing/*w w  w  . j  a v  a  2s .co  m*/
 * 
 * @see javax.swing.JComponent#paintChildren(java.awt.Graphics)
 */
@Override
protected void paintChildren(Graphics g) {
    // 1) do layout
    Rectangle area = null;
    if (getParent() instanceof JViewport) {
        JViewport port = (JViewport) getParent();
        area = port.getViewRect();
    } else {
        Dimension size = getSize();
        area = new Rectangle(0, 0, size.width, size.height);
    }

    // do layout
    currentColumnCount = getWidth() / itemSize.width;
    currentSideOffset = (getWidth() - itemSize.width * currentColumnCount) / 2;

    int startRow = area.y / itemSize.height;
    int endRow = (area.y + area.height) / itemSize.height;

    int startCol = (area.x - currentSideOffset) / itemSize.width;
    int endCol = (area.x - currentSideOffset + area.width) / itemSize.width - 1;

    // update current rectangle
    currentArea = new Rectangle(startCol, startRow, endCol - startCol, endRow - startRow);

    // only do layout if necessary
    if (!ObjectUtils.equals(currentArea, lastViewArea)) {

        // remove unnecessary items
        for (Integer key : new HashSet<Integer>(map.keySet())) {
            int keyValue = key.intValue();
            if (keyValue >= 0 && !containesIndex(currentArea, keyValue)) {
                WheelListItem<T> item = map.get(key);
                remove(item);
                map.remove(key);
                map.put((-1) * key.intValue() - 1, item);
            }
        }

        // add new items
        for (int i = startRow; i <= endRow; i++) {
            for (int j = startCol; j <= endCol; j++) {
                // place item
                int index = i * currentColumnCount + j;
                if (index < model.getSize()) {

                    // get item
                    WheelListItem<T> item = getItemForIndex(index);

                    // prepaire item
                    item.setElement(model.getElementAt(index));
                    item.setSelected(selectionModel.isSelectedIndex(index));

                    item.setSize(itemSize);
                    item.setLocation(currentSideOffset + j * itemSize.width, i * itemSize.height);

                    // add item
                    if (item.getParent() != this) {
                        add(item);
                    }
                }
            }
        }

        validate();

        lastViewArea = currentArea;
    }

    // 2) paint
    super.paintChildren(g);
}

From source file:org.nuclos.client.ui.collect.result.SearchResultStrategy.java

private void adjustVerticalScrollBarForSearch(boolean bRefreshOnly) {
    final CollectController<Clct> cc = getCollectController();
    final JViewport viewport = cc.getResultPanel().getResultTableScrollPane().getViewport();
    if (bRefreshOnly) {
        final Rectangle rect = cc.getResultTable().getCellRect(0, 0, true);
        final Rectangle viewRect = viewport.getViewRect();
        // There seem to be different opinions about what scrollRectToVisible has to do at SUN and everywhere else...
        rect.setLocation(viewRect.x, viewRect.y);//rect.x - viewRect.x, rect.y - viewRect.y);
        viewport.scrollRectToVisible(rect);
    } else {//from w w w .  ja va 2 s  . c o  m
        Point viewPosition = viewport.getViewPosition();
        viewport.setViewPosition(new Point(viewPosition.x, 0));
    }
    final JScrollBar scrlbarVertical = cc.getResultPanel().getResultTableScrollPane().getVerticalScrollBar();
    scrlbarVertical.setValue(scrlbarVertical.getMinimum());
}