/** * Gets a Rectangle from {@link #getBox()} and returns the Point where a line from the center of * the Rectangle to the Point <i>reference</i> intersects the Rectangle. * * @param reference The reference point * @return The anchor location */ public Point getLocation(Point reference) { Rectangle r = Rectangle.SINGLETON; r.setBounds(getBox()); r.translate(-1, -1); r.resize(1, 1); getOwner().translateToAbsolute(r); float centerX = r.x + 0.5f * r.width; float centerY = r.y + 0.5f * r.height; if (r.isEmpty() || (reference.x == (int) centerX && reference.y == (int) centerY)) return new Point((int) centerX, (int) centerY); // This avoids divide-by-zero float dx = reference.x - centerX; float dy = reference.y - centerY; // r.width, r.height, dx, and dy are guaranteed to be non-zero. float scale = 0.5f / Math.max(Math.abs(dx) / r.width, Math.abs(dy) / r.height); dx *= scale; dy *= scale; centerX += dx; centerY += dy; return new Point(Math.round(centerX), Math.round(centerY)); }
public void relocate(CellEditor celleditor) { if (celleditor == null) return; Text text = (Text) celleditor.getControl(); Rectangle rect = fig.getClientArea(Rectangle.SINGLETON); if (fig instanceof Label) rect = ((Label) fig).getTextBounds().intersect(rect); fig.translateToAbsolute(rect); org.eclipse.swt.graphics.Rectangle trim = text.computeTrim(0, 0, 0, 0); rect.translate(trim.x, trim.y); rect.width += trim.width; rect.height += trim.height; text.setBounds(rect.x, rect.y, rect.width, rect.height); }