home *** CD-ROM | disk | FTP | other *** search
- package java.awt;
-
- import java.awt.peer.ContainerPeer;
- import java.io.PrintStream;
-
- public abstract class Container extends Component {
- int ncomponents;
- Component[] component = new Component[4];
- LayoutManager layoutMgr;
-
- Container() {
- }
-
- public int countComponents() {
- return this.ncomponents;
- }
-
- public synchronized Component getComponent(int n) {
- if (n >= 0 && n < this.ncomponents) {
- return this.component[n];
- } else {
- throw new ArrayIndexOutOfBoundsException("No such child: " + n);
- }
- }
-
- public synchronized Component[] getComponents() {
- Component[] list = new Component[this.ncomponents];
- System.arraycopy(this.component, 0, list, 0, this.ncomponents);
- return list;
- }
-
- public Insets insets() {
- ContainerPeer peer = (ContainerPeer)super.peer;
- return peer != null ? peer.insets() : new Insets(0, 0, 0, 0);
- }
-
- public Component add(Component comp) {
- return this.add(comp, -1);
- }
-
- public synchronized Component add(Component comp, int pos) {
- if (pos <= this.ncomponents && (pos >= 0 || pos == -1)) {
- if (comp instanceof Container) {
- for(Container cn = this; cn != null; cn = cn.parent) {
- if (cn == comp) {
- throw new IllegalArgumentException("adding container's parent to itself");
- }
- }
- }
-
- if (comp.parent != null) {
- comp.parent.remove(comp);
- }
-
- if (this.ncomponents == this.component.length) {
- Component[] newcomponents = new Component[this.ncomponents * 2];
- System.arraycopy(this.component, 0, newcomponents, 0, this.ncomponents);
- this.component = newcomponents;
- }
-
- if (pos != -1 && pos != this.ncomponents) {
- System.arraycopy(this.component, pos, this.component, pos + 1, this.ncomponents - pos);
- this.component[pos] = comp;
- ++this.ncomponents;
- } else {
- this.component[this.ncomponents++] = comp;
- }
-
- comp.parent = this;
- ((Component)this).invalidate();
- if (super.peer != null) {
- comp.addNotify();
- }
-
- return comp;
- } else {
- throw new IllegalArgumentException("illegal component position");
- }
- }
-
- public synchronized Component add(String name, Component comp) {
- Component c = this.add(comp);
- LayoutManager layoutMgr = this.layoutMgr;
- if (layoutMgr != null) {
- layoutMgr.addLayoutComponent(name, comp);
- }
-
- return c;
- }
-
- public synchronized void remove(Component comp) {
- if (comp.parent == this) {
- for(int i = 0; i < this.ncomponents; ++i) {
- if (this.component[i] == comp) {
- if (super.peer != null) {
- comp.removeNotify();
- }
-
- if (this.layoutMgr != null) {
- this.layoutMgr.removeLayoutComponent(comp);
- }
-
- comp.parent = null;
- System.arraycopy(this.component, i + 1, this.component, i, this.ncomponents - i - 1);
- this.component[--this.ncomponents] = null;
- ((Component)this).invalidate();
- return;
- }
- }
- }
-
- }
-
- public synchronized void removeAll() {
- Component comp;
- for(; this.ncomponents > 0; comp.parent = null) {
- comp = this.component[--this.ncomponents];
- this.component[this.ncomponents] = null;
- if (super.peer != null) {
- comp.removeNotify();
- }
-
- if (this.layoutMgr != null) {
- this.layoutMgr.removeLayoutComponent(comp);
- }
- }
-
- ((Component)this).invalidate();
- }
-
- public LayoutManager getLayout() {
- return this.layoutMgr;
- }
-
- public void setLayout(LayoutManager mgr) {
- this.layoutMgr = mgr;
- ((Component)this).invalidate();
- }
-
- public synchronized void layout() {
- LayoutManager layoutMgr = this.layoutMgr;
- if (layoutMgr != null) {
- layoutMgr.layoutContainer(this);
- }
-
- }
-
- public synchronized void validate() {
- super.validate();
-
- for(int i = 0; i < this.ncomponents; ++i) {
- Component comp = this.component[i];
- if (!comp.valid) {
- comp.validate();
- }
- }
-
- }
-
- public synchronized Dimension preferredSize() {
- LayoutManager layoutMgr = this.layoutMgr;
- return layoutMgr != null ? layoutMgr.preferredLayoutSize(this) : super.preferredSize();
- }
-
- public synchronized Dimension minimumSize() {
- LayoutManager layoutMgr = this.layoutMgr;
- return layoutMgr != null ? layoutMgr.minimumLayoutSize(this) : super.minimumSize();
- }
-
- public void paintComponents(Graphics g) {
- for(int i = 0; i < this.ncomponents; ++i) {
- Component comp = this.component[i];
- if (comp != null) {
- Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
-
- try {
- comp.paintAll(cg);
- } finally {
- cg.dispose();
- }
- }
- }
-
- }
-
- public void printComponents(Graphics g) {
- for(int i = 0; i < this.ncomponents; ++i) {
- Component comp = this.component[i];
- if (comp != null) {
- Graphics cg = g.create(comp.x, comp.y, comp.width, comp.height);
-
- try {
- comp.printAll(cg);
- } finally {
- cg.dispose();
- }
- }
- }
-
- }
-
- public void deliverEvent(Event e) {
- Component comp = this.locate(e.x, e.y);
- if (comp != null && comp != this) {
- e.translate(-comp.x, -comp.y);
- comp.deliverEvent(e);
- } else {
- ((Component)this).postEvent(e);
- }
- }
-
- public Component locate(int x, int y) {
- if (!((Component)this).inside(x, y)) {
- return null;
- } else {
- for(int i = 0; i < this.ncomponents; ++i) {
- Component comp = this.component[i];
- if (comp != null && comp.inside(x - comp.x, y - comp.y)) {
- return comp;
- }
- }
-
- return this;
- }
- }
-
- public synchronized void addNotify() {
- for(int i = 0; i < this.ncomponents; ++i) {
- this.component[i].addNotify();
- }
-
- super.addNotify();
- }
-
- public synchronized void removeNotify() {
- for(int i = 0; i < this.ncomponents; ++i) {
- this.component[i].removeNotify();
- }
-
- super.removeNotify();
- }
-
- protected String paramString() {
- String str = super.paramString();
- LayoutManager layoutMgr = this.layoutMgr;
- if (layoutMgr != null) {
- str = str + ",layout=" + layoutMgr.getClass().getName();
- }
-
- return str;
- }
-
- public void list(PrintStream out, int indent) {
- super.list(out, indent);
-
- for(int i = 0; i < this.ncomponents; ++i) {
- Component comp = this.component[i];
- if (comp != null) {
- comp.list(out, indent + 1);
- }
- }
-
- }
- }
-