KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * An implementation of the {@link Component} interface for slave components.
34  */

35
36 public class SharedComponent extends AbstractComponent {
37
38   /**
39    * The master component of this slave component.
40    */

41
42   private BasicComponent masterComponent;
43
44   /**
45    * Status of this component.
46    */

47
48   private long status;
49
50   /**
51    * Name of this component.
52    */

53
54   private String JavaDoc name;
55
56   /**
57    * A map associating the interface of this component to the interfaces of its
58    * master component.
59    */

60
61   private Map JavaDoc interfaces;
62
63   /**
64    * Constructs a new slave component.
65    *
66    * @param masterComponent the master component of this slave component.
67    */

68
69   SharedComponent (final BasicComponent masterComponent) {
70     if (masterComponent.getMasterComponent() != null) {
71       throw new RuntimeException JavaDoc("Internal error");
72     }
73     this.masterComponent = masterComponent;
74     this.status = NAME_MISSING;
75     this.name = "";
76     this.interfaces = new HashMap JavaDoc();
77     masterComponent.addSlaveComponent(this);
78   }
79
80
81   public Configuration getConfiguration () {
82     return masterComponent.getConfiguration();
83   }
84   public long getStatus () {
85     return status | masterComponent.getStatus();
86   }
87
88   public void setStatus (final long status) {
89     this.status = status;
90   }
91
92   // -------------------------------------------------------------------------
93
// Name, Type, Implementation
94
// -------------------------------------------------------------------------
95

96   public String JavaDoc getName () {
97     return name;
98   }
99
100   public void setName (final String JavaDoc name) {
101     if (name == null) {
102       throw new IllegalArgumentException JavaDoc();
103     }
104     String JavaDoc oldName = this.name;
105     if (!name.equals(oldName)) {
106       List JavaDoc vetoableListeners = masterComponent.getOwner().getVetoableListeners();
107       for (int i = 0; i < vetoableListeners.size(); ++i) {
108         VetoableConfigurationListener l =
109           (VetoableConfigurationListener)vetoableListeners.get(i);
110         l.canChangeName(this);
111       }
112       this.name = name;
113       if (name.equals("")) {
114         status |= NAME_MISSING;
115       } else {
116         status &= ~NAME_MISSING;
117       }
118       List JavaDoc listeners = masterComponent.getOwner().getListeners();
119       for (int i = 0; i < listeners.size(); ++i) {
120         ConfigurationListener cl = (ConfigurationListener)listeners.get(i);
121         cl.nameChanged(this, oldName);
122       }
123     }
124   }
125
126   public String JavaDoc getType () {
127     return masterComponent.getType();
128   }
129
130   public void setType (final String JavaDoc type) {
131     masterComponent.setType(type);
132   }
133
134   public String JavaDoc getImplementation () {
135     return masterComponent.getImplementation();
136   }
137
138   public void setImplementation (final String JavaDoc implementation) {
139     masterComponent.setImplementation(implementation);
140   }
141
142   // -------------------------------------------------------------------------
143
// Client and Server interfaces
144
// -------------------------------------------------------------------------
145

146   public List JavaDoc getClientInterfaces () {
147     List JavaDoc l = masterComponent.getClientInterfaces();
148     List JavaDoc result = new ArrayList JavaDoc();
149     for (int i = 0; i < l.size(); ++i) {
150       result.add(getInterface((Interface)l.get(i)));
151     }
152     return Collections.unmodifiableList(result);
153   }
154
155   public Interface getClientInterface (final String JavaDoc name) {
156     return getInterface(masterComponent.getClientInterface(name));
157   }
158
159   public void addClientInterface (final ClientInterface itf) {
160     masterComponent.addClientInterface(itf);
161   }
162
163   public void removeClientInterface (ClientInterface itf) {
164     if (itf instanceof SharedInterface) {
165       itf = (ClientInterface)((SharedInterface)itf).masterInterface;
166     }
167     masterComponent.removeClientInterface(itf);
168   }
169
170   public List JavaDoc getServerInterfaces () {
171     List JavaDoc l = masterComponent.getServerInterfaces();
172     List JavaDoc result = new ArrayList JavaDoc();
173     for (int i = 0; i < l.size(); ++i) {
174       result.add(getInterface((Interface)l.get(i)));
175     }
176     return Collections.unmodifiableList(result);
177   }
178
179   public Interface getServerInterface (final String JavaDoc name) {
180     return getInterface(masterComponent.getServerInterface(name));
181   }
182
183   public void addServerInterface (final ServerInterface itf) {
184     masterComponent.addServerInterface(itf);
185   }
186
187   public void removeServerInterface (ServerInterface itf) {
188     if (itf instanceof SharedInterface) {
189       itf = (ServerInterface)((SharedInterface)itf).masterInterface;
190     }
191     masterComponent.removeServerInterface(itf);
192   }
193
194   // -------------------------------------------------------------------------
195
// Bindings
196
// -------------------------------------------------------------------------
197

198   public void bind (final ClientInterface citf, final String JavaDoc citfName, final ServerInterface sitf) {
199     Interface itf = ((SharedInterface)citf).masterInterface;
200     masterComponent.bind((ClientInterface)itf, citfName, sitf);
201   }
202
203   public void rebind (final ClientInterface citf, final ServerInterface sitf) {
204     Interface itf = ((SharedInterface)citf).masterInterface;
205     masterComponent.rebind((ClientInterface)itf, sitf);
206   }
207
208   public void unbind (final ClientInterface citf) {
209     Interface itf = ((SharedInterface)citf).masterInterface;
210     masterComponent.unbind((ClientInterface)itf);
211   }
212
213   // -------------------------------------------------------------------------
214
// Attributes
215
// -------------------------------------------------------------------------
216

217   public String JavaDoc getAttributeController () {
218     return masterComponent.getAttributeController();
219   }
220
221   public void setAttributeController (final String JavaDoc attributeController) {
222     masterComponent.setAttributeController(attributeController);
223   }
224
225   public List JavaDoc getAttributeNames () {
226     return masterComponent.getAttributeNames();
227   }
228
229   public String JavaDoc getAttribute (final String JavaDoc attributeName) {
230     return masterComponent.getAttribute(attributeName);
231   }
232
233   public void setAttribute (
234     final String JavaDoc attributeName,
235     final String JavaDoc attributeValue)
236   {
237     masterComponent.setAttribute(attributeName, attributeValue);
238   }
239
240   // -------------------------------------------------------------------------
241
// Controller descriptors
242
// -------------------------------------------------------------------------
243

244   public String JavaDoc getTemplateControllerDescriptor () {
245     return masterComponent.getTemplateControllerDescriptor();
246   }
247
248   public void setTemplateControllerDescriptor (final String JavaDoc desc) {
249     masterComponent.setTemplateControllerDescriptor(desc);
250   }
251
252   public String JavaDoc getComponentControllerDescriptor () {
253     return masterComponent.getComponentControllerDescriptor();
254   }
255
256   public void setComponentControllerDescriptor (final String JavaDoc desc) {
257     masterComponent.setComponentControllerDescriptor(desc);
258   }
259
260   // -------------------------------------------------------------------------
261
// Sharing
262
// -------------------------------------------------------------------------
263

264   public boolean isShared () {
265     return true;
266   }
267
268   public Component getMasterComponent () {
269     return masterComponent;
270   }
271
272   public List JavaDoc getSlaveComponents () {
273     return Collections.EMPTY_LIST;
274   }
275
276   // -------------------------------------------------------------------------
277
// Composite specific informations
278
// -------------------------------------------------------------------------
279

280   public boolean isComposite () {
281     return false;
282   }
283
284   public List JavaDoc getSubComponents () {
285     return Collections.EMPTY_LIST;
286   }
287
288   public Component getSubComponent (final String JavaDoc name) {
289     return null;
290   }
291
292   public void addSubComponent (final Component child) {
293     throw new IllegalOperationException(
294       "Cannot add a sub component inside a shared component");
295   }
296
297   public void removeSubComponent (final Component child) {
298     throw new RuntimeException JavaDoc("Internal error");
299   }
300
301   // -------------------------------------------------------------------------
302
// Other methods
303
// -------------------------------------------------------------------------
304

305   /**
306    * Returns the interface of this slave component that correspond to the given
307    * interface.
308    *
309    * @param masterInterface an interface of the master component of this slave
310    * component.
311    * @return the interface of this slave component that correspond to the given
312    * interface.
313    */

314
315   Interface getInterface (final Interface masterInterface) {
316     if (masterInterface == null) {
317       return null;
318     }
319     Interface i = (Interface)interfaces.get(masterInterface);
320     if (i == null) {
321       if (masterInterface instanceof ClientInterface) {
322         i = new SharedClientInterface(this, masterInterface);
323       } else {
324         i = new SharedServerInterface(this, masterInterface);
325       }
326       interfaces.put(masterInterface, i);
327     }
328     return i;
329   }
330 }
331
Popular Tags