KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > model > AbstractComponent


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.model;
25
26 import org.objectweb.fractal.gui.model.Configuration;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  * Abstract implementation of the {@link Component} interface.
32  */

33
34 public abstract class AbstractComponent implements Component {
35
36   /**
37    * The configuration to which this component belongs.
38    */

39
40   private Configuration owner;
41
42   /**
43    * The parent component of this component. May be <tt>null</tt> if this
44    * component is a root component.
45    */

46
47   protected Component parent;
48
49   // -------------------------------------------------------------------------
50
// Implementation of the Component interface
51
// -------------------------------------------------------------------------
52

53   public Component getParent () {
54     return parent;
55   }
56
57   /**
58    * Returns the configuration to which this component belongs.
59    *
60    * @return the configuration to which this component belongs.
61    */

62
63   public Configuration getConfiguration () {
64     return owner;
65   }
66
67   /**
68    * Sets the parent of this component.
69    *
70    * @param parent the new parent of this component.
71    * @see #getParent
72    */

73
74   void setParent (final Component parent) {
75     this.parent = parent;
76   }
77
78   public Component getRootComponent () {
79     Component c = this;
80     while (c != null && c.getParent() != null) {
81       c = c.getParent();
82     }
83     return c;
84   }
85
86   public Object JavaDoc[] getPath () {
87     Component c = this;
88     List JavaDoc pathList = new ArrayList JavaDoc();
89     while (c != null) {
90       pathList.add(0, c);
91       c = c.getParent();
92     }
93     return pathList.toArray();
94   }
95
96   public boolean contains (final Component child) {
97     Component c = child;
98     while (c != null && c != this) {
99       c = c.getParent();
100     }
101     return c == this;
102   }
103
104   public boolean containsSlaveOfExternalComponent (final Component c) {
105     Component master = getMasterComponent();
106     if (master != null && !c.contains(master)) {
107       return true;
108     }
109     List JavaDoc subComponents = getSubComponents();
110     for (int i = 0; i < subComponents.size(); ++i) {
111       Component subComponent = (Component)subComponents.get(i);
112       if (subComponent.containsSlaveOfExternalComponent(c)) {
113         return true;
114       }
115     }
116     return false;
117   }
118
119   public boolean containsMasterOfExternalComponent (final Component c) {
120     List JavaDoc slaves = getSlaveComponents();
121     for (int i = 0; i < slaves.size(); ++i) {
122       Component slave = (Component)slaves.get(i);
123       if (!c.contains(slave)) {
124         return true;
125       }
126     }
127     List JavaDoc subComponents = getSubComponents();
128     for (int i = 0; i < subComponents.size(); ++i) {
129       Component subComponent = (Component)subComponents.get(i);
130       if (subComponent.containsMasterOfExternalComponent(c)) {
131         return true;
132       }
133     }
134     return false;
135   }
136 }
137
Popular Tags