KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.log4j.Category;
30 import org.eclipse.jface.window.ApplicationWindow;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.custom.ScrolledComposite;
33 import org.eclipse.swt.events.ModifyEvent;
34 import org.eclipse.swt.events.ModifyListener;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.events.SelectionListener;
37 import org.eclipse.swt.layout.FillLayout;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Combo;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Label;
43 import org.objectweb.clif.scenario.util.isac.gui.ScenarioGUIEditor;
44 import org.objectweb.clif.scenario.util.isac.plugin.PluginDescription;
45 import org.objectweb.clif.scenario.util.isac.plugin.PluginManager;
46 import org.objectweb.clif.scenario.util.isac.util.tree.Node;
47 import org.objectweb.clif.scenario.util.isac.util.tree.NodeDescription;
48 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode;
49 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
50 import org.objectweb.clif.scenario.util.isac.util.tree.nodes.NonePluginNode;
51 /**
52  * Implementation of the object which will store the parameters definition
53  * panels
54  *
55  * @author JC Meillaud
56  * @author A Peyrard
57  */

58 public class PluginGUIManager implements SelectionListener, ModifyListener {
59     static Category cat = Category.getInstance(PluginGUIManager.class.getName()) ;
60     
61     /**
62      * The unique instance of the object
63      */

64     protected static PluginGUIManager instance = null;
65     /**
66      * The table which will store all the editable panels for each gui key
67      */

68     private Hashtable JavaDoc panels;
69     private Composite parent;
70     private ScrolledComposite top;
71     private ScrolledComposite contents;
72     private Composite contentsComposite;
73     private Composite topComposite;
74     private Composite mainComposite;
75     private Composite main;
76     private Label topLabel;
77     private GridData gridDataTop;
78     private ParameterPanel parametersPanel;
79     private ScenarioNode currentNode;
80     private Combo comboTop;
81     private ScenarioGUIEditor window;
82     private Hashtable JavaDoc nonePluginPanels ;
83
84     /**
85      * Inaccessible constructor, build the instance of PluginGUIManager
86      */

87     protected PluginGUIManager(ApplicationWindow window) {
88         cat.debug("-> constructor") ;
89         this.panels = new Hashtable JavaDoc();
90         this.window = (ScenarioGUIEditor) window;
91     }
92
93     /**
94      * Instance of the PluginGUIManager getter
95      *
96      * @return The instance of this object
97      */

98     public static PluginGUIManager getPluginGUIManager(ApplicationWindow window) {
99         cat.debug("-> getPluginGUIManager") ;
100         if (instance == null) {
101             if (window == null)
102                 return null;
103             instance = new PluginGUIManager(window);
104         }
105         return instance;
106     }
107
108     /**
109      * Method which will set the parent composite, we will create the parameters
110      * panel into it
111      *
112      * @param parent
113      * The parent composite
114      */

115     public void setParentComposite(Composite parent) {
116         cat.debug("-> setParentComposite") ;
117         // store the parent composite
118
this.parent = new Composite(parent, SWT.FLAT);
119         // set the parent layout
120
GridLayout gridLayout = new GridLayout();
121         gridLayout.verticalSpacing = 4;
122         gridLayout.numColumns = 1;
123         gridLayout.marginHeight = 0;
124         gridLayout.marginWidth = 0;
125         this.parent.setLayout(gridLayout);
126         // create the top composite
127
this.top = new ScrolledComposite(this.parent, SWT.BORDER | SWT.H_SCROLL
128                 | SWT.V_SCROLL);
129         this.top.setExpandHorizontal(true);
130         this.top.setExpandVertical(true);
131         this.top.setAlwaysShowScrollBars(false);
132         // set the grid data of the top composite
133
this.gridDataTop = new GridData();
134         gridDataTop.grabExcessHorizontalSpace = true;
135         gridDataTop.horizontalAlignment = GridData.FILL;
136         this.top.setLayoutData(this.gridDataTop);
137         // create the main composite
138
this.main = new Composite(this.parent, SWT.FLAT);
139         this.main.setLayout(new FillLayout());
140         this.main.setBackground(this.parent.getDisplay().getSystemColor(
141                 SWT.COLOR_WHITE));
142         GridData gridDataMain = new GridData();
143         gridDataMain.grabExcessHorizontalSpace = true;
144         gridDataMain.grabExcessVerticalSpace = true;
145         gridDataMain.horizontalAlignment = GridData.FILL;
146         gridDataMain.verticalAlignment = GridData.FILL;
147         this.main.setLayoutData(gridDataMain);
148         // add a listener
149
this.mainComposite = new Composite(this.main, SWT.BORDER);
150         this.mainComposite.setBackground(this.parent.getDisplay()
151                 .getSystemColor(SWT.COLOR_WHITE));
152
153         // create the top elements
154
this.createLabelTopComposite("Panel for parameters nodes edition");
155     }
156
157     /**
158      * Update the scrolled composite which contains the top panel
159      */

160     private void updateTopPanel() {
161         cat.debug("-> updateTopPanel") ;
162         this.top.setContent(this.topComposite);
163         this.top.setMinSize(this.topComposite.computeSize(SWT.DEFAULT,
164                 SWT.DEFAULT));
165         this.top.layout();
166         this.topComposite.layout();
167         // set the size of the top composite
168
this.gridDataTop.heightHint = this.topComposite.computeSize(
169                 SWT.DEFAULT, SWT.DEFAULT).y;
170         this.parent.layout();
171     }
172
173     /**
174      * Create a new Panel for the top of the form in this panel we will print
175      * the name of the node
176      *
177      * @param text
178      * The text to be printed in the label
179      */

180     private void createLabelTopComposite(String JavaDoc text) {
181         cat.debug("-> createLabelTopComposite") ;
182         // create the top composite
183
this.topComposite = new Composite(this.top, SWT.FLAT);
184         this.topComposite.setBackground(this.top.getDisplay().getSystemColor(
185                 SWT.COLOR_WHITE));
186         // define the layout of the composite
187
GridLayout gridLayout = new GridLayout();
188         gridLayout.numColumns = 1;
189         this.topComposite.setLayout(gridLayout);
190         this.topLabel = new Label(this.topComposite, SWT.CENTER | SWT.BORDER);
191         // Set the alignement of the label that will be added
192
GridData gridData = new GridData();
193         gridData.horizontalAlignment = GridData.FILL;
194         gridData.grabExcessHorizontalSpace = true;
195         this.topLabel.setLayoutData(gridData);
196         this.topLabel.setText(text);
197         this.updateTopPanel();
198     }
199
200     /**
201      * This method create a new switcher, to select the test for the controler
202      * node
203      *
204      * @param type
205      * The type of the controler node
206      */

207     private void createTestSwitcherTopComposite(String JavaDoc type) {
208         cat.debug("-> createTestSwitcherTopComposite") ;
209         this.topComposite = new Composite(this.top, SWT.FLAT);
210         this.topComposite.setBackground(this.top.getDisplay().getSystemColor(
211                 SWT.COLOR_WHITE));
212         // define the layout of the composite
213
GridLayout gridLayout = new GridLayout();
214         gridLayout.numColumns = 1;
215         this.topComposite.setLayout(gridLayout);
216         this.topLabel = new Label(this.topComposite, SWT.CENTER | SWT.BORDER);
217         // Set the alignement of the label that will be added
218
GridData gridData = new GridData();
219         gridData.horizontalAlignment = GridData.FILL;
220         gridData.grabExcessHorizontalSpace = true;
221         this.topLabel.setLayoutData(gridData);
222         this.topLabel.setText("Select a test condition of the " + type
223                 + " controler node :");
224         // add the combo to show the diffrents choices
225
this.comboTop = new Combo(this.topComposite, SWT.FLAT | SWT.READ_ONLY);
226         this.comboTop.setBackground(this.top.getDisplay().getSystemColor(
227                 SWT.COLOR_WHITE));
228         GridData gridDataCombo = new GridData();
229         gridDataCombo.horizontalAlignment = GridData.FILL;
230         gridDataCombo.grabExcessHorizontalSpace = true;
231         comboTop.setLayoutData(gridDataCombo);
232         // add a modify listener
233
comboTop.addSelectionListener(this);
234 // comboTop.addModifyListener(this) ;
235
// get the used plugins...
236
Vector JavaDoc usedPlugins = (TreeManager.getTreeManager(null)).getUsedPluginsName() ;
237         Vector JavaDoc nodesDescs = (PluginManager.getPluginManager())
238                 .createNodesDescriptionsByPlugins(usedPlugins, Node.TEST);
239         // add to the combo the different tests
240
for (int i = 0; i < nodesDescs.size(); i++) {
241             NodeDescription nodeDesc = (NodeDescription) nodesDescs
242                     .elementAt(i);
243             comboTop.add(nodeDesc.getPlugin() + "." + nodeDesc.getActionName());
244         }
245         // set the default text of the combo
246
this.comboTop.setText("");
247         this.updateTopPanel();
248     }
249
250     /**
251      * Change the parameters panels
252      *
253      * @param node
254      * The node which the parameters must be show
255      */

256     public void switchPanel(ScenarioNode node) {
257         cat.debug("-> switchPanel") ;
258         // update the current node variable
259
this.currentNode = node;
260         // check if the two switchable panels are not null
261
// if they aren't dispose them
262
if (this.topComposite != null)
263             this.topComposite.dispose();
264         if (this.mainComposite != null)
265             this.mainComposite.dispose();
266         // get the tree manager instance
267
TreeManager treeManager = TreeManager.getTreeManager(null);
268         // get the type of the node
269
String JavaDoc type = treeManager.getNodeType(node);
270         if (type == null) {
271             this.createLabelTopComposite("Panel for parameters nodes edition");
272             showParametersEditionPanel(null, null);
273             return;
274         }
275         // check if the node is a plugin node
276
if (Node.isPluginNode(type)) {
277             NodeDescription nodeDesc = treeManager.getNodeDescription(node);
278             this.createLabelTopComposite(type + " : " + nodeDesc.getPlugin()
279                     + "." + nodeDesc.getActionName());
280             String JavaDoc guiKey = (PluginManager.getPluginManager())
281                     .getPluginActionGUIKey(nodeDesc.getPlugin(), nodeDesc
282                             .getType(), nodeDesc.getActionName());
283             showParametersEditionPanel(guiKey, nodeDesc.getParams());
284             return;
285         }
286         // if controler node
287
if (Node.isControllerNode(type)) {
288             NodeDescription desc = TreeManager.getTreeManager(null).getNodeDescription(this.currentNode) ;
289             this.createTestSwitcherTopComposite(type);
290             // show the right panel, if the plugin and the test has been edited
291
if ((desc.getPlugin() != null)&&(desc.getActionName() != null)) {
292                 switchTestParametersPanel(desc.getPlugin(),desc.getActionName()) ;
293                 this.comboTop.setText(desc.getPlugin() + "." + desc.getActionName()) ;
294             }
295             else
296                 showParametersEditionPanel(null, null);
297             return;
298         }
299         // if others node
300
if (!Node.isPluginNode(type)) {
301             NodeDescription desc = TreeManager.getTreeManager(null).getNodeDescription(this.currentNode) ;
302             this.createLabelTopComposite("The node is a " + type);
303             this.showParametersNonePluginPanel(desc) ;
304             return ;
305         }
306     }
307     
308     /**
309      * Show a none plugin node parameters edition panels
310      * @param desc The description node
311      */

312     private void showParametersNonePluginPanel(NodeDescription desc) {
313         cat.debug("-> showParametersNonePluginPanel") ;
314         if (!panels.containsKey(desc.getType())) {
315             ParametersWidgetsNode pwn = NonePluginNode.createParametersWidgetsNode(desc.getType()) ;
316             if (pwn == null)
317                 pwn = new ParametersWidgetsNode(null) ;
318             this.panels.put(desc.getType(), pwn) ;
319         }
320         showParametersEditionPanel(desc.getType(), desc.getParams()) ;
321     }
322
323     /**
324      * Show the parameters edition panels
325      *
326      * @param guiKey
327      * The gui key of the panel to shown
328      * @param params
329      * The parameters values to init the widgets
330      */

331     private void showParametersEditionPanel(String JavaDoc guiKey, Hashtable JavaDoc params) {
332         cat.debug("-> showParametersEditionPanel") ;
333         // dispose it if it already exist
334
if (this.mainComposite != null) {
335             this.mainComposite.dispose();
336         }
337         // exit if they are no guikey
338
if (guiKey == null) {
339             this.mainComposite = new Composite(this.main, SWT.BORDER);
340             this.mainComposite.setBackground(this.parent.getDisplay()
341                     .getSystemColor(SWT.COLOR_WHITE));
342             this.main.layout();
343             return;
344         }
345         // create the main composite
346
this.mainComposite = new Composite(this.main, SWT.FLAT);
347         GridLayout gridLayout = new GridLayout();
348         gridLayout.marginHeight = 0;
349         gridLayout.marginWidth = 0;
350         gridLayout.numColumns = 1;
351         this.mainComposite.setLayout(gridLayout);
352         // create the content composite
353
this.contents = new ScrolledComposite(this.mainComposite, SWT.BORDER
354                 | SWT.H_SCROLL | SWT.V_SCROLL);
355         this.contents.setExpandHorizontal(true);
356         this.contents.setExpandVertical(true);
357         this.contents.setAlwaysShowScrollBars(false);
358         // set the contents grid data
359
GridData gridData = new GridData();
360         gridData.grabExcessVerticalSpace = true;
361         gridData.grabExcessHorizontalSpace = true;
362         gridData.verticalAlignment = GridData.FILL;
363         gridData.horizontalAlignment = GridData.FILL;
364         this.contents.setLayoutData(gridData);
365         // create the new panel which will contains all the parameters widgets
366
this.contentsComposite = new Composite(this.contents, SWT.FLAT);
367         this.contents.setContent(this.contentsComposite);
368         this.contentsComposite.setLayout(new FillLayout());
369         // get the tree description
370
ParametersWidgetsNode node = (ParametersWidgetsNode) this.panels
371                 .get(guiKey);
372         this.parametersPanel = ParameterPanel.createParameterPanel(node,
373                 this.contentsComposite, this);
374         // analyse the type of the current node
375
TreeManager treeManager = TreeManager.getTreeManager(null) ;
376         String JavaDoc type = treeManager.getNodeType(this.currentNode) ;
377         
378         if (!type.equals(Node.USE)) {
379             if (Node.isPluginNode(type) || Node.isControllerNode(type)) {
380                 Vector JavaDoc values = treeManager.getPluginObjectByName(treeManager.getNodeDescription(this.currentNode).getPlugin()) ;
381                 this.parametersPanel.setComboValues(values) ;
382             }
383         }
384         this.parametersPanel.setParametersValues(params);
385         
386         // set the minSize of the contents composite
387
this.contents.setMinSize(this.contentsComposite.computeSize(
388                 SWT.DEFAULT, SWT.DEFAULT));
389         // update the main composite
390
this.main.layout();
391     }
392
393     /**
394      * Switch the edition panel to the test edition parameters panel selected
395      *
396      * @param pluginName
397      * The plugin name of the test
398      * @param testName
399      * The test name
400      */

401     private void switchTestParametersPanel(String JavaDoc pluginName, String JavaDoc testName) {
402         cat.debug("-> switchTestParametersPanel") ;
403         String JavaDoc guiKey = (PluginManager.getPluginManager())
404                 .getPluginActionGUIKey(pluginName, Node.TEST, testName);
405         Hashtable JavaDoc values = (TreeManager.getTreeManager(null)).getNodeDescription(
406                 this.currentNode).getParams();
407         showParametersEditionPanel(guiKey, values);
408     }
409
410     
411     /**
412      * Create panels from a gui XML file, and store it
413      *
414      * @param plugin
415      * The plugin description
416      * @param guiFile
417      * The gui file with all panels descriptions PB : Comment definir
418      * les guikey dans les nodes descriptions !??? peut etre en
419      * passant un vector a remplir en parametre => A priori c fait
420      * !!!
421      */

422     public void createPanels(PluginDescription plugin, String JavaDoc guiFile) {
423         cat.debug("-> createPanels") ;
424         if (parent != null)
425             ParameterPanel.createNewPanelsFromXML(plugin, guiFile, this.panels);
426         else
427             System.out
428                     .println("Unable to generate Panels, because they don't have any parent composite to be defined");
429     }
430
431     /**
432      * Method which update the parameters values of the current node
433      */

434     private void updateParametersValues() {
435         cat.debug("-> updateParametersValues") ;
436         if (this.parametersPanel == null)
437             return;
438         // get the values
439
Hashtable JavaDoc values = this.parametersPanel.getParametersValues();
440         boolean flag = (TreeManager.getTreeManager(null)).setParametersValues(this.currentNode,
441                 values);
442         if (!flag) {
443             // show the old parameters, because the new one are not right
444
NodeDescription desc = TreeManager.getTreeManager(null).getNodeDescription(this.currentNode) ;
445             Hashtable JavaDoc oldValues = desc.getParams() ;
446             this.parametersPanel.setParametersValues(oldValues) ;
447         }
448     }
449
450     /**
451      * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
452      */

453     public void widgetDefaultSelected(SelectionEvent event) {
454         cat.debug("-> widgetDefaultSelected") ;
455         cat.debug("default selected" + event.getSource());
456
457     }
458     /**
459      * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
460      */

461     public void widgetSelected(SelectionEvent event) {
462         cat.debug("-> widgetSelected") ;
463         if (event.getSource() == this.comboTop) {
464             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(this.comboTop.getText(),
465                     ".");
466             if (st.countTokens() < 2)
467                 return;
468             String JavaDoc plugin = "";
469             // the token before the last are the name of the plugin
470
while (st.countTokens() > 2) {
471                 String JavaDoc token = st.nextToken();
472                 plugin = plugin.concat(token + ".");
473             }
474             plugin = plugin.concat(st.nextToken());
475             String JavaDoc test = st.nextToken();
476             // Show the help of the test action
477
Vector JavaDoc help = (PluginManager.getPluginManager()).getPluginActionHelp(plugin,
478                     Node.TEST, test);
479             this.window.updateHelp(help) ;
480             //switch the panel
481
TreeManager.getTreeManager(null).setNodeCondition(this.currentNode, plugin, test) ;
482             this.switchTestParametersPanel(plugin, test);
483             return ;
484         }
485         if (this.parametersPanel.addButtonSelected(event.getSource())) {
486             // refreash the scrolled pane
487
// set the minSize of the contents composite
488
this.contents.setMinSize(this.contentsComposite.computeSize(
489                     SWT.DEFAULT, SWT.DEFAULT));
490             // update the main composite
491
this.main.layout();
492             return ;
493         }
494         if (this.parametersPanel.removeButtonSelected(event.getSource())) {
495             this.updateParametersValues() ;
496             // refreash the scrolled pane
497
// set the minSize of the contents composite
498
this.contents.setMinSize(this.contentsComposite.computeSize(
499                     SWT.DEFAULT, SWT.DEFAULT));
500             // update the main composite
501
this.main.layout();
502             return ;
503         }
504         // else it a radio group param or a check box what has been modify
505
this.updateParametersValues() ;
506     }
507     
508     /**
509      * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
510      */

511     public void modifyText(ModifyEvent event) {
512         cat.debug("-> modifyText") ;
513         this.parametersPanel.modifySomething(event.getSource()) ;
514         this.updateParametersValues() ;
515     }
516 }
Popular Tags