/** * Update the visibility of the buttons Only show them if the Viewport is too small for the * content. */ public void showOrHideButtons() { boolean needButtons = vp.getViewSize().height > vp.getViewRect().height || vp.getViewSize().width > vp.getViewRect().width; for (JButton b : buttons) { b.setVisible(needButtons); } }
@Override public void update(Observable o, Object arg) { SortedSet<Feature> fs = model.selectionModel().getFeatureSelection(); if (fs.size() == 1) { if (fs.first().type() == listModel.getType()) { int row = listModel.getRow(fs.first()); // getSelectionModel().setSelectionInterval(row, row); if (!(getParent() instanceof JViewport)) { return; } JViewport viewport = (JViewport) getParent(); // This rectangle is relative to the table where the // northwest corner of cell (0,0) is always (0,0). Rectangle rect = getCellRect(row, 0, true); // The location of the view relative to the table Rectangle viewRect = viewport.getViewRect(); int topVisible = viewport.getViewRect().y; int bottomVisible = viewport.getViewRect().height + topVisible; /* When the cell is visible, don't do anything */ if (rect.y > topVisible && rect.y + rect.height < bottomVisible) { return; } // 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); } } }
public synchronized void scroll() { int direction = scrollDirection; if (component == null || direction == NO_SCROLL) return; Rectangle viewRect = vp.getViewRect(); int deltaX = 0; int deltaY = 0; if (direction < LEFT_DIRECTION) { deltaY = viewRect.height * 2 / 7; } else { deltaX = viewRect.width * 2 / 7; } switch (direction) { case UP_DIRECTION: deltaY *= -1; break; case LEFT_DIRECTION: deltaX *= -1; break; } scroll(deltaX, deltaY); }
private static void check() { try { JViewport vp = null; for (Component c : tabbedPane.getComponents()) { if (c instanceof JViewport) { vp = (JViewport) c; break; } } JComponent v = (JComponent) vp.getView(); Rectangle vr = vp.getViewRect(); Dimension vs = v.getSize(); // The tab view must be scrolled to the end so that the last tab is visible if (vs.width != (vr.x + vr.width)) { throw new RuntimeException( "tabScroller.tabPanel view is positioned incorrectly: " + vs.width + " vs " + (vr.x + vr.width)); } } catch (Exception e) { exception = e; } }
public void scrollToLine(FilePanel fp, int line) { JScrollPane scrollPane; FilePanel fp2; BufferDocumentIF bd; JTextComponent editor; JViewport viewport; Rectangle rect; int offset; Point p; Rectangle viewRect; Dimension viewSize; Dimension extentSize; int x; fp2 = fp == filePanelLeft ? filePanelRight : filePanelLeft; bd = fp.getBufferDocument(); if (bd == null) { return; } offset = bd.getOffsetForLine(line); if (offset < 0) { return; } viewport = fp.getScrollPane().getViewport(); editor = fp.getEditor(); try { rect = editor.modelToView(offset); if (rect == null) { return; } p = rect.getLocation(); p.y -= getHeightOffset(fp); p.y += getCorrectionOffset(fp2); // Do not allow scrolling before the begin. if (p.y < 0) { p.y = 0; } // Do not allow scrolling after the end. viewSize = viewport.getViewSize(); viewRect = viewport.getViewRect(); extentSize = viewport.getExtentSize(); if (p.y > viewSize.height - extentSize.height) { p.y = viewSize.height - extentSize.height; } p.x = viewRect.x; viewport.setViewPosition(p); } catch (Exception ex) { ex.printStackTrace(); } }
private void scrollToCenterPath(final GeneralPath geoBoundaryPath, final JViewport viewport) { final GeneralPath pixelPath = ProductUtils.convertToPixelPath(geoBoundaryPath, wmPainter.getGeoCoding()); final Rectangle viewRect = viewport.getViewRect(); final Rectangle pathBounds = pixelPath.getBounds(); setCenter(viewRect, new Point((int) pathBounds.getCenterX(), (int) pathBounds.getCenterY())); final Dimension bounds = new Dimension(scaledImage.getWidth(null), scaledImage.getHeight(null)); ensureRectIsInBounds(viewRect, bounds); viewport.scrollRectToVisible(viewRect); }
@Override protected int getMaxAvailablePageWidth( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Insets insets = list.getInsets(); // OWLFrameList.ITEM_BORDER.getBorderInsets(); int componentWidth = list.getWidth(); JViewport vp = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, list); if (vp != null) { componentWidth = vp.getViewRect().width; } return componentWidth - list.getInsets().left - list.getInsets().right - insets.left + insets.right - 20; }
/** * Moves the current location by a given amount. * * @param dr the number of rows by which to move the location * @param dc the number of columns by which to move the location */ public void moveLocation(int dr, int dc) { Location newLocation = new Location(currentLocation.getRow() + dr, currentLocation.getCol() + dc); if (!grid.isValid(newLocation)) return; currentLocation = newLocation; JViewport viewPort = getEnclosingViewport(); if (isPannableUnbounded()) { if (originRow > currentLocation.getRow()) originRow = currentLocation.getRow(); if (originCol > currentLocation.getCol()) originCol = currentLocation.getCol(); Dimension dim = viewPort.getSize(); int rows = dim.height / (cellSize + 1); int cols = dim.width / (cellSize + 1); if (originRow + rows - 1 < currentLocation.getRow()) originRow = currentLocation.getRow() - rows + 1; if (originCol + rows - 1 < currentLocation.getCol()) originCol = currentLocation.getCol() - cols + 1; } else if (viewPort != null) { int dx = 0; int dy = 0; Point p = pointForLocation(currentLocation); Rectangle locRect = new Rectangle(p.x - cellSize / 2, p.y - cellSize / 2, cellSize + 1, cellSize + 1); Rectangle viewRect = viewPort.getViewRect(); if (!viewRect.contains(locRect)) { while (locRect.x < viewRect.x + dx) dx -= cellSize + 1; while (locRect.y < viewRect.y + dy) dy -= cellSize + 1; while (locRect.getMaxX() > viewRect.getMaxX() + dx) dx += cellSize + 1; while (locRect.getMaxY() > viewRect.getMaxY() + dy) dy += cellSize + 1; Point pt = viewPort.getViewPosition(); pt.x += dx; pt.y += dy; viewPort.setViewPosition(pt); } } repaint(); showTip(getToolTipText(currentLocation), pointForLocation(currentLocation)); }
public synchronized void scroll(int deltaX, int deltaY) { if (component == null) return; Dimension compSize = component.getSize(); Rectangle viewRect = vp.getViewRect(); int newX = viewRect.x + deltaX; int newY = viewRect.y + deltaY; if (newY < 0) { newY = 0; } if (newY > compSize.height - viewRect.height) { newY = compSize.height - viewRect.height; } if (newX < 0) { newX = 0; } if (newX > compSize.width - viewRect.width) { newX = compSize.width - viewRect.width; } vp.setViewPosition(new Point(newX, newY)); }
public static void ScrollToLine(JScrollPane scrollPane, JTable table) { Rectangle vr = table.getVisibleRect(); int first = table.rowAtPoint(vr.getLocation()); Rectangle cellRect = table.getCellRect(first + 1, 0, true); JViewport viewport = scrollPane.getViewport(); Rectangle rr = viewport.getViewRect(); int shift = cellRect.y - rr.y; // String tt; // tt = String.format("%d %d %d",cellRect.y, rr.y, shift); for (int i = 0; i < shift; i++) { int cur = scrollPane.getVerticalScrollBar().getValue(); scrollPane.getVerticalScrollBar().setValue(cur + 1); try { Thread.sleep(45); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } // JOptionPane.showMessageDialog(null, tt); // viewport.setViewPosition(cellRect.getLocation()); }
/** * Method to check that the current position is in the viewing area and if not scroll to center * the current position if possible */ public void checkScroll() { // get the x and y position in pixels int xPos = (int) (colIndex * zoomFactor); int yPos = (int) (rowIndex * zoomFactor); // only do this if the image is larger than normal if (zoomFactor > 1) { // get the rectangle that defines the current view JViewport viewport = scrollPane.getViewport(); Rectangle rect = viewport.getViewRect(); int rectMinX = (int) rect.getX(); int rectWidth = (int) rect.getWidth(); int rectMaxX = rectMinX + rectWidth - 1; int rectMinY = (int) rect.getY(); int rectHeight = (int) rect.getHeight(); int rectMaxY = rectMinY + rectHeight - 1; // get the maximum possible x and y index int macolIndexX = (int) (picture.getWidth() * zoomFactor) - rectWidth - 1; int macolIndexY = (int) (picture.getHeight() * zoomFactor) - rectHeight - 1; // calculate how to position the current position in the middle of the viewing // area int viewX = xPos - (int) (rectWidth / 2); int viewY = yPos - (int) (rectHeight / 2); // reposition the viewX and viewY if outside allowed values if (viewX < 0) viewX = 0; else if (viewX > macolIndexX) viewX = macolIndexX; if (viewY < 0) viewY = 0; else if (viewY > macolIndexY) viewY = macolIndexY; // move the viewport upper left point viewport.scrollRectToVisible(new Rectangle(viewX, viewY, rectWidth, rectHeight)); } }
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); }
public void layoutContainer(Container parent) { int tabPlacement = tabPane.getTabPlacement(); int tabCount = tabPane.getTabCount(); Insets insets = tabPane.getInsets(); int selectedIndex = tabPane.getSelectedIndex(); Component visibleComponent = getVisibleComponent(); calculateLayoutInfo(); if (selectedIndex < 0) { if (visibleComponent != null) { // The last tab was removed, so remove the component setVisibleComponent(null); } } else { Component selectedComponent = tabPane.getComponentAt(selectedIndex); boolean shouldChangeFocus = false; // In order to allow programs to use a single component // as the display for multiple tabs, we will not change // the visible compnent if the currently selected tab // has a null component. This is a bit dicey, as we don't // explicitly state we support this in the spec, but since // programs are now depending on this, we're making it work. // if (selectedComponent != null) { if (selectedComponent != visibleComponent && visibleComponent != null) { if (SwingUtilities.findFocusOwner(visibleComponent) != null) { shouldChangeFocus = true; } } setVisibleComponent(selectedComponent); } int tx, ty, tw, th; // tab area bounds int cx, cy, cw, ch; // content area bounds Insets contentInsets = getContentBorderInsets(tabPlacement); Rectangle bounds = tabPane.getBounds(); int numChildren = tabPane.getComponentCount(); if (numChildren > 0) { // calculate tab area bounds tw = bounds.width - insets.left - insets.right; th = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); tx = insets.left; ty = insets.top; // calculate content area bounds cx = tx + contentInsets.left; cy = ty + th + contentInsets.top; cw = bounds.width - insets.left - insets.right - contentInsets.left - contentInsets.right; ch = bounds.height - insets.top - insets.bottom - th - contentInsets.top - contentInsets.bottom; for (int i = 0; i < numChildren; i++) { Component child = tabPane.getComponent(i); if (child instanceof ScrollableTabViewport) { JViewport viewport = (JViewport) child; Rectangle viewRect = viewport.getViewRect(); int vw = tw; int vh = th; int totalTabWidth = rects[tabCount - 1].x + rects[tabCount - 1].width; if (totalTabWidth > tw) { // Need to allow space for scrollbuttons vw = Math.max(tw - 36, 36); ; if (totalTabWidth - viewRect.x <= vw) { // Scrolled to the end, so ensure the // viewport size is // such that the scroll offset aligns with a // tab vw = totalTabWidth - viewRect.x; } } child.setBounds(tx, ty, vw, vh); } else if (child instanceof ScrollableTabButton) { ScrollableTabButton scrollbutton = (ScrollableTabButton) child; Dimension bsize = scrollbutton.getPreferredSize(); int bx = 0; int by = 0; int bw = bsize.width; int bh = bsize.height; boolean visible = false; int totalTabWidth = rects[tabCount - 1].x + rects[tabCount - 1].width; if (totalTabWidth > tw) { int dir = scrollbutton.scrollsForward() ? EAST : WEST; scrollbutton.setDirection(dir); visible = true; bx = dir == EAST ? bounds.width - insets.left - bsize.width : bounds.width - insets.left - 2 * bsize.width; by = (tabPlacement == TOP ? ty + th - bsize.height : ty); } child.setVisible(visible); if (visible) { child.setBounds(bx, by, bw, bh); } } else { // All content children... child.setBounds(cx, cy, cw, ch); } } if (shouldChangeFocus) { if (!requestMyFocusForVisibleComponent()) { tabPane.requestFocus(); } } } } }
public Rectangle getViewRect() { return vp.getViewRect(); }