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

The following examples show how to use javax.swing.JViewport#getViewRect() . 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: ZoomManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    Scene scene = manager.getScene();
    JScrollPane pane = (JScrollPane) SwingUtilities.getAncestorOfClass(
            JScrollPane.class, scene.getView());
    if (pane == null) {
        // Unlikely, but we cannot assume it exists.
        return;
    }
    JViewport viewport = pane.getViewport();
    Rectangle visRect = viewport.getViewRect();
    Rectangle compRect = scene.getPreferredBounds();
    int zoomX = visRect.width * 100 / compRect.width;
    int zoomY = visRect.height * 100 / compRect.height;
    int zoom = Math.min(zoomX, zoomY);
    manager.setZoom(zoom);
}
 
Example 2
Source File: UIUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void focusGained(FocusEvent e) {
    if (e.isTemporary()) {
        return;
    }
    Component cmp = e.getComponent();
    if(cmp instanceof JComponent) {
        JViewport vp = getViewport(container);
        if(vp == null) {
            return;
        }
        Rectangle vr = vp.getViewRect();
        Point p = SwingUtilities.convertPoint(cmp.getParent(), cmp.getLocation(), container);
        final Rectangle r = new Rectangle(p, cmp.getSize());
        if(vr.intersects(r)) {
            return; 
        }
        container.scrollRectToVisible(r);
    }
}
 
Example 3
Source File: CloseTabPaneUI.java    From iBioSim with Apache License 2.0 6 votes vote down vote up
@Override
public void stateChanged(ChangeEvent e) {
	JViewport viewport = (JViewport) e.getSource();
	int tabPlacement = tabPane.getTabPlacement();
	int tabCount = tabPane.getTabCount();
	Rectangle vpRect = viewport.getBounds();
	Dimension viewSize = viewport.getViewSize();
	Rectangle viewRect = viewport.getViewRect();

	leadingTabIndex = getClosestTab(viewRect.x, viewRect.y);

	// If the tab isn't right aligned, adjust it.
	if (leadingTabIndex + 1 < tabCount) {

		if (rects[leadingTabIndex].x < viewRect.x) {
			leadingTabIndex++;
		}

	}
	Insets contentInsets = getContentBorderInsets(tabPlacement);

	tabPane.repaint(vpRect.x, vpRect.y + vpRect.height, vpRect.width, contentInsets.top);
	scrollBackwardButton.setEnabled(viewRect.x > 0);
	scrollForwardButton.setEnabled(leadingTabIndex < tabCount - 1 && viewSize.width - viewRect.x > viewRect.width);

}
 
Example 4
Source File: GltfBrowserPanel.java    From JglTF with MIT License 6 votes vote down vote up
/**
 * Scroll the given tree so that the given tree path is at the upper
 * border of the containing scroll pane
 * 
 * @param tree The tree
 * @param treePath The tree path
 */
private static void scrollToTop(JTree tree, TreePath treePath)
{
    Rectangle bounds = tree.getPathBounds(treePath);
    if (bounds == null)
    {
        return;
    }
    Container parent = tree.getParent();
    if (parent instanceof JViewport)
    {
        JViewport viewport = (JViewport)parent;
        Rectangle viewRect = viewport.getViewRect();
        Rectangle rectangle = new Rectangle(
            0, bounds.y, viewRect.width, viewRect.height);
        tree.scrollRectToVisible(rectangle);
    }
}
 
Example 5
Source File: AWTWrapper.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
public AWTWrapper(PreviewPanel inner) {
    innerPanel = inner;
    setLayout(null);
    heavyContainer = new java.awt.Panel();
    add(heavyContainer);
    heavyContainer.add((Component)innerPanel);
    setSize(innerPanel.getGui().getPreferredSize());
    heavyContainer.setSize(getSize());
    heavyContainer.repaint();
    wandora = Wandora.getWandora();
    final JViewport vp = wandora.getViewPort();
    cl = new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            Rectangle viewRec = vp.getViewRect();
            
            try
            {
                Point p = getTopDistance();
                viewRec.x -= p.x;
                viewRec.y -= p.y;
                
                resizeHeavy(viewRec);
            }
            catch(Exception ex) {
                System.err.println(ex.toString());
            }
        }
    };
    prefSize = innerPanel.getGui().getPreferredSize();
    setSize(prefSize);
    setPreferredSize(prefSize);
    setMinimumSize(prefSize);
    setMaximumSize(prefSize);
    heavyContainer.setBounds(new Rectangle(0, 0, getWidth(), getHeight()));
    heavyContainer.repaint();
    wandora.getViewPort().addChangeListener(cl);
}
 
Example 6
Source File: ViewDragScrollListener.java    From openAGV with Apache License 2.0 5 votes vote down vote up
private boolean isFigureCompletelyInView(Figure figure,
                                         JViewport viewport,
                                         OpenTCSDrawingView drawingView) {
  Rectangle viewPortBounds = viewport.getViewRect();
  Rectangle figureBounds = drawingView.drawingToView(figure.getDrawingArea());

  return (figureBounds.getMinX() > viewPortBounds.getMinX())
      && (figureBounds.getMinY() > viewPortBounds.getMinY())
      && (figureBounds.getMaxX() < viewPortBounds.getMaxX())
      && (figureBounds.getMaxY() < viewPortBounds.getMaxY());
}
 
Example 7
Source File: CardsTablePanel.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
protected void scrollRowToViewportCenter(int row) {
    JViewport viewport = (JViewport) table.getParent();
    Rectangle viewRect = viewport.getViewRect();
    Rectangle rect = table.getCellRect(row, 0, true);
    int y = rect.y < viewRect.y || rect.y > (viewRect.y + viewRect.height)
        ? rect.y - (viewRect.height / 2) + rect.height
        : viewRect.y;
    viewport.setViewPosition(new Point(viewRect.x, y));
}
 
Example 8
Source File: MMDGraphEditor.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void onEnsureVisibilityOfTopic(@Nonnull final MindMapPanel source, @Nonnull final Topic topic) {
  mindMapPanel.doLayout();

  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 = mainScrollPane.getViewport();
  final Rectangle visible = viewport.getViewRect();

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

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

  mainScrollPane.revalidate();
  SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
      viewport.scrollRectToVisible(bounds);
      mainScrollPane.repaint();
    }
  });
}
 
Example 9
Source File: CustomScroll.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
private void displayHorizontalScrollBarIfNecessary(JViewport viewPort) {
    Rectangle viewRect = viewPort.getViewRect();
    Dimension viewSize = viewPort.getViewSize();
    boolean shouldDisplayHorizontalScrollBar =
            viewSize.getWidth() > viewRect.getWidth();
    horizontalScrollBar.setVisible(shouldDisplayHorizontalScrollBar);
}
 
Example 10
Source File: CustomScroll.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
private void displayVerticalScrollBarIfNecessary(JViewport viewPort) {
    Rectangle viewRect = viewPort.getViewRect();
    Dimension viewSize = viewPort.getViewSize();
    boolean shouldDisplayVerticalScrollBar =
            viewSize.getHeight() > viewRect.getHeight();
    verticalScrollBar.setVisible(shouldDisplayVerticalScrollBar);
}
 
Example 11
Source File: ExtendedJTablePacker.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
private int preferredWidth(ExtendedJTable table, int col) {
	TableColumn tableColumn = table.getColumnModel().getColumn(col);
	int width = (int) table.getTableHeader().getDefaultRenderer()
			.getTableCellRendererComponent(table, tableColumn.getIdentifier(), false, false, -1, col).getPreferredSize()
			.getWidth();

	if (table.getRowCount() != 0) {
		int from = 0;
		int to = table.getRowCount();
		ExtendedJScrollPane scrollPane = (table).getExtendedScrollPane();
		if (scrollPane != null) {
			JViewport viewport = scrollPane.getViewport();
			Rectangle viewRect = viewport.getViewRect();
			from = table.rowAtPoint(new Point(0, viewRect.y));
			from = Math.max(0, from);
			to = table.rowAtPoint(new Point(0, viewRect.y + viewRect.height - 2));
			to = Math.min(to, table.getRowCount());
		}

		for (int row = from; row < to; row++) {
			int preferedWidth = (int) table.getCellRenderer(row, col)
					.getTableCellRendererComponent(table, table.getValueAt(row, col), false, false, row, col)
					.getPreferredSize().getWidth();
			width = Math.max(width, preferedWidth);
		}
	}

	return width + table.getIntercellSpacing().width;
}
 
Example 12
Source File: DecoratedTreeUI.java    From pumpernickel with MIT License 5 votes vote down vote up
private int getMaxWidth() {
	int maxWidth = tree.getWidth();
	if (tree.getParent() instanceof JViewport) {
		JViewport viewport = (JViewport) tree.getParent();
		maxWidth = viewport.getViewRect().width;
	}
	return maxWidth;
}
 
Example 13
Source File: DebuggingViewComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Restore stored scroll position.
 */
private void restoreScrollPosition(boolean delayScrollWithMarkingDirtyRegion) {
    if (visibleTreePosition != null) {
        JTree tree = getJTree();
        if (tree != null) {
            int row = tree.getRowForPath(visibleTreePosition.getPath());
            if (row != -1) {
                Rectangle bounds = tree.getRowBounds(row);
                if (bounds != null) {
                    int scrollY = bounds.y - visibleTreePosition.getOffset();
                    JViewport viewport = mainScrollPane.getViewport();
                    Rectangle rect = viewport.getViewRect();
                    rect.y = scrollY;
                    if (!rect.isEmpty()) {
                        JComponent view = (JComponent) viewport.getView();
                        if (delayScrollWithMarkingDirtyRegion) {
                            RepaintManager.currentManager(viewport).addDirtyRegion(
                                    view,
                                    rect.x, rect.x, rect.width, rect.height);
                        }
                        ignoreScrollAdjustment = true;
                        try {
                            view.scrollRectToVisible(
                                    rect);
                        } finally {
                            ignoreScrollAdjustment = false;
                        }
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: ZoomManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    Scene scene = manager.getScene();
    JScrollPane pane = (JScrollPane) SwingUtilities.getAncestorOfClass(
            JScrollPane.class, scene.getView());
    if (pane == null) {
        // Unlikely, but we cannot assume it exists.
        return;
    }
    JViewport viewport = pane.getViewport();
    Rectangle visRect = viewport.getViewRect();
    Rectangle compRect = scene.getPreferredBounds();
    int zoom = visRect.width * 100 / compRect.width;
    manager.setZoom(zoom);
}
 
Example 15
Source File: ExtendedJTable.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
public void packColumn() {
	JTableHeader header = getTableHeader();
	if (header != null) {
		int col = getSelectedColumn();
		if (col >= 0) {
			TableColumn tableColumn = header.getColumnModel().getColumn(col);

			if (tableColumn != null) {
				int width = (int) header.getDefaultRenderer()
						.getTableCellRendererComponent(this, tableColumn.getIdentifier(), false, false, -1, col)
						.getPreferredSize().getWidth();

				int firstRow = 0;
				int lastRow = getRowCount();

				ExtendedJScrollPane scrollPane = getExtendedScrollPane();
				if (scrollPane != null) {
					JViewport viewport = scrollPane.getViewport();
					Rectangle viewRect = viewport.getViewRect();
					if (viewport.getHeight() < getHeight()) {
						firstRow = rowAtPoint(new Point(0, viewRect.y));
						firstRow = Math.max(0, firstRow);
						lastRow = rowAtPoint(new Point(0, viewRect.y + viewRect.height - 1));
						lastRow = Math.min(lastRow, getRowCount());
					}
				}

				for (int row = firstRow; row < lastRow; row++) {
					int preferedWidth = (int) getCellRenderer(row, col)
							.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col)
							.getPreferredSize().getWidth();
					width = Math.max(width, preferedWidth);
				}

				header.setResizingColumn(tableColumn); // this line is very important

				tableColumn.setWidth(width + getIntercellSpacing().width);
			}
		}
	}
}
 
Example 16
Source File: ExtendedJTableColumnFitMouseListener.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
	if (e.getClickCount() == 2) {
		JTableHeader header = (JTableHeader) e.getSource();
		TableColumn tableColumn = getResizingColumn(header, e.getPoint());

		if (tableColumn == null) {
			return;
		}

		JTable table = header.getTable();

		if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
			if (table instanceof ExtendedJTable) {
				((ExtendedJTable) table).pack();
				e.consume();
			}
		} else {
			int col = header.getColumnModel().getColumnIndex(tableColumn.getIdentifier());
			int width = (int) header.getDefaultRenderer()
					.getTableCellRendererComponent(table, tableColumn.getIdentifier(), false, false, -1, col)
					.getPreferredSize().getWidth();

			int firstRow = 0;
			int lastRow = table.getRowCount();

			if (table instanceof ExtendedJTable) {
				ExtendedJScrollPane scrollPane = ((ExtendedJTable) table).getExtendedScrollPane();
				if (scrollPane != null) {
					JViewport viewport = scrollPane.getViewport();
					Rectangle viewRect = viewport.getViewRect();
					if (viewport.getHeight() < table.getHeight()) {
						firstRow = table.rowAtPoint(new Point(0, viewRect.y));
						firstRow = Math.max(0, firstRow);
						lastRow = table.rowAtPoint(new Point(0, viewRect.y + viewRect.height - 1));
						lastRow = Math.min(lastRow, table.getRowCount());
					}
				}
			}

			for (int row = firstRow; row < lastRow; row++) {
				int preferedWidth = (int) table.getCellRenderer(row, col)
						.getTableCellRendererComponent(table, table.getValueAt(row, col), false, false, row, col)
						.getPreferredSize().getWidth();
				width = Math.max(width, preferedWidth);
			}
			header.setResizingColumn(tableColumn); // this line is very important
			tableColumn.setWidth(width + table.getIntercellSpacing().width);

			e.consume();
		}
	}
}
 
Example 17
Source File: MMDEditor.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void onScaledByMouse(
        @Nonnull final MindMapPanel source,
        @Nonnull final Point mousePoint,
        final double oldScale,
        final double newScale,
        @Nonnull final Dimension oldSize,
        @Nonnull final Dimension newSize
) {
  if (Double.compare(oldScale, newScale) != 0) {
    this.scrollPane.setViewportView(source);
    final JViewport viewport = this.scrollPane.getViewport();

    final Rectangle viewPos = viewport.getViewRect();

    final Dimension size = source.getSize();
    final Dimension extentSize = viewport.getExtentSize();

    if (extentSize.width < size.width || extentSize.height < size.height) {

      final int dx = mousePoint.x - viewPos.x;
      final int dy = mousePoint.y - viewPos.y;

      final double scaleX = newSize.getWidth() / oldSize.getWidth();
      final double scaleY = newSize.getHeight() / oldSize.getHeight();

      final int newMouseX = (int) (Math.round(mousePoint.x * scaleX));
      final int newMouseY = (int) (Math.round(mousePoint.y * scaleY));

      viewPos.x = Math.max(0, newMouseX - dx);
      viewPos.y = Math.max(0, newMouseY - dy);
      viewport.setView(source);

      source.scrollRectToVisible(viewPos);
    } else {
      viewPos.x = 0;
      viewPos.y = 0;
      source.scrollRectToVisible(viewPos);
    }
    this.scrollPane.repaint();
  }
}
 
Example 18
Source File: MindMapDocumentEditor.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void onScaledByMouse(
    @Nonnull final MindMapPanel source,
    @Nonnull final Point mousePoint,
    final double oldScale,
    final double newScale,
    @Nonnull final Dimension oldSize,
    @Nonnull final Dimension newSize
) {
  if (Double.compare(oldScale, newScale) != 0) {
    this.mainScrollPane.setViewportView(source);

    final JViewport viewport = this.mainScrollPane.getViewport();

    final Rectangle viewPos = viewport.getViewRect();

    final Dimension size = source.getSize();
    final Dimension extentSize = viewport.getExtentSize();

    if (extentSize.width < size.width || extentSize.height < size.height) {

      final int dx = mousePoint.x - viewPos.x;
      final int dy = mousePoint.y - viewPos.y;

      final double scaleX = newSize.getWidth() / oldSize.getWidth();
      final double scaleY = newSize.getHeight() / oldSize.getHeight();

      final int newMouseX = (int) (Math.round(mousePoint.x * scaleX));
      final int newMouseY = (int) (Math.round(mousePoint.y * scaleY));

      viewPos.x = Math.max(0, newMouseX - dx);
      viewPos.y = Math.max(0, newMouseY - dy);

      source.scrollRectToVisible(viewPos);
    } else {
      viewPos.x = 0;
      viewPos.y = 0;
      source.scrollRectToVisible(viewPos);
    }

    this.mainScrollPane.repaint();
  }
}
 
Example 19
Source File: MMDGraphEditor.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void onScaledByMouse(
    @Nonnull final MindMapPanel source,
    @Nonnull final Point mousePoint,
    final double oldScale,
    final double newScale,
    @Nonnull final Dimension oldSize,
    @Nonnull final Dimension newSize
) {
  if (Double.compare(oldScale, newScale) != 0) {
    final JViewport viewport = this.mainScrollPane.getViewport();

    final Rectangle viewPos = viewport.getViewRect();

    final Dimension size = source.getSize();
    final Dimension extentSize = viewport.getExtentSize();

    if (extentSize.width < size.width || extentSize.height < size.height) {

      final int dx = mousePoint.x - viewPos.x;
      final int dy = mousePoint.y - viewPos.y;

      final double scaleX = newSize.getWidth() / oldSize.getWidth();
      final double scaleY = newSize.getHeight() / oldSize.getHeight();

      final int newMouseX = (int) (Math.round(mousePoint.x * scaleX));
      final int newMouseY = (int) (Math.round(mousePoint.y * scaleY));

      viewPos.x = Math.max(0, newMouseX - dx);
      viewPos.y = Math.max(0, newMouseY - dy);
      viewport.setView(source);

      source.scrollRectToVisible(viewPos);
    } else {
      viewPos.x = 0;
      viewPos.y = 0;
      source.scrollRectToVisible(viewPos);
    }

    this.mainScrollPane.revalidate();
    this.mainScrollPane.repaint();
  }
}