KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.swing.JOptionPane JavaDoc;
33
34 /**
35  * Abstract implementation of the {@link Interface} interface.
36  */

37
38 public abstract class BasicInterface implements Interface {
39
40   /**
41    * The component that owns this interface.
42    */

43
44   protected BasicComponent owner;
45
46   /**
47    * Status of this interface.
48    */

49
50   private long status;
51
52   /**
53    * Name of this interface.
54    */

55
56   String JavaDoc name;
57
58   /**
59    * Singature of this interface.
60    */

61
62   private String JavaDoc signature;
63
64   /**
65    * If this interface is an internal interface or an external interface.
66    */

67
68   private boolean isInternal;
69
70   /**
71    * If this interface is optional or mandatory.
72    */

73
74   private boolean isOptional;
75
76   /**
77    * If this interface is a collection interface or a single interface.
78    */

79
80   private boolean isCollection;
81
82   /**
83    * The master collection interface of this interface, or <tt>null</tt> if this
84    * interface is not a slave collection interface.
85    */

86
87   protected Interface masterCollectionItf;
88
89   /**
90    * The slave collection interfaces of this master collection interface. This
91    * list is a list of {@link Interface} objects. It is empty if this interface
92    * is not a master collection interface.
93    */

94
95   protected List JavaDoc slaveCollectionItfs;
96
97   /**
98    * The complementary interface of this interface.
99    */

100
101   protected Interface complementaryItf;
102
103   /**
104    * Constructs a new external interface.
105    *
106    * @param owner the component that will own this interface.
107    */

108
109   BasicInterface (final BasicComponent owner) {
110     this.owner = owner;
111     status = NAME_MISSING | SIGNATURE_MISSING;
112     name = "";
113     signature = "";
114     isInternal = false;
115     slaveCollectionItfs = new ArrayList JavaDoc();
116   }
117
118   /**
119    * Constructs a new internal interface.
120    *
121    * @param externalItf the complementary interface of this interface.
122    */

123
124   BasicInterface (final Interface externalItf) {
125     isInternal = true;
126     complementaryItf = externalItf;
127   }
128
129   /**
130    * Constructs a new slave collection interface.
131    *
132    * @param masterCollectionItf a master collection interface.
133    * @param ignored ignored.
134    */

135
136   BasicInterface (final Interface masterCollectionItf, final int ignored) {
137     this.isCollection = true;
138     this.masterCollectionItf = masterCollectionItf;
139   }
140
141   public Component getOwner () {
142     return isInternal
143       ? complementaryItf.getOwner()
144       : masterCollectionItf != null
145         ? masterCollectionItf.getOwner()
146         : owner;
147   }
148
149   public long getStatus () {
150     return isInternal
151       ? complementaryItf.getStatus()
152       : masterCollectionItf != null
153         ? masterCollectionItf.getStatus()
154         : status;
155   }
156
157   public void setStatus (final long status) {
158     this.status = status;
159   }
160
161   public String JavaDoc getName () {
162     if (isInternal) {
163       return complementaryItf.getName();
164     } else if (masterCollectionItf != null) {
165       if (name == null) {
166         List JavaDoc l = masterCollectionItf.getSlaveCollectionInterfaces();
167         Set JavaDoc s = new HashSet JavaDoc();
168         for (int i = 0; i < l.size(); ++i) {
169           if (l.get(i) != this) {
170             s.add(((Interface)l.get(i)).getName());
171           }
172         }
173         int i = 0;
174         do {
175           name = Integer.toString(100 + (i++)).substring(1);
176         } while (s.contains(masterCollectionItf.getName() + name));
177       }
178       return masterCollectionItf.getName() + name;
179       /*int i = masterCollectionItf.getSlaveCollectionInterfaces().indexOf(this);
180       if (i != -1) {
181         name = masterCollectionItf.getName() + Integer.toString(100 + i).substring(1);
182       }
183       return name;*/

184     } else {
185       return name;
186     }
187   }
188
189   public void setName (final String JavaDoc name) {
190     if (name == null) {
191       throw new IllegalArgumentException JavaDoc();
192     }
193     if (isInternal) {
194       complementaryItf.setName(name);
195     } else if (masterCollectionItf != null) {
196       masterCollectionItf.setName(name);
197     } else {
198       String JavaDoc oldName = this.name;
199       if (!name.equals(oldName)) {
200         List JavaDoc vetoableListeners = owner.getOwner().getVetoableListeners();
201         for (int i = 0; i < vetoableListeners.size(); ++i) {
202           VetoableConfigurationListener l =
203             (VetoableConfigurationListener)vetoableListeners.get(i);
204           l.canChangeInterfaceName(this);
205         }
206         this.name = name;
207         List JavaDoc listeners = owner.getOwner().getListeners();
208         for (int i = 0; i < listeners.size(); ++i) {
209           ConfigurationListener l = (ConfigurationListener)listeners.get(i);
210           l.interfaceNameChanged(this, oldName);
211         }
212       }
213     }
214   }
215
216   public String JavaDoc getSignature () {
217     return isInternal
218       ? complementaryItf.getSignature()
219       : masterCollectionItf != null
220         ? masterCollectionItf.getSignature()
221         : signature;
222   }
223
224   public void setSignature (final String JavaDoc signature) {
225     if (signature == null) {
226       throw new IllegalArgumentException JavaDoc();
227     }
228     if (isInternal) {
229       complementaryItf.setSignature(signature);
230     } else if (masterCollectionItf != null) {
231       masterCollectionItf.setSignature(signature);
232     } else {
233       String JavaDoc oldSignature = this.signature;
234       if (!signature.equals(oldSignature)) {
235         List JavaDoc vetoableListeners = owner.getOwner().getVetoableListeners();
236         for (int i = 0; i < vetoableListeners.size(); ++i) {
237           VetoableConfigurationListener l =
238             (VetoableConfigurationListener)vetoableListeners.get(i);
239           l.canChangeInterfaceSignature(this);
240         }
241         this.signature = signature;
242         List JavaDoc listeners = owner.getOwner().getListeners();
243         for (int i = 0; i < listeners.size(); ++i) {
244           ConfigurationListener l = (ConfigurationListener)listeners.get(i);
245           l.interfaceSignatureChanged(this, oldSignature);
246         }
247       }
248     }
249   }
250
251   public boolean isInternal () {
252     return isInternal;
253   }
254
255   public boolean isOptional () {
256     return isInternal
257       ? complementaryItf.isOptional()
258       : masterCollectionItf != null
259         ? masterCollectionItf.isOptional()
260         : isOptional;
261   }
262
263   public void setIsOptional (final boolean isOptional) {
264     if (isInternal) {
265       complementaryItf.setIsOptional(isOptional);
266     } else if (masterCollectionItf != null) {
267       masterCollectionItf.setIsOptional(isOptional);
268     } else {
269       boolean oldIsOptional = this.isOptional;
270       if (isOptional != oldIsOptional) {
271         List JavaDoc vetoableListeners = owner.getOwner().getVetoableListeners();
272         for (int i = 0; i < vetoableListeners.size(); ++i) {
273           VetoableConfigurationListener l =
274             (VetoableConfigurationListener)vetoableListeners.get(i);
275           l.canChangeInterfaceContingency(this);
276         }
277         this.isOptional = isOptional;
278         List JavaDoc listeners = owner.getOwner().getListeners();
279         for (int i = 0; i < listeners.size(); ++i) {
280           ConfigurationListener l = (ConfigurationListener)listeners.get(i);
281           l.interfaceContingencyChanged(this, oldIsOptional);
282         }
283       }
284     }
285   }
286
287   public boolean isCollection () {
288     return isInternal ? complementaryItf.isCollection() : isCollection;
289   }
290
291
292   // continuer à raler et catcher exception dialog
293
public void setIsCollection (final boolean isCollection) {
294 //@@@
295
boolean faut_raler = false;
296     if (isCollection) {
297       if (this instanceof ClientInterface) {
298         ClientInterface citf = (ClientInterface)this;
299         if (citf.getBinding() != null) {
300           faut_raler = true;
301         }
302       }
303       else if (this instanceof ServerInterface) {
304         ServerInterface sitf = (ServerInterface)this;
305         if (sitf.getBindings().size() > 0) {
306           faut_raler = true;
307         }
308       }
309     }
310     if (faut_raler) {
311       JOptionPane.showMessageDialog (null,
312       "Cannot set this interface as a collection interface."+
313       "because, it's already bound to another.", "Error",
314       JOptionPane.ERROR_MESSAGE);
315       return;
316     }
317
318     if (isInternal) {
319       complementaryItf.setIsCollection(isCollection);
320     } else {
321       boolean oldIsCollection = this.isCollection;
322       if (isCollection != oldIsCollection) {
323         if (masterCollectionItf != null ||
324             getSlaveCollectionInterfaces().size() > 0)
325         {
326           throw new IllegalOperationException(
327             "Cannot change the cardinality of a root collection interface. " +
328             "You must first remove all the interfaces of the collection");
329         }
330         List JavaDoc vetoableListeners = owner.getOwner().getVetoableListeners();
331         for (int i = 0; i < vetoableListeners.size(); ++i) {
332           VetoableConfigurationListener l =
333             (VetoableConfigurationListener)vetoableListeners.get(i);
334           l.canChangeInterfaceCardinality(this);
335         }
336         this.isCollection = isCollection;
337         List JavaDoc listeners = owner.getOwner().getListeners();
338         for (int i = 0; i < listeners.size(); ++i) {
339           ConfigurationListener l = (ConfigurationListener)listeners.get(i);
340           l.interfaceCardinalityChanged(this, oldIsCollection);
341         }
342       }
343     }
344   }
345
346   public Interface getMasterCollectionInterface () {
347     return masterCollectionItf;
348   }
349
350   public List JavaDoc getSlaveCollectionInterfaces () {
351     return isInternal
352       ? complementaryItf.getSlaveCollectionInterfaces()
353       : masterCollectionItf != null
354         ? Collections.EMPTY_LIST
355         : Collections.unmodifiableList(slaveCollectionItfs);
356   }
357
358   /**
359    * Adds the given interface to the list of slave collection interface of this
360    * interface.
361    *
362    * @param slaveItf a new slave collection interface of this interface.
363    */

364
365   void addSlaveCollectionInterface (final Interface slaveItf) {
366     if (slaveCollectionItfs == null) {
367       throw new RuntimeException JavaDoc("Internal error");
368     }
369     if (!slaveCollectionItfs.contains(slaveItf)) {
370       slaveCollectionItfs.add(slaveItf);
371     }
372   }
373
374   /**
375    * Removes the given interface from the list of slave collection interface of
376    * this interface.
377    *
378    * @param slaveItf a slave collection interface of this interface.
379    */

380
381   void removeSlaveCollectionInterface (final Interface slaveItf) {
382     if (slaveCollectionItfs == null) {
383       throw new RuntimeException JavaDoc("Internal error");
384     }
385     slaveCollectionItfs.remove(slaveItf);
386   }
387
388   public Interface getComplementaryInterface () {
389     return complementaryItf;
390   }
391
392   public Interface getMasterInterface () {
393     return null;
394   }
395 }
396
Popular Tags