KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > plugin > gui > ParameterPanel


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 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  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.plugin.gui;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.EventListener JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.apache.log4j.Category;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Composite;
35 import org.objectweb.clif.scenario.util.isac.plugin.ObjectDescription;
36 import org.objectweb.clif.scenario.util.isac.plugin.PluginDescription;
37 import org.objectweb.clif.scenario.util.isac.plugin.SampleDescription;
38 import org.objectweb.clif.scenario.util.isac.plugin.TestDescription;
39 import org.objectweb.clif.scenario.util.isac.plugin.TimerDescription;
40 import org.objectweb.clif.scenario.util.isac.util.tree.Node;
41
42 /**
43  * Implementation of the panel which will store all the paraemters widgets
44  *
45  * @author JC Meillaud
46  * @author A Peyrard
47  */

48 public class ParameterPanel extends Composite {
49     private Hashtable JavaDoc params;
50
51     private Composite parent;
52
53     static Category cat = Category.getInstance(ParameterPanel.class.getName());
54
55     /**
56      * Build a new parameter panel
57      *
58      * @param parent
59      * The parent composite of this panel
60      */

61     public ParameterPanel(Composite parent) {
62         super(parent, SWT.FLAT);
63         cat.debug("-> constructor");
64         this.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
65         GridLayout grid = new GridLayout();
66         grid.numColumns = 1;
67         this.setLayout(grid);
68         this.params = new Hashtable JavaDoc();
69     }
70
71     /**
72      * Add a new widget to this panel
73      *
74      * @param params
75      * The widgets parameters
76      * @param style
77      * The style of the widget
78      * @param name
79      * The name of the parameter which is representing by this widget
80      * @param listener
81      * The modify listener which will be used for each parameter
82      * widgets
83      *
84      */

85     public Composite addWidget(Hashtable JavaDoc params, int style, String JavaDoc name,
86             String JavaDoc label, Composite c, EventListener JavaDoc listener) {
87         cat.debug("-> addWidget");
88         ParameterWidget pw = new ParameterWidget(style, name, label, params, c,
89                 listener);
90         this.params.put(name, pw);
91         this.layout();
92         return pw.getComposite();
93     }
94
95     /**
96      * Build a hashtable with all parameters names and parameters values
97      *
98      * @return The hashtable containing the values and names of parameters
99      */

100     public Hashtable JavaDoc getParametersValues() {
101         cat.debug("-> getParametersValues");
102         Hashtable JavaDoc values = new Hashtable JavaDoc();
103         Enumeration JavaDoc e = this.params.elements();
104         while (e.hasMoreElements()) {
105             ParameterWidget temp = (ParameterWidget) e.nextElement();
106             if (temp.getStyle() != WidgetDescription.GROUP) {
107                 if (temp.getValue() != null)
108                     values.put(temp.getName(), temp.getValue());
109                 else
110                     values.put(temp.getName(), "");
111             }
112         }
113         return values;
114     }
115
116     /**
117      * Analyse a gui XML file, this kind of file define all the panels
118      * parameters for a plugin
119      *
120      * @param plugin
121      * The plugin description
122      * @param fileName
123      * The fileName to be analysed
124      * @param panels
125      * The table where the panels must be stored
126      */

127     public static void createNewPanelsFromXML(PluginDescription plugin,
128             String JavaDoc fileName, Hashtable JavaDoc panels) {
129
130         cat.debug("-> createNewPanelsFromXML");
131         // if the file does not exist, create the default panels
132
if ((fileName == null) || (fileName.equals("null"))) {
133             createNewDefaultsPanels(plugin, panels);
134             return;
135         }
136
137         // load the gui description file and get the errors
138
Vector JavaDoc errors = GUIDescriptionParser.loadGUIDescriptionFile(fileName,
139                 plugin, panels);
140     }
141
142     /**
143      * create all the default widgets tree for the actions of the given plugin
144      *
145      * @param plugin
146      * The plugin
147      * @param panels
148      * The table of widgets tree, to be completed
149      */

150     private static void createNewDefaultsPanels(PluginDescription plugin,
151             Hashtable JavaDoc panels) {
152         cat.debug("-> createNewDefaultsPanels");
153         // load the object description
154
ObjectDescription obj = plugin.getObject();
155         // get the parameters
156
if (obj != null) {
157             Vector JavaDoc params = obj.getParams();
158             cat.warn("THE NB PARAMS = " + params.size());
159             ParametersWidgetsNode tree = ParametersWidgetsNode
160                     .createParametersWidgetsNode(params.elements(), Node.USE,
161                             plugin.getName());
162             int intKey = tree.hashCode();
163             while (panels.containsKey(new String JavaDoc((new Integer JavaDoc(intKey))
164                     .toString())))
165                 intKey++;
166             panels.put(new String JavaDoc((new Integer JavaDoc(intKey)).toString()), tree);
167             obj.setGUIKey(new String JavaDoc((Integer.toString(intKey))));
168         }
169         // load the samples descriptions
170
Hashtable JavaDoc samples = plugin.getSamples();
171         if (samples != null) {
172             // for each sample
173
Enumeration JavaDoc samplesKeys = samples.keys();
174             while (samplesKeys.hasMoreElements()) {
175                 String JavaDoc sampleKey = (String JavaDoc) samplesKeys.nextElement();
176                 SampleDescription sample = (SampleDescription) samples
177                         .get(sampleKey);
178                 Vector JavaDoc params = sample.getParams();
179                 if (params != null) {
180                     ParametersWidgetsNode tree = ParametersWidgetsNode
181                             .createParametersWidgetsNode(params.elements(),
182                                     Node.SAMPLE, plugin.getName());
183                     int intKey = tree.hashCode();
184                     while (panels.containsKey(new String JavaDoc((new Integer JavaDoc(intKey))
185                             .toString())))
186                         intKey++;
187                     panels.put(new String JavaDoc((new Integer JavaDoc(intKey)).toString()),
188                             tree);
189                     sample.setGUIKey(new String JavaDoc((new Integer JavaDoc(intKey))
190                             .toString()));
191                 } else {
192                     ParametersWidgetsNode tree = ParametersWidgetsNode
193                             .createParametersWidgetsNode(null, Node.SAMPLE,
194                                     plugin.getName());
195                     int intKey = tree.hashCode();
196                     while (panels.containsKey(new String JavaDoc((new Integer JavaDoc(intKey))
197                             .toString())))
198                         intKey++;
199                     panels.put(new String JavaDoc((new Integer JavaDoc(intKey)).toString()),
200                             tree);
201                     sample.setGUIKey(new String JavaDoc((new Integer JavaDoc(intKey))
202                             .toString()));
203                 }
204             }
205         }
206         // load the timers descriptions
207
Hashtable JavaDoc timers = plugin.getTimers();
208         if (timers != null) {
209             // for each timer
210
Enumeration JavaDoc timersKeys = timers.keys();
211             while (timersKeys.hasMoreElements()) {
212                 String JavaDoc timerKey = (String JavaDoc) timersKeys.nextElement();
213                 TimerDescription timer = (TimerDescription) timers
214                         .get(timerKey);
215                 Vector JavaDoc params = timer.getParams();
216                 if (params != null) {
217                     ParametersWidgetsNode tree = ParametersWidgetsNode
218                             .createParametersWidgetsNode(params.elements(),
219                                     Node.TIMER, plugin.getName());
220                     int intKey = tree.hashCode();
221                     while (panels.containsKey(new String JavaDoc((new Integer JavaDoc(intKey))
222                             .toString())))
223                         intKey++;
224                     panels.put(new String JavaDoc((new Integer JavaDoc(intKey)).toString()),
225                             tree);
226                     timer
227                             .setGUIKey(new String JavaDoc((new Integer JavaDoc(intKey))
228                                     .toString()));
229                 } else {
230                     ParametersWidgetsNode tree = ParametersWidgetsNode
231                             .createParametersWidgetsNode(null, Node.TIMER,
232                                     plugin.getName());
233                     int intKey = tree.hashCode();
234                     while (panels.containsKey(new String JavaDoc((new Integer JavaDoc(intKey))
235                             .toString())))
236                         intKey++;
237                     panels.put(new String JavaDoc((new Integer JavaDoc(intKey)).toString()),
238                             tree);
239                     timer
240                             .setGUIKey(new String JavaDoc((new Integer JavaDoc(intKey))
241                                     .toString()));
242                 }
243             }
244         }
245         // load the tests descriptions
246
Hashtable JavaDoc tests = plugin.getTests();
247         if (tests != null) {
248             // for each test
249
Enumeration JavaDoc testsKeys = tests.keys();
250             while (testsKeys.hasMoreElements()) {
251                 String JavaDoc testKey = (String JavaDoc) testsKeys.nextElement();
252                 TestDescription test = (TestDescription) tests.get(testKey);
253                 Vector JavaDoc params = test.getParams();
254                 if (params != null) {
255                     ParametersWidgetsNode tree = ParametersWidgetsNode
256                             .createParametersWidgetsNode(params.elements(),
257                                     Node.TEST, plugin.getName());
258                     int intKey = tree.hashCode();
259                     while (panels.containsKey(new String JavaDoc((new Integer JavaDoc(intKey))
260                             .toString())))
261                         intKey++;
262                     panels.put(new String JavaDoc((new Integer JavaDoc(intKey)).toString()),
263                             tree);
264                     test
265                             .setGUIKey(new String JavaDoc((new Integer JavaDoc(intKey))
266                                     .toString()));
267                 } else {
268                     ParametersWidgetsNode tree = ParametersWidgetsNode
269                             .createParametersWidgetsNode(null, Node.TEST,
270                                     plugin.getName());
271                     int intKey = tree.hashCode();
272                     while (panels.containsKey(new String JavaDoc((new Integer JavaDoc(intKey))
273                             .toString())))
274                         intKey++;
275                     panels.put(new String JavaDoc((new Integer JavaDoc(intKey)).toString()),
276                             tree);
277                     test
278                             .setGUIKey(new String JavaDoc((new Integer JavaDoc(intKey))
279                                     .toString()));
280                 }
281             }
282         }
283     }
284
285     /**
286      * Build a new parameter panel from a parameters widgets tree
287      *
288      * @param node
289      * The tree
290      * @param parent
291      * The parent composite
292      * @param listener
293      * The modify listener which will be used for each parameter
294      * widgets
295      * @return The parameter panel created
296      */

297     public static ParameterPanel createParameterPanel(
298             ParametersWidgetsNode node, Composite parent,
299             ModifyListener listener) {
300         cat.debug("-> createParameterPanel");
301         ParameterPanel panel = new ParameterPanel(parent);
302         analyseParametersWidgetsNode(panel, node, panel, listener);
303
304         return panel;
305     }
306
307     /**
308      * Analyse a parameters widgets node, to build the panel
309      *
310      * @param panel
311      * The panel to be build
312      * @param node
313      * The node to be analysed
314      * @param parent
315      * The parent composite to add the new widget
316      * @param The
317      * modify listener which will be used for each parameter widgets
318      */

319     private static void analyseParametersWidgetsNode(ParameterPanel panel,
320             ParametersWidgetsNode node, Composite parent,
321             ModifyListener listener) {
322         cat.debug("-> analyseParametersWidgetsNode");
323         WidgetDescription desc = node.getWidget();
324         Composite composite = null;
325         if (desc != null) {
326             composite = panel.addWidget(desc.getParams(), desc.getType(), desc
327                     .getText(), desc.getLabel(), parent, listener);
328             if (composite == null)
329                 return;
330         } else
331             composite = parent;
332         // analyse the children of the node
333
Vector JavaDoc children = node.getChildren();
334         for (int i = 0; i < children.size(); i++) {
335             ParametersWidgetsNode child = (ParametersWidgetsNode) children
336                     .elementAt(i);
337             analyseParametersWidgetsNode(panel, child, composite, listener);
338         }
339     }
340
341     /**
342      * method wich set the values for the combo which is named "id"
343      *
344      * @param values
345      * The values
346      */

347     public void setComboValues(Vector JavaDoc values) {
348         cat.debug("-> setComboValues");
349         if (this.params.containsKey("id")) {
350             ParameterWidget pw = (ParameterWidget) this.params.get("id");
351             pw.setComboValues(values);
352         }
353     }
354
355     /**
356      * method which set the values of the differents parameters
357      *
358      * @param values
359      * The parameters values
360      */

361     public void setParametersValues(Hashtable JavaDoc values) {
362         cat.debug("-> set ParametersValues");
363         if (values == null)
364             return;
365         // get the parameters name
366
Enumeration JavaDoc keys = values.keys();
367         while (keys.hasMoreElements()) {
368             String JavaDoc name = (String JavaDoc) keys.nextElement();
369             if (this.params.containsKey(name)) {
370                 ((ParameterWidget) this.params.get(name))
371                         .setValue((String JavaDoc) values.get(name));
372             } else {
373                 cat.warn(name + " can't be found");
374             }
375         }
376     }
377
378     public boolean addButtonSelected(Object JavaDoc source) {
379         Enumeration JavaDoc elements = params.elements();
380         while (elements.hasMoreElements()) {
381             ParameterWidget pw = (ParameterWidget) elements.nextElement();
382             if (pw.getStyle() == WidgetDescription.NFIELD) {
383                 if (pw.addEmtyFieldForNField(source))
384                     return true;
385             } else if (pw.getStyle() == WidgetDescription.TABLE) {
386                 if (pw.addEmtyEntryForTable(source))
387                     return true;
388             }
389         }
390         return false;
391     }
392
393     public boolean removeButtonSelected(Object JavaDoc source) {
394         Enumeration JavaDoc elements = params.elements();
395         while (elements.hasMoreElements()) {
396             ParameterWidget pw = (ParameterWidget) elements.nextElement();
397             if (pw.getStyle() == WidgetDescription.NFIELD) {
398                 if (pw.removeLastFieldForNField(source))
399                     return true;
400             } else if (pw.getStyle() == WidgetDescription.TABLE) {
401                 if (pw.removeLastEntryForTable(source))
402                     return true;
403             }
404         }
405         return false;
406     }
407
408     public boolean modifySomething(Object JavaDoc source) {
409         Enumeration JavaDoc elements = params.elements();
410         while (elements.hasMoreElements()) {
411             ParameterWidget pw = (ParameterWidget) elements.nextElement();
412             if (pw.getStyle() == WidgetDescription.TABLE) {
413                 if (pw.modifyText(source))
414                     return true;
415             }
416         }
417         return false;
418     }
419
420     /**
421      * dispose all the elements of the panel
422      */

423     public void dispose() {
424         cat.debug("-> dispose");
425         if (params != null) {
426             Enumeration JavaDoc elements = params.elements();
427             while (elements.hasMoreElements()) {
428                 ParameterWidget pw = (ParameterWidget) elements.nextElement();
429                 pw.dispose();
430             }
431         }
432         this.dispose();
433     }
434 }
Popular Tags