/** @see IFigure#invalidateTree() */ public void invalidateTree() { invalidate(); for (Iterator iter = children.iterator(); iter.hasNext(); ) { IFigure child = (IFigure) iter.next(); child.invalidateTree(); } }
/** * Removes the given child Figure from this Figure's hierarchy and revalidates this Figure. The * child Figure's {@link #removeNotify()} method is also called. * * @param figure The Figure to remove */ public void remove(IFigure figure) { if ((figure.getParent() != this)) throw new IllegalArgumentException("Figure is not a child"); // $NON-NLS-1$ if (getFlag(FLAG_REALIZED)) figure.removeNotify(); if (layoutManager != null) layoutManager.remove(figure); // The updates in the UpdateManager *have* to be // done asynchronously, else will result in // incorrect dirty region corrections. figure.erase(); figure.setParent(null); children.remove(figure); revalidate(); }
/** * Paints this Figure's children. The caller must save the state of the graphics prior to calling * this method, such that <code>graphics.restoreState()</code> may be called safely, and doing so * will return the graphics to its original state when the method was entered. * * <p>This method must leave the Graphics in its original state upon return. * * @param graphics the graphics used to paint * @since 2.0 */ protected void paintChildren(Graphics graphics) { IFigure child; Rectangle clip = Rectangle.SINGLETON; for (int i = 0; i < children.size(); i++) { child = (IFigure) children.get(i); if (child.isVisible() && child.intersects(graphics.getClip(clip))) { graphics.clipRect(child.getBounds()); child.paint(graphics); graphics.restoreState(); } } }
/** @see IFigure#setConstraint(IFigure, Object) */ public void setConstraint(IFigure child, Object constraint) { if (child.getParent() != this) throw new IllegalArgumentException("Figure must be a child"); // $NON-NLS-1$ if (layoutManager != null) layoutManager.setConstraint(child, constraint); revalidate(); }
/** * Implements the algorithm to layout the components of the given container figure. Each component * is laid out using its own layout constraint specifying its size and position. * * @see LayoutManager#layout(IFigure) */ public void layout(IFigure parent) { Iterator children = parent.getChildren().iterator(); Point offset = getOrigin(parent); IFigure f; while (children.hasNext()) { f = (IFigure) children.next(); Rectangle bounds = (Rectangle) getConstraint(f); if (bounds == null) continue; if (bounds.width == -1 || bounds.height == -1) { Dimension preferredSize = f.getPreferredSize(bounds.width, bounds.height); bounds = bounds.getCopy(); if (bounds.width == -1) bounds.width = preferredSize.width; if (bounds.height == -1) bounds.height = preferredSize.height; } bounds = bounds.getTranslated(offset); f.setBounds(bounds); } }
/** * Searches this Figure's children for the deepest descendant for which {@link * #isMouseEventTarget()} returns <code>true</code> and returns that descendant or <code>null * </code> if none found. * * @see #findMouseEventTargetAt(int, int) * @param x The X coordinate * @param y The Y coordinate * @return The deepest descendant for which isMouseEventTarget() returns true */ protected IFigure findMouseEventTargetInDescendantsAt(int x, int y) { PRIVATE_POINT.setLocation(x, y); translateFromParent(PRIVATE_POINT); if (!getClientArea(Rectangle.SINGLETON).contains(PRIVATE_POINT)) return null; IFigure fig; for (int i = children.size(); i > 0; ) { i--; fig = (IFigure) children.get(i); if (fig.isVisible() && fig.isEnabled()) { if (fig.containsPoint(PRIVATE_POINT.x, PRIVATE_POINT.y)) { fig = fig.findMouseEventTargetAt(PRIVATE_POINT.x, PRIVATE_POINT.y); return fig; } } } return null; }
/** * Returns a descendant of this Figure such that the Figure returned contains the point (x, y), * and is accepted by the given TreeSearch. Returns <code>null</code> if none found. * * @param x The X coordinate * @param y The Y coordinate * @param search the TreeSearch * @return The descendant Figure at (x,y) */ protected IFigure findDescendantAtExcluding(int x, int y, TreeSearch search) { PRIVATE_POINT.setLocation(x, y); translateFromParent(PRIVATE_POINT); if (!getClientArea(Rectangle.SINGLETON).contains(PRIVATE_POINT)) return null; x = PRIVATE_POINT.x; y = PRIVATE_POINT.y; IFigure fig; for (int i = children.size(); i > 0; ) { i--; fig = (IFigure) children.get(i); if (fig.isVisible()) { fig = fig.findFigureAt(x, y, search); if (fig != null) return fig; } } // No descendants were found return null; }
/** * Calculates and returns the preferred size of the input figure. Since in XYLayout the location * of the child should be preserved, the preferred size would be a region which would hold all the * children of the input figure. If no constraint is set, that child is ignored for calculation. * If width and height are not positive, the preferred dimensions of the child are taken. * * @see AbstractLayout#calculatePreferredSize(IFigure, int, int) * @since 2.0 */ protected Dimension calculatePreferredSize(IFigure f, int wHint, int hHint) { Rectangle rect = new Rectangle(); ListIterator children = f.getChildren().listIterator(); while (children.hasNext()) { IFigure child = (IFigure) children.next(); Rectangle r = (Rectangle) constraints.get(child); if (r == null) continue; if (r.width == -1 || r.height == -1) { Dimension preferredSize = child.getPreferredSize(r.width, r.height); r = r.getCopy(); if (r.width == -1) r.width = preferredSize.width; if (r.height == -1) r.height = preferredSize.height; } rect.union(r); } Dimension d = rect.getSize(); Insets insets = f.getInsets(); return new Dimension(d.width + insets.getWidth(), d.height + insets.getHeight()) .union(getBorderPreferredSize(f)); }
/** @see IFigure#add(IFigure, Object, int) */ public void add(IFigure figure, Object constraint, int index) { if (children == Collections.EMPTY_LIST) children = new ArrayList(2); if (index < -1 || index > children.size()) throw new IndexOutOfBoundsException("Index does not exist"); // $NON-NLS-1$ // Check for Cycle in hierarchy for (IFigure f = this; f != null; f = f.getParent()) if (figure == f) throw new IllegalArgumentException("Figure being added introduces cycle"); // $NON-NLS-1$ // Detach the child from previous parent if (figure.getParent() != null) figure.getParent().remove(figure); if (index == -1) children.add(figure); else children.add(index, figure); figure.setParent(this); if (layoutManager != null) layoutManager.setConstraint(figure, constraint); revalidate(); if (getFlag(FLAG_REALIZED)) figure.addNotify(); figure.repaint(); }
private static void show(IFigure f) { f.draw(); System.out.println("My area : " + f.getArea()); System.out.println("My girth : " + f.getGirth()); }
/** * Returns the origin for the given figure. * * @param parent the figure whose origin is requested * @return the origin */ public Point getOrigin(IFigure parent) { return parent.getClientArea().getLocation(); }
/** * Constructs a new FigureIterator for the given Figure. * * @param figure The Figure whose children to iterate over */ public FigureIterator(IFigure figure) { list = figure.getChildren(); index = list.size(); }
public void draw(IPainter painter) { figure.draw(painter); }