/** * Stores the position and size of the bouncing box that would be painted for the current * animation index in <code>r</code> and returns <code>r</code>. Subclasses that add to the * painting performed in this class's implementation of <code>paintIndeterminate</code> -- to draw * an outline around the bouncing box, for example -- can use this method to get the location of * the bouncing box that was just painted. By overriding this method, you have complete control * over the size and position of the bouncing box, without having to reimplement <code> * paintIndeterminate</code>. * * @param r the Rectangle instance to be modified; may be <code>null</code> * @return <code>null</code> if no box should be drawn; otherwise, returns the passed-in rectangle * (if non-null) or a new rectangle * @see #setAnimationIndex * @since 1.4 */ protected Rectangle getBox(Rectangle r) { int currentFrame = getAnimationIndex(); int middleFrame = numFrames / 2; if (sizeChanged() || delta == 0.0 || maxPosition == 0.0) { updateSizes(); } r = getGenericBox(r); if (r == null) { return null; } if (middleFrame <= 0) { return null; } // assert currentFrame >= 0 && currentFrame < numFrames if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { if (currentFrame < middleFrame) { r.x = componentInnards.x + (int) Math.round(delta * (double) currentFrame); } else { r.x = maxPosition - (int) Math.round(delta * (currentFrame - middleFrame)); } } else { // VERTICAL indeterminate progress bar if (currentFrame < middleFrame) { r.y = componentInnards.y + (int) Math.round(delta * currentFrame); } else { r.y = maxPosition - (int) Math.round(delta * (currentFrame - middleFrame)); } } return r; }
/** * Paints the word-wrapped text. * * @param g The graphics context in which to paint. * @param a The shape (usually a rectangle) in which to paint. */ public void paint(Graphics g, Shape a) { Rectangle alloc = (a instanceof Rectangle) ? (Rectangle) a : a.getBounds(); tabBase = alloc.x; Graphics2D g2d = (Graphics2D) g; host = (RSyntaxTextArea) getContainer(); int ascent = host.getMaxAscent(); int fontHeight = host.getLineHeight(); FoldManager fm = host.getFoldManager(); TokenPainter painter = host.getTokenPainter(); Element root = getElement(); // Whether token styles should always be painted, even in selections int selStart = host.getSelectionStart(); int selEnd = host.getSelectionEnd(); boolean useSelectedTextColor = host.getUseSelectedTextColor(); int n = getViewCount(); // Number of lines. int x = alloc.x + getLeftInset(); tempRect.y = alloc.y + getTopInset(); Rectangle clip = g.getClipBounds(); for (int i = 0; i < n; i++) { tempRect.x = x + getOffset(X_AXIS, i); // tempRect.y = y + getOffset(Y_AXIS, i); tempRect.width = getSpan(X_AXIS, i); tempRect.height = getSpan(Y_AXIS, i); // System.err.println("For line " + i + ": tempRect==" + tempRect); if (tempRect.intersects(clip)) { Element lineElement = root.getElement(i); int startOffset = lineElement.getStartOffset(); int endOffset = lineElement.getEndOffset() - 1; // Why always "-1"? View view = getView(i); if (!useSelectedTextColor || selStart == selEnd || (startOffset >= selEnd || endOffset < selStart)) { drawView(painter, g2d, alloc, view, fontHeight, tempRect.y + ascent); } else { // System.out.println("Drawing line with selection: " + i); drawViewWithSelection( painter, g2d, alloc, view, fontHeight, tempRect.y + ascent, selStart, selEnd); } } tempRect.y += tempRect.height; Fold possibleFold = fm.getFoldForLine(i); if (possibleFold != null && possibleFold.isCollapsed()) { i += possibleFold.getCollapsedLineCount(); // Visible indicator of collapsed lines Color c = RSyntaxUtilities.getFoldedLineBottomColor(host); if (c != null) { g.setColor(c); g.drawLine(x, tempRect.y - 1, alloc.width, tempRect.y - 1); } } } }
/** * Sets the allocation rectangle for a given line's view, but sets the y value to the passed-in * value. This should be used instead of {@link #childAllocation(int, Rectangle)} since it allows * you to account for hidden lines in collapsed fold regions. * * @param line * @param y * @param alloc */ private void childAllocation2(int line, int y, Rectangle alloc) { alloc.x += getOffset(X_AXIS, line); alloc.y += y; alloc.width = getSpan(X_AXIS, line); alloc.height = getSpan(Y_AXIS, line); // FIXME: This is required due to a bug that I can't track down. The // top margin is being added twice somewhere in wrapped views, so we // have to adjust for it here. alloc.y -= host.getMargin().top; }
public TaskbarPositionTest() { super("Use CTRL-down to show a JPopupMenu"); setContentPane(panel = createContentPane()); setJMenuBar(createMenuBar("1 - First Menu", true)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // CTRL-down will show the popup. panel .getInputMap() .put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK), "OPEN_POPUP"); panel.getActionMap().put("OPEN_POPUP", new PopupHandler()); pack(); Toolkit toolkit = Toolkit.getDefaultToolkit(); fullScreenBounds = new Rectangle(new Point(), toolkit.getScreenSize()); screenBounds = new Rectangle(new Point(), toolkit.getScreenSize()); // Place the frame near the bottom. This is a pretty wild guess. this.setLocation(0, (int) screenBounds.getHeight() - 2 * this.getHeight()); // Reduce the screen bounds by the insets. GraphicsConfiguration gc = this.getGraphicsConfiguration(); if (gc != null) { Insets screenInsets = toolkit.getScreenInsets(gc); screenBounds = gc.getBounds(); screenBounds.width -= (screenInsets.left + screenInsets.right); screenBounds.height -= (screenInsets.top + screenInsets.bottom); screenBounds.x += screenInsets.left; screenBounds.y += screenInsets.top; } setVisible(true); }
/** Assumes that the component innards, max position, etc. are up-to-date. */ private Rectangle getGenericBox(Rectangle r) { if (r == null) { r = new Rectangle(); } if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { r.width = getBoxLength(componentInnards.width, componentInnards.height); if (r.width < 0) { r = null; } else { r.height = componentInnards.height; r.y = componentInnards.y; } // end of HORIZONTAL } else { // VERTICAL progress bar r.height = getBoxLength(componentInnards.height, componentInnards.width); if (r.height < 0) { r = null; } else { r.width = componentInnards.width; r.x = componentInnards.x; } } // end of VERTICAL return r; }
/** * Provides a mapping from the document model coordinate space to the coordinate space of the * view mapped to it. * * @param pos the position to convert * @param a the allocated region to render into * @return the bounding box of the given position is returned * @exception BadLocationException if the given position does not represent a valid location in * the associated document. */ @Override public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { // System.err.println("--- begin modelToView ---"); Rectangle alloc = a.getBounds(); RSyntaxTextArea textArea = (RSyntaxTextArea) getContainer(); alloc.height = textArea.getLineHeight(); // metrics.getHeight(); alloc.width = 1; int p0 = getStartOffset(); int p1 = getEndOffset(); int testP = (b == Position.Bias.Forward) ? pos : Math.max(p0, pos - 1); // Get the token list for this line so we don't have to keep // recomputing it if this logical line spans multiple physical // lines. RSyntaxDocument doc = (RSyntaxDocument) getDocument(); Element map = doc.getDefaultRootElement(); int line = map.getElementIndex(p0); Token tokenList = doc.getTokenListForLine(line); float x0 = alloc.x; // 0; while (p0 < p1) { TokenSubList subList = TokenUtils.getSubTokenList( tokenList, p0, WrappedSyntaxView.this, textArea, x0, lineCountTempToken); x0 = subList != null ? subList.x : x0; tokenList = subList != null ? subList.tokenList : null; int p = calculateBreakPosition(p0, tokenList, x0); if ((pos >= p0) && (testP < p)) { // pos < p)) { // it's in this line alloc = RSyntaxUtilities.getLineWidthUpTo( textArea, s, p0, pos, WrappedSyntaxView.this, alloc, alloc.x); // System.err.println("--- end modelToView ---"); return alloc; } // if (p == p1 && pos == p1) { if (p == p1 - 1 && pos == p1 - 1) { // Wants end. if (pos > p0) { alloc = RSyntaxUtilities.getLineWidthUpTo( textArea, s, p0, pos, WrappedSyntaxView.this, alloc, alloc.x); } // System.err.println("--- end modelToView ---"); return alloc; } p0 = (p == p0) ? p1 : p; // System.err.println("... ... Incrementing y"); alloc.y += alloc.height; } throw new BadLocationException(null, pos); }
/** * Returns an point which has been adjusted to take into account of the desktop bounds, taskbar * and multi-monitor configuration. * * <p>This adustment may be cancelled by invoking the application with * -Djavax.swing.adjustPopupLocationToFit=false */ Point adjustPopupLocationToFitScreen(int xPosition, int yPosition) { Point popupLocation = new Point(xPosition, yPosition); if (popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) { return popupLocation; } // Get screen bounds Rectangle scrBounds; GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation); Toolkit toolkit = Toolkit.getDefaultToolkit(); if (gc != null) { // If we have GraphicsConfiguration use it to get screen bounds scrBounds = gc.getBounds(); } else { // If we don't have GraphicsConfiguration use primary screen scrBounds = new Rectangle(toolkit.getScreenSize()); } // Calculate the screen size that popup should fit Dimension popupSize = JPopupMenu.this.getPreferredSize(); long popupRightX = (long) popupLocation.x + (long) popupSize.width; long popupBottomY = (long) popupLocation.y + (long) popupSize.height; int scrWidth = scrBounds.width; int scrHeight = scrBounds.height; if (!canPopupOverlapTaskBar()) { // Insets include the task bar. Take them into account. Insets scrInsets = toolkit.getScreenInsets(gc); scrBounds.x += scrInsets.left; scrBounds.y += scrInsets.top; scrWidth -= scrInsets.left + scrInsets.right; scrHeight -= scrInsets.top + scrInsets.bottom; } int scrRightX = scrBounds.x + scrWidth; int scrBottomY = scrBounds.y + scrHeight; // Ensure that popup menu fits the screen if (popupRightX > (long) scrRightX) { popupLocation.x = scrRightX - popupSize.width; if (popupLocation.x < scrBounds.x) { popupLocation.x = scrBounds.x; } } if (popupBottomY > (long) scrBottomY) { popupLocation.y = scrBottomY - popupSize.height; if (popupLocation.y < scrBounds.y) { popupLocation.y = scrBounds.y; } } return popupLocation; }
/** [Internal] */ public void paintComponent(Graphics g) { boolean opq = true; if (theOpaque != null) opq = theOpaque; super.setOpaque(opq); // if(theBackground!=null)super.setBackground(theBackground); super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; Rectangle rt = getBounds(); rt.x = 0; rt.y = 0; doBuffer(g2, opq, rt); chkFPS(); }
/** * Paints the word-wrapped text. * * @param g The graphics context in which to paint. * @param a The shape (usually a rectangle) in which to paint. */ public void paint(Graphics g, Shape a) { Rectangle alloc = (a instanceof Rectangle) ? (Rectangle) a : a.getBounds(); tabBase = alloc.x; Graphics2D g2d = (Graphics2D) g; host = (RSyntaxTextArea) getContainer(); int ascent = host.getMaxAscent(); int fontHeight = host.getLineHeight(); FoldManager fm = host.getFoldManager(); int n = getViewCount(); // Number of lines. int x = alloc.x + getLeftInset(); tempRect.y = alloc.y + getTopInset(); Rectangle clip = g.getClipBounds(); for (int i = 0; i < n; i++) { tempRect.x = x + getOffset(X_AXIS, i); // tempRect.y = y + getOffset(Y_AXIS, i); tempRect.width = getSpan(X_AXIS, i); tempRect.height = getSpan(Y_AXIS, i); // System.err.println("For line " + i + ": tempRect==" + tempRect); if (tempRect.intersects(clip)) { View view = getView(i); drawView(g2d, alloc, view, fontHeight, tempRect.y + ascent); } tempRect.y += tempRect.height; Fold possibleFold = fm.getFoldForLine(i); if (possibleFold != null && possibleFold.isCollapsed()) { i += possibleFold.getCollapsedLineCount(); // Visible indicator of collapsed lines Color c = RSyntaxUtilities.getFoldedLineBottomColor(host); if (c != null) { g.setColor(c); g.drawLine(x, tempRect.y - 1, alloc.width, tempRect.y - 1); } } } }
private void adjust( Rectangle bounds, Dimension min, int deltaX, int deltaY, int deltaWidth, int deltaHeight) { bounds.x += deltaX; bounds.y += deltaY; bounds.width += deltaWidth; bounds.height += deltaHeight; if (min != null) { if (bounds.width < min.width) { int correction = min.width - bounds.width; if (deltaX != 0) { bounds.x -= correction; } bounds.width = min.width; } if (bounds.height < min.height) { int correction = min.height - bounds.height; if (deltaY != 0) { bounds.y -= correction; } bounds.height = min.height; } } }
public void actionPerformed(ActionEvent e) { JGrid grid = (JGrid) e.getSource(); if (toLimit) { if (vertically) { int rowCount = grid.getRowCount(); this.dx = 0; this.dy = forwards ? rowCount : -rowCount; } else { int colCount = grid.getColumnCount(); this.dx = forwards ? colCount : -colCount; this.dy = 0; } } else { if (!(grid.getParent().getParent() instanceof JScrollPane)) { return; } Dimension delta = grid.getParent().getSize(); SelectionModel sm = grid.getSelectionModel(); int start = 0; if (vertically) { start = (extend) ? sm.getLeadRow() : sm.getAnchorRow(); } else { start = (extend) ? sm.getLeadColumn() : sm.getAnchorColumn(); } if (vertically) { Rectangle r = grid.getCellBounds(start, 0); r.y += forwards ? delta.height : -delta.height; this.dx = 0; int newRow = grid.rowAtPoint(r.getLocation()); if (newRow == -1 && forwards) { newRow = grid.getRowCount(); } this.dy = newRow - start; } else { Rectangle r = grid.getCellBounds(0, start); r.x += forwards ? delta.width : -delta.width; int newColumn = grid.columnAtPoint(r.getLocation()); if (newColumn == -1 && forwards) { newColumn = grid.getColumnCount(); } this.dx = newColumn - start; this.dy = 0; } } super.actionPerformed(e); }
private void paintBorder(Graphics g, Rectangle cellRect, int row, int column) { if (grid.getSpanModel().isCellSpan(row, column)) { CellSpan span = grid.getSpanModel().getSpanOver(row, column); row = span.getRow(); column = span.getColumn(); } // Paint border javax.swing.border.Border border = grid.getStyleModel().getCellStyle(row, column).getBorder(); Insets borderInsets = border.getBorderInsets(grid); Rectangle borderRect = new Rectangle(cellRect); int topOffset = (borderInsets.top >> 1); int leftOffset = (borderInsets.left >> 1); int bottomOffset = (borderInsets.bottom / 3) + topOffset; int rightOffset = (borderInsets.right / 3) + leftOffset; borderRect.x -= leftOffset; borderRect.y -= topOffset; borderRect.width += rightOffset; borderRect.height += bottomOffset; border.paintBorder(grid, g, borderRect.x, borderRect.y, borderRect.width, borderRect.height); }
/** * Provides a mapping from the view coordinate space to the logical coordinate space of the * model. * * @param fx the X coordinate * @param fy the Y coordinate * @param a the allocated region to render into * @return the location within the model that best represents the given point in the view * @see View#viewToModel */ @Override public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) { // PENDING(prinz) implement bias properly bias[0] = Position.Bias.Forward; Rectangle alloc = (Rectangle) a; RSyntaxDocument doc = (RSyntaxDocument) getDocument(); int x = (int) fx; int y = (int) fy; if (y < alloc.y) { // above the area covered by this icon, so the the position // is assumed to be the start of the coverage for this view. return getStartOffset(); } else if (y > alloc.y + alloc.height) { // below the area covered by this icon, so the the position // is assumed to be the end of the coverage for this view. return getEndOffset() - 1; } else { // positioned within the coverage of this view vertically, // so we figure out which line the point corresponds to. // if the line is greater than the number of lines // contained, then simply use the last line as it represents // the last possible place we can position to. RSyntaxTextArea textArea = (RSyntaxTextArea) getContainer(); alloc.height = textArea.getLineHeight(); int p1 = getEndOffset(); // Get the token list for this line so we don't have to keep // recomputing it if this logical line spans multiple // physical lines. Element map = doc.getDefaultRootElement(); int p0 = getStartOffset(); int line = map.getElementIndex(p0); Token tlist = doc.getTokenListForLine(line); // Look at each physical line-chunk of this logical line. while (p0 < p1) { // We can always use alloc.x since we always break // lines so they start at the beginning of a physical // line. TokenSubList subList = TokenUtils.getSubTokenList( tlist, p0, WrappedSyntaxView.this, textArea, alloc.x, lineCountTempToken); tlist = subList != null ? subList.tokenList : null; int p = calculateBreakPosition(p0, tlist, alloc.x); // If desired view position is in this physical chunk. if ((y >= alloc.y) && (y < (alloc.y + alloc.height))) { // Point is to the left of the line if (x < alloc.x) { return p0; } // Point is to the right of the line else if (x > alloc.x + alloc.width) { return p - 1; } // Point is in this physical line! else { // Start at alloc.x since this chunk starts // at the beginning of a physical line. int n = tlist.getListOffset(textArea, WrappedSyntaxView.this, alloc.x, x); // NOTE: We needed to add the max() with // p0 as getTokenListForLine returns -1 // for empty lines (just a null token). // How did this work before? // FIXME: Have null tokens have their // offset but a -1 length. return Math.max(Math.min(n, p1 - 1), p0); } // End of else. } // End of if ((y>=alloc.y) && ... p0 = (p == p0) ? p1 : p; alloc.y += alloc.height; } // End of while (p0<p1). return getEndOffset() - 1; } // End of else. }
/** * Adjusts the allocation given to the view to be a suitable allocation for a text field. If the * view has been allocated more than the preferred span vertically, the allocation is changed to * be centered vertically. Horizontally the view is adjusted according to the horizontal alignment * property set on the associated JTextField (if that is the type of the hosting component). * * @param a the allocation given to the view, which may need to be adjusted. * @return the allocation that the superclass should use. */ protected Shape adjustAllocation(Shape a) { if (a != null) { Rectangle bounds = a.getBounds(); int vspan = (int) getPreferredSpan(Y_AXIS); int hspan = (int) getPreferredSpan(X_AXIS); if (bounds.height != vspan) { int slop = bounds.height - vspan; bounds.y += slop / 2; bounds.height -= slop; } // horizontal adjustments Component c = getContainer(); if (c instanceof JTextField) { JTextField field = (JTextField) c; BoundedRangeModel vis = field.getHorizontalVisibility(); int max = Math.max(hspan, bounds.width); int value = vis.getValue(); int extent = Math.min(max, bounds.width - 1); if ((value + extent) > max) { value = max - extent; } vis.setRangeProperties(value, extent, vis.getMinimum(), max, false); if (hspan < bounds.width) { // horizontally align the interior int slop = bounds.width - 1 - hspan; int align = ((JTextField) c).getHorizontalAlignment(); if (Utilities.isLeftToRight(c)) { if (align == LEADING) { align = LEFT; } else if (align == TRAILING) { align = RIGHT; } } else { if (align == LEADING) { align = RIGHT; } else if (align == TRAILING) { align = LEFT; } } switch (align) { case SwingConstants.CENTER: bounds.x += slop / 2; bounds.width -= slop; break; case SwingConstants.RIGHT: bounds.x += slop; bounds.width -= slop; break; } } else { // adjust the allocation to match the bounded range. bounds.width = hspan; bounds.x -= vis.getValue(); } } return bounds; } return null; }
/** * Sets the allocation rectangle for a given line's view, but sets the y value to the passed-in * value. This should be used instead of {@link #childAllocation(int, Rectangle)} since it allows * you to account for hidden lines in collapsed fold regions. * * @param line * @param y * @param alloc */ private void childAllocation2(int line, int y, Rectangle alloc) { alloc.x += getOffset(X_AXIS, line); alloc.y += y; alloc.width = getSpan(X_AXIS, line); alloc.height = getSpan(Y_AXIS, line); }