KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > dialog > control > BasicDialogController


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.control;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.dialog.model.DialogModel;
29 import org.objectweb.fractal.gui.dialog.view.DialogViewListener;
30 import org.objectweb.fractal.gui.model.ClientInterface;
31 import org.objectweb.fractal.gui.model.Component;
32 import org.objectweb.fractal.gui.model.Configuration;
33 import org.objectweb.fractal.gui.model.Factory;
34 import org.objectweb.fractal.gui.model.ServerInterface;
35 import org.objectweb.fractal.gui.model.IllegalOperationException;
36 import org.objectweb.fractal.gui.selection.model.Selection;
37
38 import javax.swing.JOptionPane JavaDoc;
39
40 /**
41  * A controller component for dialog view components. This component modifies
42  * a dialog model, in reaction to events emitted by a dialog view.
43  */

44
45 public class BasicDialogController implements
46   DialogViewListener,
47   BindingController
48 {
49
50   /**
51    * A mandatory client interface bound to a {@link Configuration configuration}
52    * model. This model is used to get the component to which interfaces or
53    * attributes must be added or removed by this controller (the component used
54    * is the root component of the configuration).
55    */

56
57   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
58
59   /**
60    * A mandatory client interface bound to a {@link Selection selection}
61    * model. This model is used to get the interface that must be removed when
62    * a "remove interface" button is clicked.
63    */

64
65   public final static String JavaDoc SELECTION_BINDING = "selection";
66
67   /**
68    * A mandatory client interface bound to a {@link DialogModel dialog} model.
69    * This model is used to get selection model of the attribute table, which is
70    * itself used to find the attribute to be removed when the "remove attribute"
71    * button is clicked.
72    */

73
74   public final static String JavaDoc DIALOG_MODEL_BINDING = "dialog-model";
75
76   /**
77    * A mandatory client interface bound to a {@link Factory factory}. This
78    * factory is used to create interfaces when a "add interface" button is
79    * clicked.
80    */

81
82   public final static String JavaDoc FACTORY_BINDING = "configuration-factory";
83
84   /**
85    * The configuration client interface.
86    */

87
88   private Configuration configuration;
89
90   /**
91    * The selection client interface.
92    */

93
94   private Selection selection;
95
96   /**
97    * The dialog model client interface.
98    */

99
100   private DialogModel model;
101
102   /**
103    * The factory client interface.
104    */

105
106   private Factory factory;
107
108   /**
109    * Constructs a new {@link BasicDialogController} component.
110    */

111
112   public BasicDialogController () {
113   }
114
115   // -------------------------------------------------------------------------
116
// Implementation of the BindingController interface
117
// -------------------------------------------------------------------------
118

119   public String JavaDoc[] listFc () {
120     return new String JavaDoc[] {
121       CONFIGURATION_BINDING,
122       SELECTION_BINDING,
123       DIALOG_MODEL_BINDING,
124       FACTORY_BINDING
125     };
126   }
127
128   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
129     if (CONFIGURATION_BINDING.equals(clientItfName)) {
130       return configuration;
131     } else if (SELECTION_BINDING.equals(clientItfName)) {
132       return selection;
133     } else if (DIALOG_MODEL_BINDING.equals(clientItfName)) {
134       return model;
135     } else if (FACTORY_BINDING.equals(clientItfName)) {
136       return factory;
137     }
138     return null;
139   }
140
141   public void bindFc (
142     final String JavaDoc clientItfName,
143     final Object JavaDoc serverItf)
144   {
145     if (CONFIGURATION_BINDING.equals(clientItfName)) {
146       configuration = (Configuration) serverItf;
147     } else if (SELECTION_BINDING.equals(clientItfName)) {
148       selection = (Selection)serverItf;
149     } else if (DIALOG_MODEL_BINDING.equals(clientItfName)) {
150       model = (DialogModel) serverItf;
151     } else if (FACTORY_BINDING.equals(clientItfName)) {
152       factory = (Factory) serverItf;
153     }
154   }
155
156   public void unbindFc (final String JavaDoc clientItfName) {
157     if (CONFIGURATION_BINDING.equals(clientItfName)) {
158       configuration = null;
159     } else if (SELECTION_BINDING.equals(clientItfName)) {
160       selection = null;
161     } else if (DIALOG_MODEL_BINDING.equals(clientItfName)) {
162       model = null;
163     } else if (FACTORY_BINDING.equals(clientItfName)) {
164       factory = null;
165     }
166   }
167
168   // -------------------------------------------------------------------------
169
// Implementation of the DialogViewListener interface
170
// -------------------------------------------------------------------------
171

172   public void addClientInterfaceButtonClicked () {
173     Component component = configuration.getRootComponent();
174     ClientInterface i = factory.createClientInterface(component);
175     component.addClientInterface(i);
176   }
177
178   public void removeClientInterfaceButtonClicked () {
179     try {
180       ClientInterface i = (ClientInterface)selection.getSelection();
181       i.getOwner().removeClientInterface(i);
182       selection.clearSelection();
183     } catch (IllegalOperationException ioe) {
184       JOptionPane.showMessageDialog(
185         null, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
186     }
187   }
188
189   public void addServerInterfaceButtonClicked () {
190     Component component = configuration.getRootComponent();
191     ServerInterface i = factory.createServerInterface(component);
192     component.addServerInterface(i);
193   }
194
195   public void removeServerInterfaceButtonClicked () {
196     try {
197       ServerInterface i = (ServerInterface)selection.getSelection();
198       i.getOwner().removeServerInterface(i);
199       selection.clearSelection();
200     } catch (IllegalOperationException ioe) {
201       JOptionPane.showMessageDialog(
202         null, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
203     }
204   }
205
206   public void addAttributeButtonClicked () {
207     Component component = configuration.getRootComponent();
208     int index = component.getAttributeNames().size();
209     component.setAttribute("attribute " + index, "empty");
210   }
211
212   public void removeAttributeButtonClicked () {
213     Component component = configuration.getRootComponent();
214     int index = model.getAttributesTableSelectionModel().getMinSelectionIndex();
215     String JavaDoc name = (String JavaDoc)component.getAttributeNames().get(index);
216     component.setAttribute(name, null);
217   }
218 }
219
Popular Tags