Eclipse plug-in for service provider.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / AttributeView.java
1 package oic.simulator.serviceprovider.view;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8
9 import oic.simulator.serviceprovider.Activator;
10 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
11 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
12 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
13 import oic.simulator.serviceprovider.manager.ResourceManager;
14 import oic.simulator.serviceprovider.resource.AutomationSettingHelper;
15 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
16 import oic.simulator.serviceprovider.resource.ResourceAttribute;
17 import oic.simulator.serviceprovider.resource.SimulatorResource;
18 import oic.simulator.serviceprovider.utils.Constants;
19 import oic.simulator.serviceprovider.utils.Utility;
20
21 import org.eclipse.jface.viewers.CellEditor;
22 import org.eclipse.jface.viewers.CellLabelProvider;
23 import org.eclipse.jface.viewers.ColumnLabelProvider;
24 import org.eclipse.jface.viewers.ComboBoxCellEditor;
25 import org.eclipse.jface.viewers.EditingSupport;
26 import org.eclipse.jface.viewers.ISelectionChangedListener;
27 import org.eclipse.jface.viewers.IStructuredContentProvider;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.StyledCellLabelProvider;
31 import org.eclipse.jface.viewers.TableViewer;
32 import org.eclipse.jface.viewers.TableViewerColumn;
33 import org.eclipse.jface.viewers.Viewer;
34 import org.eclipse.jface.viewers.ViewerCell;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.custom.CCombo;
37 import org.eclipse.swt.events.SelectionAdapter;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.events.TraverseEvent;
40 import org.eclipse.swt.events.TraverseListener;
41 import org.eclipse.swt.events.VerifyEvent;
42 import org.eclipse.swt.events.VerifyListener;
43 import org.eclipse.swt.graphics.Color;
44 import org.eclipse.swt.graphics.Point;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.layout.GridLayout;
47 import org.eclipse.swt.widgets.Button;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Display;
50 import org.eclipse.swt.widgets.Group;
51 import org.eclipse.swt.widgets.Label;
52 import org.eclipse.swt.widgets.Table;
53 import org.eclipse.ui.part.ViewPart;
54 import org.oic.simulator.AutomationType;
55
56 public class AttributeView extends ViewPart {
57
58     public static final String                  VIEW_ID            = "oic.simulator.serviceprovider.view.attribute";
59
60     private TableViewer                         attTblViewer;
61     private TableViewer                         automtnSettingsTblViewer;
62
63     private IResourceSelectionChangedUIListener resourceSelectionChangedListener;
64     private IResourceModelChangedUIListener     resourceModelChangedUIListener;
65     private IAutomationUIListener               automationUIListener;
66
67     private Label                               newValueLbl;
68     private CCombo                              valueCmb;
69     private Button                              submitButton;
70     private Button                              clearButton;
71
72     private ResourceAttribute                   attributeInSelection;
73     private AttributeAutomationSettingEditor    automationSettingEditor;
74     private List<AutomationSettingHelper>       localSettingList;
75
76     private final String[]                      attTblHeaders      = { "Name",
77             "Value", "Automation Status"                          };
78     private final String[]                      settingTblHeaders  = {
79             "Setting", "Value"                                    };
80     private final Integer[]                     attTblColWidth     = { 150,
81             190, 150                                              };
82     private final Integer[]                     settingTblColWidth = { 140, 85 };
83
84     private ResourceManager                     resourceManager;
85
86     public AttributeView() {
87
88         resourceManager = Activator.getDefault().getResourceManager();
89
90         resourceSelectionChangedListener = new IResourceSelectionChangedUIListener() {
91
92             @Override
93             public void onResourceSelectionChange() {
94                 Display.getDefault().asyncExec(new Runnable() {
95
96                     @Override
97                     public void run() {
98                         if (null != attTblViewer) {
99                             updateViewer(checkSelection());
100                         }
101                     }
102                 });
103             }
104         };
105
106         resourceModelChangedUIListener = new IResourceModelChangedUIListener() {
107
108             @Override
109             public void onResourceModelChange(
110                     final ModelChangeNotificationType notificationType,
111                     final String resourceURI) {
112                 Display.getDefault().asyncExec(new Runnable() {
113                     @Override
114                     public void run() {
115                         // Handle the notification only if it is for the current
116                         // resource in selection
117                         SimulatorResource resource = resourceManager
118                                 .getCurrentResourceInSelection();
119                         if (null == resource) {
120                             return;
121                         }
122                         if (!resourceURI.equals(resource.getResourceURI())) {
123                             return;
124                         }
125                         // Refresh the table viewers which will display
126                         // the updated values
127                         if (null != attTblViewer) {
128                             attTblViewer.refresh();
129                         }
130                         // If this call has a new value for the current
131                         // attribute
132                         // in selection, then update it in the combo box
133                         if (notificationType != ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED) {
134                             return;
135                         }
136                         if (null != valueCmb && !valueCmb.isDisposed()
137                                 && null != attributeInSelection) {
138                             Map<String, ResourceAttribute> attributeMap = resource
139                                     .getResourceAttributesMap();
140                             if (null != attributeMap) {
141                                 ResourceAttribute attribute = attributeMap
142                                         .get(attributeInSelection
143                                                 .getAttributeName());
144                                 if (null != attribute) {
145                                     Object valueObj = attribute
146                                             .getAttributeValue();
147                                     if (null != valueObj) {
148                                         String valueStr = String
149                                                 .valueOf(valueObj);
150                                         int index = valueCmb.indexOf(valueStr);
151                                         if (index != -1) {
152                                             valueCmb.select(index);
153                                         } else {
154                                             valueCmb.add(valueStr);
155                                             valueCmb.select(valueCmb
156                                                     .indexOf(valueStr));
157                                         }
158                                     }
159                                 }
160                             }
161                         }
162                     }
163                 });
164             }
165         };
166
167         automationUIListener = new IAutomationUIListener() {
168
169             @Override
170             public void onResourceAutomationStart(final String resourceURI) {
171                 Display.getDefault().asyncExec(new Runnable() {
172
173                     @Override
174                     public void run() {
175                         if (null == resourceURI) {
176                             return;
177                         }
178                         SimulatorResource resource = resourceManager
179                                 .getCurrentResourceInSelection();
180                         if (null == resource) {
181                             return;
182                         }
183                         String uri = resource.getResourceURI();
184                         // Checking whether attributes view is currently
185                         // displaying the attributes of the
186                         // resource whose automation has just started
187                         if (null != uri && uri.equals(resourceURI)) {
188                             Table tbl;
189                             tbl = attTblViewer.getTable();
190                             if (!tbl.isDisposed()) {
191                                 attTblViewer.refresh();
192                             }
193
194                             // If any attribute is in selection, then update
195                             // the automation setting to enable.
196                             if (null != attributeInSelection) {
197                                 tbl = automtnSettingsTblViewer.getTable();
198                                 if (!tbl.isDisposed()) {
199                                     // Change the local automation settings
200                                     AutomationSettingHelper
201                                             .updateAutomationStatus(
202                                                     localSettingList,
203                                                     Constants.ENABLE);
204                                     automtnSettingsTblViewer.refresh();
205                                 }
206                             }
207
208                             // Disable the manual change UI controls
209                             setVisibilityForManualValueChange(false);
210                         }
211                     }
212                 });
213             }
214
215             @Override
216             public void onAutomationComplete(final String resourceURI,
217                     final String attName) {
218                 // This method notifies the completion of attribute level
219                 // automation.
220                 Display.getDefault().asyncExec(new Runnable() {
221
222                     @Override
223                     public void run() {
224                         if (null == resourceURI) {
225                             return;
226                         }
227                         // Check if the given resourceURI is the uri of the
228                         // resource whose attributes are currently being
229                         // displayed by this view.
230                         SimulatorResource resource = resourceManager
231                                 .getCurrentResourceInSelection();
232                         if (null == resource) {
233                             return;
234                         }
235                         String uri = resource.getResourceURI();
236                         if (null != uri && uri.equals(resourceURI)) {
237                             Table tbl;
238                             tbl = attTblViewer.getTable();
239                             if (!tbl.isDisposed()) {
240                                 attTblViewer.refresh();
241                             }
242
243                             if (null != attributeInSelection) {
244                                 if (null == attName
245                                         || attributeInSelection
246                                                 .getAttributeName().equals(
247                                                         attName)) {
248                                     tbl = automtnSettingsTblViewer.getTable();
249                                     if (!tbl.isDisposed()) {
250                                         // Change the local automation settings
251                                         AutomationSettingHelper
252                                                 .updateAutomationStatus(
253                                                         localSettingList,
254                                                         Constants.DISABLE);
255                                         automtnSettingsTblViewer.refresh();
256                                     }
257                                     setVisibilityForManualValueChange(true);
258                                 }
259                             }
260                         }
261                     }
262                 });
263             }
264         };
265     }
266
267     @Override
268     public void createPartControl(Composite parent) {
269         Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
270
271         parent.setLayout(new GridLayout(2, false));
272         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
273         parent.setLayoutData(gd);
274
275         Group attGroup = new Group(parent, SWT.NONE);
276         attGroup.setLayout(new GridLayout(1, false));
277         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
278         gd.horizontalSpan = 2;
279         gd.minimumHeight = 175;
280         attGroup.setLayoutData(gd);
281         attGroup.setText("Attributes");
282         attGroup.setBackground(color);
283
284         attTblViewer = new TableViewer(attGroup, SWT.MULTI | SWT.H_SCROLL
285                 | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
286
287         createAttributeColumns(attTblViewer);
288
289         // make lines and header visible
290         Table table = attTblViewer.getTable();
291         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
292         table.setHeaderVisible(true);
293         table.setLinesVisible(true);
294
295         attTblViewer.setContentProvider(new AttributeContentProvider());
296
297         Group automationSettingsTableGrp = new Group(parent, SWT.NONE);
298         automationSettingsTableGrp.setLayout(new GridLayout(1, false));
299         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
300         automationSettingsTableGrp.setLayoutData(gd);
301         automationSettingsTableGrp.setText("Automation Settings");
302         automationSettingsTableGrp.setBackground(color);
303
304         automtnSettingsTblViewer = new TableViewer(automationSettingsTableGrp,
305                 SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION
306                         | SWT.BORDER);
307
308         createSettingColumns(automtnSettingsTblViewer);
309
310         // make lines visible
311         table = automtnSettingsTblViewer.getTable();
312         gd = new GridData(SWT.FILL, SWT.FILL, true, true);
313         table.setLayoutData(gd);
314         table.setHeaderVisible(true);
315         table.setLinesVisible(true);
316
317         automtnSettingsTblViewer
318                 .setContentProvider(new AttributeContentProvider());
319
320         Composite rightViewComp = new Composite(parent, SWT.NONE);
321         rightViewComp.setLayout(new GridLayout(1, false));
322
323         Group valueUpdateGroup = new Group(rightViewComp, SWT.NONE);
324         valueUpdateGroup.setLayout(new GridLayout(2, false));
325         gd = new GridData();
326         gd.horizontalAlignment = SWT.FILL;
327         gd.grabExcessHorizontalSpace = true;
328         gd.horizontalSpan = 2;
329         valueUpdateGroup.setLayoutData(gd);
330         valueUpdateGroup.setText("Change Attribute Value");
331         valueUpdateGroup.setBackground(color);
332
333         newValueLbl = new Label(valueUpdateGroup, SWT.NONE);
334         newValueLbl.setText("Select New Value");
335
336         valueCmb = new CCombo(valueUpdateGroup, SWT.BORDER);
337         gd = new GridData();
338         gd.widthHint = 100;
339         valueCmb.setLayoutData(gd);
340
341         submitButton = new Button(rightViewComp, SWT.PUSH);
342         gd = new GridData();
343         gd.widthHint = 150;
344         gd.verticalIndent = 10;
345         gd.horizontalSpan = 2;
346         gd.horizontalAlignment = SWT.CENTER;
347         submitButton.setLayoutData(gd);
348         submitButton.setText("Apply Changes");
349
350         clearButton = new Button(rightViewComp, SWT.PUSH);
351         gd = new GridData();
352         gd.widthHint = 150;
353         gd.verticalIndent = 10;
354         gd.horizontalSpan = 2;
355         gd.horizontalAlignment = SWT.CENTER;
356         clearButton.setLayoutData(gd);
357         clearButton.setText("Reset Changes");
358
359         addUIListeners();
360
361         addManagerListeners();
362
363         // Check whether there is any resource selected already
364         List<ResourceAttribute> propertyList = checkSelection();
365         if (null != propertyList) {
366             updateViewer(propertyList);
367         }
368
369         setSettingSubmitVisibility(false);
370     }
371
372     public void createAttributeColumns(TableViewer tableViewer) {
373         TableViewerColumn attName = new TableViewerColumn(tableViewer, SWT.NONE);
374         attName.getColumn().setWidth(attTblColWidth[0]);
375         attName.getColumn().setText(attTblHeaders[0]);
376         attName.setLabelProvider(new ColumnLabelProvider() {
377             @Override
378             public String getText(Object element) {
379                 if (element instanceof ResourceAttribute) {
380                     ResourceAttribute attribute = (ResourceAttribute) element;
381                     if (null != attribute) {
382                         return attribute.getAttributeName();
383                     }
384                 }
385                 return "";
386             }
387         });
388
389         TableViewerColumn attValue = new TableViewerColumn(tableViewer,
390                 SWT.NONE);
391         attValue.getColumn().setWidth(attTblColWidth[1]);
392         attValue.getColumn().setText(attTblHeaders[1]);
393         attValue.setLabelProvider(new ColumnLabelProvider() {
394             @Override
395             public String getText(Object element) {
396                 if (element instanceof ResourceAttribute) {
397                     ResourceAttribute attribute = (ResourceAttribute) element;
398                     if (null != attribute) {
399                         Object val = attribute.getAttributeValue();
400                         if (null != val) {
401                             return String.valueOf(val);
402                         }
403                     }
404                 }
405                 return "";
406             }
407         });
408
409         TableViewerColumn automationStatus = new TableViewerColumn(tableViewer,
410                 SWT.NONE);
411         automationStatus.getColumn().setWidth(attTblColWidth[2]);
412         automationStatus.getColumn().setText(attTblHeaders[2]);
413         automationStatus.setLabelProvider(new ColumnLabelProvider() {
414             @Override
415             public String getText(Object element) {
416                 if (element instanceof ResourceAttribute) {
417                     ResourceAttribute attribute = (ResourceAttribute) element;
418                     if (null != attribute) {
419                         boolean progress = attribute.isAutomationInProgress();
420                         return Utility.getAutomationStatus(progress);
421                     }
422                 }
423                 return "";
424             }
425         });
426     }
427
428     public void createSettingColumns(TableViewer tableViewer) {
429         TableViewerColumn key = new TableViewerColumn(tableViewer, SWT.NONE);
430         key.getColumn().setWidth(settingTblColWidth[0]);
431         key.getColumn().setText(settingTblHeaders[0]);
432         key.setLabelProvider(new StyledCellLabelProvider() {
433             @Override
434             public void update(ViewerCell cell) {
435                 Object element = cell.getElement();
436                 if (element instanceof AutomationSettingHelper) {
437                     AutomationSettingHelper setting = (AutomationSettingHelper) element;
438                     cell.setText(setting.getSettingID());
439                 }
440             }
441         });
442
443         TableViewerColumn value = new TableViewerColumn(tableViewer, SWT.NONE);
444         value.getColumn().setWidth(settingTblColWidth[1]);
445         value.getColumn().setText(settingTblHeaders[1]);
446         value.setLabelProvider(new CellLabelProvider() {
447             @Override
448             public void update(ViewerCell cell) {
449                 Object element = cell.getElement();
450                 if (element instanceof AutomationSettingHelper) {
451                     AutomationSettingHelper setting = (AutomationSettingHelper) element;
452                     if (null != setting) {
453                         cell.setText(String.valueOf(setting.getSettingValue()));
454                     }
455                 }
456             }
457         });
458         automationSettingEditor = new AttributeAutomationSettingEditor(
459                 tableViewer);
460         value.setEditingSupport(automationSettingEditor);
461     }
462
463     private void addUIListeners() {
464         attTblViewer
465                 .addSelectionChangedListener(new ISelectionChangedListener() {
466
467                     @Override
468                     public void selectionChanged(SelectionChangedEvent e) {
469                         IStructuredSelection selection = (IStructuredSelection) attTblViewer
470                                 .getSelection();
471                         Object firstElement = selection.getFirstElement();
472                         if (firstElement instanceof ResourceAttribute) {
473                             ResourceAttribute attribute = (ResourceAttribute) firstElement;
474
475                             // Store the attribute selection
476                             attributeInSelection = attribute;
477
478                             // Change the visibility of the manual change
479                             // controls according to the automation status
480                             if (attribute.isAutomationInProgress()) {
481                                 setVisibilityForManualValueChange(false);
482                             } else {
483                                 setVisibilityForManualValueChange(true);
484                             }
485
486                             // Hide the visibility of submit and clear buttons
487                             setSettingSubmitVisibility(false);
488
489                             if (null != attribute) {
490                                 if (null != automtnSettingsTblViewer) {
491                                     localSettingList = AutomationSettingHelper
492                                             .getAutomationSettings(attribute);
493                                     if (null != localSettingList) {
494                                         automtnSettingsTblViewer.getTable()
495                                                 .setLinesVisible(true);
496                                         automtnSettingsTblViewer
497                                                 .setInput(localSettingList
498                                                         .toArray());
499                                     }
500                                 }
501                                 // Populate the attribute value combo
502                                 if (null != valueCmb && !valueCmb.isDisposed()) {
503                                     // Clear the existing combo list contents
504                                     valueCmb.removeAll();
505                                     Set<Object> valueList = attribute
506                                             .getValues();
507                                     Object value;
508                                     if (null != valueList) {
509                                         Iterator<Object> valueItr = valueList
510                                                 .iterator();
511                                         while (valueItr.hasNext()) {
512                                             value = valueItr.next();
513                                             if (null != value) {
514                                                 // Casting the value to String
515                                                 // for showing in combo
516                                                 valueCmb.add(String
517                                                         .valueOf(value));
518                                             }
519                                         }
520                                         // Setting the selection to the current
521                                         // value of the attribute
522                                         String attValue = String
523                                                 .valueOf(attribute
524                                                         .getAttributeValue());
525                                         int index = valueCmb.indexOf(attValue);
526                                         if (index != -1) {
527                                             valueCmb.select(index);
528                                         }
529                                     }
530                                 }
531                             }
532                         }
533                     }
534                 });
535
536         valueCmb.addSelectionListener(new SelectionAdapter() {
537             @Override
538             public void widgetSelected(SelectionEvent e) {
539                 if (null != attributeInSelection) {
540                     // attributeInSelection.setAttributeValue(valueCmb.getText());
541                     if (!valueCmb.getText().equals(
542                             String.valueOf(attributeInSelection
543                                     .getAttributeValue()))) {
544                         // Enable the submit and clear button controls
545                         setSettingSubmitVisibility(true);
546                     } else {
547                         setSettingSubmitVisibility(false);
548                     }
549                 }
550             }
551         });
552
553         submitButton.addSelectionListener(new SelectionAdapter() {
554             @Override
555             public void widgetSelected(SelectionEvent e) {
556
557                 if (null != attributeInSelection && null != localSettingList) {
558                     if (attributeInSelection.isAutomationInProgress()
559                             || valueCmb.getText().equals(
560                                     String.valueOf(attributeInSelection
561                                             .getAttributeValue()))) {
562                         // Change in automation settings
563                         Iterator<AutomationSettingHelper> settingItr = localSettingList
564                                 .iterator();
565
566                         // These variables will hold the new automation
567                         // settings.
568                         boolean autoEnable = false;
569                         AutomationType autoType = AutomationType.NORMAL;
570                         int autoUpdateInterval = Constants.DEFAULT_AUTOMATION_INTERVAL;
571
572                         // These variables will hold the old automation settings
573                         // and are used to roll-back the settings
574                         // if automation fails.
575                         boolean autoEnablePrev = false;
576                         AutomationType autoTypePrev = AutomationType.NORMAL;
577                         int autoUpdateIntervalPrev = Constants.DEFAULT_AUTOMATION_INTERVAL;
578
579                         String id;
580                         String value;
581                         AutomationSettingHelper setting;
582                         while (settingItr.hasNext()) {
583                             setting = settingItr.next();
584                             id = setting.getSettingID();
585                             value = setting.getSettingValue();
586                             if (id.equals(Constants.AUTOMATION)) {
587                                 autoEnable = Utility
588                                         .getAutomationBoolean(value);
589                                 autoEnablePrev = attributeInSelection
590                                         .isAutomationInProgress();
591
592                                 attributeInSelection
593                                         .setAutomationInProgress(autoEnable);
594                                 // Automation is disabled and hence the manual
595                                 // controls should be enabled
596                                 if (!autoEnable) {
597                                     setVisibilityForManualValueChange(true);
598                                 }
599                             } else if (id.equals(Constants.AUTOMATION_TYPE)) {
600                                 autoType = AutomationType.valueOf(value);
601                                 autoTypePrev = attributeInSelection
602                                         .getAutomationType();
603
604                                 attributeInSelection
605                                         .setAutomationType(autoType);
606                             } else if (id
607                                     .equals(Constants.UPDATE_INTERVAL_IN_MS)) {
608                                 autoUpdateInterval = Utility
609                                         .getUpdateIntervalFromString(value);
610                                 autoUpdateIntervalPrev = attributeInSelection
611                                         .getAutomationUpdateInterval();
612
613                                 attributeInSelection
614                                         .setAutomationUpdateInterval(autoUpdateInterval);
615                             }
616                         }
617
618                         // Updating the automation status in the resource
619                         SimulatorResource resource = resourceManager
620                                 .getCurrentResourceInSelection();
621                         resource.setAttributeAutomationInProgress(autoEnable);
622
623                         if (autoEnable) {
624                             // Send automation enable request
625                             int autoId = resourceManager.startAutomation(
626                                     resource, attributeInSelection, autoType,
627                                     autoUpdateInterval);
628                             if (-1 == autoId) {
629                                 // Automation failed
630                                 // Roll-back the attribute model and automation
631                                 // settings
632                                 settingItr = localSettingList.iterator();
633                                 while (settingItr.hasNext()) {
634                                     setting = settingItr.next();
635                                     id = setting.getSettingID();
636                                     value = setting.getSettingValue();
637                                     if (id.equals(Constants.AUTOMATION)) {
638                                         setting.setSettingValue(Utility
639                                                 .getAutomationString(autoEnablePrev));
640                                     } else if (id
641                                             .equals(Constants.AUTOMATION_TYPE)) {
642                                         setting.setSettingValue(autoTypePrev
643                                                 .toString());
644                                     } else if (id
645                                             .equals(Constants.UPDATE_INTERVAL_IN_MS)) {
646                                         setting.setSettingValue(String
647                                                 .valueOf(autoUpdateIntervalPrev));
648                                     }
649                                 }
650
651                                 attributeInSelection
652                                         .setAutomationInProgress(autoEnablePrev);
653                                 attributeInSelection
654                                         .setAutomationType(autoTypePrev);
655                                 attributeInSelection
656                                         .setAutomationUpdateInterval(autoUpdateIntervalPrev);
657
658                                 automtnSettingsTblViewer.refresh();
659
660                                 // Automation is disabled and hence the manual
661                                 // controls should be enabled
662                                 setVisibilityForManualValueChange(true);
663                             }
664                         } else {
665                             // Send automation disable request
666                             int autoId = attributeInSelection.getAutomationId();
667                             resourceManager.stopAutomation(resource, autoId);
668                         }
669                     } else {
670                         // Change in attribute value manually
671                         String input = valueCmb.getText();
672                         if (null != input && input.length() > 0) {
673                             attributeInSelection.setAttributeValue(input);
674
675                             SimulatorResource resource = resourceManager
676                                     .getCurrentResourceInSelection();
677                             resourceManager.attributeValueUpdated(resource,
678                                     attributeInSelection.getAttributeName(),
679                                     input);
680                         }
681                     }
682                 }
683
684                 attTblViewer.refresh();
685
686                 setSettingSubmitVisibility(false);
687             }
688         });
689
690         clearButton.addSelectionListener(new SelectionAdapter() {
691             @Override
692             public void widgetSelected(SelectionEvent e) {
693                 if (null != attributeInSelection) {
694                     // Update the settings table
695                     List<AutomationSettingHelper> settingList = AutomationSettingHelper
696                             .getAutomationSettings(attributeInSelection);
697                     if (null != settingList) {
698                         // clear the existing table contents
699                         Table tbl = automtnSettingsTblViewer.getTable();
700                         if (!tbl.isDisposed()) {
701                             tbl.removeAll();
702                             localSettingList = settingList;
703                             automtnSettingsTblViewer.setInput(localSettingList
704                                     .toArray());
705                         }
706                     }
707                     // Update the allowed values combo
708                     if (!valueCmb.isDisposed()) {
709                         valueCmb.setText(String.valueOf(attributeInSelection
710                                 .getAttributeValue()));
711                         setVisibilityForManualValueChange(!attributeInSelection
712                                 .isAutomationInProgress());
713                     }
714                 }
715                 setSettingSubmitVisibility(false);
716             }
717         });
718     }
719
720     private void addManagerListeners() {
721         resourceManager
722                 .addResourceSelectionChangedUIListener(resourceSelectionChangedListener);
723         resourceManager
724                 .addResourceModelChangedUIListener(resourceModelChangedUIListener);
725         resourceManager.addAutomationUIListener(automationUIListener);
726     }
727
728     private void setSettingSubmitVisibility(boolean visible) {
729         if (!submitButton.isDisposed())
730             submitButton.setEnabled(visible);
731         if (!clearButton.isDisposed())
732             clearButton.setEnabled(visible);
733     }
734
735     private List<ResourceAttribute> checkSelection() {
736         SimulatorResource resourceInSelection = resourceManager
737                 .getCurrentResourceInSelection();
738         if (null != resourceInSelection) {
739             List<ResourceAttribute> attList = resourceManager
740                     .getAttributes(resourceInSelection);
741             return attList;
742         } else {
743             return null;
744         }
745     }
746
747     private void updateViewer(List<ResourceAttribute> attList) {
748         Table tbl;
749         if (null != attList) {
750             tbl = attTblViewer.getTable();
751             if (null != tbl && !tbl.isDisposed()) {
752                 tbl.setLinesVisible(true);
753                 attTblViewer.setInput(attList.toArray());
754             }
755         } else {
756             // Clear the attributes table viewer
757             if (null != attTblViewer) {
758                 tbl = attTblViewer.getTable();
759                 if (null != tbl && !tbl.isDisposed()) {
760                     // tbl.deselectAll();
761                     tbl.removeAll();
762                     tbl.setLinesVisible(false);
763                 }
764             }
765         }
766
767         attributeInSelection = null;
768
769         // Clear the Settings table viewer
770         if (null != automtnSettingsTblViewer) {
771             tbl = automtnSettingsTblViewer.getTable();
772             if (null != tbl && !tbl.isDisposed()) {
773                 // tbl.deselectAll();
774                 tbl.removeAll();
775                 tbl.setLinesVisible(false);
776             }
777         }
778         // Clear the value list combo
779         if (!valueCmb.isDisposed()) {
780             valueCmb.removeAll();
781             valueCmb.setEnabled(false);
782         }
783
784         // Hide the visibility of submit and clear buttons
785         setSettingSubmitVisibility(false);
786     }
787
788     class AttributeContentProvider implements IStructuredContentProvider {
789
790         @Override
791         public void dispose() {
792         }
793
794         @Override
795         public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
796         }
797
798         @Override
799         public Object[] getElements(Object element) {
800             return (Object[]) element;
801         }
802
803     }
804
805     class AttributeAutomationSettingEditor extends EditingSupport {
806
807         private final TableViewer       viewer;
808
809         private AutomationSettingHelper currentSettingInSelection;
810
811         public AttributeAutomationSettingEditor(TableViewer viewer) {
812             super(viewer);
813             this.viewer = viewer;
814         }
815
816         @Override
817         protected boolean canEdit(Object element) {
818             return true;
819         }
820
821         @Override
822         protected CellEditor getCellEditor(Object element) {
823             ComboBoxCellEditor combo = null;
824             if (null != element) {
825                 List<String> valueList;
826                 boolean readOnly = true;
827                 AutomationSettingHelper setting = (AutomationSettingHelper) element;
828
829                 currentSettingInSelection = setting;
830
831                 String settingID = setting.getSettingID();
832                 if (settingID.equals(Constants.UPDATE_INTERVAL_IN_MS)) {
833                     readOnly = false;
834                 }
835                 valueList = setting.getAllowedValues();
836                 if (null == valueList) {
837                     valueList = new ArrayList<String>();
838                 }
839                 // Convert list to String[] for passing it to ComboBoxCellEditor
840                 String[] values;
841                 values = valueList.toArray(new String[1]);
842                 if (readOnly) {
843                     combo = new ComboBoxCellEditor(viewer.getTable(), values,
844                             SWT.READ_ONLY);
845                 } else {
846                     combo = new ComboBoxCellEditor(viewer.getTable(), values);
847                 }
848
849                 // If resource level automation is enabled, then all setting
850                 // should be disabled
851                 SimulatorResource resourceInSelection;
852                 resourceInSelection = resourceManager
853                         .getCurrentResourceInSelection();
854                 if (null != resourceInSelection
855                         && resourceInSelection.isResourceAutomationInProgress()) {
856                     CCombo c = (CCombo) combo.getControl();
857                     c.setEnabled(false);
858                 } else {
859                     // Enable the automation type and update interval combo
860                     // boxes only if the current value of automation is 'enable'
861                     if (settingID.equals(Constants.AUTOMATION_TYPE)
862                             || settingID
863                                     .equals(Constants.UPDATE_INTERVAL_IN_MS)) {
864                         if (!showSettings()) {
865                             // Disable the combo box
866                             CCombo c = (CCombo) combo.getControl();
867                             c.setEnabled(false);
868                         }
869                     }
870                 }
871                 // Enabling editing support in update interval combo box
872                 if (settingID.equals(Constants.UPDATE_INTERVAL_IN_MS)) {
873                     final CCombo c = (CCombo) combo.getControl();
874                     c.addVerifyListener(new VerifyListener() {
875                         @Override
876                         public void verifyText(VerifyEvent e) {
877                             String text = c.getText();
878                             String newText = text.substring(0, e.start)
879                                     + e.text + text.substring(e.end);
880                             try {
881                                 if (newText.length() != 0) {
882                                     Integer.parseInt(newText);
883                                 }
884                             } catch (NumberFormatException ex) {
885                                 e.doit = false;
886                             }
887                         }
888                     });
889                     c.addTraverseListener(new TraverseListener() {
890                         @Override
891                         public void keyTraversed(TraverseEvent e) {
892                             if (e.detail == SWT.TRAVERSE_RETURN) {
893                                 e.doit = false;
894                                 e.detail = SWT.TRAVERSE_NONE;
895                                 String newText = c.getText();
896                                 try {
897                                     Integer.parseInt(newText);
898                                     if (null != currentSettingInSelection
899                                             && currentSettingInSelection
900                                                     .getSettingID()
901                                                     .equals(Constants.UPDATE_INTERVAL_IN_MS)) {
902                                         currentSettingInSelection
903                                                 .addAllowedValue(newText);
904                                     }
905                                     c.add(newText);
906                                     c.setSelection(new Point(0, newText
907                                             .length()));
908                                     currentSettingInSelection
909                                             .setSettingValue(newText);
910                                 } catch (NumberFormatException ex) {
911                                 }
912                             }
913                         }
914                     });
915                 }
916             }
917
918             return combo;
919         }
920
921         @Override
922         protected Object getValue(Object element) {
923             // This method returns the index of the item selected in the combo
924             // box
925             int index;
926             AutomationSettingHelper setting = (AutomationSettingHelper) element;
927             List<String> itemList = setting.getAllowedValues();
928             String currentValue = setting.getSettingValue();
929             index = itemList.indexOf(currentValue);
930             return index;
931         }
932
933         @Override
934         protected void setValue(Object element, Object value) {
935             AutomationSettingHelper setting = (AutomationSettingHelper) element;
936             int index = (Integer) value;
937             List<String> valueList = setting.getAllowedValues();
938             if (null != valueList) {
939                 String targetValue;
940                 if (index != -1) {
941                     targetValue = valueList.get(index);
942                     setting.setSettingValue(targetValue);
943
944                     if (setting.getSettingID().equals(Constants.AUTOMATION)) {
945
946                         if (attributeInSelection.isAutomationInProgress()) {
947                             // settingsVisibility = false;
948                             setVisibilityForManualValueChange(false);
949                         } else {
950                             if (targetValue.equals(Constants.ENABLE)) {
951                                 // settingsVisibility = true;
952                                 setVisibilityForManualValueChange(false);
953                             } else {
954                                 // settingsVisibility = false;
955                                 setVisibilityForManualValueChange(true);
956                             }
957                         }
958
959                         // If status is changed, then enable the submit and
960                         // clear buttons
961                         if (!targetValue.equals(Utility
962                                 .getAutomationString(attributeInSelection
963                                         .isAutomationInProgress()))) {
964                             setSettingSubmitVisibility(true);
965                         } else {
966                             setSettingSubmitVisibility(false);
967                         }
968                     }
969                 }
970             }
971             viewer.refresh();
972         }
973     }
974
975     private boolean showSettings() {
976         boolean show = false;
977         if (!attributeInSelection.isAutomationInProgress()) {
978             AutomationSettingHelper setting;
979             Iterator<AutomationSettingHelper> list = localSettingList
980                     .iterator();
981             while (list.hasNext()) {
982                 setting = list.next();
983                 if (setting.getSettingID().equals(Constants.AUTOMATION)) {
984                     if (setting.getSettingValue().equals(Constants.ENABLE)) {
985                         show = true;
986                     }
987                     break;
988                 }
989             }
990         }
991         return show;
992     }
993
994     // Disable the appropriate controls which are used for manual value change
995     private void setVisibilityForManualValueChange(boolean visibility) {
996         if (null != valueCmb && !valueCmb.isDisposed()) {
997             valueCmb.setEnabled(visibility);
998         }
999         if (null != newValueLbl && !newValueLbl.isDisposed()) {
1000             newValueLbl.setEnabled(visibility);
1001         }
1002     }
1003
1004     @Override
1005     public void dispose() {
1006         // Unregister the selection listener
1007         if (null != resourceSelectionChangedListener) {
1008             resourceManager
1009                     .removeResourceSelectionChangedUIListener(resourceSelectionChangedListener);
1010         }
1011
1012         // Unregister the model change listener
1013         if (null != resourceModelChangedUIListener) {
1014             resourceManager
1015                     .removeResourceModelChangedUIListener(resourceModelChangedUIListener);
1016         }
1017
1018         // Unregister the automation complete listener
1019         if (null != automationUIListener) {
1020             resourceManager.removeAutomationUIListener(automationUIListener);
1021         }
1022
1023         super.dispose();
1024     }
1025
1026     @Override
1027     public void setFocus() {
1028
1029     }
1030 }