KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > control > content > CheckContentMixin


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 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: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.julia.control.content;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.NoSuchInterfaceException;
28 import org.objectweb.fractal.api.control.ContentController;
29 import org.objectweb.fractal.api.control.IllegalContentException;
30 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
31
32 import java.util.List JavaDoc;
33
34 /**
35  * Provides basic checks to a {@link ContentController}.
36  * <br>
37  * <br>
38  * <b>Requirements</b>
39  * <ul>
40  * <li>the component to which this controller object belongs must provide the
41  * {@link Component} interface.</li>
42  * </ul>
43  */

44
45 public abstract class CheckContentMixin implements ContentController {
46
47   // -------------------------------------------------------------------------
48
// Private constructor
49
// -------------------------------------------------------------------------
50

51   private CheckContentMixin () {
52   }
53
54   // -------------------------------------------------------------------------
55
// Fields and methods added and overriden by the mixin class
56
// -------------------------------------------------------------------------
57

58   /**
59    * Checks that the given component is not already a sub component, and then
60    * calls the overriden method. This method also checks that the addition of
61    * this sub component will not create a cycle in the component hierarchy.
62    *
63    * @param subComponent the component to be added inside this component.
64    * @throws IllegalContentException if the given component cannot be added
65    * inside this component.
66    * @throws IllegalLifeCycleException if this component has a {@link
67    * LifeCycleController} interface, but it is not in an appropriate state
68    * to perform this operation.
69    */

70
71   public void addFcSubComponent (final Component subComponent)
72     throws IllegalContentException, IllegalLifeCycleException
73   {
74     if (containsFcSubComponent(subComponent)) {
75       throw new ChainedIllegalContentException(
76         null, _this_weaveableC, subComponent, "Already a sub component");
77     }
78
79     // gets the Component interface of this component,
80
// and checks that it is not equal to 'subComponent'
81
Component thisComponent;
82     try {
83       thisComponent = (Component)_this_weaveableC.getFcInterface("component");
84     } catch (NoSuchInterfaceException e) {
85       throw new ChainedIllegalContentException(
86         e, _this_weaveableC, subComponent, "Cannot check this operation");
87     }
88     if (subComponent.equals(thisComponent)) {
89       throw new ChainedIllegalContentException(
90         null,
91         _this_weaveableC,
92         subComponent,
93         "A component cannot be a sub component of itself");
94     }
95
96     // finds all the direct and indirect sub components of 'subComponent' and,
97
// for each sub component checks that it is not equal to 'thisComponent'
98
List JavaDoc allSubComponents = Util.getAllSubComponents(subComponent);
99     for (int i = 0; i < allSubComponents.size(); ++i) {
100       if (allSubComponents.get(i).equals(thisComponent)) {
101         throw new ChainedIllegalContentException(
102           null,
103           _this_weaveableC,
104           subComponent,
105           "Would create a cycle in the component hierarchy");
106       }
107     }
108
109     // calls the overriden method
110
_super_addFcSubComponent(subComponent);
111   }
112
113   /**
114    * Checks that the given component is really a sub component, and then
115    * calls the overriden method.
116    *
117    * @param subComponent the component to be removed from this component.
118    * @throws IllegalContentException if the given component cannot be removed
119    * from this component.
120    * @throws IllegalLifeCycleException if this component has a {@link
121    * LifeCycleController} interface, but it is not in an appropriate state
122    * to perform this operation.
123    */

124
125   public void removeFcSubComponent (final Component subComponent)
126     throws IllegalContentException, IllegalLifeCycleException
127   {
128     if (!containsFcSubComponent(subComponent)) {
129       throw new ChainedIllegalContentException(
130         null, _this_weaveableC, subComponent, "Not a sub component");
131     }
132     _super_removeFcSubComponent(subComponent);
133   }
134
135   /**
136    * Tests if this component contains the given sub component.
137    *
138    * @param subComponent a component.
139    * @return <tt>true</tt> if this component contains the given sub component,
140    * or <tt>false</tt> otherwise.
141    */

142
143   public boolean containsFcSubComponent (final Component subComponent) {
144     Component[] subComponents = _this_getFcSubComponents();
145     for (int i = 0; i < subComponents.length; ++i) {
146       if (subComponents[i].equals(subComponent)) {
147         return true;
148       }
149     }
150     return false;
151   }
152
153   // -------------------------------------------------------------------------
154
// Fields and methods required by the mixin class in the base class
155
// -------------------------------------------------------------------------
156

157   /**
158    * The <tt>weaveableC</tt> field required by this mixin. This field is
159    * supposed to reference the {@link Component} interface of the component to
160    * which this controller object belongs.
161    */

162
163   public Component _this_weaveableC;
164
165   /**
166    * The {@link ContentController#getFcSubComponents getFcSubComponents} method
167    * required by this mixin.
168    *
169    * @return the {@link Component} interfaces of the sub-components of the
170    * component to which this interface belongs.
171    */

172
173   public abstract Component[] _this_getFcSubComponents ();
174
175   /**
176    * The {@link ContentController#addFcSubComponent addFcSubComponent} method
177    * overriden by this mixin.
178    *
179    * @param subComponent the component to be added inside this component.
180    * @throws IllegalContentException if the given component cannot be added
181    * inside this component.
182    * @throws IllegalLifeCycleException if this component has a {@link
183    * LifeCycleController} interface, but it is not in an appropriate state
184    * to perform this operation.
185    */

186
187   public abstract void _super_addFcSubComponent (Component subComponent)
188     throws IllegalContentException, IllegalLifeCycleException;
189
190   /**
191    * The {@link ContentController#removeFcSubComponent removeFcSubComponent}
192    * method overriden by this mixin.
193    *
194    * @param subComponent the component to be removed from this component.
195    * @throws IllegalContentException if the given component cannot be removed
196    * from this component.
197    * @throws IllegalLifeCycleException if this component has a {@link
198    * LifeCycleController} interface, but it is not in an appropriate state
199    * to perform this operation.
200    */

201
202   public abstract void _super_removeFcSubComponent (Component subComponent)
203     throws IllegalContentException, IllegalLifeCycleException;
204 }
205
Popular Tags