Java Code Examples for javax.swing.JViewport#getViewPosition()

The following examples show how to use javax.swing.JViewport#getViewPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CPSTable.java    From cropplanning with GNU General Public License v3.0 6 votes vote down vote up
public void ensureRowIsVisible( int rowIndex ) {
    if ( !( this.getParent() instanceof JViewport ) ) {
        return;
    }
    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, 1, true );

    // The location of the viewport relative to the table
    Point pt = viewport.getViewPosition();

    // 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 - pt.x, rect.y - pt.y );

    // Scroll the area into view
    viewport.scrollRectToVisible( rect );
}
 
Example 2
Source File: Test6526631.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void validateThird() {
    JViewport viewport = this.pane.getViewport();
    JScrollBar scroller = this.pane.getHorizontalScrollBar();
    if (!scroller.getComponentOrientation().equals(ComponentOrientation.RIGHT_TO_LEFT)) {
        throw new Error("unexpected component orientation");
    }
    int value = scroller.getValue();
    if (value != 0) {
        throw new Error("unexpected scroll value");
    }
    int extent = viewport.getExtentSize().width;
    if (extent != scroller.getVisibleAmount()) {
        throw new Error("unexpected visible amount");
    }
    int size = viewport.getViewSize().width;
    if (size != scroller.getMaximum()) {
        throw new Error("unexpected maximum");
    }
    int pos = size - extent - value;
    if (pos != viewport.getViewPosition().x) {
        throw new Error("unexpected position");
    }
}
 
Example 3
Source File: Test6526631.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void validateThird() {
    JViewport viewport = this.pane.getViewport();
    JScrollBar scroller = this.pane.getHorizontalScrollBar();
    if (!scroller.getComponentOrientation().equals(ComponentOrientation.RIGHT_TO_LEFT)) {
        throw new Error("unexpected component orientation");
    }
    int value = scroller.getValue();
    if (value != 0) {
        throw new Error("unexpected scroll value");
    }
    int extent = viewport.getExtentSize().width;
    if (extent != scroller.getVisibleAmount()) {
        throw new Error("unexpected visible amount");
    }
    int size = viewport.getViewSize().width;
    if (size != scroller.getMaximum()) {
        throw new Error("unexpected maximum");
    }
    int pos = size - extent - value;
    if (pos != viewport.getViewPosition().x) {
        throw new Error("unexpected position");
    }
}
 
Example 4
Source File: Test6526631.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void validateThird() {
    JViewport viewport = this.pane.getViewport();
    JScrollBar scroller = this.pane.getHorizontalScrollBar();
    if (!scroller.getComponentOrientation().equals(ComponentOrientation.RIGHT_TO_LEFT)) {
        throw new Error("unexpected component orientation");
    }
    int value = scroller.getValue();
    if (value != 0) {
        throw new Error("unexpected scroll value");
    }
    int extent = viewport.getExtentSize().width;
    if (extent != scroller.getVisibleAmount()) {
        throw new Error("unexpected visible amount");
    }
    int size = viewport.getViewSize().width;
    if (size != scroller.getMaximum()) {
        throw new Error("unexpected maximum");
    }
    int pos = size - extent - value;
    if (pos != viewport.getViewPosition().x) {
        throw new Error("unexpected position");
    }
}
 
Example 5
Source File: ComponentPeer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private int[] computeVisibleSpan() {
    Component parent = pane.getParent();
    
    if (parent instanceof JLayeredPane) {
        parent = parent.getParent();
    }

    if (parent instanceof JViewport) {
        JViewport vp = (JViewport) parent;

        Point start = vp.getViewPosition();
        Dimension size = vp.getExtentSize();
        Point end = new Point((int) (start.getX() + size.getWidth()), (int) (start.getY() + size.getHeight()));

        int startPosition = pane.viewToModel(start);
        int endPosition = pane.viewToModel(end);

        if (parentWithListener != vp) {
            vp.addChangeListener(WeakListeners.change(this, vp));
            parentWithListener = vp;
        }
        return new int[] {startPosition, endPosition};
    }

    return new int[] {0, pane.getDocument().getLength()};
}
 
Example 6
Source File: Test6526631.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void validateThird() {
    JViewport viewport = this.pane.getViewport();
    JScrollBar scroller = this.pane.getHorizontalScrollBar();
    if (!scroller.getComponentOrientation().equals(ComponentOrientation.RIGHT_TO_LEFT)) {
        throw new Error("unexpected component orientation");
    }
    int value = scroller.getValue();
    if (value != 0) {
        throw new Error("unexpected scroll value");
    }
    int extent = viewport.getExtentSize().width;
    if (extent != scroller.getVisibleAmount()) {
        throw new Error("unexpected visible amount");
    }
    int size = viewport.getViewSize().width;
    if (size != scroller.getMaximum()) {
        throw new Error("unexpected maximum");
    }
    int pos = size - extent - value;
    if (pos != viewport.getViewPosition().x) {
        throw new Error("unexpected position");
    }
}
 
Example 7
Source File: XTextAreaPeer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static Point toViewportChildLocalSpace( JViewport v, Point inViewportParentSpace )
{
    Point l = toLocalSpace(v, inViewportParentSpace);
    Point p = v.getViewPosition();
    l.x += p.x;
    l.y += p.y;
    return l;
}
 
Example 8
Source File: XTextAreaPeer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static Point toViewportChildLocalSpace( JViewport v, Point inViewportParentSpace )
{
    Point l = toLocalSpace(v, inViewportParentSpace);
    Point p = v.getViewPosition();
    l.x += p.x;
    l.y += p.y;
    return l;
}
 
Example 9
Source File: XTextAreaPeer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Point toViewportChildLocalSpace( JViewport v, Point inViewportParentSpace )
{
    Point l = toLocalSpace(v, inViewportParentSpace);
    Point p = v.getViewPosition();
    l.x += p.x;
    l.y += p.y;
    return l;
}
 
Example 10
Source File: PreviewPanel.java    From swingsane with Apache License 2.0 5 votes vote down vote up
@Override
public final void mouseWheelMoved(MouseWheelEvent e) {

  Point clickPoint = e.getPoint();

  JViewport viewport = ((JScrollPane) e.getSource()).getViewport();
  Point viewPosition = viewport.getViewPosition();

  // 1. Get viewport rectangle.
  Rectangle viewportRectangle = new Rectangle(viewPosition, viewport.getSize());

  // 2. Get the midpoint of the viewport.
  Point viewportMidpoint = getViewportMidpoint(viewport);

  // 3. calculate the distance from the click point to the midpoint
  int deltaX = (viewPosition.x + clickPoint.x) - viewportMidpoint.x;
  int deltaY = (viewPosition.y + clickPoint.y) - viewportMidpoint.y;

  // 4. translate the viewport rectangle
  viewportRectangle.translate(deltaX, deltaY);

  // 5. scroll the viewport to the new viewport rectangle position
  imagePreviewLabel.scrollRectToVisible(viewportRectangle);

  int notches = e.getWheelRotation();
  if (notches < 0) {
    zoomIn();
  } else {
    zoomOut();
  }

}
 
Example 11
Source File: XTextAreaPeer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static Point toViewportChildLocalSpace( JViewport v, Point inViewportParentSpace )
{
    Point l = toLocalSpace(v, inViewportParentSpace);
    Point p = v.getViewPosition();
    l.x += p.x;
    l.y += p.y;
    return l;
}
 
Example 12
Source File: AutoscrollSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Performs autoscroll operation.
*/
public void autoscroll(Point cursorLoc) {
    JViewport viewport = getViewport();

    if (viewport == null) {
        return;
    }

    Point viewPos = viewport.getViewPosition();
    int viewHeight = viewport.getExtentSize().height;
    int viewWidth = viewport.getExtentSize().width;

    // perform scrolling
    if ((cursorLoc.y - viewPos.y) < insets.top) {
        // scroll up
        viewport.setViewPosition(new Point(viewPos.x, Math.max(viewPos.y - scrollUnits.top, 0)));
    } else if (((viewPos.y + viewHeight) - cursorLoc.y) < insets.bottom) {
        // scroll down
        viewport.setViewPosition(
            new Point(viewPos.x, Math.min(viewPos.y + scrollUnits.bottom, comp.getHeight() - viewHeight))
        );
    } else if ((cursorLoc.x - viewPos.x) < insets.left) {
        // scroll left
        viewport.setViewPosition(new Point(Math.max(viewPos.x - scrollUnits.left, 0), viewPos.y));
    } else if (((viewPos.x + viewWidth) - cursorLoc.x) < insets.right) {
        // scroll right
        viewport.setViewPosition(
            new Point(Math.min(viewPos.x + scrollUnits.right, comp.getWidth() - viewWidth), viewPos.y)
        );
    }
}
 
Example 13
Source File: HeaderlessColumnResizer.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
  int mouseX = e.getX();

  TableColumn resizingColumn = table.getTableHeader().getResizingColumn();

  boolean headerLeftToRight =
    table.getTableHeader().getComponentOrientation().isLeftToRight();

  if (resizingColumn != null) {
    int oldWidth = resizingColumn.getWidth();
    int newWidth;
    if (headerLeftToRight) {
      newWidth = mouseX - mouseXOffset;
    } else {
      newWidth = mouseXOffset - mouseX;
    }
    resizingColumn.setWidth(newWidth);

    Container container;
    if ((table.getTableHeader().getParent() == null)
      || ((container = table.getTableHeader().getParent().getParent()) == null)
      || !(container instanceof JScrollPane)) {
      return;
    }

    if (!container.getComponentOrientation().isLeftToRight()
      && !headerLeftToRight) {
      if (table != null) {
        JViewport viewport = ((JScrollPane)container).getViewport();
        int viewportWidth = viewport.getWidth();
        int diff = newWidth - oldWidth;
        int newHeaderWidth = table.getWidth() + diff;

        /* Resize a table */
        Dimension tableSize = table.getSize();
        tableSize.width += diff;
        table.setSize(tableSize);

        /*
         * If this table is in AUTO_RESIZE_OFF mode and has a horizontal
         * scrollbar, we need to update a view's position.
         */
        if ((newHeaderWidth >= viewportWidth)
          && (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
          Point p = viewport.getViewPosition();
          p.x =
            Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
          viewport.setViewPosition(p);

          /* Update the original X offset value. */
          mouseXOffset += diff;
        }
      }
    }
  }
}
 
Example 14
Source File: XMLTextRepresentation.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void updateTextInAWT(EditorCookie es, final String in) throws IOException, BadLocationException {
    StyledDocument tmpdoc = es.getDocument();
    if (tmpdoc == null)
        tmpdoc = es.openDocument();

    //sample editor position

    JEditorPane[] eps = es.getOpenedPanes();
    JEditorPane pane = null;
    JViewport port = null;
    int caretPosition = 0;
    Point viewPosition = null;
    if (eps != null) {
        pane = eps[0];
        caretPosition = pane.getCaretPosition();
        port = getParentViewport (pane);
        if (port != null)
            viewPosition = port.getViewPosition();
    }

    // prepare modification task

    final Exception[] taskEx = new Exception[] {null};
    final StyledDocument sdoc = tmpdoc;

    Runnable task = new Runnable() {
        public void run() {
            try {
                sdoc.remove (0, sdoc.getLength());  // right alternative

                // we are at Unicode level
                sdoc.insertString (0, in, null);
            } catch (Exception iex) {
                taskEx[0] = iex;
            }
        }
    };

    // perform document modification

    org.openide.text.NbDocument.runAtomicAsUser(sdoc, task);

    //??? setModified (true);  

    //restore editor position

    if (eps != null) {
        try {
            pane.setCaretPosition (caretPosition);
        } catch (IllegalArgumentException e) {
        }
        port.setViewPosition (viewPosition);
    }

    if (taskEx[0]!=null) {
        if (taskEx[0] instanceof IOException) {
            throw (IOException)taskEx[0];
        }
        throw new IOException(taskEx[0]);
    }
    
}
 
Example 15
Source File: BoardPanel.java    From freerouting with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the position of the viewport
 */
public java.awt.Point get_viewport_position()
{
    JViewport viewport =  scroll_pane.getViewport();
    return viewport.getViewPosition();
}
 
Example 16
Source File: BoardPanel.java    From Freerouting with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the position of the viewport
 */
public java.awt.Point get_viewport_position()
{
    JViewport viewport =  scroll_pane.getViewport();
    return viewport.getViewPosition();
}
 
Example 17
Source File: HeaderlessColumnResizer.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
  int mouseX = e.getX();

  TableColumn resizingColumn = table.getTableHeader().getResizingColumn();

  boolean headerLeftToRight =
    table.getTableHeader().getComponentOrientation().isLeftToRight();

  if (resizingColumn != null) {
    int oldWidth = resizingColumn.getWidth();
    int newWidth;
    if (headerLeftToRight) {
      newWidth = mouseX - mouseXOffset;
    } else {
      newWidth = mouseXOffset - mouseX;
    }
    resizingColumn.setWidth(newWidth);

    Container container;
    if ((table.getTableHeader().getParent() == null)
      || ((container = table.getTableHeader().getParent().getParent()) == null)
      || !(container instanceof JScrollPane)) {
      return;
    }

    if (!container.getComponentOrientation().isLeftToRight()
      && !headerLeftToRight) {
      if (table != null) {
        JViewport viewport = ((JScrollPane)container).getViewport();
        int viewportWidth = viewport.getWidth();
        int diff = newWidth - oldWidth;
        int newHeaderWidth = table.getWidth() + diff;

        /* Resize a table */
        Dimension tableSize = table.getSize();
        tableSize.width += diff;
        table.setSize(tableSize);

        /*
         * If this table is in AUTO_RESIZE_OFF mode and has a horizontal
         * scrollbar, we need to update a view's position.
         */
        if ((newHeaderWidth >= viewportWidth)
          && (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
          Point p = viewport.getViewPosition();
          p.x =
            Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
          viewport.setViewPosition(p);

          /* Update the original X offset value. */
          mouseXOffset += diff;
        }
      }
    }
  }
}
 
Example 18
Source File: TableRowUtilities.java    From swing_library with MIT License 4 votes vote down vote up
public TableListener(JTable rowHeadersTable, JTable userTable) {
	this.userTable = userTable;
	this.rowHeadersTable = rowHeadersTable;

	Container p = userTable.getParent();
	userTableViewPort = (JViewport) p;

	Container p2 = rowHeadersTable.getParent();
	rowHeadersViewPort = (JViewport) p2;

	Point newPosition = userTableViewPort.getViewPosition();
	rowHeadersViewPort.setViewPosition(newPosition);

	// userTableViewPort.setViewPosition(newPosition);

	rowHeadersTable.getSelectionModel().addListSelectionListener(this);
	userTable.getSelectionModel().addListSelectionListener(this);

}
 
Example 19
Source File: SeaGlassScrollPaneUI.java    From seaglass with Apache License 2.0 4 votes vote down vote up
private void vsbStateChanged(JViewport viewport, ChangeEvent e) {
    BoundedRangeModel model = (BoundedRangeModel) (e.getSource());
    Point p = viewport.getViewPosition();
    p.y = model.getValue();
    viewport.setViewPosition(p);
}
 
Example 20
Source File: Preferences.java    From pdfxtk with Apache License 2.0 votes vote down vote up
void          grab(JViewport cmp)  {position = cmp.getViewPosition();}