KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > admin > model > BasicAdminModel


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.admin.model;
25
26 import org.objectweb.fractal.adl.Factory;
27 import org.objectweb.fractal.adl.FactoryFactory;
28 import org.objectweb.fractal.api.control.BindingController;
29 import org.objectweb.fractal.api.control.ContentController;
30 import org.objectweb.fractal.api.control.LifeCycleController;
31 import org.objectweb.fractal.api.control.AttributeController;
32 import org.objectweb.fractal.api.control.NameController;
33 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
34 import org.objectweb.fractal.api.NoSuchInterfaceException;
35
36 import org.objectweb.fractal.util.Fractal;
37
38 import org.objectweb.fractal.gui.model.Binding;
39 import org.objectweb.fractal.gui.model.ClientInterface;
40 import org.objectweb.fractal.gui.model.Component;
41 import org.objectweb.fractal.gui.model.Configuration;
42 import org.objectweb.fractal.gui.model.ConfigurationListener;
43 import org.objectweb.fractal.gui.model.IllegalOperationException;
44 import org.objectweb.fractal.gui.model.Interface;
45 import org.objectweb.fractal.gui.model.ServerInterface;
46 import org.objectweb.fractal.gui.model.VetoableConfigurationListener;
47 import org.objectweb.fractal.gui.repository.api.Storage;
48 import org.objectweb.fractal.gui.repository.api.Repository;
49 import org.objectweb.fractal.gui.admin.model.AdminModelListener;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.HashSet JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.Set JavaDoc;
55 import java.util.WeakHashMap JavaDoc;
56 import java.util.List JavaDoc;
57
58 import java.lang.reflect.Method JavaDoc;
59
60 import java.awt.*;
61 import java.awt.event.*;
62 import javax.swing.*;
63 import javax.swing.border.*;
64 import java.awt.FontMetrics JavaDoc;
65 import javax.swing.JOptionPane JavaDoc;
66
67
68 public class BasicAdminModel implements
69   BindingController,
70   AdminModel,
71   VetoableConfigurationListener,
72   ConfigurationListener
73 {
74
75   /**
76    * A mandatory client interface bound to a {@link Configuration configuration}
77    * model. This is the model on which this model is based and synchronized.
78    */

79
80   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
81
82   /**
83    * A mandatory client interface bound to a {@link Storage storage}. This is
84    * the storage into which the saved configurations are stored.
85    */

86
87   public final static String JavaDoc STORAGE_BINDING = "storage";
88
89   /**
90    * A mandatory client interface bound to a {@link Repository repository}. This
91    * repository is used to store the configurations in the storage.
92    */

93
94   public final static String JavaDoc REPOSITORY_BINDING = "repository";
95
96   /**
97    * A mandatory client interface bound to an {@link AdminModelListener}.
98    */

99
100   public final static String JavaDoc ADMIN_MODEL_LISTENER_BINDING = "admin-model-listener";
101
102   /**
103    * The configuration client interface.
104    */

105
106   private Configuration configuration;
107
108   /**
109    * The storage client interface.
110    */

111
112   private Storage storage;
113
114   /**
115    * The repository client interface.
116    */

117
118   private Repository repository;
119
120   /**
121    * The admin model listener client interface.
122    */

123
124   private AdminModelListener adminlistener;
125
126   /**
127    * A map associating ComponentIdentity interfaces to Component objects.
128    */

129
130   private Map JavaDoc instances;
131
132   private int range = -1;
133
134   public BasicAdminModel () {
135     instances = new WeakHashMap JavaDoc();
136   }
137
138   // -------------------------------------------------------------------------
139
// Implementation of the BindingController interface
140
// -------------------------------------------------------------------------
141

142   public String JavaDoc[] listFc () {
143     return new String JavaDoc[] {
144       CONFIGURATION_BINDING,
145       STORAGE_BINDING,
146       ADMIN_MODEL_LISTENER_BINDING,
147       REPOSITORY_BINDING
148     };
149   }
150
151   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
152     if (CONFIGURATION_BINDING.equals(clientItfName)) {
153       return configuration;
154     } else if (STORAGE_BINDING.equals(clientItfName)) {
155       return storage;
156     } else if (ADMIN_MODEL_LISTENER_BINDING.equals(clientItfName)) {
157       return adminlistener;
158     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
159       return repository;
160     }
161     return null;
162   }
163
164   public void bindFc (final String JavaDoc clientItfName, final Object JavaDoc serverItf) {
165     if (CONFIGURATION_BINDING.equals(clientItfName)) {
166       configuration = (Configuration)serverItf;
167     } else if (STORAGE_BINDING.equals(clientItfName)) {
168       storage = (Storage)serverItf;
169     } else if (ADMIN_MODEL_LISTENER_BINDING.equals(clientItfName)) {
170       adminlistener = (AdminModelListener)serverItf;
171     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
172       repository = (Repository)serverItf;
173     }
174   }
175
176   public void unbindFc (final String JavaDoc clientItfName) {
177     if (CONFIGURATION_BINDING.equals(clientItfName)) {
178       configuration = null;
179     } else if (STORAGE_BINDING.equals(clientItfName)) {
180       storage = null;
181     } else if (ADMIN_MODEL_LISTENER_BINDING.equals(clientItfName)) {
182       adminlistener = null;
183     } else if (REPOSITORY_BINDING.equals(clientItfName)) {
184       repository = null;
185     }
186   }
187
188   // -------------------------------------------------------------------------
189
// Implementation of the AdminModel interface
190
// -------------------------------------------------------------------------
191

192   public org.objectweb.fractal.api.Component getInstance (final Component model) {
193     if (!configuration.getRootComponent().contains(model)) {
194       return null;
195     }
196     return (org.objectweb.fractal.api.Component)instances.get(model);
197   }
198
199   // --- createInstance
200

201   public org.objectweb.fractal.api.Component createInstance (
202     final Component model,
203     final org.objectweb.fractal.api.Component bootstrapComponent) throws Exception JavaDoc
204   {
205     if (!configuration.getRootComponent().contains (model)) {
206       return null;
207     }
208     if (getInstance(model) != null) {
209       throw new IllegalOperationException (
210         "Instance already created !");
211     }
212
213     // ADL generation
214
storage.open ("tmp");
215     try {
216       String JavaDoc root = repository.storeComponent (model, null, "inline");
217     } catch (Exception JavaDoc ex) {
218       throw new IllegalOperationException (ex.getMessage());
219     } finally {
220       storage.close();
221     }
222
223     Factory f = FactoryFactory.getFactory(
224       "org.objectweb.fractal.gui.admin.model.TmpFactory",
225       FactoryFactory.FRACTAL_BACKEND, new HashMap JavaDoc());
226     
227     org.objectweb.fractal.api.Component comp;
228     
229     try {
230       Map JavaDoc ctxt = new HashMap JavaDoc();
231       ctxt.put("bootstrap", bootstrapComponent);
232       comp = (org.objectweb.fractal.api.Component)f.newComponent(model.getType(), ctxt);
233       addInstance(model, comp);
234       if (model.getParent() != null && getInstance(model.getParent()) != null) {
235         subComponentAdded(model.getParent(), model, 0);
236       }
237     } catch (Exception JavaDoc ex) {
238       ex.printStackTrace();
239       throw new IllegalOperationException (
240         "No instance created : maybe, the exec. dir is no correct or missing");
241     }
242     
243     adminlistener.componentCreated(model);
244     return comp;
245   }
246
247   // --- addInstances
248

249   private void addInstance (
250     final Component model,
251     final org.objectweb.fractal.api.Component instance) throws Exception JavaDoc
252   {
253     Component c = model;
254     if (model.getMasterComponent() != null) {
255       c = model.getMasterComponent();
256     }
257     instances.put(c, instance);
258     List JavaDoc slaves = c.getSlaveComponents();
259     for (int i = 0; i < slaves.size(); ++i) {
260       instances.put(slaves.get(i), instance);
261     }
262     
263     List JavaDoc subComponents = model.getSubComponents();
264     if (subComponents.size() > 0) {
265       ContentController cc = Fractal.getContentController(instance);
266       org.objectweb.fractal.api.Component[] subCi = cc.getFcSubComponents();
267       
268       for (int i = 0; i < subComponents.size(); ++i) {
269         Component subC = (Component)subComponents.get(i);
270         Set JavaDoc s = new HashSet JavaDoc();
271         if (subC.getMasterComponent() != null) {
272           s.add(subC.getMasterComponent().getName());
273           List JavaDoc l = subC.getMasterComponent().getSlaveComponents();
274           for (int j = 0; j < l.size(); ++j) {
275             s.add(((Component)l.get(j)).getName());
276           }
277         } else {
278           s.add(subC.getName());
279           List JavaDoc l = subC.getSlaveComponents();
280           for (int j = 0; j < l.size(); ++j) {
281             s.add(((Component)l.get(j)).getName());
282           }
283         }
284
285         org.objectweb.fractal.api.Component subM = null;
286         for (int j = 0; j < subCi.length; ++j) {
287           org.objectweb.fractal.api.Component sci = subCi[j];
288           if (s.contains(Fractal.getNameController(sci).getFcName())) {
289             subM = sci;
290             break;
291           }
292         }
293         
294         if (subM == null) {
295           throw new Exception JavaDoc("No instance for sub component '" + subC.getName() + "' " + s);
296         }
297         
298         addInstance(subC, subM);
299       }
300     }
301   }
302
303   // --- deleteInstance
304

305   public void deleteInstance (final Component model) {
306     if (!configuration.getRootComponent().contains(model)) {
307       return;
308     }
309     if (getInstance(model) == null) {
310       throw new IllegalOperationException(
311         "Instance does not exist");
312     }
313     if (isStarted(model)) {
314       throw new IllegalOperationException(
315         "Cannot delete an instance which is started");
316     }
317
318     if (model.getParent() != null) {
319       if (isStarted(model.getParent())) {
320         throw new IllegalOperationException(
321           "Cannot delete an instance whose parent is started");
322       }
323       
324       org.objectweb.fractal.api.Component p = getInstance(model.getParent());
325       org.objectweb.fractal.api.Component c = getInstance(model);
326
327       subComponentRemoved(model.getParent(), model, 0);
328     }
329     
330     removeInstance(model);
331     
332     adminlistener.componentDeleted(model);
333   }
334   
335   private void removeInstance (final Component model) {
336     Component c = model;
337     if (model.getMasterComponent() != null) {
338       c = model.getMasterComponent();
339     }
340     instances.remove(c);
341     List JavaDoc slaves = c.getSlaveComponents();
342     for (int i = 0; i < slaves.size(); ++i) {
343       instances.remove(slaves.get(i));
344     }
345     
346     List JavaDoc subComponents = model.getSubComponents();
347     for (int i = 0; i < subComponents.size(); ++i) {
348       removeInstance((Component)subComponents.get(i));
349     }
350   }
351
352   // --- isStarted
353

354   public boolean isStarted (final Component model) {
355     org.objectweb.fractal.api.Component ci = getInstance(model);
356     if (ci != null) {
357       try {
358         LifeCycleController lc = Fractal.getLifeCycleController(ci);
359         return lc.getFcState().equals("STARTED");
360       } catch (NoSuchInterfaceException e) {
361         return true;
362       }
363     }
364     return false;
365   }
366
367   // --- start
368

369   public void start (final Component model) {
370     org.objectweb.fractal.api.Component ci = getInstance(model);
371
372     if (ci != null) {
373       try {
374         LifeCycleController lc = Fractal.getLifeCycleController(ci);
375         if (lc != null) {
376           lc.startFc();
377
378           // faire comme runAction ?
379
List JavaDoc lser = model.getServerInterfaces();
380           /*if (lser.size() > 0) {
381             ServerInterface ser = (ServerInterface)lser.get(0);
382             String signat = ser.getSignature();
383             String lien = ser.getName();
384             
385             Object iref = null;
386             try {
387               iref = ci.getFcInterface (lien);
388             } catch (Exception ex) {
389               ex.printStackTrace();
390             }
391             
392             // call main method
393             ClassLoader cl = this.getClass().getClassLoader();
394             try {
395               Class cla = cl.loadClass(signat);
396               
397               java.lang.reflect.Method[] meth = cla.getMethods();
398               if (meth.length > 0) {
399                 Object[] arguments = new Object[] { };
400                 String [] items = new String [meth.length];
401                 for (int i = 0; i < meth.length; i++) {
402                   items[i] = meth[i].getName();
403                 }
404                 FrameChoiceBox fcb =
405                   new FrameChoiceBox ("Method for start", items);
406                 if (range < 0) return;
407                 meth[range].invoke(iref, arguments);
408               }
409             } catch (Exception ex) {
410               //ex.printStackTrace();
411               avert ("Invocation failed : "+ex.getMessage());
412               return;
413             }
414           }*/

415         }
416         adminlistener.componentStarted(model);
417       } catch (NoSuchInterfaceException e) {
418       } catch (IllegalLifeCycleException e) {
419 // throw new IllegalOperationException (
420
avert ("Cannot start this instance");
421       }
422     }
423   }
424
425   // --- stop
426

427   public void stop (final Component model) {
428     org.objectweb.fractal.api.Component ci = getInstance(model);
429     if (ci != null) {
430       try {
431         LifeCycleController lc = Fractal.getLifeCycleController(ci);
432         if (lc != null) {
433           lc.stopFc();
434
435           // faire comme runAction ?
436
List JavaDoc lser = model.getServerInterfaces();
437           /*if (lser.size() > 0) {
438             ServerInterface ser = (ServerInterface)lser.get(0);
439             String signat = ser.getSignature();
440             String lien = ser.getName();
441             
442             Object iref = null;
443             try {
444               iref = ci.getFcInterface (lien);
445             } catch (Exception ex) {
446               ex.printStackTrace();
447             }
448             
449             // call main method
450             ClassLoader cl = this.getClass().getClassLoader();
451             try {
452               Class cla = cl.loadClass(signat);
453               
454               java.lang.reflect.Method[] meth = cla.getMethods();
455               if (meth.length > 0) {
456                 Object[] arguments = new Object[] { };
457                 String [] items = new String [meth.length];
458                 for (int i = 0; i < meth.length; i++) {
459                   items[i] = meth[i].getName();
460                 }
461                 FrameChoiceBox fcb =
462                   new FrameChoiceBox ("Method for stop", items);
463                 if (range < 0) return;
464                 meth[range].invoke(iref, arguments);
465               }
466             } catch (Exception ex) { return; }
467           }*/

468         }
469         adminlistener.componentStopped(model);
470       } catch (NoSuchInterfaceException e) {
471       } catch (IllegalLifeCycleException e) {
472         throw new IllegalOperationException (
473           "Cannot stop this instance");
474       }
475     }
476   }
477
478   // -------------------------------------------------------------------------
479
// Implementation of the VetoableConfigurationListener interface
480
// -------------------------------------------------------------------------
481

482   public void canChangeRootComponent () {
483     // nothing to do (no veto)
484
}
485
486   public void canChangeName (final Component component) {
487     // nothing to do (no veto)
488
}
489
490   public void canChangeType (final Component component) {
491     // nothing to do (no veto)
492
}
493
494   public void canChangeImplementation (final Component component) {
495     org.objectweb.fractal.api.Component ci = getInstance(component);
496     if (ci != null) {
497 // throw new IllegalOperationException(
498
avert ("Cannot change the implementation of an instanciated component");
499     }
500   }
501
502   public void canChangeInterfaceName (final Interface i) {
503     org.objectweb.fractal.api.Component ci = getInstance(i.getOwner());
504     if (ci != null) {
505 // throw new IllegalOperationException(
506
avert ("Cannot change the name of an interface of an instanciated component");
507     }
508   }
509
510   public void canChangeInterfaceSignature (final Interface i) {
511     org.objectweb.fractal.api.Component ci = getInstance(i.getOwner());
512     if (ci != null) {
513 // throw new IllegalOperationException(
514
avert ("Cannot change the signature of an interface of an instanciated component");
515     }
516   }
517
518   public void canChangeInterfaceContingency (final Interface i) {
519     org.objectweb.fractal.api.Component ci = getInstance(i.getOwner());
520     if (ci != null) {
521 // throw new IllegalOperationException(
522
avert ("Cannot change the contingency of an interface of an instanciated component");
523     }
524   }
525
526   public void canChangeInterfaceCardinality (final Interface i) {
527     org.objectweb.fractal.api.Component ci = getInstance(i.getOwner());
528     if (ci != null) {
529 // throw new IllegalOperationException(
530
avert ("Cannot change the cardinality of an interface of an instanciated component");
531     }
532   }
533
534   public void canAddClientInterface (
535     final Component component,
536     final ClientInterface i)
537   {
538     org.objectweb.fractal.api.Component ci = getInstance(component);
539     if (ci != null) {
540       if (i.getMasterCollectionInterface() == null) {
541 // throw new IllegalOperationException(
542
avert ("Cannot add an interface to an instanciated component");
543       }
544     }
545   }
546
547   public void canRemoveClientInterface (
548     final Component component,
549     final ClientInterface i)
550   {
551     org.objectweb.fractal.api.Component ci = getInstance(component);
552     if (ci != null) {
553       if (i.getMasterCollectionInterface() == null) {
554 // throw new IllegalOperationException(
555
avert ("Cannot remove an interface from an instanciated component");
556       }
557     }
558   }
559
560   public void canAddServerInterface (
561     final Component component,
562     final ServerInterface i)
563   {
564     org.objectweb.fractal.api.Component ci = getInstance(component);
565     if (ci != null) {
566       if (i.getMasterCollectionInterface() == null) {
567 // throw new IllegalOperationException(
568
avert ("Cannot add an interface to an instanciated component");
569       }
570     }
571   }
572
573   public void canRemoveServerInterface (
574     final Component component,
575     final ServerInterface i)
576   {
577     org.objectweb.fractal.api.Component ci = getInstance(component);
578     if (ci != null) {
579       if (i.getMasterCollectionInterface() == null) {
580 // throw new IllegalOperationException(
581
avert ("Cannot remove an interface from an instanciated component");
582       }
583     }
584   }
585
586   public void canBindInterface (final ClientInterface citf) {
587     if (isStarted(citf.getOwner())) {
588 // throw new IllegalOperationException(
589
// avert ("Cannot bind a client interface of a started component");
590
}
591   }
592
593   public void canRebindInterface (final ClientInterface citf) {
594     if (isStarted(citf.getOwner())) {
595 // throw new IllegalOperationException(
596
avert ("Cannot rebind a client interface of a started component");
597     }
598   }
599
600   public void canUnbindInterface (final ClientInterface citf) {
601     if (isStarted(citf.getOwner())) {
602 // throw new IllegalOperationException(
603
avert ("Cannot unbind a client interface of a started component");
604     }
605   }
606
607   public void canChangeAttributeController (final Component component) {
608     org.objectweb.fractal.api.Component ci = getInstance(component);
609     if (ci != null) {
610 // throw new IllegalOperationException(
611
avert ("Cannot change the attribute controller "
612         +"of an instanciated component");
613     }
614   }
615
616   public void canChangeAttribute (
617     final Component component,
618     final String JavaDoc attributeName)
619   {
620     // nothing to do (no veto)
621
}
622
623   public void canChangeTemplateControllerDescriptor (
624     final Component component)
625   {
626     org.objectweb.fractal.api.Component ci = getInstance(component);
627     if (ci != null) {
628 // throw new IllegalOperationException(
629
avert ("Cannot change the template controller descriptor"
630         +" of an instanciated component");
631     }
632   }
633
634   public void canChangeComponentControllerDescriptor (
635     final Component component)
636   {
637     org.objectweb.fractal.api.Component ci = getInstance(component);
638     if (ci != null) {
639 // throw new IllegalOperationException(
640
avert ("Cannot change the controller descriptor "
641       +"of an instanciated component");
642     }
643   }
644
645   public void canAddSubComponent (
646     final Component parent,
647     final Component child)
648   {
649     if (isStarted(parent) && getInstance(child) != null) {
650 // throw new IllegalOperationException(
651
avert ("Cannot add an instantiated sub component in a started component");
652     }
653   }
654
655   public void canRemoveSubComponent (
656     final Component parent,
657     final Component child)
658   {
659     if (isStarted(parent) && getInstance(child) != null) {
660 // throw new IllegalOperationException(
661
avert ("Cannot remove an instantiated sub component from a started component");
662     }
663   }
664
665   // -------------------------------------------------------------------------
666
// Implementation of the ConfigurationListener interface
667
// -------------------------------------------------------------------------
668

669   public void changeCountChanged (final Component component, long changeCount) {
670     // does nothing
671
}
672
673   public void rootComponentChanged (final Component oldValue) {
674     // does nothing
675
}
676
677   public void nameChanged (final Component component, final String JavaDoc oldValue) {
678     org.objectweb.fractal.api.Component ci = getInstance(component);
679     if (ci != null) {
680       try {
681         NameController nc = Fractal.getNameController(ci);
682         nc.setFcName(component.getName());
683       } catch (NoSuchInterfaceException e) {
684         avert (e.getMessage());
685       }
686     }
687   }
688
689   public void typeChanged (final Component component, final String JavaDoc oldValue) {
690     // does nothing
691
}
692
693   public void implementationChanged (
694     final Component component,
695     final String JavaDoc oldValue)
696   {
697     // nothing to do (should never be called, because of veto)
698
}
699
700   public void interfaceNameChanged (final Interface i, final String JavaDoc oldValue) {
701     // nothing to do (should never be called, because of veto)
702
}
703
704   public void interfaceSignatureChanged (
705     final Interface i,
706     final String JavaDoc oldValue)
707   {
708     // nothing to do (should never be called, because of veto)
709
}
710
711   public void interfaceContingencyChanged (
712     final Interface i,
713     final boolean oldValue)
714   {
715     // nothing to do (should never be called, because of veto)
716
}
717
718   public void interfaceCardinalityChanged (
719     final Interface i,
720     final boolean oldValue)
721   {
722     // nothing to do (should never be called, because of veto)
723
}
724
725   public void clientInterfaceAdded (
726     final Component component,
727     final ClientInterface i,
728     final int index)
729   {
730     // only called for collection interface
731
Binding b = i.getBinding();
732     if (b != null) {
733       interfaceBound(i, b.getServerInterface());
734     }
735   }
736
737   public void clientInterfaceRemoved (
738     final Component component,
739     final ClientInterface i,
740     final int index)
741   {
742     // only called for collection interface
743
Binding b = i.getBinding();
744     if (b != null) {
745       interfaceUnbound(i, b.getServerInterface());
746     }
747   }
748
749   public void serverInterfaceAdded (
750     final Component component,
751     final ServerInterface i,
752     final int index)
753   {
754     // nothing to do (should never be called, because of veto)
755
}
756
757   public void serverInterfaceRemoved (
758     final Component component,
759     final ServerInterface i,
760     final int index)
761   {
762     // nothing to do (should never be called, because of veto)
763
}
764
765   public void interfaceBound (
766     final ClientInterface citf,
767     final ServerInterface sitf)
768   {
769     org.objectweb.fractal.api.Component ci = getInstance(citf.getOwner());
770     org.objectweb.fractal.api.Component si = getInstance(sitf.getOwner());
771     if (ci != null && si != null) {
772       BindingController bc;
773       try {
774         bc = Fractal.getBindingController(ci);
775         Object JavaDoc itf;
776         if (sitf.isInternal()) {
777           itf = Fractal.getContentController(si).getFcInternalInterface(sitf.getName());
778         } else {
779           itf = si.getFcInterface(sitf.getName());
780         }
781         bc.bindFc (citf.getName(), itf);
782       } catch (Exception JavaDoc e) {
783         avert (e.getMessage());
784         return;
785       }
786     }
787   }
788
789   public void interfaceRebound (
790     final ClientInterface citf,
791     final ServerInterface oldSitf)
792   {
793     org.objectweb.fractal.api.Component ci = getInstance(citf.getOwner());
794     interfaceUnbound(citf, oldSitf);
795     interfaceBound(citf, citf.getBinding().getServerInterface());
796   }
797
798   public void interfaceUnbound (
799     final ClientInterface citf,
800     final ServerInterface sitf)
801   {
802     org.objectweb.fractal.api.Component ci = getInstance(citf.getOwner());
803     org.objectweb.fractal.api.Component si = getInstance(sitf.getOwner());
804     if (ci != null && si != null) {
805       BindingController bc;
806       try {
807         bc = Fractal.getBindingController(ci);
808         bc.unbindFc (citf.getName());
809       } catch (Exception JavaDoc e) {
810         avert (e.getMessage());
811       }
812     }
813   }
814
815   public void attributeControllerChanged (
816     final Component component,
817     final String JavaDoc oldValue)
818   {
819     // nothing to do (should never be called, because of veto)
820
}
821
822   public void attributeChanged (
823     final Component component,
824     final String JavaDoc attributeName,
825     final String JavaDoc oldValue)
826   {
827     org.objectweb.fractal.api.Component ci = getInstance(component);
828     if (ci != null) {
829       try {
830         AttributeController ac = (AttributeController)ci.getFcInterface(
831           "attribute-controller");
832         if (ac != null) {
833           Class JavaDoc acc = ac.getClass();
834
835           String JavaDoc attrName = Character.toUpperCase(attributeName.charAt(0)) + attributeName.substring(1);
836           String JavaDoc getterName = "get" + attrName;
837           String JavaDoc setterName = "set" + attrName;
838           Method JavaDoc getter = acc.getMethod (getterName, new Class JavaDoc[0]);
839           Method JavaDoc setter = acc.getMethod (setterName, new Class JavaDoc[] {
840             getter.getReturnType()
841           });
842
843           Class JavaDoc attrType = getter.getReturnType();
844           String JavaDoc v = component.getAttribute(attributeName);
845           Object JavaDoc value;
846           if (attrType.equals(String JavaDoc.class)) {
847             value = v;
848           } else if (attrType.isPrimitive()) {
849             if (attrType.equals(Integer.TYPE)) {
850               value = Integer.valueOf(v);
851             } else if (attrType.equals(Long.TYPE)) {
852               value = Long.valueOf(v);
853             } else if (attrType.equals(Float.TYPE)) {
854               value = Float.valueOf(v);
855             } else if (attrType.equals(Double.TYPE)) {
856               value = Double.valueOf(v);
857             } else if (attrType.equals(Byte.TYPE)) {
858               value = Byte.valueOf(v);
859             } else if (attrType.equals(Character.TYPE)) {
860               if (v.length() != 1) {
861                 avert ("Bad char value: " + v);
862                 return;
863               }
864               value = new Character JavaDoc(v.charAt(0));
865             } else if (attrType.equals(Short.TYPE)) {
866               value = Short.valueOf(v);
867             } else if (attrType.equals(Boolean.TYPE)) {
868               if (!v.equals("true") &&
869                 !v.equals("false"))
870               {
871                 avert ("Bad boolean value: " + v);
872                 return;
873               }
874               value = new Boolean JavaDoc(v.equals("true"));
875             } else {
876               avert ("Unexpected case");
877               return;
878             }
879           } else {
880             avert ("Unsupported attribute type: " + attrType);
881             return;
882           }
883           setter.invoke(ac, new Object JavaDoc[]{value});
884         }
885       } catch (Exception JavaDoc ex) {
886         avert (ex.getMessage());
887      }
888     }
889   }
890
891   public void templateControllerDescriptorChanged (
892     final Component component,
893     final String JavaDoc oldValue)
894   {
895     // nothing to do (should never be called, because of veto)
896
}
897
898   public void componentControllerDescriptorChanged (
899     final Component component,
900     final String JavaDoc oldValue)
901   {
902     // nothing to do (should never be called, because of veto)
903
}
904
905   public void subComponentAdded (
906     final Component parent,
907     final Component child,
908     final int index)
909   {
910     org.objectweb.fractal.api.Component pi = getInstance(parent);
911     org.objectweb.fractal.api.Component ci = getInstance(child);
912     if (pi != null && ci != null) {
913       try {
914         ContentController cc = Fractal.getContentController(pi);
915         cc.addFcSubComponent(ci);
916         List JavaDoc itfs = child.getClientInterfaces();
917         for (int i = 0; i < itfs.size(); ++i) {
918           Binding b = ((ClientInterface)itfs.get(i)).getBinding();
919           if (b != null) {
920             addBinding(b, parent, child);
921           }
922         }
923         itfs = child.getServerInterfaces();
924         for (int i = 0; i < itfs.size(); ++i) {
925           List JavaDoc bs = ((ServerInterface)itfs.get(i)).getBindings();
926           for (int j = 0; j < bs.size(); ++j) {
927             Binding b = (Binding)bs.get(j);
928             addBinding(b, parent, child);
929           }
930         }
931       } catch (Exception JavaDoc e) {
932         avert(e.getMessage());
933       }
934     }
935   }
936   
937   private void addBinding (Binding b, Component parent, Component child) throws Exception JavaDoc {
938     Component c = b.getClientInterface().getOwner();
939     org.objectweb.fractal.api.Component ci = getInstance(c);
940     String JavaDoc citf = b.getClientInterface().getName();
941     Component s = b.getServerInterface().getOwner();
942     if ((child == s || parent == s || parent == s.getParent()) &&
943         (child == c || parent == c || parent == c.getParent()))
944     {
945       org.objectweb.fractal.api.Component si = getInstance(s);
946       Object JavaDoc sitf;
947       if (b.getServerInterface().isInternal()) {
948         sitf = Fractal.getContentController(si).getFcInternalInterface(
949             b.getServerInterface().getName());
950       } else {
951         sitf = si.getFcInterface(b.getServerInterface().getName());
952       }
953       Fractal.getBindingController(ci).bindFc(citf, sitf);
954     }
955   }
956   
957   public void subComponentRemoved (
958     final Component parent,
959     final Component child,
960     final int index)
961   {
962     org.objectweb.fractal.api.Component pi = getInstance(parent);
963     org.objectweb.fractal.api.Component ci = (org.objectweb.fractal.api.Component)instances.get(child);
964     if (pi != null && ci != null) {
965       try {
966         List JavaDoc itfs = child.getClientInterfaces();
967         for (int i = 0; i < itfs.size(); ++i) {
968           Binding b = ((ClientInterface)itfs.get(i)).getBinding();
969           if (b != null) {
970             removeBinding(b, parent, child);
971           }
972         }
973         itfs = child.getServerInterfaces();
974         for (int i = 0; i < itfs.size(); ++i) {
975           List JavaDoc bs = ((ServerInterface)itfs.get(i)).getBindings();
976           for (int j = 0; j < bs.size(); ++j) {
977             Binding b = (Binding)bs.get(j);
978             removeBinding(b, parent, child);
979           }
980         }
981         
982         ContentController cc;
983         cc = Fractal.getContentController(pi);
984         cc.removeFcSubComponent(ci);
985       } catch (Exception JavaDoc e) {
986         e.printStackTrace();
987         avert (e.getMessage());
988       }
989     }
990   }
991   
992   private void removeBinding (Binding b, Component parent, Component child) throws Exception JavaDoc {
993     Component c = b.getClientInterface().getOwner();
994     org.objectweb.fractal.api.Component ci =
995       (org.objectweb.fractal.api.Component)instances.get(c);
996     Component s = b.getServerInterface().getOwner();
997     if ((child == s || parent == s || parent == s.getParent()) &&
998         (child == c || parent == c || parent == c.getParent()))
999     {
1000      Fractal.getBindingController(ci).unbindFc(b.getClientInterface().getName());
1001    }
1002  }
1003  
1004  private void avert (String JavaDoc motif) {
1005    JOptionPane.showMessageDialog (
1006      null, motif, "Error", JOptionPane.ERROR_MESSAGE);
1007    throw new IllegalOperationException(motif);
1008  }
1009
1010/**
1011 * This subclass asks for a method name.
1012 */

1013
1014  /**
1015   * Font Arial, plain, 13 pts
1016   */

1017  static public final Font fnta13 = new Font ("Arial", Font.PLAIN, 13);
1018
1019  class FrameChoiceBox extends JDialog implements ActionListener {
1020      JPanel panel = new JPanel();
1021    JButton [] unit;
1022
1023      protected void processWindowEvent(WindowEvent e) {
1024          if (e.getID() == WindowEvent.WINDOW_CLOSING) { cancel(); }
1025          super.processWindowEvent(e);
1026      }
1027
1028      void cancel() { range = -1; dispose(); }
1029
1030      public void actionPerformed (ActionEvent e) {
1031      range = -1;
1032      for (int i = 0; i < unit.length; i++) {
1033        if (e.getActionCommand().equals(unit[i].getText())) {
1034          range = i; break;
1035        }
1036      }
1037      dispose();
1038      }
1039
1040      public FrameChoiceBox (String JavaDoc titre, String JavaDoc [] items) {
1041          unit = new JButton [items.length];
1042          enableEvents (AWTEvent.WINDOW_EVENT_MASK);
1043          Dimension dim = new Dimension (300, 20+items.length*25);
1044
1045          try {
1046              this.setTitle(titre);
1047              Container cp = getContentPane ();
1048              cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
1049              setResizable(true);
1050
1051              Color curcol = Color.lightGray;
1052              for (int i = 0; i < items.length; i++) {
1053                  unit[i] = new JButton (items[i]);
1054                  unit[i].setPreferredSize(new Dimension(150, 20));
1055                  unit[i].setFont(fnta13);
1056                  FontMetrics JavaDoc fm = unit[i].getFontMetrics(fnta13);
1057                  int d = 0;
1058                  for (int z = 0; z < items[i].length(); z++)
1059                d = d + fm.charWidth(items[i].charAt(z));
1060                  unit[i].setBorder(new EmptyBorder (2, 1, 2, 70-d));
1061                  unit[i].setBackground(curcol);
1062                  unit[i].setForeground(Color.black);
1063                  unit[i].addActionListener(this);
1064                  cp.add(unit[i]);
1065              }
1066              setModal(true);
1067              this.setSize(dim);
1068              this.setLocation(300, 300);
1069              pack ();
1070              show ();
1071          }
1072          catch(Exception JavaDoc e) { e.printStackTrace(); }
1073      }
1074  }
1075// --------------------------
1076
}
1077
Popular Tags