/*
* @(#)AbstractCompositeFigure.java
*
* Copyright (c) 2007-2010 by the original authors of JHotDraw and all its
* contributors. All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with the copyright holders. For details
* see accompanying license terms.
*/
package org.jhotdraw.draw;
import edu.umd.cs.findbugs.annotations.Nullable;
import org.jhotdraw.draw.layouter.Layouter;
import org.jhotdraw.draw.event.FigureAdapter;
import org.jhotdraw.draw.event.FigureEvent;
import org.jhotdraw.draw.event.CompositeFigureEvent;
import org.jhotdraw.draw.event.CompositeFigureListener;
import java.io.IOException;
import org.jhotdraw.util.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.Serializable;
import java.util.*;
import javax.swing.event.*;
import org.jhotdraw.draw.handle.BoundsOutlineHandle;
import org.jhotdraw.draw.handle.Handle;
import org.jhotdraw.draw.handle.TransformHandleKit;
import org.jhotdraw.geom.*;
import org.jhotdraw.xml.DOMInput;
import org.jhotdraw.xml.DOMOutput;
import org.jhotdraw.xml.DOMStorable;
import static org.jhotdraw.draw.AttributeKeys.*;
/**
* This abstract class can be extended to implement a {@link CompositeFigure}.
* AbstractCompositeFigure.
*
* @author Werner Randelshofer
* @version $Id: AbstractCompositeFigure.java 717 2010-11-21 12:30:57Z rawcoder $
*/
public abstract class AbstractCompositeFigure
extends AbstractFigure
implements CompositeFigure, DOMStorable {
/**
* A Layouter determines how the children of the CompositeFigure
* are laid out graphically.
*/
protected Layouter layouter;
/**
* The children that this figure is composed of
*
* @see #add
* @see #removeChild
*/
protected ArrayList children = new ArrayList();
/**
* Caches the drawing area to improve the performance of method {@link #getDrawingArea}.
*/
@Nullable protected transient Rectangle2D.Double cachedDrawingArea;
/**
* Caches the bounds to improve the performance of method {@link #getBounds}.
*/
@Nullable protected transient Rectangle2D.Double cachedBounds;
/**
* Handles figure changes in the children.
*/
protected EventHandler eventHandler;
protected class EventHandler extends FigureAdapter implements UndoableEditListener, Serializable {
@Override
public void figureRequestRemove(FigureEvent e) {
remove(e.getFigure());
}
@Override
public void figureChanged(FigureEvent e) {
if (!isChanging()) {
Rectangle2D.Double invalidatedArea = getDrawingArea();
invalidatedArea.add(e.getInvalidatedArea());
// We call invalidate/validate here, because we must layout
// the figure again.
invalidate();
validate();
// Forward the figureChanged event to listeners on AbstractCompositeFigure.
invalidatedArea.add(getDrawingArea());
fireFigureChanged(invalidatedArea);
}
}
@Override
public void areaInvalidated(FigureEvent e) {
fireAreaInvalidated(e);
}
@Override
public void undoableEditHappened(UndoableEditEvent e) {
fireUndoableEditHappened(e.getEdit());
}
@Override
public void attributeChanged(FigureEvent e) {
invalidate();
}
@Override
public void figureAdded(FigureEvent e) {
invalidate();
}
@Override
public void figureRemoved(FigureEvent e) {
invalidate();
}
}
public AbstractCompositeFigure() {
eventHandler = createEventHandler();
}
@Override
public Collection createHandles(int detailLevel) {
LinkedList handles = new LinkedList();
if (detailLevel == 0) {
handles.add(new BoundsOutlineHandle(this, true, false));
TransformHandleKit.addScaleMoveTransformHandles(this, handles);
}
return handles;
}
protected EventHandler createEventHandler() {
return new EventHandler();
}
@Override
public boolean add(Figure figure) {
add(getChildCount(), figure);
return true;
}
@Override
public void add(int index, Figure figure) {
basicAdd(index, figure);
if (getDrawing() != null) {
figure.addNotify(getDrawing());
}
fireFigureAdded(figure, index);
invalidate();
}
public void addAll(Collection extends Figure> figures) {
addAll(getChildCount(), figures);
}
public final void addAll(int index, Collection extends Figure> figures) {
for (Figure f : figures) {
basicAdd(index++, f);
if (getDrawing() != null) {
f.addNotify(getDrawing());
}
fireFigureAdded(f, index);
}
invalidate();
}
@Override
public void basicAdd(Figure figure) {
basicAdd(getChildCount(), figure);
}
public void basicAddAll(int index, Collection extends Figure> newFigures) {
for (Figure f : newFigures) {
basicAdd(index++, f);
}
}
@Override
public void addNotify(Drawing drawing) {
super.addNotify(drawing);
for (Figure child : getChildren()) {
child.addNotify(drawing);
}
}
@Override
public void removeNotify(Drawing drawing) {
super.removeNotify(drawing);
// Copy children collection to avoid concurrent modification exception
for (Figure child : new LinkedList(getChildren())) {
child.removeNotify(drawing);
}
}
@Override
public boolean remove(final Figure figure) {
int index = children.indexOf(figure);
if (index == -1) {
return false;
} else {
basicRemoveChild(index);
if (getDrawing() != null) {
figure.removeNotify(getDrawing());
}
fireFigureRemoved(figure, index);
return true;
}
}
@Override
public Figure removeChild(int index) {
Figure removed = basicRemoveChild(index);
if (getDrawing() != null) {
removed.removeNotify(getDrawing());
}
return removed;
}
/**
* Removes all specified children.
*
* @see #add
*/
public void removeAll(Collection extends Figure> figures) {
willChange();
for (Figure f : new LinkedList(figures)) {
remove(f);
}
changed();
}
/**
* Removes all children.
*
* @see #add
*/
@Override
public void removeAllChildren() {
removeAll(getChildren());
}
/**
* Removes all children.
*
* @see #add
*/
@Override
public void basicRemoveAllChildren() {
for (Figure f : new LinkedList(getChildren())) {
basicRemove(f);
}
}
/**
* Removes all children.
*
* @see #add
*/
public void basicRemoveAll(Collection extends Figure> figures) {
for (Figure f : figures) {
basicRemove(f);
}
}
/**
* Sends a figure to the back of the composite figure.
*
* @param figure that is part of this composite figure
*/
public void sendToBack(Figure figure) {
if (basicRemove(figure) != -1) {
basicAdd(0, figure);
fireAreaInvalidated(figure.getDrawingArea());
}
}
/**
* Brings a figure to the front of the drawing.
*
* @param figure that is part of the drawing
*/
public void bringToFront(Figure figure) {
if (basicRemove(figure) != -1) {
basicAdd(figure);
fireAreaInvalidated(figure.getDrawingArea());
}
}
/**
* Transforms the figure.
*/
@Override
public void transform(AffineTransform tx) {
for (Figure f : getChildren()) {
f.transform(tx);
}
invalidate();
//invalidate();
}
@Override
public void setBounds(Point2D.Double anchor, Point2D.Double lead) {
Rectangle2D.Double oldBounds = getBounds();
Rectangle2D.Double newBounds = new Rectangle2D.Double(
Math.min(anchor.x, lead.x),
Math.min(anchor.y, lead.y),
Math.abs(anchor.x - lead.x),
Math.abs(anchor.y - lead.y));
double sx = newBounds.width / oldBounds.width;
double sy = newBounds.height / oldBounds.height;
AffineTransform tx = new AffineTransform();
tx.translate(-oldBounds.x, -oldBounds.y);
if (!Double.isNaN(sx) && !Double.isNaN(sy)
&& !Double.isInfinite(sx) && !Double.isInfinite(sy)
&& (sx != 1d || sy != 1d)
&& !(sx < 0.0001) && !(sy < 0.0001)) {
transform(tx);
tx.setToIdentity();
tx.scale(sx, sy);
transform(tx);
tx.setToIdentity();
}
tx.translate(newBounds.x, newBounds.y);
transform(tx);
}
/**
* Returns an iterator to iterate in
* Z-order front to back over the children.
*/
public java.util.List getChildrenFrontToBack() {
return children.size() == 0 ? new LinkedList() : new ReversedList(getChildren());
}
@Override
public void set(AttributeKey key, T value) {
for (Figure child : getChildren()) {
child.set(key, value);
}
invalidate();
}
@Override
public T get(AttributeKey name) {
return null;
}
@Override
public Map getAttributes() {
return new HashMap();
}
@Override
public Object getAttributesRestoreData() {
LinkedList
Ads help maintain this website.