// Helper function to test any rectangle private static void testMove(Rectangle r) { Point ll = r.lowerLeft(); Point ur = r.upperRight(); Point c = r.center(); double wd = r.width(); double ht = r.height(); r.move(0., 0.); // Nothing should change assertTrue(r.lowerLeft().equals(ll)); assertTrue(r.upperRight().equals(ur)); assertTrue(r.center().equals(c)); assertTrue(r.width() == wd); assertTrue(r.height() == ht); double dx = 10.0; // how much to move. double dy = -10.0; r.move(dx, dy); Point newll = new Point(ll.getX() + dx, ll.getY() + dy); Point newur = new Point(ur.getX() + dx, ur.getY() + dy); Point newc = new Point(c.getX() + dx, c.getY() + dy); assertTrue(r.lowerLeft().equals(newll)); assertTrue(r.upperRight().equals(newur)); assertTrue(r.center().equals(newc)); assertTrue(r.width() == wd); // no change in size assertTrue(r.height() == ht); }
/** * Returns a rectangle describing the receiver's size and location relative to its parent. * * @return the receiver's bounding rectangle * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> * * @since 1.3 */ public Rectangle getBounds() { checkWidget(); Rectangle result = new Rectangle(0, 0, 0, 0); int index = parent.indexOf(this); if (index != -1) { int selectionIndex = parent.getSelectionIndex(); boolean selected = index == selectionIndex; Rectangle padding = parent.getItemPadding(selected); String text = getText(); if (text != null) { Point extent = Graphics.stringExtent(parent.getFont(), text); result.width = extent.x; result.height = extent.y; } Image image = getImage(); if (image != null) { Rectangle imageSize = image.getBounds(); result.width += imageSize.width + IMAGE_TEXT_SPACING; result.height = Math.max(result.height, imageSize.height); } result.width += 2 * ITEM_BORDER + padding.width; result.height += ITEM_BORDER + padding.height; if (selected) { result.height += SELECTED_ITEM_BORDER; } if (selectionIndex != -1) { if (index + 1 == selectionIndex || index - 1 == selectionIndex) { result.width -= ITEM_BORDER; } } if (isBarTop()) { if (index != selectionIndex) { result.y += SELECTED_ITEM_BORDER; } } else { result.y = parent.getBounds().height - 2 * parent.getBorderWidth() - result.height; if (index != selectionIndex) { result.y -= SELECTED_ITEM_BORDER; } } if (index > 0) { TabItem leftItem = parent.getItem(index - 1); Rectangle leftItemBounds = leftItem.getBounds(); result.x = leftItemBounds.x + leftItemBounds.width + TABS_SPACING; if (index == selectionIndex || index - 1 == selectionIndex) { result.x -= TABS_SPACING; } } } return result; }
void tableMouseDown(Event event) { if (isDisposed() || !isVisible()) return; Point pt = new Point(event.x, event.y); int lineWidth = table.getLinesVisible() ? table.getGridLineWidth() : 0; TableItem item = table.getItem(pt); if ((table.getStyle() & SWT.FULL_SELECTION) != 0) { if (item == null) return; } else { int start = item != null ? table.indexOf(item) : table.getTopIndex(); int end = table.getItemCount(); Rectangle clientRect = table.getClientArea(); for (int i = start; i < end; i++) { TableItem nextItem = table.getItem(i); Rectangle rect = nextItem.getBounds(0); if (pt.y >= rect.y && pt.y < rect.y + rect.height + lineWidth) { item = nextItem; break; } if (rect.y > clientRect.y + clientRect.height) return; } if (item == null) return; } TableColumn newColumn = null; int columnCount = table.getColumnCount(); if (columnCount == 0) { if ((table.getStyle() & SWT.FULL_SELECTION) == 0) { Rectangle rect = item.getBounds(0); rect.width += lineWidth; rect.height += lineWidth; if (!rect.contains(pt)) return; } } else { for (int i = 0; i < columnCount; i++) { Rectangle rect = item.getBounds(i); rect.width += lineWidth; rect.height += lineWidth; if (rect.contains(pt)) { newColumn = table.getColumn(i); break; } } if (newColumn == null) { if ((table.getStyle() & SWT.FULL_SELECTION) == 0) return; newColumn = table.getColumn(0); } } setRowColumn(item, newColumn, true); setFocus(); return; }
public void positionWindow(boolean resetToDefault) { String mapWindow; mapShellBounds = mapShell.getBounds(); mapWindow = prefs.get(RadarConsts.PREF_KEY_MAP_WINDOW, ""); if (mapWindow.equals("") || resetToDefault) { Rectangle dispBounds, tableBounds; dispBounds = ((Display.getCurrent()).getPrimaryMonitor()).getClientArea(); tableBounds = display.getShells()[1].getBounds(); mapShellBounds.width = dispBounds.width - tableBounds.width; mapShellBounds.height = dispBounds.height; mapShellBounds.x = dispBounds.x + tableBounds.width; mapShellBounds.y = dispBounds.y; } else { String[] tokens = mapWindow.split(","); mapShellBounds = new Rectangle( new Integer(tokens[0]), // X value new Integer(tokens[1]), // Y value new Integer(tokens[2]), // Width new Integer(tokens[3]) // Height ); } mapShell.setBounds(mapShellBounds); }
private Rectangle getDrawBounds(TableCellSWT cell) { Rectangle bounds = cell.getBounds(); bounds.height -= 12; bounds.y += 6; bounds.x += 4; bounds.width -= 4; return bounds; }
// Helper function to test any non-empty rectangle private static void testInterior(Rectangle r) { assertTrue(r.interior(r.center())); assertFalse(r.interior(r.lowerRight())); assertFalse(r.interior(r.upperLeft())); assertFalse(r.interior(r.upperLeft().offset(0, 1))); assertFalse(r.interior(r.lowerLeft().offset(0, -1))); assertFalse(r.interior(r.upperRight().offset(1, 0))); assertFalse(r.interior(r.lowerRight().offset(1, 1))); assertTrue(r.interior(r.lowerLeft().offset(r.width() / 4, r.height() / 10))); }
/** * Scrolls the help browser to the element with id <code>id</code> * * @param id the id * @return true, if an element with this id was found and scrolling was successful; false, * otherwise */ protected boolean scrollToElementWithId(String id) { Document d = help.getDocument(); if (d instanceof HTMLDocument) { HTMLDocument doc = (HTMLDocument) d; Element element = doc.getElement(id); try { Rectangle r = help.modelToView(element.getStartOffset()); if (r != null) { Rectangle vis = help.getVisibleRect(); r.height = vis.height; help.scrollRectToVisible(r); return true; } } catch (BadLocationException e) { System.err.println( tr("Warning: bad location in HTML document. Exception was: {0}", e.toString())); e.printStackTrace(); } } return false; }
// Helper function for any rectangle. // Expects the lower left coordinates (llx,lly) // and upper-right coordinates (urx, ury), // even if that's not how the rectangle is // internally represented. private static void testGeometry(Rectangle r, double llx, double lly, double urx, double ury) { // Sanity-check the inputs assertTrue(llx <= urx); assertTrue(lly <= ury); // Check the corners assertTrue(r.lowerLeft().getX() == llx); assertTrue(r.lowerLeft().getY() == lly); assertTrue(r.upperRight().getX() == urx); assertTrue(r.upperRight().getY() == ury); assertTrue(r.lowerRight().getX() == urx); assertTrue(r.lowerRight().getY() == lly); assertTrue(r.upperLeft().getX() == llx); assertTrue(r.upperLeft().getY() == ury); // Check the center assertTrue(r.center().getX() == (llx + urx) / 2.0); assertTrue(r.center().getY() == (lly + ury) / 2.0); // Check the width assertTrue(r.width() == (urx - llx)); assertTrue(r.height() == (ury - lly)); }
/** * Returns an axis-aligned bounding box of this polygon. * * <p>Note the returned Rectangle is cached in this polygon, and will be reused if this Polygon is * changed. * * @return this polygon's bounding box {@link Rectangle} */ public Rectangle getBoundingRectangle() { float[] vertices = getTransformedVertices(); float minX = vertices[0]; float minY = vertices[1]; float maxX = vertices[0]; float maxY = vertices[1]; final int numFloats = vertices.length; for (int i = 2; i < numFloats; i += 2) { minX = minX > vertices[i] ? vertices[i] : minX; minY = minY > vertices[i + 1] ? vertices[i + 1] : minY; maxX = maxX < vertices[i] ? vertices[i] : maxX; maxY = maxY < vertices[i + 1] ? vertices[i + 1] : maxY; } if (bounds == null) bounds = new Rectangle(); bounds.x = minX; bounds.y = minY; bounds.width = maxX - minX; bounds.height = maxY - minY; return bounds; }
/** * 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; }
/* * Returns true if the pointer's orientation was initialized in some dimension, * and false otherwise. */ boolean resizeRectangles(int xChange, int yChange) { if (bounds == null) return false; boolean orientationInit = false; /* * If the cursor orientation has not been set in the orientation of * this change then try to set it here. */ if (xChange < 0 && ((style & SWT.LEFT) != 0) && ((cursorOrientation & SWT.RIGHT) == 0)) { if ((cursorOrientation & SWT.LEFT) == 0) { cursorOrientation |= SWT.LEFT; orientationInit = true; } } if (xChange > 0 && ((style & SWT.RIGHT) != 0) && ((cursorOrientation & SWT.LEFT) == 0)) { if ((cursorOrientation & SWT.RIGHT) == 0) { cursorOrientation |= SWT.RIGHT; orientationInit = true; } } if (yChange < 0 && ((style & SWT.UP) != 0) && ((cursorOrientation & SWT.DOWN) == 0)) { if ((cursorOrientation & SWT.UP) == 0) { cursorOrientation |= SWT.UP; orientationInit = true; } } if (yChange > 0 && ((style & SWT.DOWN) != 0) && ((cursorOrientation & SWT.UP) == 0)) { if ((cursorOrientation & SWT.DOWN) == 0) { cursorOrientation |= SWT.DOWN; orientationInit = true; } } /* * If the bounds will flip about the x or y axis then apply the adjustment * up to the axis (ie.- where bounds width/height becomes 0), change the * cursor's orientation accordingly, and flip each Rectangle's origin (only * necessary for > 1 Rectangles) */ if ((cursorOrientation & SWT.LEFT) != 0) { if (xChange > bounds.width) { if ((style & SWT.RIGHT) == 0) return orientationInit; cursorOrientation |= SWT.RIGHT; cursorOrientation &= ~SWT.LEFT; bounds.x += bounds.width; xChange -= bounds.width; bounds.width = 0; if (proportions.length > 1) { for (int i = 0; i < proportions.length; i++) { Rectangle proportion = proportions[i]; proportion.x = 100 - proportion.x - proportion.width; } } } } else if ((cursorOrientation & SWT.RIGHT) != 0) { if (bounds.width < -xChange) { if ((style & SWT.LEFT) == 0) return orientationInit; cursorOrientation |= SWT.LEFT; cursorOrientation &= ~SWT.RIGHT; xChange += bounds.width; bounds.width = 0; if (proportions.length > 1) { for (int i = 0; i < proportions.length; i++) { Rectangle proportion = proportions[i]; proportion.x = 100 - proportion.x - proportion.width; } } } } if ((cursorOrientation & SWT.UP) != 0) { if (yChange > bounds.height) { if ((style & SWT.DOWN) == 0) return orientationInit; cursorOrientation |= SWT.DOWN; cursorOrientation &= ~SWT.UP; bounds.y += bounds.height; yChange -= bounds.height; bounds.height = 0; if (proportions.length > 1) { for (int i = 0; i < proportions.length; i++) { Rectangle proportion = proportions[i]; proportion.y = 100 - proportion.y - proportion.height; } } } } else if ((cursorOrientation & SWT.DOWN) != 0) { if (bounds.height < -yChange) { if ((style & SWT.UP) == 0) return orientationInit; cursorOrientation |= SWT.UP; cursorOrientation &= ~SWT.DOWN; yChange += bounds.height; bounds.height = 0; if (proportions.length > 1) { for (int i = 0; i < proportions.length; i++) { Rectangle proportion = proportions[i]; proportion.y = 100 - proportion.y - proportion.height; } } } } // apply the bounds adjustment if ((cursorOrientation & SWT.LEFT) != 0) { bounds.x += xChange; bounds.width -= xChange; } else if ((cursorOrientation & SWT.RIGHT) != 0) { bounds.width += xChange; } if ((cursorOrientation & SWT.UP) != 0) { bounds.y += yChange; bounds.height -= yChange; } else if ((cursorOrientation & SWT.DOWN) != 0) { bounds.height += yChange; } Rectangle[] newRects = new Rectangle[rectangles.length]; for (int i = 0; i < rectangles.length; i++) { Rectangle proportion = proportions[i]; newRects[i] = new Rectangle( proportion.x * bounds.width / 100 + bounds.x, proportion.y * bounds.height / 100 + bounds.y, proportion.width * bounds.width / 100, proportion.height * bounds.height / 100); } rectangles = newRects; return orientationInit; }
void keyDown(Event event) { if (row == null) return; switch (event.character) { case SWT.CR: notifyListeners(SWT.DefaultSelection, new Event()); return; } int rowIndex = table.indexOf(row); int columnIndex = column == null ? 0 : table.indexOf(column); switch (event.keyCode) { case SWT.ARROW_UP: setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true); break; case SWT.ARROW_DOWN: setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true); break; case SWT.ARROW_LEFT: case SWT.ARROW_RIGHT: { int columnCount = table.getColumnCount(); if (columnCount == 0) break; int[] order = table.getColumnOrder(); int index = 0; while (index < order.length) { if (order[index] == columnIndex) break; index++; } if (index == order.length) index = 0; int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) != 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT; if (event.keyCode == leadKey) { setRowColumn(rowIndex, order[Math.max(0, index - 1)], true); } else { setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true); } break; } case SWT.HOME: setRowColumn(0, columnIndex, true); break; case SWT.END: { int i = table.getItemCount() - 1; setRowColumn(i, columnIndex, true); break; } case SWT.PAGE_UP: { int index = table.getTopIndex(); if (index == rowIndex) { Rectangle rect = table.getClientArea(); TableItem item = table.getItem(index); Rectangle itemRect = item.getBounds(0); rect.height -= itemRect.y; int height = table.getItemHeight(); int page = Math.max(1, rect.height / height); index = Math.max(0, index - page + 1); } setRowColumn(index, columnIndex, true); break; } case SWT.PAGE_DOWN: { int index = table.getTopIndex(); Rectangle rect = table.getClientArea(); TableItem item = table.getItem(index); Rectangle itemRect = item.getBounds(0); rect.height -= itemRect.y; int height = table.getItemHeight(); int page = Math.max(1, rect.height / height); int end = table.getItemCount() - 1; index = Math.min(end, index + page - 1); if (index == rowIndex) { index = Math.min(end, index + page - 1); } setRowColumn(index, columnIndex, true); break; } } }
public void draw(AbstractGraphics graphics) { graphics.setColor(buttonClr); graphics.fillRect(leftButton.left, leftButton.top, leftButton.width(), leftButton.height()); graphics.fillRect(rightButton.left, rightButton.top, rightButton.width(), rightButton.height()); graphics.fillRect(fireButton.left, fireButton.top, fireButton.width(), fireButton.height()); }