KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > dialog > model > BasicDialogModel


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.dialog.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 import org.objectweb.fractal.gui.selection.model.Selection;
35 import org.objectweb.fractal.gui.selection.model.SelectionListener;
36
37 import javax.swing.DefaultListSelectionModel JavaDoc;
38 import javax.swing.ListSelectionModel JavaDoc;
39 import javax.swing.table.TableModel JavaDoc;
40 import javax.swing.text.Document JavaDoc;
41
42 /**
43  * Basic implementation of the {@link DialogModel} interface. This mode listens
44  * to the configuration and selection models in order to update itself when the
45  * configuration or the selection changes.
46  */

47
48 public class BasicDialogModel implements
49   DialogModel,
50   ConfigurationListener,
51   SelectionListener,
52   BindingController
53 {
54
55   /**
56    * A mandatory client interface bound to a {@link Configuration configuration}
57    * model. This configuration is used to get the component on which this dialog
58    * model is based (this component is the root component of the configuration).
59    */

60
61   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
62
63   /**
64    * A mandatory client interface bound to a {@link Selection selection} model.
65    * This is the model on which the Swing list selection models of this model
66    * are based.
67    */

68
69   public final static String JavaDoc SELECTION_BINDING = "selection";
70
71   /**
72    * The configuration client interface.
73    */

74
75   private Configuration configuration;
76
77   /**
78    * The selection client interface.
79    */

80
81   private Selection selection;
82
83   /**
84    * The table models representing the external interfaces.
85    */

86
87   private InterfaceTableModel[] itfTableModel;
88
89   /**
90    * The table selection models representing the external interfaces.
91    */

92
93   private InterfaceTableSelectionModel[] itfTableSelectionModel;
94
95   /**
96    * The table model representing the attributes.
97    */

98
99   private AttributeTableModel attrTableModel;
100
101   /**
102    * The table selection model representing the attributes.
103    */

104
105   private ListSelectionModel JavaDoc attrTableSelectionModel;
106
107   /**
108    * The text field model representing the component's name.
109    */

110
111   private TextFieldModel nameFieldModel;
112
113   /**
114    * The text field model representing the component's type.
115    */

116
117   private TextFieldModel typeFieldModel;
118
119   /**
120    * The text field model representing the component's implementation.
121    */

122
123   private TextFieldModel implementationFieldModel;
124
125   /**
126    * The text field model representing the component's attribute controller.
127    */

128
129   private TextFieldModel attrControllerFieldModel;
130
131   /**
132    * The text field model representing the component's template controller
133    * descriptor.
134    */

135
136   private TextFieldModel tmplControllerDescFieldModel;
137
138   /**
139    * The text field model representing the component's controller descriptor.
140    */

141
142   private TextFieldModel compControllerDescFieldModel;
143
144   /**
145    * Constructs a new {@link BasicDialogModel} component.
146    */

147
148   public BasicDialogModel () {
149     itfTableModel = new InterfaceTableModel[2];
150     itfTableSelectionModel = new InterfaceTableSelectionModel[2];
151     for (int i = 0; i < 2; ++i) {
152       itfTableModel[i] = new InterfaceTableModel(i == 0);
153       itfTableSelectionModel[i] = new InterfaceTableSelectionModel(i == 0);
154     }
155
156     attrTableModel = new AttributeTableModel();
157     attrTableSelectionModel = new DefaultListSelectionModel JavaDoc();
158     attrTableSelectionModel.setSelectionMode(
159       ListSelectionModel.SINGLE_SELECTION);
160
161     nameFieldModel = new TextFieldModel(TextFieldModel.NAME);
162     typeFieldModel = new TextFieldModel(TextFieldModel.TYPE);
163     implementationFieldModel =
164       new TextFieldModel(TextFieldModel.IMPLEMENTATION);
165     attrControllerFieldModel =
166       new TextFieldModel(TextFieldModel.ATTRIBUTE_CONTROLLER);
167     tmplControllerDescFieldModel =
168       new TextFieldModel(TextFieldModel.TEMPLATE_CONTROLLER_DESC);
169     compControllerDescFieldModel =
170       new TextFieldModel(TextFieldModel.COMPONENT_CONTROLLER_DESC);
171   }
172
173   // -------------------------------------------------------------------------
174
// Implementation of the BindingController interface
175
// -------------------------------------------------------------------------
176

177   public String JavaDoc[] listFc () {
178     return new String JavaDoc[] {
179       CONFIGURATION_BINDING,
180       SELECTION_BINDING
181     };
182   }
183
184   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
185     if (CONFIGURATION_BINDING.equals(clientItfName)) {
186       return configuration;
187     } else if (SELECTION_BINDING.equals(clientItfName)) {
188       return selection;
189     }
190     return null;
191   }
192
193   public void bindFc (
194     final String JavaDoc clientItfName,
195     final Object JavaDoc serverItf)
196   {
197     if (CONFIGURATION_BINDING.equals(clientItfName)) {
198       configuration = (Configuration) serverItf;
199     } else if (SELECTION_BINDING.equals(clientItfName)) {
200       selection = (Selection)serverItf;
201       for (int i = 0; i < itfTableSelectionModel.length; ++i) {
202         itfTableSelectionModel[i].setSelection(selection);
203       }
204     }
205   }
206
207   public void unbindFc (final String JavaDoc clientItfName) {
208     if (CONFIGURATION_BINDING.equals(clientItfName)) {
209       configuration = null;
210     } else if (SELECTION_BINDING.equals(clientItfName)) {
211       selection = null;
212       for (int i = 0; i < itfTableSelectionModel.length; ++i) {
213         itfTableSelectionModel[i].setSelection(null);
214       }
215     }
216   }
217
218   // -------------------------------------------------------------------------
219
// Implementation of the DialogModel interface
220
// -------------------------------------------------------------------------
221

222   public TableModel JavaDoc getClientInterfacesTableModel () {
223     return itfTableModel[0];
224   }
225
226   public ListSelectionModel JavaDoc getClientInterfacesTableSelectionModel() {
227     return itfTableSelectionModel[0];
228   }
229
230   public TableModel JavaDoc getServerInterfacesTableModel() {
231     return itfTableModel[1];
232   }
233
234   public InterfaceTableSelectionModel getServerInterfacesTableSelectionModel() {
235     return itfTableSelectionModel[1];
236   }
237
238   public TableModel JavaDoc getAttributesTableModel() {
239     return attrTableModel;
240   }
241
242   public ListSelectionModel JavaDoc getAttributesTableSelectionModel() {
243     return attrTableSelectionModel;
244   }
245
246   public Document JavaDoc getNameFieldModel() {
247     return nameFieldModel;
248   }
249
250   public Document JavaDoc getTypeFieldModel() {
251     return typeFieldModel;
252   }
253
254   public Document JavaDoc getImplementationFieldModel() {
255     return implementationFieldModel;
256   }
257
258   public Document JavaDoc getAttrControllerFieldModel() {
259     return attrControllerFieldModel;
260   }
261
262   public Document JavaDoc getTmplControllerDescFieldModel() {
263     return tmplControllerDescFieldModel;
264   }
265
266   public Document JavaDoc getCompControllerDescFieldModel() {
267     return compControllerDescFieldModel;
268   }
269
270   // -------------------------------------------------------------------------
271
// Implementation of the ConfigurationListener interface
272
// -------------------------------------------------------------------------
273

274   public void changeCountChanged (Component component, long changeCount) {
275     // does nothing
276
}
277
278   public void rootComponentChanged (final Component oldValue) {
279     final Component root = configuration.getRootComponent();
280     for (int i = 0; i < itfTableModel.length; ++i) {
281       itfTableModel[i].setComponentModel(root);
282       itfTableSelectionModel[i].setComponentModel(root);
283     }
284
285     attrTableModel.setComponentModel(root);
286
287     nameFieldModel.setComponentModel(root);
288     typeFieldModel.setComponentModel(root);
289     implementationFieldModel.setComponentModel(root);
290     attrControllerFieldModel.setComponentModel(root);
291     tmplControllerDescFieldModel.setComponentModel(root);
292     compControllerDescFieldModel.setComponentModel(root);
293   }
294
295   public void nameChanged (final Component component, final String JavaDoc oldValue) {
296     if (component == configuration.getRootComponent()) {
297       nameFieldModel.componentTextChanged(component.getName());
298     }
299   }
300
301   public void typeChanged (final Component component, final String JavaDoc oldValue) {
302     if (component == configuration.getRootComponent()) {
303       typeFieldModel.componentTextChanged(component.getType());
304     }
305   }
306
307   public void implementationChanged (
308     final Component component,
309     final String JavaDoc oldValue)
310   {
311     if (component == configuration.getRootComponent()) {
312       implementationFieldModel.componentTextChanged(
313         component.getImplementation());
314     }
315   }
316
317   public void interfaceNameChanged (final Interface i, final String JavaDoc oldValue) {
318     interfaceChanged(i);
319   }
320
321   public void interfaceSignatureChanged (
322     final Interface i,
323     final String JavaDoc oldValue)
324   {
325     interfaceChanged(i);
326   }
327
328   public void interfaceContingencyChanged (
329     final Interface i,
330     final boolean oldValue)
331   {
332     interfaceChanged(i);
333   }
334
335   public void interfaceCardinalityChanged (
336     final Interface i,
337     final boolean oldValue)
338   {
339     interfaceChanged(i);
340   }
341
342   /**
343    * Notifies the appropriate table model that an interface has changed.
344    *
345    * @param i the interface that has changed.
346    */

347
348   private void interfaceChanged (final Interface i) {
349     if (i.getOwner() == configuration.getRootComponent()) {
350       if (i instanceof ClientInterface) {
351         itfTableModel[0].interfaceChanged(i);
352       } else {
353         itfTableModel[1].interfaceChanged(i);
354       }
355     }
356   }
357
358   public void clientInterfaceAdded (
359     final Component component,
360     final ClientInterface i,
361     final int index)
362   {
363     if (component == configuration.getRootComponent()) {
364       itfTableModel[0].interfaceAdded(i, index);
365     }
366   }
367
368   public void clientInterfaceRemoved (
369     final Component component,
370     final ClientInterface i,
371     final int index)
372   {
373     if (component == configuration.getRootComponent()) {
374       itfTableModel[0].interfaceRemoved(i, index);
375     }
376   }
377
378   public void serverInterfaceAdded (
379     final Component component,
380     final ServerInterface i,
381     final int index)
382   {
383     if (component == configuration.getRootComponent()) {
384       itfTableModel[1].interfaceAdded(i, index);
385     }
386   }
387
388   public void serverInterfaceRemoved (
389     final Component component,
390     final ServerInterface i,
391     final int index)
392   {
393     if (component == configuration.getRootComponent()) {
394       itfTableModel[1].interfaceRemoved(i, index);
395     }
396   }
397
398   public void interfaceBound (
399     final ClientInterface citf,
400     final ServerInterface sitf)
401   {
402     if (citf.getOwner() == configuration.getRootComponent()) {
403       // TODO
404
}
405   }
406
407   public void interfaceRebound (
408     final ClientInterface citf,
409     final ServerInterface oldSitf)
410   {
411     if (citf.getOwner() == configuration.getRootComponent()) {
412       // TODO
413
}
414   }
415
416   public void interfaceUnbound (
417     final ClientInterface citf,
418     final ServerInterface sitf)
419   {
420     if (citf.getOwner() == configuration.getRootComponent()) {
421       // TODO
422
}
423   }
424
425   public void attributeControllerChanged (
426     final Component component,
427     final String JavaDoc oldValue)
428   {
429     if (component == configuration.getRootComponent()) {
430       attrControllerFieldModel.componentTextChanged(
431         component.getAttributeController());
432     }
433   }
434
435   public void attributeChanged (
436     final Component component,
437     final String JavaDoc attributeName,
438     final String JavaDoc oldValue)
439   {
440     if (component == configuration.getRootComponent()) {
441       attrTableModel.attributeChanged(attributeName, oldValue);
442     }
443   }
444
445   public void templateControllerDescriptorChanged (
446     final Component component,
447     final String JavaDoc oldValue)
448   {
449     if (component == configuration.getRootComponent()) {
450       tmplControllerDescFieldModel.componentTextChanged(
451         component.getTemplateControllerDescriptor());
452     }
453   }
454
455   public void componentControllerDescriptorChanged (
456     final Component component,
457     final String JavaDoc oldValue)
458   {
459     if (component == configuration.getRootComponent()) {
460       compControllerDescFieldModel.componentTextChanged(
461         component.getComponentControllerDescriptor());
462     }
463   }
464
465   public void subComponentAdded (
466     final Component parent,
467     final Component child,
468     final int index)
469   {
470     // does nothing
471
}
472
473   public void subComponentRemoved (
474     final Component parent,
475     final Component child,
476     final int index)
477   {
478     // does nothing
479
}
480
481   // -------------------------------------------------------------------------
482
// Implementation of the SelectionListener interface
483
// -------------------------------------------------------------------------
484

485   public void selectionChanged () {
486     Object JavaDoc o = selection.getSelection();
487     if (o != null) {
488       if (o instanceof Interface) {
489         o = ((Interface)o).getOwner();
490       }
491       configuration.setRootComponent((Component)o);
492     }
493     for (int i = 0; i < itfTableSelectionModel.length; ++i) {
494       itfTableSelectionModel[i].selectionChanged();
495     }
496   }
497 }
498
Popular Tags