OperationTabControl.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.ui.views;
022 
023 import java.util.ArrayList;
024 import java.util.Collection;
025 import java.util.HashMap;
026 import java.util.List;
027 import java.util.Map.Entry;
028 
029 import javax.management.openmbean.CompositeData;
030 import javax.management.openmbean.TabularDataSupport;
031 
032 import static org.apache.qpid.management.ui.Constants.*;
033 
034 import org.apache.qpid.management.ui.ApplicationRegistry;
035 import org.apache.qpid.management.ui.ManagedBean;
036 import org.apache.qpid.management.ui.ServerRegistry;
037 import org.apache.qpid.management.ui.jmx.JMXServerRegistry;
038 import org.apache.qpid.management.ui.jmx.MBeanUtility;
039 import org.apache.qpid.management.ui.model.OperationData;
040 import org.apache.qpid.management.ui.model.ParameterData;
041 import org.eclipse.swt.SWT;
042 import org.eclipse.swt.custom.ScrolledComposite;
043 import org.eclipse.swt.events.KeyAdapter;
044 import org.eclipse.swt.events.KeyEvent;
045 import org.eclipse.swt.events.KeyListener;
046 import org.eclipse.swt.events.SelectionAdapter;
047 import org.eclipse.swt.events.SelectionEvent;
048 import org.eclipse.swt.events.SelectionListener;
049 import org.eclipse.swt.events.VerifyEvent;
050 import org.eclipse.swt.events.VerifyListener;
051 import org.eclipse.swt.layout.FormAttachment;
052 import org.eclipse.swt.layout.FormData;
053 import org.eclipse.swt.layout.FormLayout;
054 import org.eclipse.swt.layout.GridData;
055 import org.eclipse.swt.layout.GridLayout;
056 import org.eclipse.swt.widgets.Button;
057 import org.eclipse.swt.widgets.Combo;
058 import org.eclipse.swt.widgets.Composite;
059 import org.eclipse.swt.widgets.Control;
060 import org.eclipse.swt.widgets.Display;
061 import org.eclipse.swt.widgets.Label;
062 import org.eclipse.swt.widgets.Shell;
063 import org.eclipse.swt.widgets.TabFolder;
064 import org.eclipse.swt.widgets.Text;
065 import org.eclipse.ui.forms.widgets.Form;
066 import org.eclipse.ui.forms.widgets.FormToolkit;
067 
068 
069 /**
070  * Control class for the MBean operations tab. It creates the required widgets
071  * for the selected MBean.
072  @author Bhupendra Bhardwaj
073  @author Robert Gemmell
074  */
075 public class OperationTabControl extends TabControl
076 {
077     private static final int heightForAParameter = 30;
078     private static final int labelWidth = 30;
079     private static final int valueWidth = labelWidth + 25;
080     
081     private FormToolkit _toolkit;
082     private Form        _form;
083     private OperationData _opData;
084     
085     private SelectionListener operationExecutionListener = new OperationExecutionListener()
086     private SelectionListener refreshListener = new RefreshListener()
087     private SelectionListener parameterSelectionListener = new ParameterSelectionListener();
088     private SelectionListener booleanSelectionListener = new BooleanSelectionListener();
089     private VerifyListener    verifyListener = new VerifyListenerImpl();
090     private KeyListener       keyListener = new KeyListenerImpl();
091     private KeyListener       headerBindingListener = new HeaderBindingKeyListener();
092     
093     private Composite _headerComposite = null;
094     private Composite _paramsComposite = null;
095     private Composite _resultsComposite = null;
096     private Button _executionButton = null;
097     
098     // for customized method in header exchange
099     private HashMap<Text, Text> headerBindingHashMap = null;
100     private String _virtualHostName = null;
101     
102     public OperationTabControl(TabFolder tabFolder, OperationData opData)
103     {
104         super(tabFolder);
105         _toolkit = new FormToolkit(_tabFolder.getDisplay());
106         _form = _toolkit.createForm(_tabFolder);
107         _form.getBody().setLayout(new GridLayout());
108         _opData = opData;
109         createComposites();
110         setHeader();
111     }
112     
113     /**
114      * Form area is devided in four parts:
115      * Header composite - displays operaiton information
116      * Patameters composite - displays parameters if there
117      * Button - operation execution button
118      * Results composite - displays results for operations, which have 
119      *                     no parameters but have some return value
120      */
121     private void createComposites()
122     {
123         // 
124         _headerComposite = _toolkit.createComposite(_form.getBody(), SWT.NONE);
125         _headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
126         
127         List<ParameterData> params = _opData.getParameters();
128         if (params != null && !params.isEmpty())
129         {
130             _paramsComposite = _toolkit.createComposite(_form.getBody(), SWT.NONE);
131             _paramsComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
132         }
133         _executionButton = _toolkit.createButton(_form.getBody(), BUTTON_EXECUTE, SWT.PUSH | SWT.CENTER);
134         _executionButton.setFont(ApplicationRegistry.getFont(FONT_BUTTON));
135         GridData layoutData = new GridData(SWT.CENTER, SWT.TOP, true, false);
136         layoutData.verticalIndent = 20;
137         _executionButton.setLayoutData(layoutData);
138         
139         _resultsComposite = _toolkit.createComposite(_form.getBody(), SWT.NONE);
140         layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
141         layoutData.verticalIndent = 20;
142         _resultsComposite.setLayoutData(layoutData);
143         _resultsComposite.setLayout(new GridLayout());
144     }
145     
146     /**
147      @see TabControl#getControl()
148      */
149     public Control getControl()
150     {
151         return _form;
152     }
153     
154     @Override
155     public void refresh(ManagedBean mbean)
156     {
157         _mbean = mbean;
158         _virtualHostName = _mbean.getVirtualHostName();
159         
160         // Setting the form to be invisible. Just in case the mbean server connection
161         // is done and it takes time in getting the response, then the ui should be blank
162         // instead of having half the widgets displayed.
163         _form.setVisible(false);
164         
165         ViewUtility.disposeChildren(_paramsComposite);
166         createParameterWidgets();
167         
168         // Set button text and add appropriate listener to button.
169         // If there are no parameters and it is info operation, then operation gets executed
170         // and result is displayed
171         List<ParameterData> params = _opData.getParameters();
172         if (params != null && !params.isEmpty())
173         {            
174             setButton(BUTTON_EXECUTE);
175         }
176         else if (_opData.getImpact() == OPERATION_IMPACT_ACTION)
177         {
178             setButton(BUTTON_EXECUTE);
179         }
180         else if (_opData.getImpact() == OPERATION_IMPACT_INFO)
181         {
182             setButton(BUTTON_REFRESH);
183             executeAndShowResults();
184         }
185         
186         _form.setVisible(true);
187         layout();
188     }
189     
190     public void layout()
191     {
192         _form.layout(true);
193         _form.getBody().layout(true, true);
194     }
195     
196     /**
197      * populates the header composite, containing the operation name and description.
198      */
199     private void setHeader()
200     {
201         _form.setText(ViewUtility.getDisplayText(_opData.getName()));
202         _headerComposite.setLayout(new GridLayout(2false));
203         //operation description
204         Label label = _toolkit.createLabel(_headerComposite,  DESCRIPTION + " : ");
205         label.setFont(ApplicationRegistry.getFont(FONT_BOLD));
206         label.setLayoutData(new GridData(SWT.LEAD, SWT.TOP, false, false));
207         
208         label = _toolkit.createLabel(_headerComposite,  _opData.getDescription());
209         label.setFont(ApplicationRegistry.getFont(FONT_NORMAL));
210         label.setLayoutData(new GridData(SWT.LEAD, SWT.TOP, true, false));
211         
212         _headerComposite.layout();
213     }
214     
215     /**
216      * Creates the widgets for operation parameters if there are any
217      */
218     private void createParameterWidgets()
219     {
220         List<ParameterData> params = _opData.getParameters();
221         if (params == null || params.isEmpty())
222         {
223             return;
224         }
225         
226         // Customised parameter widgets        
227         if (_mbean.isExchange() &&
228             EXCHANGE_TYPE_VALUES[2].equals(_mbean.getProperty(EXCHANGE_TYPE)) &&
229             _opData.getName().equalsIgnoreCase(OPERATION_CREATE_BINDING))
230         {                                  
231             customCreateNewBinding()
232             return;
233         }
234         // end of Customised parameter widgets       
235         
236         _paramsComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
237         _paramsComposite.setLayout(new FormLayout());
238         int parameterPositionOffset = 0;
239         for (ParameterData param : params)
240         {            
241             boolean valueInCombo = false;
242             Label label = _toolkit.createLabel(_paramsComposite, ViewUtility.getDisplayText(param.getName()));
243             FormData formData = new FormData();
244             if (params.indexOf(param== 0)
245             {
246                 parameterPositionOffset = 0;
247             }
248             else
249             {
250                 parameterPositionOffset += heightForAParameter;
251             }
252             formData.top = new FormAttachment(0, parameterPositionOffset + 2);
253             formData.right = new FormAttachment(labelWidth);
254             label.setLayoutData(formData);
255             label.setToolTipText(param.getDescription());
256             
257             formData = new FormData();
258             formData.top = new FormAttachment(0, parameterPositionOffset);
259             formData.left = new FormAttachment(label, 5);
260             formData.right = new FormAttachment(valueWidth);
261             // this will contain the list of items, if the list is to be made available to choose from
262             // e.g. the list of exchanges
263             String[] items = null;
264             if (param.getName().equals(QUEUE))
265             {
266                 List<String> qList = ApplicationRegistry.getServerRegistry(_mbean).getQueueNames(_virtualHostName);
267                 // Customization for AMQQueueMBean method OPERATION_MOVE_MESSAGES
268                 if (_opData.getName().equals(OPERATION_MOVE_MESSAGES))
269                 {
270                     qList.remove(_mbean.getName());    
271                 }
272                 // End of Customization
273                 items = qList.toArray(new String[0]);
274             }
275             else if (param.getName().equals(EXCHANGE))
276             {
277                 items = ApplicationRegistry.getServerRegistry(_mbean).getExchangeNames(_virtualHostName);
278             }
279             else if (param.getName().equals(EXCHANGE_TYPE))
280             {
281                 items = EXCHANGE_TYPE_VALUES;
282             }
283             else if (isUserListParameter(param))
284             {
285                 List<String> list = ApplicationRegistry.getServerRegistry(_mbean).getUsernames();
286                 if (list != null && !list.isEmpty())
287                 {
288                     items = list.toArray(new String[0]);
289                 }
290             }
291             
292             if (items != null)
293             {
294                 org.eclipse.swt.widgets.List _list = new org.eclipse.swt.widgets.List(_paramsComposite, SWT.BORDER | SWT.V_SCROLL);
295                 int listSize = _form.getClientArea().height * 3;
296                 int itemsHeight = items.length * (_list.getItemHeight() 2);
297                 // Set a min height for the list widget (set it to min 4 items)
298                 if (items.length < 4)
299                 {
300                     itemsHeight = (_list.getItemHeight() 2);
301                 }
302                 
303                 listSize = (listSize > itemsHeight? itemsHeight : listSize;
304                 parameterPositionOffset = parameterPositionOffset + listSize;
305                 formData.bottom = new FormAttachment(0, parameterPositionOffset);
306                 _list.setLayoutData(formData);
307                 _list.setData(param);
308                 _list.setItems(items);
309                 _list.addSelectionListener(parameterSelectionListener);
310                 valueInCombo = true;
311             }
312             else if (param.isBoolean())
313             {
314                 Button booleanButton = _toolkit.createButton(_paramsComposite, "", SWT.CHECK);
315                 booleanButton.setLayoutData(formData);
316                 booleanButton.setData(param);
317                 booleanButton.addSelectionListener(booleanSelectionListener);
318                 valueInCombo = true;                
319             }
320             else
321             {
322                 int style = SWT.NONE;
323                 if (PASSWORD.equalsIgnoreCase(param.getName()))
324                 {
325                     style = SWT.PASSWORD;
326                 }
327                 Text text = _toolkit.createText(_paramsComposite, "", style);
328                 formData = new FormData();
329                 formData.top = new FormAttachment(0, parameterPositionOffset);
330                 formData.left = new FormAttachment(label, 5);
331                 formData.right = new FormAttachment(valueWidth);
332                 text.setLayoutData(formData);
333                 // Listener to assign value to the parameter
334                 text.addKeyListener(keyListener);
335                 // Listener to verify if the entered key is valid
336                 text.addVerifyListener(verifyListener);
337                 text.setData(param);
338             }
339             
340             // display the parameter data type next to the text field
341             if (valueInCombo)
342             {
343                 label = _toolkit.createLabel(_paramsComposite, "");
344             }
345             else if (PASSWORD.equalsIgnoreCase(param.getName()))
346             {
347                 label = _toolkit.createLabel(_paramsComposite, "(String)");
348             }
349             else
350             {
351                 String str = param.getType();
352                 
353                 if (param.getType().lastIndexOf("."!= -1)
354                     str = param.getType().substring(+ param.getType().lastIndexOf("."));
355                 
356                 label = _toolkit.createLabel(_paramsComposite, "(" + str + ")");
357             }
358             formData = new FormData();
359             formData.top = new FormAttachment(0, parameterPositionOffset);
360             formData.left = new FormAttachment(valueWidth, 5);
361             label.setLayoutData(formData);
362         }
363     }
364     
365     private boolean isUserListParameter(ParameterData param)
366     {
367         if (_mbean.isAdmin() && param.getName().equals(OPERATION_PARAM_USERNAME)
368                 && !_opData.getName().equals(OPERATION_CREATEUSER))
369         {
370             return true;
371         }
372         
373         return false;
374     }
375     
376     /**
377      * Creates customized dispaly for a method "CreateNewBinding" for Headers exchange
378      *
379      */
380     private void customCreateNewBinding()
381     {
382         headerBindingHashMap = new HashMap<Text, Text>();
383  
384         _paramsComposite.setLayout(new GridLayout());
385         _paramsComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
386         final ScrolledComposite scrolledComposite = new ScrolledComposite(_paramsComposite, SWT.BORDER | SWT.V_SCROLL);
387         scrolledComposite.setExpandHorizontal(true);
388         scrolledComposite.setExpandVertical(true);   
389         GridData layoutData = new GridData(SWT.FILL, SWT.TOP, true, true);
390         scrolledComposite.setLayoutData(layoutData);
391         scrolledComposite.setLayout(new GridLayout());
392         
393         final Composite composite = _toolkit.createComposite(scrolledComposite, SWT.NONE);
394         scrolledComposite.setContent(composite);
395         layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);            
396         layoutData.verticalIndent = 20;
397         composite.setLayoutData(layoutData);
398         composite.setLayout(new FormLayout());
399         
400         List<ParameterData> params = _opData.getParameters();
401         ParameterData param = params.get(0);
402         // Queue selection widget
403         Label label = _toolkit.createLabel(composite, ViewUtility.getDisplayText(param.getName()));
404         FormData formData = new FormData();
405         formData.top = new FormAttachment(02);
406         formData.right = new FormAttachment(labelWidth);
407         label.setLayoutData(formData);
408         label.setToolTipText(param.getDescription());
409         
410         formData = new FormData();
411         formData.top = new FormAttachment(0);
412         formData.left = new FormAttachment(label, 5);
413         formData.right = new FormAttachment(valueWidth);
414 
415         Combo combo = new Combo(composite, SWT.READ_ONLY | SWT.DROP_DOWN);
416         List<String> qList = ApplicationRegistry.getServerRegistry(_mbean).getQueueNames(_virtualHostName);
417         combo.setItems(qList.toArray(new String[0]));
418         combo.add("Select Queue"0)
419         combo.select(0);
420         combo.setLayoutData(formData);
421         combo.setData(param);
422         combo.addSelectionListener(parameterSelectionListener);
423 
424         // Binding creation widgets
425         createARowForCreatingHeadersBinding(composite, 1);
426         createARowForCreatingHeadersBinding(composite, 2);
427         createARowForCreatingHeadersBinding(composite, 3);
428         createARowForCreatingHeadersBinding(composite, 4);
429         createARowForCreatingHeadersBinding(composite, 5);
430         createARowForCreatingHeadersBinding(composite, 6);
431         createARowForCreatingHeadersBinding(composite, 7);
432         createARowForCreatingHeadersBinding(composite, 8);
433         
434         final Button addMoreButton = _toolkit.createButton(composite, "Add More", SWT.PUSH);
435         formData = new FormData();
436         formData.top = new FormAttachment(0, heightForAParameter);
437         formData.left = new FormAttachment(705);
438         addMoreButton.setLayoutData(formData);
439         addMoreButton.setData("rowCount"8);
440         addMoreButton.addSelectionListener(new SelectionAdapter()
441             {
442                 public void widgetSelected(SelectionEvent e)
443                 {
444                     int count = Integer.parseInt(addMoreButton.getData("rowCount").toString());
445                     createARowForCreatingHeadersBinding(composite, ++count);
446                     addMoreButton.setData("rowCount", count);
447                     scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
448                     composite.layout();
449                     _form.layout();
450                 }
451             });
452           
453         scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
454         composite.layout();
455     }
456     
457     /**
458      * Adds a row for adding a binding for Headers Exchange. Used by the method, which creates the customized
459      * layout and widgest for Header's exchange method createNewBinding.
460      @param parent composite
461      @param rowCount - row number
462      */
463     private void createARowForCreatingHeadersBinding(Composite parent, int rowCount)
464     {  
465         Label key = _toolkit.createLabel(parent, "Name");
466         FormData formData = new FormData();
467         formData.top = new FormAttachment(0, rowCount * heightForAParameter + 2);
468         formData.right = new FormAttachment(15);
469         key.setLayoutData(formData);
470         
471         Text keyText = _toolkit.createText(parent, "", SWT.NONE);
472         formData = new FormData();
473         formData.top = new FormAttachment(0, rowCount * heightForAParameter);
474         formData.left = new FormAttachment(key, 5);
475         formData.right = new FormAttachment(40);
476         keyText.setLayoutData(formData);
477         keyText.addKeyListener(headerBindingListener);
478         
479         Label value = _toolkit.createLabel(parent, "Value");
480         formData = new FormData();
481         formData.top = new FormAttachment(0, rowCount * heightForAParameter + 2);
482         formData.right = new FormAttachment(45);
483         value.setLayoutData(formData);
484         
485         Text valueText = _toolkit.createText(parent, "", SWT.NONE);
486         formData = new FormData();
487         formData.top = new FormAttachment(0, rowCount * heightForAParameter);
488         formData.left = new FormAttachment(value, 5);
489         formData.right = new FormAttachment(70);
490         valueText.setLayoutData(formData);
491         valueText.addKeyListener(headerBindingListener);
492         
493         // Add these to the map, to retrieve the values while setting the parameter value
494         headerBindingHashMap.put(keyText, valueText);
495     }
496     
497     /**
498      * Sets text and listener for the operation execution button
499      @param text
500      */
501     private void setButton(String text)
502     {
503         _executionButton.setText(text);
504         _executionButton.removeSelectionListener(refreshListener);
505         _executionButton.removeSelectionListener(operationExecutionListener);
506         
507         if (BUTTON_EXECUTE.equals(text))
508         {
509             _executionButton.addSelectionListener(operationExecutionListener);    
510         }
511         else
512         {
513             _executionButton.addSelectionListener(refreshListener);
514         }
515     }   
516 
517     /**
518      * displays the operation result in a pop-up window
519      @param result
520      */
521     private void populateResults(Object result)
522     {
523         Display display = Display.getCurrent();
524         int width = 600;
525         int height = 400;
526         Shell shell = ViewUtility.createPopupShell(RESULT, width, height);
527         shell.setImage(ApplicationRegistry.getImage(CONSOLE_IMAGE));
528         ViewUtility.populateCompositeWithData(_toolkit, shell, result);
529         
530         shell.open();
531         while (!shell.isDisposed()) {
532             if (!display.readAndDispatch()) {
533                 display.sleep();
534             }
535         }
536         shell.dispose();
537     }
538     
539     /**
540      * Clears the parameter values of the operation
541      */
542     private void clearParameters()
543     {
544         List<ParameterData> params = _opData.getParameters();
545         if (params != null && !params.isEmpty())
546         {
547             for (ParameterData param : params)
548             {
549                 param.setDefaultValue();
550             }
551         }
552     }
553     
554     /**
555      * Clears the values entered by the user from parameter value widgets
556      @param control
557      */
558     private void clearParameterValues(Composite control)
559     {
560         if (control == null || (control.isDisposed()))
561             return;
562         
563         Control[] controls = control.getChildren();
564         if (controls == null || controls.length == 0)
565             return;
566         
567         for (int i = 0; i < controls.length; i++)
568         {
569             if (controls[iinstanceof Combo)
570                 ((Combo)controls[i]).select(0);
571             if (controls[iinstanceof org.eclipse.swt.widgets.List)
572                 ((org.eclipse.swt.widgets.List)controls[i]).deselectAll();
573             else if (controls[iinstanceof Text)
574                 ((Text)controls[i]).setText("");
575             else if (controls[iinstanceof Button)
576                 ((Button)controls[i]).setSelection(false);
577             else if (controls[iinstanceof Composite)
578                 clearParameterValues((Composite)controls[i]);
579         }
580     }
581     
582     /**
583      * Listener class for operation execution events
584      */
585     private class OperationExecutionListener extends SelectionAdapter
586     {
587         public void widgetSelected(SelectionEvent e)
588         {
589             List<ParameterData> params = _opData.getParameters();
590             if (params != null && !params.isEmpty())
591             {
592                 for (ParameterData param : params)
593                 
594                     if (param.getValue() == null || param.getValue().toString().length() == 0)
595                     {
596                         // Customized check, because for this parameter null is allowed
597                         if (param.getName().equals(ATTRIBUTE_QUEUE_OWNER&&
598                             _opData.getName().equals(OPERATION_CREATE_QUEUE))
599                         {
600                             continue;
601                         }
602                         // End of custom code
603                         
604                         ViewUtility.popupInfoMessage(_form.getText()"Please select the " + ViewUtility.getDisplayText(param.getName()));                       
605                         return;
606                     }
607                     
608                     // customized for passwords
609                     if (PASSWORD.equalsIgnoreCase(param.getName()))
610                     {
611                         if (param.getType().equals("[C"))
612                         {
613                             try
614                             {
615                                 param.setValue(ViewUtility.getHash((String)param.getValue()));
616                             }
617                             catch (Exception ex)
618                             {
619                                 MBeanUtility.handleException(_mbean, ex);
620                                 return;
621                             }
622                         }
623                     }
624                     // end of customization
625                 }
626             }
627             
628             if (_opData.getImpact() == OPERATION_IMPACT_ACTION)
629             {
630                 String bean = _mbean.getName() == null ? _mbean.getType() : _mbean.getName();
631                 int response = ViewUtility.popupConfirmationMessage(bean, "Do you want to " + _form.getText()" ?");
632                 if (response == SWT.YES)
633                 {
634                     executeAndShowResults();
635                 }            
636             }
637             else
638             {
639                 executeAndShowResults();
640             }
641             
642             if (_mbean.isAdmin() && _opData.getName().equals(OPERATION_DELETEUSER))
643             {
644                 refresh(_mbean);
645             }
646             else
647             {
648                 clearParameters();
649                 clearParameterValues(_paramsComposite);
650             }
651         }
652     }
653     
654     // Listener for the "Refresh" execution button
655     private class RefreshListener extends SelectionAdapter
656     {
657         public void widgetSelected(SelectionEvent e)
658         {
659             executeAndShowResults();
660         }
661     }
662     
663     /**
664      * Executres the operation, gets the result from server and displays to the user
665      */
666     private void executeAndShowResults()
667     {
668         Object result = null;
669         try
670         {
671             result = MBeanUtility.execute(_mbean, _opData);     
672         }
673         catch(Exception ex)
674         {
675             MBeanUtility.handleException(_mbean, ex);
676             return;
677         }
678         
679         // Custom code for Admin mbean operation
680         /* These custome codes here are to make the GUI look more user friendly. 
681          * Here we are adding the users to a list, which will be used to list username to be selected on
682          * pages like "delete user", "set password" instead of typing the username
683         */
684         if (_mbean.isAdmin())
685         {
686             if (_opData.getName().equals(OPERATION_VIEWUSERS))
687             {
688                 ApplicationRegistry.getServerRegistry(_mbean).setUserList(extractUserList(result));
689             }
690             else if (_opData.getName().equals(OPERATION_DELETEUSER))
691             {
692                 List<String> list = ApplicationRegistry.getServerRegistry(_mbean).getUsernames();
693                 Object userName = _opData.getParameterValue(OPERATION_PARAM_USERNAME);
694                 if ((list != null&& !list.isEmpty() && (userName != null))
695                 {
696                     list.remove(userName);
697                     ApplicationRegistry.getServerRegistry(_mbean).setUserList(list);
698                 }                
699             }
700             else if (_opData.getName().equals(OPERATION_CREATEUSER))
701             {
702                 List<String> list = ApplicationRegistry.getServerRegistry(_mbean).getUsernames();
703                 Object userName = _opData.getParameterValue(OPERATION_PARAM_USERNAME);
704                 if ((list != null&& !list.isEmpty() && (userName != null))
705                 {
706                     list.add(userName.toString());
707                     ApplicationRegistry.getServerRegistry(_mbean).setUserList(list);
708                 }                
709             }
710         }
711         // end of custom code
712         
713         // Some mbeans have only "type" and no "name".
714         String title = _mbean.getType();
715         if (_mbean.getName() != null && _mbean.getName().length() != 0)
716         {
717             title = _mbean.getName();
718         }
719         
720         if (_opData.isReturnTypeVoid())
721         {
722             ViewUtility.popupInfoMessage(title, OPERATION_SUCCESSFUL);
723         }
724         else if (_opData.isReturnTypeBoolean())
725         {
726             boolean success = Boolean.parseBoolean(result.toString());
727             String message = success ? OPERATION_SUCCESSFUL : OPERATION_UNSUCCESSFUL;
728             ViewUtility.popupInfoMessage(title, message);
729         }
730         else if (_opData.getParameters() != null && !_opData.getParameters().isEmpty())
731         {
732             populateResults(result);
733         }
734         else
735         {
736             ViewUtility.disposeChildren(_resultsComposite);
737             ViewUtility.populateCompositeWithData(_toolkit, _resultsComposite, result);
738             _resultsComposite.layout();
739             _form.layout();
740         }
741 
742     }
743     
744     private List<String> extractUserList(Object result)
745     {
746         if (!(result instanceof TabularDataSupport))
747         {
748             return null;
749         }
750         
751         TabularDataSupport tabularData = (TabularDataSupport)result;
752         Collection<Object> records = tabularData.values();
753         List<String> list = new ArrayList<String>();
754         for (Object o : records)
755         {
756             CompositeData data = (CompositeDatao;
757             if (data.containsKey(USERNAME))
758             {
759                 list.add(data.get(USERNAME).toString());
760             }
761         }
762         
763         return list;
764     }
765     
766     /**
767      * Listener class for the operation parameters widget
768      */
769     private class ParameterSelectionListener extends SelectionAdapter
770     {
771         public void widgetSelected(SelectionEvent e)
772         {
773             ParameterData parameter = (ParameterData)e.widget.getData();
774             parameter.setValue(null);
775             if (e.widget instanceof Combo)
776             {
777                 Combo combo = (Combo)e.widget;
778                 if (combo.getSelectionIndex() 0)
779                 {
780                     String item = combo.getItem(combo.getSelectionIndex());                
781                     parameter.setValueFromString(item);
782                 }
783             }
784             else if (e.widget instanceof org.eclipse.swt.widgets.List)
785             {
786                 org.eclipse.swt.widgets.List list = (org.eclipse.swt.widgets.List)e.widget;
787                 String[] selectedItems = list.getSelection();
788                 if (selectedItems.length > 0)
789                 {
790                     parameter.setValueFromString(selectedItems[0]);
791                 }
792             }
793         }
794     }
795     
796     /**
797      * Listener class for boolean parameter widgets
798      */
799     private class BooleanSelectionListener extends SelectionAdapter
800     {
801         public void widgetSelected(SelectionEvent e)
802         {
803             ParameterData parameter = (ParameterData)(e.widget.getData());
804             if (e.widget instanceof Button)
805             {
806                 Button button = (Button)e.widget;
807                 parameter.setValue(button.getSelection());
808             }
809             else if (e.widget instanceof Combo)
810             {
811                 Combo combo = (Combo)e.widget;
812                 String item = combo.getItem(combo.getSelectionIndex());                
813                 parameter.setValueFromString(item);
814             }
815         }
816     }
817     
818     /**
819      * Listener class for the operation parameter value widget (Text field)
820      */
821     private class KeyListenerImpl extends KeyAdapter
822     {
823         public void keyReleased(KeyEvent e
824         {
825             if (!(e.widget instanceof Text))
826                 return;
827             
828             Text text = (Text)e.widget;
829             // Get the parameters widget and assign the text to the parameter
830             String strValue = text.getText();
831             ParameterData parameter = (ParameterData)text.getData();
832             try
833             {
834                 parameter.setValueFromString(strValue);
835             }
836             catch(Exception ex)
837             {
838                 // Exception occured in setting parameter value. 
839                 // ignore it. The value will not be assigned to the parameter
840             }
841         }
842     }
843     
844     /**
845      * Listener class for HeaderExchange's new binding widgets. Used when the new bindings are 
846      * being created for Header's Exchange
847      */
848     private class HeaderBindingKeyListener extends KeyAdapter
849     {
850         public void keyReleased(KeyEvent e
851         {
852             ParameterData param = _opData.getParameters().get(1);
853             StringBuffer paramValue = new StringBuffer();
854             for (Entry<Text, Text> entry : headerBindingHashMap.entrySet())
855             {
856                 
857                 Text nameText = entry.getKey();
858                 String name = nameText.getText();
859                 Text valueText = entry.getValue();
860                 String value = valueText.getText();
861                 if ((name != null&& (name.length() != 0&& (value != null&& (value.length() != 0))
862                 {
863                     if (paramValue.length() != 0)
864                     {
865                         paramValue.append(",");
866                     }
867                     paramValue.append(name + "=" + value);
868                 }
869             }
870             
871             param.setValue(paramValue.toString());
872         }
873     }
874     
875     /**
876      * Listener class for verifying the user input with parameter type
877      */
878     private class VerifyListenerImpl implements VerifyListener
879     {
880         public void verifyText(VerifyEvent event)
881         {
882             ParameterData parameter = (ParameterData)event.widget.getData();
883             String text = event.text;
884             char [] chars = new char [text.length ()];
885             text.getChars(0, chars.length, chars, 0);           
886             String type = parameter.getType();
887             if (type.equals("int"|| type.equals("java.lang.Integer"||
888                 type.equals("long"|| type.equals("java.lang.Long"))
889             {
890                 for (int i=0; i<chars.length; i++)
891                 {
892                     if (!('0' <= chars [i&& chars [i<= '9'))
893                     {
894                         event.doit = false;
895                         return;
896                     }
897                 }
898                 
899             }
900         }
901     }
902     
903 }