KickJava   Java API By Example, From Geeks To Geeks.

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


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.EventListener JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.StringTokenizer 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.custom.TableEditor;
33 import org.eclipse.swt.events.ModifyListener;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.events.SelectionListener;
36 import org.eclipse.swt.layout.FillLayout;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Combo;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.swt.widgets.Group;
44 import org.eclipse.swt.widgets.Label;
45 import org.eclipse.swt.widgets.Table;
46 import org.eclipse.swt.widgets.TableColumn;
47 import org.eclipse.swt.widgets.TableItem;
48 import org.eclipse.swt.widgets.Text;
49
50 /**
51  * Implementation of the object which included widgets to edit parameters
52  *
53  * @author JC Meillaud
54  * @author A Peyrard
55  */

56 public class ParameterWidget implements SelectionListener {
57
58     static Category cat = Category.getInstance(ParameterWidget.class.getName());
59
60     private static final String JavaDoc EMPTY = "to be edited...";
61
62     /**
63      * Store the widget style
64      */

65     private int style;
66
67     private Composite composite;
68
69     private String JavaDoc name;
70
71     private String JavaDoc labelValue;
72
73     private Text textField;
74
75     private Group radioGroup;
76
77     private Group checkBox;
78
79     private Group group;
80
81     private Vector JavaDoc buttons;
82
83     private Combo combo;
84
85     private EventListener JavaDoc listener;
86
87     private Vector JavaDoc nfield;
88
89     private Vector JavaDoc nfieldLabel;
90
91     private Button nfieldAdd;
92
93     private Button nfieldRemove;
94
95     private Group nfieldGroup;
96
97     private Composite parent;
98
99     private Table table;
100
101     private Vector JavaDoc cellEditors;
102
103     private int cellYAxis;
104
105     private Vector JavaDoc cellText;
106
107     private Vector JavaDoc tableColumns;
108
109     /**
110      * Constructor, build a new WidgetParam
111      *
112      * @param style
113      * The style of the widget for editing this param
114      * @param name
115      * The name of the parameter
116      * @param labelValue
117      * The value of the label for this widget, if it is not defined
118      * use the name of the param
119      * @param params
120      * The parameter to build the widget
121      * @param parent
122      * The parent composite to add the new widget
123      * @param listener
124      * The modify listener to set on the parameter widget
125      */

126     public ParameterWidget(int style, String JavaDoc name, String JavaDoc labelValue,
127             Hashtable JavaDoc params, Composite parent, EventListener JavaDoc listener) {
128         cat.debug("-> constructor");
129         this.listener = listener;
130         this.style = style;
131         this.name = name;
132         if (labelValue == null) {
133             this.labelValue = name;
134         } else {
135             if (labelValue.equals("")) {
136                 this.labelValue = name;
137             } else {
138                 this.labelValue = labelValue;
139             }
140         }
141         // init the widgets
142
this.textField = null;
143         this.radioGroup = null;
144         this.checkBox = null;
145         this.group = null;
146         this.buttons = null;
147         this.nfield = null;
148         this.nfieldLabel = null;
149         this.nfieldAdd = null;
150         this.nfieldRemove = null;
151         this.nfieldGroup = null;
152         this.table = null;
153         this.tableColumns = null;
154         // costruct the main composite
155
this.parent = parent;
156         this.composite = new Composite(parent, SWT.FLAT);
157         this.composite.setBackground(parent.getDisplay().getSystemColor(
158                 SWT.COLOR_WHITE));
159         GridLayout gridLayout = new GridLayout();
160         gridLayout.numColumns = 1;
161         this.composite.setLayout(gridLayout);
162         // set grid data to the main composite
163
GridData gridData = new GridData();
164         gridData.horizontalAlignment = GridData.FILL;
165         gridData.grabExcessHorizontalSpace = true;
166         this.composite.setLayoutData(gridData);
167         // consruct the widget depending it style
168
switch (style) {
169         case WidgetDescription.TEXT_FIELD:
170             initField(params, this.composite);
171             break;
172         case WidgetDescription.RADIO_GROUP:
173             initRadioGroup(params, this.composite);
174             break;
175         case WidgetDescription.CHECK_BOX:
176             initCheckBox(params, this.composite);
177             break;
178         case WidgetDescription.GROUP:
179             initGroup(params, this.composite);
180             break;
181         case WidgetDescription.COMBO:
182             initCombo(params, this.composite);
183             break;
184         case WidgetDescription.NFIELD:
185             initNField(params, this.composite);
186             break;
187         case WidgetDescription.TABLE:
188             initTable(params, this.composite);
189             break;
190         default:
191             cat.warn("UNKNOW WIDGET STYLE : " + style);
192         }
193     }
194
195     /**
196      * Method which permit to get the parameter value
197      */

198     public String JavaDoc getValue() {
199         cat.debug("-> getValue");
200         switch (this.style) {
201         case WidgetDescription.TEXT_FIELD:
202             return getValueField();
203         case WidgetDescription.RADIO_GROUP:
204             return getValueRadioGroup();
205         case WidgetDescription.CHECK_BOX:
206             return getValueCheckBox();
207         case WidgetDescription.GROUP:
208             return getValueGroup();
209         case WidgetDescription.COMBO:
210             return getValueCombo();
211         case WidgetDescription.NFIELD:
212             return getValueNField();
213         case WidgetDescription.TABLE:
214             return getValueTable();
215         default:
216             cat.warn("UNKNOW WIDGET STYLE...");
217             return null;
218         }
219     }
220
221     /**
222      * Set the widget value
223      *
224      * @param value
225      * The value to set
226      */

227     public void setValue(String JavaDoc value) {
228         cat.debug("-> setValue");
229         // remove the listeners because this changes are made by the system
230
// and it do not need to be ntoify of this changes...
231
removeListeners();
232         switch (this.style) {
233         case WidgetDescription.TEXT_FIELD:
234             setValueField(value);
235             break;
236         case WidgetDescription.RADIO_GROUP:
237             setValueRadioGroup(value);
238             break;
239         case WidgetDescription.CHECK_BOX:
240             setValueCheckBox(value);
241             break;
242         case WidgetDescription.GROUP:
243             setValueGroup(value);
244             break;
245         case WidgetDescription.COMBO:
246             setValueCombo(value);
247             break;
248         case WidgetDescription.NFIELD:
249             setValueNField(value);
250             break;
251         case WidgetDescription.TABLE:
252             setValueTable(value);
253             break;
254         default:
255             cat.warn("UNKNOW WIDGET STYLE...");
256         }
257         addListeners();
258     }
259
260     /**
261      * Set the values of a combo
262      *
263      * @param values
264      * The values to be setted
265      */

266     public void setComboValues(Vector JavaDoc values) {
267         cat.debug("-> setComboValues");
268         for (int i = 0; i < values.size(); i++) {
269             this.combo.add((String JavaDoc) values.elementAt(i));
270         }
271     }
272
273     /**
274      * Add a new field to the current nfield
275      *
276      * @param source
277      * The source of the event
278      */

279     public boolean addEmtyFieldForNField(Object JavaDoc source) {
280         cat.debug("-> addEmptyFieldForNField");
281         // if the source is the add widget button
282
if (source != this.nfieldAdd)
283             return false;
284         // create a new label
285
Label label = new Label(this.nfieldGroup, SWT.NONE);
286         label
287                 .setBackground(parent.getDisplay().getSystemColor(
288                         SWT.COLOR_WHITE));
289         // init the text label
290
label.setText("field " + this.nfield.size() + " : ");
291         // add it to the label vector
292
this.nfieldLabel.add(label);
293         // create the text field
294
Text text = new Text(this.nfieldGroup, SWT.BORDER);
295         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
296         // add a modify listener
297
text.addModifyListener((ModifyListener) this.listener);
298         // add the field to the fields vector
299
this.nfield.add(text);
300         // refresh the group
301
this.nfieldGroup.layout();
302         this.composite.layout();
303         this.parent.layout();
304         return true;
305     }
306
307     /**
308      * This method adds an entry on the table widget
309      *
310      * @param source
311      * The source of the event
312      * @return true if the source is our add button, else false
313      */

314     public boolean addEmtyEntryForTable(Object JavaDoc source) {
315         if (source != this.nfieldAdd)
316             return false;
317         TableItem temp = new TableItem(table, SWT.NONE);
318         // set the default values for each cell
319
String JavaDoc[] values = new String JavaDoc[this.tableColumns.size()];
320         for (int i = 0; i < this.tableColumns.size(); i++) {
321             values[i] = new String JavaDoc(EMPTY);
322         }
323         temp.setText(values);
324         return true;
325     }
326
327     /**
328      * This method removes the last entry of the table widget
329      *
330      * @param source
331      * The source of the event
332      * @return true if the source is our remove button, else false
333      */

334     public boolean removeLastEntryForTable(Object JavaDoc source) {
335         if (source != this.nfieldRemove)
336             return false;
337         this.table.remove(this.table.getItemCount() - 1);
338         return true;
339     }
340
341     /**
342      * Remove the last field of the nfield
343      *
344      * @param source
345      * The source of the event
346      */

347     public boolean removeLastFieldForNField(Object JavaDoc source) {
348         cat.debug("-> removeLastFieldForNField");
349         // if the source is the remove widget button
350
if (source != this.nfieldRemove)
351             return false;
352         if (this.nfield.isEmpty())
353             return true;
354         // get the last label and last text
355
Label lastLabel = (Label) this.nfieldLabel.elementAt(this.nfieldLabel
356                 .size() - 1);
357         Text lastText = (Text) this.nfield.elementAt(this.nfield.size() - 1);
358         // remove the vector entries
359
this.nfield.remove(lastText);
360         this.nfieldLabel.remove(lastLabel);
361         // dispose the widgets
362
lastLabel.dispose();
363         lastText.dispose();
364         // refresh the group
365
this.nfieldGroup.layout();
366         this.composite.layout();
367         this.parent.layout();
368         return true;
369     }
370
371     ///////////////////////////////////////////
372
// Listeners methods
373
//////////////////////////////////////////
374

375     /**
376      * This method remove the listener of the current widget
377      */

378     public void removeListeners() {
379         switch (this.style) {
380         case WidgetDescription.TEXT_FIELD:
381             this.textField.removeModifyListener((ModifyListener) listener);
382             return;
383         case WidgetDescription.RADIO_GROUP:
384             this.removeButtonsListeners();
385             return;
386         case WidgetDescription.CHECK_BOX:
387             this.removeButtonsListeners();
388             return;
389         case WidgetDescription.COMBO:
390             this.combo.removeSelectionListener((SelectionListener) listener);
391             return;
392         case WidgetDescription.NFIELD:
393             this.removeNFieldListeners();
394             return;
395         case WidgetDescription.TABLE:
396             // this.table.removeListener(SWT.Modify, (Listener)this.listener) ;
397
return;
398         default:
399             cat.warn("UNKNOW WIDGET STYLE...");
400         }
401     }
402
403     /**
404      * This method add a listener to the current widget
405      */

406     public void addListeners() {
407         switch (this.style) {
408         case WidgetDescription.TEXT_FIELD:
409             this.textField.addModifyListener((ModifyListener) listener);
410             return;
411         case WidgetDescription.RADIO_GROUP:
412             this.addButtonsListeners();
413             return;
414         case WidgetDescription.CHECK_BOX:
415             this.addButtonsListeners();
416             return;
417         case WidgetDescription.COMBO:
418             this.combo.addSelectionListener((SelectionListener) listener);
419             return;
420         case WidgetDescription.NFIELD:
421             this.addNFieldListeners();
422             return;
423         case WidgetDescription.TABLE:
424             // this.table.addListener(SWT.Modify, (Listener)this.listener) ;
425
return;
426         default:
427             cat.warn("UNKNOW WIDGET STYLE...");
428         }
429     }
430
431     /**
432      * This method remove the listeners on the buttons (used for check box
433      * widgets or radio group...)
434      */

435     private void removeButtonsListeners() {
436         for (int i = 0; i < this.buttons.size(); i++)
437             ((Button) this.buttons.elementAt(i))
438                     .removeSelectionListener((SelectionListener) listener);
439     }
440
441     private void addButtonsListeners() {
442         for (int i = 0; i < this.buttons.size(); i++)
443             ((Button) this.buttons.elementAt(i))
444                     .addSelectionListener((SelectionListener) listener);
445     }
446
447     /**
448      * Remove the listeners of each fields
449      */

450     private void removeNFieldListeners() {
451         for (int i = 0; i < this.nfield.size(); i++) {
452             ((Text) this.nfield.elementAt(i))
453                     .removeModifyListener((ModifyListener) this.listener);
454         }
455     }
456
457     /**
458      * Add the listeners of each fields
459      */

460     private void addNFieldListeners() {
461         for (int i = 0; i < this.nfield.size(); i++) {
462             ((Text) this.nfield.elementAt(i))
463                     .addModifyListener((ModifyListener) this.listener);
464         }
465     }
466
467     ///////////////////////////////////////////
468
// Initialisation widgets
469
///////////////////////////////////////////
470

471     /**
472      * Method which initialize a new table widget
473      */

474     private void initTable(Hashtable JavaDoc params, Composite parent) {
475         cat.debug("-> initTable, p=" + params);
476         // initialize a new group
477
this.nfieldGroup = new Group(parent, SWT.BORDER);
478         this.nfieldGroup.setBackground(parent.getDisplay().getSystemColor(
479                 SWT.COLOR_WHITE));
480         this.nfieldGroup.setText(this.labelValue + " : ");
481         // set the grid data of the group
482
this.nfieldGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
483         // init the layout of the group
484
GridLayout gd = new GridLayout();
485         gd.numColumns = 1;
486         this.nfieldGroup.setLayout(gd);
487         Composite cButtons = new Composite(this.nfieldGroup, SWT.FLAT);
488         GridLayout gdc = new GridLayout();
489         gdc.numColumns = 2;
490         cButtons.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
491         cButtons.setBackground(parent.getDisplay().getSystemColor(
492                 SWT.COLOR_WHITE));
493         cButtons.setLayout(gdc);
494         // initialise the two buttons
495
this.nfieldAdd = new Button(cButtons, SWT.FLAT);
496         // set the grid data
497
this.nfieldAdd.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
498         this.nfieldAdd.setText("Add entry");
499         this.nfieldAdd.addSelectionListener((SelectionListener) this.listener);
500         this.nfieldRemove = new Button(cButtons, SWT.FLAT);
501         this.nfieldRemove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
502         this.nfieldRemove.setText("Remove entry");
503         this.nfieldRemove
504                 .addSelectionListener((SelectionListener) this.listener);
505         // init the new table
506
this.table = new Table(this.nfieldGroup, SWT.BORDER);
507         this.table.setLayoutData(new GridData(GridData.FILL_BOTH));
508         // this.table.addListener(SWT.Modify, (Listener)this.listener) ;
509
this.tableColumns = new Vector JavaDoc();
510         // init the columns
511
String JavaDoc cols = (String JavaDoc) params.get("cols");
512         Vector JavaDoc colsNames = getRealTokens(";",cols);
513         for (int i=0;i<colsNames.size();i++) {
514             TableColumn col = new TableColumn(this.table, SWT.LEFT);
515             col.setText((String JavaDoc)colsNames.elementAt(i));
516             col.setWidth(60);
517             this.tableColumns.add(col);
518         }
519         table.setHeaderVisible(true);
520         table.setLinesVisible(true);
521         // set the line editable
522
table.addSelectionListener(this);
523         //init vectors
524
this.cellEditors = new Vector JavaDoc();
525         this.cellText = new Vector JavaDoc();
526     }
527
528     /**
529      * Method which initialise the label of the parameter
530      */

531     private void initLabel() {
532         cat.debug("-> initLabel");
533         Label label = new Label(this.composite, SWT.RIGHT);
534         label.setBackground(composite.getDisplay().getSystemColor(
535                 SWT.COLOR_WHITE));
536         label.setText(this.labelValue + " : ");
537     }
538
539     /**
540      * Init a new text field
541      *
542      * @param params
543      * The parameters to build the widget
544      * @param parent
545      * The parent composite to add the new widget
546      */

547     private void initField(Hashtable JavaDoc params, Composite parent) {
548         cat.debug("-> initField");
549         // change the composite layout
550
((GridLayout) this.composite.getLayout()).numColumns = 2;
551         // intialise the label of the parameter
552
this.initLabel();
553         // intiailise the text field
554
this.textField = new Text(this.composite, SWT.SINGLE | SWT.BORDER);
555         // set the background color
556
this.textField.setBackground(parent.getDisplay().getSystemColor(
557                 SWT.COLOR_WHITE));
558
559         // set grid data to the textfield
560
GridData gridData = new GridData();
561         gridData.horizontalAlignment = GridData.FILL;
562         gridData.grabExcessHorizontalSpace = true;
563         this.textField.setLayoutData(gridData);
564         // set the default text to the field
565
if (params != null) {
566             if (params.containsKey("text")) {
567                 this.textField.setText((String JavaDoc) params.get("text"));
568             }
569         }
570         // add the modify listener
571
this.textField.addModifyListener((ModifyListener) listener);
572     }
573
574     /**
575      * Init a new nField
576      *
577      * @param params
578      * The parameters to build the widget
579      * @param parent
580      * The parent composite to add the new widget
581      */

582     private void initNField(Hashtable JavaDoc params, Composite parent) {
583         cat.debug("-> initNField");
584         // initialize a new group
585
this.nfieldGroup = new Group(parent, SWT.BORDER);
586         this.nfieldGroup.setBackground(parent.getDisplay().getSystemColor(
587                 SWT.COLOR_WHITE));
588         this.nfieldGroup.setText(this.labelValue + " : ");
589         // set the grid data of the group
590
this.nfieldGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
591         // init the layout of the group
592
GridLayout gd = new GridLayout();
593         gd.numColumns = 2;
594         this.nfieldGroup.setLayout(gd);
595         // initialise the two buttons
596
this.nfieldAdd = new Button(this.nfieldGroup, SWT.FLAT);
597         // set the grid data
598
this.nfieldAdd.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
599         this.nfieldAdd.setText("Add field");
600         this.nfieldAdd.addSelectionListener((SelectionListener) this.listener);
601         this.nfieldRemove = new Button(this.nfieldGroup, SWT.FLAT);
602         this.nfieldRemove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
603         this.nfieldRemove.setText("Remove field");
604         this.nfieldRemove
605                 .addSelectionListener((SelectionListener) this.listener);
606         // init the fields vector
607
this.nfield = new Vector JavaDoc();
608         this.nfieldLabel = new Vector JavaDoc();
609     }
610
611     /**
612      * Init a new radio group
613      *
614      * @param params
615      * The parameters to build the widget
616      * @param parent
617      * The parent composite to add the new widget
618      */

619     private void initRadioGroup(Hashtable JavaDoc params, Composite parent) {
620         cat.debug("-> initRadioGroup");
621         this.radioGroup = new Group(parent, SWT.BORDER);
622         // set the background color
623
this.radioGroup.setBackground(parent.getDisplay().getSystemColor(
624                 SWT.COLOR_WHITE));
625
626         this.radioGroup.setText(this.labelValue);
627         // set grid data to the group
628
GridData gridData = new GridData();
629         gridData.horizontalAlignment = GridData.FILL;
630         gridData.grabExcessHorizontalSpace = true;
631         this.radioGroup.setLayoutData(gridData);
632         FillLayout fill = new FillLayout();
633         fill.type = SWT.VERTICAL;
634         this.radioGroup.setLayout(fill);
635
636         this.buttons = new Vector JavaDoc();
637         // get the differents choices
638
Vector JavaDoc choices = null;
639         if (params.containsKey("choices")) {
640             choices = (Vector JavaDoc) params.get("choices");
641         }
642         Vector JavaDoc selected = null;
643         if (params.containsKey("selected")) {
644             selected = (Vector JavaDoc) params.get("selected");
645         }
646         // add the button
647
if (choices != null) {
648             for (int i = 0; i < choices.size(); i++) {
649                 Button temp = new Button(this.radioGroup, SWT.RADIO);
650                 temp.setBackground(parent.getDisplay().getSystemColor(
651                         SWT.COLOR_WHITE));
652                 String JavaDoc buttonName = (String JavaDoc) choices.elementAt(i);
653                 temp.setText(buttonName);
654                 // add a selection listener to get changes
655
temp.addSelectionListener((SelectionListener) listener);
656                 this.buttons.add(temp);
657                 // if the button is in the selcted vector, select it
658
if (selected.contains(buttonName))
659                     temp.setSelection(true);
660             }
661         }
662     }
663
664     /**
665      * Init a new check box
666      *
667      * @param params
668      * The parameters to build the widget
669      * @param parent
670      * The parent composite to add the new widget
671      */

672     private void initCheckBox(Hashtable JavaDoc params, Composite parent) {
673         cat.debug("-> initCheckBox");
674         this.checkBox = new Group(parent, SWT.BORDER);
675         // set the background color
676
this.checkBox.setBackground(parent.getDisplay().getSystemColor(
677                 SWT.COLOR_WHITE));
678
679         this.checkBox.setText(this.labelValue);
680         // set grid data to the group
681
GridData gridData = new GridData();
682         gridData.horizontalAlignment = GridData.FILL;
683         gridData.grabExcessHorizontalSpace = true;
684         this.checkBox.setLayoutData(gridData);
685         FillLayout fill = new FillLayout();
686         fill.type = SWT.VERTICAL;
687         this.checkBox.setLayout(fill);
688
689         this.buttons = new Vector JavaDoc();
690
691         // get the differents choices
692
Vector JavaDoc choices = null;
693         if (params.containsKey("choices")) {
694             choices = (Vector JavaDoc) params.get("choices");
695         }
696         Vector JavaDoc selected = null;
697         if (params.containsKey("selected")) {
698             selected = (Vector JavaDoc) params.get("selected");
699         }
700         // add the button
701
if (choices != null) {
702             for (int i = 0; i < choices.size(); i++) {
703                 Button temp = new Button(this.checkBox, SWT.CHECK);
704                 temp.setBackground(parent.getDisplay().getSystemColor(
705                         SWT.COLOR_WHITE));
706                 String JavaDoc buttonName = (String JavaDoc) choices.elementAt(i);
707                 temp.setText(buttonName);
708                 temp.addSelectionListener((SelectionListener) listener);
709                 this.buttons.add(temp);
710                 // if the button is in the selcted vector, select it
711
if (selected.contains(buttonName))
712                     temp.setSelection(true);
713             }
714         }
715     }
716
717     /**
718      * Init a new combo
719      *
720      * @param params
721      * The parameters to build the widget
722      * @param parent
723      * The parent composite to add the new widget
724      */

725     private void initCombo(Hashtable JavaDoc params, Composite parent) {
726         cat.debug("-> initCombo");
727         // change the composite layout
728
((GridLayout) this.composite.getLayout()).numColumns = 2;
729         // intialise the label of the parameter
730
this.initLabel();
731         this.combo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
732         // set the background color
733
this.combo.setBackground(parent.getDisplay().getSystemColor(
734                 SWT.COLOR_WHITE));
735
736         // set grid data to the group
737
GridData gridData = new GridData();
738         gridData.horizontalAlignment = GridData.FILL;
739         gridData.grabExcessHorizontalSpace = true;
740         this.combo.setLayoutData(gridData);
741         // get the differents choices
742
Vector JavaDoc choices = null;
743         // if thzere is no parameters, we have all done
744
if (params == null)
745             return;
746         if (params.containsKey("choices")) {
747             choices = (Vector JavaDoc) params.get("choices");
748         }
749         if (choices != null) {
750             // add the values
751
for (int i = 0; i < choices.size(); i++) {
752                 this.combo.add((String JavaDoc) choices.elementAt(i));
753             }
754         }
755         this.combo.addSelectionListener((SelectionListener) listener);
756     }
757
758     /**
759      * Init a new group
760      *
761      * @param params
762      * The parameters to build the widget
763      * @param parent
764      * The parent composite to add the new widget
765      */

766     private void initGroup(Hashtable JavaDoc params, Composite parent) {
767         cat.debug("-> initGroup");
768         this.group = new Group(parent, SWT.BORDER);
769         // set the background color
770
this.group.setBackground(parent.getDisplay().getSystemColor(
771                 SWT.COLOR_WHITE));
772         this.group.setText(this.labelValue);
773         // set grid data to the group
774
GridData gridData = new GridData();
775         gridData.horizontalAlignment = GridData.FILL;
776         gridData.grabExcessHorizontalSpace = true;
777         this.group.setLayoutData(gridData);
778         GridLayout groupLayout = new GridLayout();
779         groupLayout.numColumns = 1;
780         this.group.setLayout(groupLayout);
781     }
782
783     /**
784      * get the value of a table
785      *
786      * @return The table values
787      */

788     private String JavaDoc getValueTable() {
789         cat.debug("-> getValueTable");
790         String JavaDoc result = "";
791         TableItem[] items = this.table.getItems();
792         for (int i = 0; i < items.length; i++) {
793             for (int j = 0; j < this.tableColumns.size(); j++) {
794                 String JavaDoc colName = ((TableColumn) this.tableColumns.elementAt(j))
795                         .getText();
796                 String JavaDoc colNameModified = addEscapeCharacter("\\", colName);
797                 colNameModified = addEscapeCharacter(";",colNameModified);
798                 colNameModified = addEscapeCharacter("|",colNameModified);
799                 colNameModified = addEscapeCharacter("=",colNameModified);
800                 String JavaDoc colValue = items[i].getText(j);
801                 String JavaDoc colValueModified = addEscapeCharacter("\\",colValue);
802                 colValueModified = addEscapeCharacter(";",colValueModified);
803                 colValueModified = addEscapeCharacter("|",colValueModified);
804                 colValueModified = addEscapeCharacter("=",colValueModified);
805                 result = result.concat(colNameModified + "=" + colValueModified
806                         + "|");
807             }
808             result = result.concat(";");
809         }
810         return result;
811     }
812
813     /**
814      * Get the value of the text field
815      */

816     private String JavaDoc getValueField() {
817         cat.debug("-> getValueField");
818         String JavaDoc value = this.textField.getText();
819         if (!value.equals(""))
820             return value;
821         return null;
822     }
823
824     /**
825      * Get the value of a nfield widget
826      *
827      * @return The value of the fields : "value1";"value2";...
828      */

829     private String JavaDoc getValueNField() {
830         cat.debug("-> getValueNField");
831         String JavaDoc result = new String JavaDoc("");
832         // get all the values of the fields and concat them
833
for (int i = 0; i < this.nfield.size(); i++) {
834             // get the field
835
Text text = (Text) this.nfield.elementAt(i);
836             // if the values already contains some ; add escape character
837
String JavaDoc value = addEscapeCharacter("\\",text.getText());
838             value = addEscapeCharacter(";",value);
839             // concat the value
840
result = result.concat(value + ";");
841         }
842         // return the result
843
return result;
844     }
845
846     /**
847      * Get the value of the radio group
848      */

849     private String JavaDoc getValueRadioGroup() {
850         cat.debug("-> getValueRadioGroup");
851         String JavaDoc result = "";
852         for (int i = 0; i < this.buttons.size(); i++) {
853             Button temp = (Button) this.buttons.elementAt(i);
854             if (temp.getSelection()) {
855                 String JavaDoc value = temp.getText();
856                 value = addEscapeCharacter("\\",value);
857                 result = result.concat(addEscapeCharacter(";",value));
858                 break;
859             }
860         }
861         if (result.equals(""))
862             return null;
863         else
864             return result;
865     }
866
867     /**
868      * Get the value of the check box
869      */

870     private String JavaDoc getValueCheckBox() {
871         cat.debug("-> getValueCheckBox");
872         String JavaDoc result = "";
873         for (int i = 0; i < this.buttons.size(); i++) {
874             Button temp = (Button) this.buttons.elementAt(i);
875             if (temp.getSelection()) {
876                 String JavaDoc value = temp.getText();
877                 value = addEscapeCharacter("\\",value);
878                 result = result.concat(addEscapeCharacter(";",value)+";");
879             }
880         }
881         if (result.equals(""))
882             return null;
883         else
884             return result;
885     }
886
887     /**
888      * Get the value of the combo
889      */

890     private String JavaDoc getValueCombo() {
891         cat.debug("-> getValueCombo");
892         String JavaDoc result = this.combo.getText();
893         return result;
894     }
895
896     /**
897      * Get the value of the group
898      */

899     private String JavaDoc getValueGroup() {
900         cat.debug("-> getValueGroup");
901         // there is no value for this kind of widget
902
return null;
903     }
904
905     /**
906      * This method set the value of the table widget
907      *
908      * @param value
909      * The value to set
910      */

911     private void setValueTable(String JavaDoc value) {
912         Vector JavaDoc entries = null;
913         try {
914             entries = getTableEntries(value);
915         } catch (Exception JavaDoc e) {
916             e.printStackTrace();
917             return;
918         }
919         for (int i = 0; i < entries.size(); i++) {
920             Vector JavaDoc values = (Vector JavaDoc) entries.elementAt(i);
921             String JavaDoc[] entryValues = (String JavaDoc[]) values.toArray(new String JavaDoc[values
922                     .size()]);
923             TableItem item = new TableItem(this.table, SWT.NONE);
924             item.setText(entryValues);
925         }
926     }
927
928     /**
929      * Set the value of a textfield widget
930      *
931      * @param value
932      * The value to set
933      */

934     private void setValueField(String JavaDoc value) {
935         cat.debug("-> setValueField");
936         this.textField.setText(value);
937     }
938
939     /**
940      * St the values of a nfield widget
941      *
942      * @param value
943      * The differents values of the fields
944      */

945     private void setValueNField(String JavaDoc value) {
946         cat.debug("-> setValueNField");
947         // remove all the fields
948
for (int i = 0; i < nfield.size(); i++) {
949             ((Text) nfield.elementAt(i)).dispose();
950             ((Label) nfieldLabel.elementAt(i)).dispose();
951         }
952         Vector JavaDoc realTokens = getRealTokens(";",value);
953         // for all values add a field
954
for (int i=0;i<realTokens.size();i++) {
955             // get the value
956
String JavaDoc v = (String JavaDoc)realTokens.elementAt(i);
957             v = removeEscapeCharacterForBackslash(v);
958             // create a new field
959
Label label = new Label(this.nfieldGroup, SWT.NONE);
960             label.setBackground(parent.getDisplay().getSystemColor(
961                     SWT.COLOR_WHITE));
962             label.setText("field " + i + " : ");
963             this.nfieldLabel.add(label);
964             Text text = new Text(this.nfieldGroup, SWT.BORDER);
965             // set the gridata of the text
966
GridData data = new GridData(GridData.FILL_HORIZONTAL);
967             text.setLayoutData(data);
968             // set the value of the field
969
text.setText(v);
970             // add a modify listener on the field
971
text.addModifyListener((ModifyListener) this.listener);
972             // add the field in the vector
973
this.nfield.add(text);
974         }
975         // refresh the group storing the fields
976
this.nfieldGroup.layout();
977     }
978
979     /**
980      * set the value of a radio group
981      *
982      * @param value
983      * The values to be set separated by ";"
984      */

985     private void setValueRadioGroup(String JavaDoc value) {
986         cat.debug("-> setValueRadioGroup");
987         // analyze the values to put
988
Vector JavaDoc realTokens = getRealTokens(";",value);
989         for (int i=0;i<realTokens.size();i++) {
990             // get the value
991
String JavaDoc v = (String JavaDoc)realTokens.elementAt(i);
992             v = removeEscapeCharacterForBackslash(v);
993             // select the right button
994
for (int j = 0; j < this.buttons.size(); j++) {
995                 Button temp = (Button) this.buttons.elementAt(j);
996                 temp.setSelection(temp.getText().equals(v));
997             }
998         }
999     }
1000
1001    /**
1002     * set the value of a check box
1003     *
1004     * @param value
1005     * The values to be set separated by ";"
1006     */

1007    private void setValueCheckBox(String JavaDoc value) {
1008        cat.debug("-> setValueCheckBox");
1009        if (value.equals("")) {
1010            return;
1011        }
1012        // unselect all values
1013
for (int i = 0; i < this.buttons.size(); i++) {
1014            Button temp = (Button) this.buttons.elementAt(i);
1015            temp.setSelection(false);
1016        }
1017
1018        // analyze the values to put
1019
Vector JavaDoc realTokens = getRealTokens(";",value);
1020        for (int i=0;i<realTokens.size();i++) {
1021            // get the value
1022
String JavaDoc v = (String JavaDoc)realTokens.elementAt(i);
1023            v = removeEscapeCharacterForBackslash(v);
1024            // select the right button
1025
for (int j = 0; j < this.buttons.size(); j++) {
1026                Button temp = (Button) this.buttons.elementAt(j);
1027                if (temp.getText().equals(v))
1028                    temp.setSelection(true);
1029            }
1030        }
1031    }
1032
1033    /**
1034     * Set the value of the combo
1035     *
1036     * @param value
1037     * The value to set
1038     */

1039    private void setValueCombo(String JavaDoc value) {
1040        cat.debug("-> setValueCombo");
1041
1042        // if value is not defined set it
1043
if (value.equals("")) {
1044            this.combo.setText(value);
1045            return;
1046        }
1047        // check if the value is a feasible value
1048
String JavaDoc[] values = this.combo.getItems();
1049        boolean flag = false;
1050        for (int i = 0; i < values.length; i++) {
1051            if (values[i].equals(value)) {
1052                flag = true;
1053                break;
1054            }
1055        }
1056        if (flag)
1057            this.combo.setText(value);
1058    }
1059
1060    /**
1061     * Do nothing
1062     *
1063     * @param value
1064     */

1065    private void setValueGroup(String JavaDoc value) {
1066        cat.debug("-> setValueGroup");
1067        // do nothing no values to set
1068
}
1069
1070    /**
1071     * Get the widget to add new widget on it, used for group widget
1072     */

1073    public Composite getComposite() {
1074        cat.debug("-> getComposite");
1075        switch (this.style) {
1076        case WidgetDescription.GROUP:
1077            return this.group;
1078        default:
1079            return this.composite;
1080        }
1081    }
1082
1083    /**
1084     * dispose the element used
1085     */

1086    public void dispose() {
1087        cat.debug("-> dispose");
1088        switch (this.style) {
1089        case WidgetDescription.GROUP:
1090            this.group.dispose();
1091            break;
1092        case WidgetDescription.TEXT_FIELD:
1093            this.textField.dispose();
1094            break;
1095        case WidgetDescription.RADIO_GROUP:
1096            this.radioGroup.dispose();
1097            break;
1098        case WidgetDescription.CHECK_BOX:
1099            this.checkBox.dispose();
1100            break;
1101        case WidgetDescription.COMBO:
1102            this.combo.dispose();
1103            break;
1104        }
1105        this.composite.dispose();
1106    }
1107
1108    /**
1109     * Attribute name getter
1110     *
1111     * @return The name of the parameter
1112     */

1113    public String JavaDoc getName() {
1114        cat.debug("-> getName");
1115        return name;
1116    }
1117
1118    /**
1119     * @return Returns the style.
1120     */

1121    public int getStyle() {
1122        return style;
1123    }
1124    
1125    //////////////////////////////////////////////////////
1126
// Escape characters managing methods
1127
//////////////////////////////////////////////////////
1128

1129    /**
1130     * This method analyse the value to be set in the table, and remove all
1131     * escape characters
1132     *
1133     * @param value
1134     * The value containing the serialization of all values of the
1135     * table
1136     * @return The vector containing all the entries, each entries is in a
1137     * vector, which contains the string values
1138     */

1139    private Vector JavaDoc getTableEntries(String JavaDoc value) {
1140        // init the result
1141
Vector JavaDoc result = new Vector JavaDoc();
1142        Vector JavaDoc tempEntries = getRealTokens(";",value);
1143        // now analyse all entries values, and get the '|' separator
1144
Vector JavaDoc allValues = new Vector JavaDoc();
1145        for (int i = 0; i < tempEntries.size(); i++) {
1146            String JavaDoc tempEntry = (String JavaDoc) tempEntries.elementAt(i);
1147            Vector JavaDoc values = getRealTokens("|",tempEntry);
1148            allValues.add(values);
1149        }
1150        // now analyse all values, and get the '=' separator
1151
for (int i = 0; i < allValues.size(); i++) {
1152            Vector JavaDoc resultValues = new Vector JavaDoc();
1153            Vector JavaDoc tempValues = (Vector JavaDoc) allValues.elementAt(i);
1154            for (int j = 0; j < tempValues.size(); j++) {
1155                String JavaDoc v = (String JavaDoc) tempValues.elementAt(j);
1156                Vector JavaDoc temp = getRealTokens("=",v);
1157                if (temp.size() != 2) {
1158                    cat.warn("The vector containing {colName,colValue}, should have only 2 elements, hier we found "+temp.size());
1159                }
1160                String JavaDoc col = (String JavaDoc)temp.elementAt(0);
1161                String JavaDoc res = (String JavaDoc)temp.elementAt(1);
1162                // remove all the escape character for the backslash
1163
res = removeEscapeCharacterForBackslash(res);
1164                resultValues.add(res);
1165            }
1166            result.add(resultValues);
1167        }
1168
1169        // return the entries
1170
return result;
1171    }
1172    
1173    /**
1174     * Get the number of escape character in the end of the string
1175     * @param value The string to be analized
1176     * @return 0 if no escape character was found, else the number
1177     */

1178    private int getNumberOfEscapeCharacter(String JavaDoc value) {
1179        int result = 0;
1180        if (value.length() > 0) {
1181            int last = value.lastIndexOf("\\");
1182            if (last == value.length()-1) {
1183                result = 1 + getNumberOfEscapeCharacter(value.substring(0,last));
1184            }
1185        }
1186        return result;
1187    }
1188    
1189    /**
1190     * Add escape separator before each separator
1191     * @param separator The separator character
1192     * @param value The value of the string to add the escape characters
1193     * @return The string modified
1194     */

1195    private String JavaDoc addEscapeCharacter(String JavaDoc separator, String JavaDoc value) {
1196        String JavaDoc result = "";
1197        StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value,separator,true);
1198        while (st.hasMoreTokens()) {
1199            String JavaDoc token = st.nextToken();
1200            if (token.equals(separator)) {
1201                result = result.concat("\\"+token);
1202            }
1203            else {
1204                result = result.concat(token) ;
1205            }
1206        }
1207        return result;
1208    }
1209    
1210    /**
1211     * Split the string value with the separator, and check if there is no espape character before
1212     * the separator
1213     * @param separator The separator
1214     * @param value The string value to be split
1215     * @return The vector containing each token
1216     */

1217    private Vector JavaDoc getRealTokens(String JavaDoc separator, String JavaDoc value) {
1218        Vector JavaDoc result = new Vector JavaDoc();
1219        StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value,separator,true);
1220        String JavaDoc currentRealToken = "";
1221        while (st.hasMoreTokens()) {
1222            String JavaDoc token = st.nextToken();
1223            // the token is the separator
1224
if (token.equals(separator)) {
1225                int nb = getNumberOfEscapeCharacter(currentRealToken);
1226                // if nb is even, we don't have any escape character
1227
if (nb%2 == 0) {
1228                    result.add(currentRealToken);
1229                    currentRealToken = "";
1230                }
1231                // nb is odd
1232
else {
1233                    // remove the escape character
1234
currentRealToken = currentRealToken.substring(0,currentRealToken.length()-1);
1235                    // add the separator character
1236
currentRealToken = currentRealToken.concat(separator);
1237                    // if it's the last token add it to the result
1238
if (!st.hasMoreTokens()) {
1239                        result.add(currentRealToken);
1240                    }
1241                }
1242            }
1243            // the token is a string
1244
else {
1245                currentRealToken = currentRealToken.concat(token);
1246                // if it's the last token add it to the result
1247
if (!st.hasMoreTokens()) {
1248                    result.add(currentRealToken);
1249                }
1250            }
1251        }
1252        return result;
1253    }
1254    
1255    /**
1256     * Replace all the double backslash by only one
1257     * @param value The string to do modifications
1258     * @return The string modified
1259     */

1260    private String JavaDoc removeEscapeCharacterForBackslash(String JavaDoc value) {
1261        String JavaDoc result = "";
1262        StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value, "\\", true) ;
1263        while (st.hasMoreTokens()) {
1264            String JavaDoc token = st.nextToken();
1265            if (token.equals("\\")) {
1266                // analyse the next token should be a backslash to
1267
if (st.hasMoreTokens()) {
1268                    st.nextToken();
1269                }
1270                result = result.concat("\\");
1271            }
1272            else {
1273                result = result.concat(token);
1274            }
1275        }
1276        return result;
1277    }
1278    
1279
1280    //////////////////////////////////////////////
1281
// LISTENERS
1282
//////////////////////////////////////////////
1283
/**
1284     * modify the text of a table entry
1285     */

1286    public boolean modifyText(Object JavaDoc source) {
1287        boolean isText = false;
1288        for (int i = 0; i < cellText.size(); i++) {
1289            if (cellText.elementAt(i) == source) {
1290                isText = true;
1291            }
1292        }
1293        if (isText) {
1294            // we change the value of an item on the table
1295
String JavaDoc[] colValues = new String JavaDoc[this.tableColumns.size()];
1296            for (int i = 0; i < cellText.size(); i++) {
1297                colValues[i] = ((Text) cellText.elementAt(i)).getText();
1298            }
1299            TableItem item = this.table.getItem(cellYAxis);
1300            item.setText(colValues);
1301        }
1302        return isText;
1303    }
1304
1305    /**
1306     * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
1307     */

1308    public void widgetDefaultSelected(SelectionEvent arg0) {
1309        // do nothing, should never append
1310
}
1311
1312    /**
1313     * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
1314     */

1315    public void widgetSelected(SelectionEvent arg0) {
1316        // Clean up any previous editor control
1317
for (int i = 0; i < cellEditors.size(); i++) {
1318            Control oldEditor = ((TableEditor) cellEditors.elementAt(i))
1319                    .getEditor();
1320            oldEditor.dispose();
1321        }
1322        // clear the editors vector
1323
cellEditors.removeAllElements();
1324        cellText.removeAllElements();
1325
1326        // Identify the selected row
1327
int index = table.getSelectionIndex();
1328        if (index == -1)
1329            return;
1330        TableItem item = table.getItem(index);
1331        cellYAxis = index;
1332        for (int i = 0; i < tableColumns.size(); i++) {
1333            // The control that will be the editor must be a child of the Table
1334
Text text = new Text(table, SWT.NONE);
1335            String JavaDoc toSet = item.getText(i);
1336            if (toSet != null) {
1337                text.setText(toSet);
1338            } else {
1339                text.setText("");
1340            }
1341            text.addModifyListener((ModifyListener) this.listener);
1342
1343            TableEditor editor = new TableEditor(table);
1344            //The text editor must have the same size as the cell and must
1345
//not be any smaller than 50 pixels.
1346
editor.horizontalAlignment = SWT.LEFT;
1347            editor.grabHorizontal = true;
1348            editor.minimumWidth = 50;
1349
1350            // Open the text editor in the second column of the selected row.
1351
editor.setEditor(text, item, i);
1352            cellEditors.add(editor);
1353            cellText.add(text);
1354        }
1355    }
1356
1357    /**
1358     * Create a new parameter widget with a widget description
1359     *
1360     * @param wd
1361     * The widget description
1362     * @param parent
1363     * The parent composite of the widget
1364     * @return The parameter widget
1365     */

1366    public static ParameterWidget createParameterWidget(WidgetDescription wd,
1367            Composite parent, EventListener JavaDoc listener) {
1368        cat.debug("-> createParameterWidget");
1369        return new ParameterWidget(wd.getType(), wd.getText(), wd.getLabel(),
1370                wd.getParams(), parent, listener);
1371    }
1372}
Popular Tags