KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.control.BindingController;
27 import org.objectweb.fractal.api.control.LifeCycleController;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * Basic implementation of the {@link Configuration} interface.
39  */

40
41 public class BasicConfiguration implements
42   Configuration,
43   Factory,
44   BindingController,
45   LifeCycleController
46 {
47
48   /**
49    * A collection client interface bound to the {@link
50    * VetoableConfigurationListener vetoable listeners} of this model.
51    */

52
53   public final static String JavaDoc VETOABLE_CONFIGURATION_LISTENERS_BINDING =
54     "vetoable-configuration-listeners";
55
56   /**
57    * A collection client interface bound to the {@link ConfigurationListener
58    * listeners} of this model.
59    */

60
61   public final static String JavaDoc CONFIGURATION_LISTENERS_BINDING =
62     "configuration-listeners";
63
64   /**
65    * The vetoable listeners client interface.
66    */

67
68   private Map JavaDoc vetoableListeners;
69
70   /**
71    * The listeners client interface.
72    */

73
74   private Map JavaDoc listeners;
75
76   /**
77    * Root component of this configuration.
78    */

79
80   private Component root;
81
82   /**
83    * If this configuration has been initialized or not.
84    */

85
86   private boolean initialized;
87
88   /**
89    * If this configuration has been started or not.
90    */

91
92   private boolean started;
93
94   /**
95    * ChangeCount of this configuration.
96    */

97
98   private long changeCount;
99
100   /**
101    * Storage directory of this configuration.
102    */

103
104   private String JavaDoc storage;
105
106   /**
107    * Constructs a new basic configuration component.
108    */

109
110   public BasicConfiguration () {
111     root = createComponent();
112     vetoableListeners = new HashMap JavaDoc();
113     listeners = new HashMap JavaDoc();
114     listeners.put("", new StatusManager());
115   }
116
117   /**
118    * Returns the vetoable listeners of this configuration.
119    *
120    * @return the vetoable listeners of this configuration.
121    */

122
123   List JavaDoc getVetoableListeners () {
124     return new ArrayList JavaDoc(vetoableListeners.values());
125   }
126
127   /**
128    * Returns the listeners of this configuration.
129    *
130    * @return the listeners of this configuration.
131    */

132
133   List JavaDoc getListeners () {
134     return new ArrayList JavaDoc(listeners.values());
135   }
136
137   // -------------------------------------------------------------------------
138
// Implementation of the BindingController interface
139
// -------------------------------------------------------------------------
140

141   public String JavaDoc[] listFc () {
142     Set JavaDoc itfs = new HashSet JavaDoc();
143     itfs.addAll(listeners.keySet());
144     itfs.addAll(vetoableListeners.keySet());
145     return (String JavaDoc[])itfs.toArray(new String JavaDoc[itfs.size()]);
146   }
147
148   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
149     if (clientItfName.startsWith(CONFIGURATION_LISTENERS_BINDING)) {
150       return listeners.get(clientItfName);
151     } else if (clientItfName.startsWith(VETOABLE_CONFIGURATION_LISTENERS_BINDING)) {
152       vetoableListeners.get(clientItfName);
153     }
154     return null;
155   }
156
157   public void bindFc (
158     final String JavaDoc clientItfName,
159     final Object JavaDoc serverItf)
160   {
161     if (clientItfName.startsWith(CONFIGURATION_LISTENERS_BINDING)) {
162       listeners.put(clientItfName, serverItf);
163     } else if (clientItfName.startsWith(VETOABLE_CONFIGURATION_LISTENERS_BINDING)) {
164       vetoableListeners.put(clientItfName, serverItf);
165     }
166   }
167
168   public void unbindFc (final String JavaDoc clientItfName) {
169     if (clientItfName.startsWith(CONFIGURATION_LISTENERS_BINDING)) {
170       listeners.remove(clientItfName);
171     } else if (clientItfName.startsWith(VETOABLE_CONFIGURATION_LISTENERS_BINDING)) {
172       vetoableListeners.remove(clientItfName);
173     }
174   }
175
176   // -------------------------------------------------------------------------
177
// Implementation of the LifeCycleController interface
178
// -------------------------------------------------------------------------
179

180   public String JavaDoc getFcState () {
181     return started ? STARTED : STOPPED;
182   }
183
184   public void startFc () {
185     if (!initialized) {
186       setRootComponent(createComponent());
187       initialized = true;
188     }
189     started = true;
190     changeCount = 0;
191   }
192
193   public void stopFc () {
194     started = false;
195   }
196
197   // -------------------------------------------------------------------------
198
// Implementation of the Configuration interface
199
// -------------------------------------------------------------------------
200

201   public Component getRootComponent () {
202     return root;
203   }
204
205   public void setRootComponent (final Component root) {
206     if (root == null) {
207       throw new IllegalArgumentException JavaDoc();
208     }
209     Component oldRoot = this.root;
210     if (root != oldRoot) {
211       Iterator JavaDoc i = vetoableListeners.values().iterator();
212       while (i.hasNext()) {
213         VetoableConfigurationListener l =
214           (VetoableConfigurationListener)i.next();
215         l.canChangeRootComponent();
216       }
217       this.root = root;
218       i = listeners.values().iterator();
219       while (i.hasNext()) {
220         ConfigurationListener l = (ConfigurationListener)i.next();
221         l.rootComponentChanged(oldRoot);
222       }
223     }
224     initialized = true;
225     changeCount = 0;
226   }
227
228   public long getChangeCount () {
229     return changeCount;
230   }
231
232   public void setChangeCount (long changeCount) {
233     this.changeCount = changeCount;
234     Iterator JavaDoc i = listeners.values().iterator();
235     while (i.hasNext()) {
236       ConfigurationListener l = (ConfigurationListener)i.next();
237       l.changeCountChanged(getRootComponent(), changeCount);
238     }
239   }
240
241   public String JavaDoc getStorage () {
242     return storage;
243   }
244
245   public void setStorage (String JavaDoc storage) {
246     this.storage = storage;
247   }
248
249   // -------------------------------------------------------------------------
250
// Implementation of the Factory interface
251
// -------------------------------------------------------------------------
252

253   public Component createComponent () {
254     return new BasicComponent(this);
255   }
256
257   public Component createComponent (final Component component) {
258     BasicComponent c;
259     if (component.getMasterComponent() != null) {
260       c = (BasicComponent)component.getMasterComponent();
261     } else {
262       c = (BasicComponent)component;
263     }
264     return new SharedComponent(c);
265   }
266
267   public ClientInterface createClientInterface (Component owner) {
268     if (owner.getMasterComponent() != null) {
269       owner = owner.getMasterComponent();
270     }
271     return new BasicClientInterface((BasicComponent)owner);
272   }
273
274   public ServerInterface createServerInterface (Component owner) {
275     if (owner.getMasterComponent() != null) {
276       owner = owner.getMasterComponent();
277     }
278     return new BasicServerInterface((BasicComponent)owner);
279   }
280 }
281
Popular Tags