KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > tree > model > BasicTreeModel


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.tree.model;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.model.ClientInterface;
29 import org.objectweb.fractal.gui.model.Component;
30 import org.objectweb.fractal.gui.model.Configuration;
31 import org.objectweb.fractal.gui.model.ConfigurationListener;
32 import org.objectweb.fractal.gui.model.Interface;
33 import org.objectweb.fractal.gui.model.ServerInterface;
34
35 import java.util.EventListener JavaDoc;
36
37 import javax.swing.event.EventListenerList JavaDoc;
38 import javax.swing.event.TreeModelEvent JavaDoc;
39 import javax.swing.event.TreeModelListener JavaDoc;
40 import javax.swing.tree.TreeModel JavaDoc;
41 import javax.swing.tree.TreePath JavaDoc;
42
43 /**
44  * A {@link TreeModel} based on a {@link Configuration}. This model makes a
45  * conversion from a {@link Configuration} model to a {@link TreeModel}. It
46  * listens to the configuration in order to update its state when the
47  * configuration changes.
48  */

49
50 public class BasicTreeModel implements
51   TreeModel JavaDoc,
52   ConfigurationListener,
53   BindingController
54 {
55
56   /**
57    * A mandatory client interface bound to a {@link Configuration configuration}
58    * model. This is the model on which this model is based and synchronized.
59    */

60
61   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
62
63   /**
64    * Root of this tree model when its content is empty.
65    */

66
67   private final static Object JavaDoc EMPTY_TREE = new Object JavaDoc();
68
69   /**
70    * The configuration client interface.
71    */

72
73   private Configuration configuration;
74
75   /**
76    * The Swing listeners of this swing tree model.
77    */

78
79   private EventListenerList JavaDoc listeners;
80
81   /**
82    * Constructs a new {@link BasicTreeModel} component.
83    */

84
85   public BasicTreeModel () {
86     this.listeners = new EventListenerList JavaDoc();
87   }
88
89   // -------------------------------------------------------------------------
90
// Implementation of the BindingController interface
91
// -------------------------------------------------------------------------
92

93   public String JavaDoc[] listFc () {
94     return new String JavaDoc[] { CONFIGURATION_BINDING };
95   }
96
97   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
98     if (CONFIGURATION_BINDING.equals(clientItfName)) {
99       return configuration;
100     }
101     return null;
102   }
103
104   public void bindFc (
105     final String JavaDoc clientItfName,
106     final Object JavaDoc serverItf)
107   {
108     if (CONFIGURATION_BINDING.equals(clientItfName)) {
109       configuration = (Configuration)serverItf;
110     }
111   }
112
113   public void unbindFc (final String JavaDoc clientItfName) {
114     if (CONFIGURATION_BINDING.equals(clientItfName)) {
115       configuration = null;
116     }
117   }
118
119   // -------------------------------------------------------------------------
120
// Implementation of the ConfigurationListener interface
121
// -------------------------------------------------------------------------
122

123   public void changeCountChanged (Component component, long changeCount) {
124     // does nothing
125
}
126
127   public void rootComponentChanged (final Component oldValue) {
128     Object JavaDoc oldRoot = oldValue == null ? EMPTY_TREE : oldValue;
129     TreeModelEvent JavaDoc e = new TreeModelEvent JavaDoc(
130       this,
131       new Object JavaDoc[]{oldRoot},
132       null,
133       null
134     );
135     EventListener JavaDoc[] l = listeners.getListeners(TreeModelListener JavaDoc.class);
136     for (int i = 0; i < l.length; ++i) {
137       ((TreeModelListener JavaDoc)l[i]).treeStructureChanged(e);
138     }
139   }
140
141   public void nameChanged (final Component component, final String JavaDoc oldValue) {
142     if (!configuration.getRootComponent().contains(component)) {
143       return;
144     }
145     TreeModelEvent JavaDoc e;
146     if (component == configuration.getRootComponent()) {
147       e = new TreeModelEvent JavaDoc(
148         this,
149         new Object JavaDoc[]{component},
150         new int[]{0},
151         new Object JavaDoc[]{component});
152     } else {
153       e = new TreeModelEvent JavaDoc(
154         this,
155         component.getParent().getPath(),
156         new int[]{getIndexOfChild(component.getParent(), component)},
157         new Object JavaDoc[]{component});
158     }
159     EventListener JavaDoc[] l = listeners.getListeners(TreeModelListener JavaDoc.class);
160     for (int i = 0; i < l.length; ++i) {
161       ((TreeModelListener JavaDoc)l[i]).treeNodesChanged(e);
162     }
163   }
164
165   public void typeChanged (final Component component, final String JavaDoc oldValue) {
166     // does nothing
167
}
168
169   public void implementationChanged (
170     final Component component,
171     final String JavaDoc oldValue)
172   {
173     // does nothing
174
}
175
176   public void interfaceNameChanged (final Interface i, final String JavaDoc oldValue) {
177     // does nothing
178
}
179
180   public void interfaceSignatureChanged (
181     final Interface i,
182     final String JavaDoc oldValue)
183   {
184     // does nothing
185
}
186
187   public void interfaceContingencyChanged (
188     final Interface i,
189     final boolean oldValue)
190   {
191     // does nothing
192
}
193
194   public void interfaceCardinalityChanged (
195     final Interface i,
196     final boolean oldValue)
197   {
198     // does nothing
199
}
200
201   public void clientInterfaceAdded (
202     final Component component,
203     final ClientInterface i,
204     final int index)
205   {
206     // does nothing
207
}
208
209   public void clientInterfaceRemoved (
210     final Component component,
211     final ClientInterface i,
212     final int index)
213   {
214     // does nothing
215
}
216
217   public void serverInterfaceAdded (
218     final Component component,
219     final ServerInterface i,
220     final int index)
221   {
222     // does nothing
223
}
224
225   public void serverInterfaceRemoved (
226     final Component component,
227     final ServerInterface i,
228     final int index)
229   {
230     // does nothing
231
}
232
233   public void interfaceBound (
234     final ClientInterface citf,
235     final ServerInterface sitf)
236   {
237     // does nothing
238
}
239
240   public void interfaceRebound (
241     final ClientInterface citf,
242     final ServerInterface oldSitf)
243   {
244     // does nothing
245
}
246
247   public void interfaceUnbound (
248     final ClientInterface citf,
249     final ServerInterface sitf)
250   {
251     // does nothing
252
}
253
254   public void attributeControllerChanged (
255     final Component component,
256     final String JavaDoc oldValue)
257   {
258     // does nothing
259
}
260
261   public void attributeChanged (
262     final Component component,
263     final String JavaDoc attributeName,
264     final String JavaDoc oldValue)
265   {
266     // does nothing
267
}
268
269   public void templateControllerDescriptorChanged (
270     final Component component,
271     final String JavaDoc oldValue)
272   {
273     // does nothing
274
}
275
276   public void componentControllerDescriptorChanged (
277     final Component component,
278     final String JavaDoc oldValue)
279   {
280     // does nothing
281
}
282
283   public void subComponentAdded (
284     final Component parent,
285     final Component child,
286     final int index)
287   {
288     if (!configuration.getRootComponent().contains(parent)) {
289       return;
290     }
291     TreeModelEvent JavaDoc e = new TreeModelEvent JavaDoc(
292       this,
293       parent.getPath(),
294       new int[]{index},
295       new Object JavaDoc[]{child});
296     EventListener JavaDoc[] l = listeners.getListeners(TreeModelListener JavaDoc.class);
297     for (int i = 0; i < l.length; ++i) {
298       ((TreeModelListener JavaDoc)l[i]).treeNodesInserted(e);
299     }
300   }
301
302   public void subComponentRemoved (
303     final Component parent,
304     final Component child,
305     final int index)
306   {
307     if (!configuration.getRootComponent().contains(parent)) {
308       return;
309     }
310     TreeModelEvent JavaDoc e = new TreeModelEvent JavaDoc(
311       this,
312       parent.getPath(),
313       new int[]{index},
314       new Object JavaDoc[]{child});
315     EventListener JavaDoc[] l = listeners.getListeners(TreeModelListener JavaDoc.class);
316     for (int i = 0; i < l.length; ++i) {
317       ((TreeModelListener JavaDoc)l[i]).treeNodesRemoved(e);
318     }
319   }
320
321   // -------------------------------------------------------------------------
322
// Implementation of the TreeModel interface
323
// -------------------------------------------------------------------------
324

325   public Object JavaDoc getRoot () {
326     if (configuration == null) {
327       return EMPTY_TREE;
328     } else {
329       return configuration.getRootComponent();
330     }
331   }
332
333   public int getChildCount (final Object JavaDoc parent) {
334     if (parent == EMPTY_TREE) {
335       return 0;
336     } else {
337       return ((Component)parent).getSubComponents().size();
338     }
339   }
340
341   public Object JavaDoc getChild (final Object JavaDoc parent, final int index) {
342     return ((Component)parent).getSubComponents().get(index);
343   }
344
345   public int getIndexOfChild (final Object JavaDoc parent, final Object JavaDoc child) {
346     if (parent == null) {
347       return 0;
348     }
349     return ((Component)parent).getSubComponents().indexOf(child);
350   }
351
352   public boolean isLeaf (final Object JavaDoc node) {
353     if (node == EMPTY_TREE) {
354       return true;
355     } else {
356       return ((Component)node).getSubComponents().size() == 0;
357     }
358   }
359
360   public void valueForPathChanged (final TreePath JavaDoc path, final Object JavaDoc newValue) {
361     // should not be called
362
}
363
364   public void addTreeModelListener (final TreeModelListener JavaDoc l) {
365     listeners.add(TreeModelListener JavaDoc.class, l);
366   }
367
368   public void removeTreeModelListener (final TreeModelListener JavaDoc l) {
369     listeners.remove(TreeModelListener JavaDoc.class, l);
370   }
371 }
372
Popular Tags