Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / AttributeEditingSupport.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.serviceprovider.view;
18
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.CellEditor;
21 import org.eclipse.jface.viewers.CheckboxCellEditor;
22 import org.eclipse.jface.viewers.ComboBoxCellEditor;
23 import org.eclipse.jface.viewers.EditingSupport;
24 import org.eclipse.jface.viewers.TextCellEditor;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.CCombo;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.MessageBox;
33 import org.eclipse.swt.widgets.Text;
34 import org.eclipse.swt.widgets.Tree;
35 import org.eclipse.swt.widgets.TreeItem;
36 import org.eclipse.ui.IPartListener2;
37 import org.eclipse.ui.IWorkbenchPartReference;
38
39 import java.util.Date;
40 import java.util.List;
41
42 import org.oic.simulator.AttributeValue;
43 import org.oic.simulator.AttributeValue.TypeInfo;
44 import org.oic.simulator.AttributeValue.ValueType;
45 import org.oic.simulator.ILogger.Level;
46 import org.oic.simulator.InvalidArgsException;
47 import org.oic.simulator.SimulatorResourceAttribute;
48 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
49
50 import oic.simulator.serviceprovider.Activator;
51 import oic.simulator.serviceprovider.manager.ResourceManager;
52 import oic.simulator.serviceprovider.model.AttributeElement;
53 import oic.simulator.serviceprovider.model.AutomationSettingHelper;
54 import oic.simulator.serviceprovider.model.Resource;
55 import oic.simulator.serviceprovider.model.ResourceRepresentation;
56 import oic.simulator.serviceprovider.model.SingleResource;
57 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
58 import oic.simulator.serviceprovider.utils.Utility;
59 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
60 import oic.simulator.serviceprovider.view.dialogs.UpdatePrimitiveArrayAttributeDialog;
61
62 /**
63  * This class provides editing support to the resources attributes table in the
64  * attributes view.
65  */
66 public class AttributeEditingSupport {
67
68     private AttributeValueEditor attValueEditor;
69     private AutomationEditor     automationEditor;
70
71     private static class SyncValueUpdate {
72         private Boolean valueChangeInProgress;
73
74         public boolean isValueChangeInProgress() {
75             return valueChangeInProgress;
76         }
77
78         public void setValueChangeInProgress(boolean value) {
79             valueChangeInProgress = value;
80         }
81     }
82
83     private final SyncValueUpdate syncValueChange = new SyncValueUpdate();
84
85     public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
86             Boolean nativeUpdateValueCall) {
87         attValueEditor = new AttributeValueEditor(viewer, nativeUpdateValueCall);
88         syncValueChange.setValueChangeInProgress(false);
89         return attValueEditor;
90     }
91
92     public AutomationEditor createAutomationEditor(TreeViewer viewer) {
93         automationEditor = new AutomationEditor(viewer);
94         return automationEditor;
95     }
96
97     class AttributeValueEditor extends EditingSupport {
98
99         private final TreeViewer viewer;
100         private CCombo           comboBox;
101
102         private Boolean          callNativeUpdateValue;
103
104         public AttributeValueEditor(TreeViewer viewer,
105                 Boolean nativeUpdateValueCall) {
106             super(viewer);
107             this.viewer = viewer;
108             callNativeUpdateValue = nativeUpdateValueCall;
109             // Using the part listener to refresh the viewer on various part
110             // events.
111             // If combo list is open, then click events on other parts of the
112             // view or outside the combo should hide the editor.
113             // Refreshing the viewer hides the combo and other editors which are
114             // active.
115             IPartListener2 partListener;
116             partListener = new IPartListener2() {
117
118                 @Override
119                 public void partVisible(IWorkbenchPartReference partRef) {
120                 }
121
122                 @Override
123                 public void partOpened(IWorkbenchPartReference partRef) {
124                 }
125
126                 @Override
127                 public void partInputChanged(IWorkbenchPartReference partRef) {
128                 }
129
130                 @Override
131                 public void partHidden(IWorkbenchPartReference partRef) {
132                 }
133
134                 @Override
135                 public void partDeactivated(IWorkbenchPartReference partRef) {
136                     String viewId = partRef.getId();
137                     if (viewId.equals(AttributeView.VIEW_ID)) {
138                         synchronized (syncValueChange) {
139                             updateUnSavedData();
140                         }
141                     }
142                 }
143
144                 @Override
145                 public void partClosed(IWorkbenchPartReference partRef) {
146                 }
147
148                 @Override
149                 public void partBroughtToTop(IWorkbenchPartReference partRef) {
150                 }
151
152                 @Override
153                 public void partActivated(IWorkbenchPartReference partRef) {
154                     String viewId = partRef.getId();
155                     if (viewId.equals(AttributeView.VIEW_ID)) {
156                         if (null == AttributeValueEditor.this.viewer)
157                             return;
158
159                         Tree tree = AttributeValueEditor.this.viewer.getTree();
160                         if (null == tree || tree.isDisposed())
161                             return;
162
163                         AttributeValueEditor.this.viewer.refresh();
164                     }
165                 }
166             };
167
168             try {
169                 Activator.getDefault().getWorkbench()
170                         .getActiveWorkbenchWindow().getActivePage()
171                         .addPartListener(partListener);
172             } catch (Exception e) {
173                 Activator
174                         .getDefault()
175                         .getLogManager()
176                         .log(Level.ERROR.ordinal(),
177                                 new Date(),
178                                 "There is an error while configuring the listener for UI.\n"
179                                         + Utility.getSimulatorErrorString(e,
180                                                 null));
181             }
182         }
183
184         public void updateUnSavedData() {
185             if (null == viewer || null == comboBox)
186                 return;
187
188             Tree tree = viewer.getTree();
189             if (null == tree || tree.isDisposed())
190                 return;
191
192             TreeItem[] selectedItems = tree.getSelection();
193             if (null == selectedItems || 1 != selectedItems.length)
194                 return;
195
196             Object element = selectedItems[0].getData();
197             if (null == element || !(element instanceof AttributeElement))
198                 return;
199
200             AttributeElement attElement = (AttributeElement) element;
201             SimulatorResourceAttribute att = attElement
202                     .getSimulatorResourceAttribute();
203             if (null == att)
204                 return;
205
206             if (Activator.getDefault().getResourceManager()
207                     .isAttHasRangeOrAllowedValues(att)) {
208                 viewer.refresh();
209                 return;
210             }
211
212             synchronized (syncValueChange) {
213                 if (!syncValueChange.isValueChangeInProgress()) {
214
215                     AttributeValue value = att.value();
216                     if (value == null)
217                         return;
218
219                     TypeInfo type = value.typeInfo();
220
221                     if (type.mBaseType == ValueType.RESOURCEMODEL
222                             || type.mType == ValueType.ARRAY) {
223                         return;
224                     }
225
226                     if (null == value.get()) {
227                         return;
228                     }
229
230                     String oldValue = String.valueOf(Utility
231                             .getAttributeValueAsString(value));
232
233                     attElement.setEditLock(true);
234                     compareAndUpdateAttribute(oldValue, comboBox.getText(),
235                             (AttributeElement) element, att, type);
236                     attElement.setEditLock(false);
237
238                     viewer.refresh();
239                 }
240             }
241         }
242
243         @Override
244         protected boolean canEdit(Object arg0) {
245             return true;
246         }
247
248         @Override
249         protected CellEditor getCellEditor(final Object element) {
250             ResourceManager resourceManager = Activator.getDefault()
251                     .getResourceManager();
252
253             Resource res = resourceManager.getCurrentResourceInSelection();
254             if (null == res) {
255                 return null;
256             }
257
258             // If selected resource is not a single resource, then editor
259             // support is not required.
260             if (!(res instanceof SingleResource)) {
261                 return null;
262             }
263
264             final SimulatorResourceAttribute attribute;
265             if (!(element instanceof AttributeElement)) {
266                 return null;
267             }
268
269             final AttributeElement attributeElement = ((AttributeElement) element);
270             attribute = attributeElement.getSimulatorResourceAttribute();
271             if (null == attribute) {
272                 return null;
273             }
274
275             // CellEditor is not required as the automation is in progress.
276             if (attributeElement.isAutoUpdateInProgress()) {
277                 return null;
278             }
279
280             final AttributeValue val = attribute.value();
281             if (null == val) {
282                 return null;
283             }
284
285             final TypeInfo type = val.typeInfo();
286             if (type.mBaseType == ValueType.RESOURCEMODEL) {
287                 return null;
288             }
289
290             synchronized (syncValueChange) {
291                 syncValueChange.setValueChangeInProgress(false);
292             }
293
294             CellEditor editor;
295             if (type.mType == ValueType.ARRAY) {
296                 editor = new TextCellEditor(viewer.getTree());
297                 editor.setStyle(SWT.READ_ONLY);
298                 final Text txt = (Text) editor.getControl();
299                 txt.addModifyListener(new ModifyListener() {
300                     @Override
301                     public void modifyText(ModifyEvent e) {
302                         String currentAttValue = txt.getText();
303                         UpdatePrimitiveArrayAttributeDialog dialog = new UpdatePrimitiveArrayAttributeDialog(
304                                 Display.getDefault().getActiveShell(),
305                                 attribute);
306                         if (dialog.open() == Window.OK) {
307                             updateAttributeValue(attributeElement, attribute,
308                                     dialog.getNewValueObj());
309
310                             if (callNativeUpdateValue) {
311                                 ResourceManager resourceManager;
312                                 resourceManager = Activator.getDefault()
313                                         .getResourceManager();
314
315                                 Resource resource = resourceManager
316                                         .getCurrentResourceInSelection();
317
318                                 SimulatorResourceAttribute result = getResultantAttribute(attributeElement);
319                                 if (null == result) {
320                                     Activator
321                                             .getDefault()
322                                             .getLogManager()
323                                             .log(Level.ERROR.ordinal(),
324                                                     new Date(),
325                                                     "There is an error while updating the attribute value.\n");
326                                     return;
327                                 }
328
329                                 boolean updated = resourceManager
330                                         .attributeValueUpdated(
331                                                 (SingleResource) resource,
332                                                 result.name(), result.value());
333                                 if (!updated) {
334                                     try {
335                                         updateAttributeValue(attributeElement,
336                                                 attribute,
337                                                 AttributeValueBuilder.build(
338                                                         currentAttValue,
339                                                         type.mBaseType));
340                                     } catch (Exception ex) {
341                                         Activator
342                                                 .getDefault()
343                                                 .getLogManager()
344                                                 .log(Level.ERROR.ordinal(),
345                                                         new Date(),
346                                                         "There is an error while updating the attribute value.\n"
347                                                                 + Utility
348                                                                         .getSimulatorErrorString(
349                                                                                 ex,
350                                                                                 null));
351                                     }
352                                     MessageDialog
353                                             .openInformation(Display
354                                                     .getDefault()
355                                                     .getActiveShell(),
356                                                     "Operation failed",
357                                                     "Failed to update the attribute value.");
358                                 }
359                             }
360                         }
361
362                         // Update the viewer in a separate UI thread.
363                         Display.getDefault().asyncExec(new Runnable() {
364                             @Override
365                             public void run() {
366                                 viewer.refresh(element, true);
367                             }
368                         });
369                     }
370                 });
371             } else {
372                 String values[] = null;
373                 List<String> valueSet = resourceManager
374                         .getAllValuesOfAttribute(attribute);
375                 values = Utility.convertListToStringArray(valueSet);
376
377                 boolean hasValueSet = resourceManager
378                         .isAttHasRangeOrAllowedValues(attribute);
379
380                 if (hasValueSet) {
381                     editor = new ComboBoxCellEditor(viewer.getTree(), values,
382                             SWT.READ_ONLY);
383                 } else {
384                     editor = new ComboBoxCellEditor(viewer.getTree(), values);
385                 }
386
387                 comboBox = (CCombo) editor.getControl();
388                 if (hasValueSet) {
389                     comboBox.addModifyListener(new ModifyListener() {
390
391                         @Override
392                         public void modifyText(ModifyEvent event) {
393                             String oldValue = String.valueOf(Utility
394                                     .getAttributeValueAsString(val));
395                             if (null == oldValue) {
396                                 oldValue = "";
397                             }
398                             String newValue = comboBox.getText();
399
400                             attributeElement.setEditLock(true);
401                             compareAndUpdateAttribute(oldValue, newValue,
402                                     (AttributeElement) element, attribute, type);
403                             attributeElement.setEditLock(false);
404
405                             comboBox.setVisible(false);
406                         }
407                     });
408                 }
409             }
410             return editor;
411         }
412
413         @Override
414         protected Object getValue(Object element) {
415             int indexOfItem = 0;
416             SimulatorResourceAttribute att = null;
417
418             if (element instanceof AttributeElement) {
419                 att = ((AttributeElement) element)
420                         .getSimulatorResourceAttribute();
421             }
422
423             if (att == null) {
424                 return 0;
425             }
426
427             final AttributeValue val = att.value();
428             if (null == val) {
429                 return null;
430             }
431
432             final TypeInfo type = val.typeInfo();
433             if (type.mBaseType == ValueType.RESOURCEMODEL) {
434                 return null;
435             }
436
437             String valueString = Utility.getAttributeValueAsString(att.value());
438             if (null == valueString) {
439                 valueString = "";
440             }
441
442             if (type.mType == ValueType.ARRAY) {
443                 return valueString;
444             }
445
446             List<String> valueSet = Activator.getDefault().getResourceManager()
447                     .getAllValuesOfAttribute(att);
448             if (null != valueSet) {
449                 indexOfItem = valueSet.indexOf(valueString);
450             }
451             if (indexOfItem == -1) {
452                 indexOfItem = 0;
453             }
454             return indexOfItem;
455         }
456
457         @Override
458         protected void setValue(Object element, Object value) {
459             synchronized (syncValueChange) {
460                 if (syncValueChange.isValueChangeInProgress()) {
461                     return;
462                 }
463
464                 SimulatorResourceAttribute att = null;
465                 if (element instanceof AttributeElement) {
466                     att = ((AttributeElement) element)
467                             .getSimulatorResourceAttribute();
468                 }
469
470                 if (att == null) {
471                     return;
472                 }
473
474                 if (Activator.getDefault().getResourceManager()
475                         .isAttHasRangeOrAllowedValues(att)) {
476                     return;
477                 }
478
479                 AttributeValue val = att.value();
480                 if (null == val) {
481                     return;
482                 }
483
484                 TypeInfo type = val.typeInfo();
485
486                 String oldValue = String.valueOf(Utility
487                         .getAttributeValueAsString(val));
488                 if (null == oldValue) {
489                     oldValue = "";
490                 }
491
492                 String newValue = comboBox.getText();
493
494                 ((AttributeElement) element).setEditLock(true);
495                 compareAndUpdateAttribute(oldValue, newValue,
496                         (AttributeElement) element, att, type);
497                 ((AttributeElement) element).setEditLock(false);
498
499             }
500
501         }
502
503         public void compareAndUpdateAttribute(String oldValue, String newValue,
504                 AttributeElement attElement, SimulatorResourceAttribute att,
505                 TypeInfo type) {
506             if (null == oldValue || null == newValue || null == attElement
507                     || null == att || null == type) {
508                 return;
509             }
510
511             synchronized (syncValueChange) {
512                 syncValueChange.setValueChangeInProgress(true);
513             }
514
515             if (!oldValue.equals(newValue)) {
516                 boolean invalid = false;
517
518                 // Get the AttriuteValue from the string
519                 AttributeValue attValue = null;
520                 try {
521                     attValue = AttributeValueBuilder.build(newValue,
522                             type.mBaseType);
523                 } catch (Exception e) {
524                     Activator
525                             .getDefault()
526                             .getLogManager()
527                             .log(Level.ERROR.ordinal(),
528                                     new Date(),
529                                     "There is an error while creating the new attribute value.\n"
530                                             + Utility.getSimulatorErrorString(
531                                                     e, null));
532                 }
533
534                 if (null == attValue) {
535                     invalid = true;
536                 } else {
537                     TypeInfo resTypeInfo = attValue.typeInfo();
538                     if (type.mDepth != resTypeInfo.mDepth
539                             || type.mType != resTypeInfo.mType
540                             || type.mBaseType != resTypeInfo.mBaseType) {
541                         invalid = true;
542                     }
543                 }
544                 if (invalid) {
545                     MessageBox dialog = new MessageBox(viewer.getTree()
546                             .getShell(), SWT.ICON_ERROR | SWT.OK);
547                     dialog.setText("Invalid Value");
548                     dialog.setMessage("Given value is invalid");
549                     dialog.open();
550                 } else {
551
552                     // To show the new value till decision made.
553                     updateAttributeValue(attElement, att, attValue);
554                     viewer.update(attElement, null);
555
556                     if (callNativeUpdateValue) {
557                         MessageBox dialog = new MessageBox(viewer.getTree()
558                                 .getShell(), SWT.ICON_QUESTION | SWT.OK
559                                 | SWT.CANCEL);
560                         dialog.setText("Confirm action");
561                         dialog.setMessage("Do you want to modify the value?");
562                         int retval = dialog.open();
563                         if (retval != SWT.OK) {
564                             try {
565                                 attValue = AttributeValueBuilder.build(
566                                         oldValue, type.mBaseType);
567                                 updateAttributeValue(attElement, att, attValue);
568                             } catch (Exception e) {
569                                 Activator
570                                         .getDefault()
571                                         .getLogManager()
572                                         .log(Level.ERROR.ordinal(),
573                                                 new Date(),
574                                                 "There is an error while updating the attribute value.\n"
575                                                         + Utility
576                                                                 .getSimulatorErrorString(
577                                                                         e, null));
578                             }
579                         } else {
580                             ResourceManager resourceManager;
581                             resourceManager = Activator.getDefault()
582                                     .getResourceManager();
583
584                             Resource resource = resourceManager
585                                     .getCurrentResourceInSelection();
586
587                             SimulatorResourceAttribute result = getResultantAttribute(attElement);
588                             if (null == result) {
589                                 Activator
590                                         .getDefault()
591                                         .getLogManager()
592                                         .log(Level.ERROR.ordinal(), new Date(),
593                                                 "There is an error while updating the attribute value.\n");
594                                 return;
595                             }
596
597                             boolean updated = resourceManager
598                                     .attributeValueUpdated(
599                                             (SingleResource) resource,
600                                             result.name(), result.value());
601                             if (!updated) {
602                                 try {
603                                     attValue = AttributeValueBuilder.build(
604                                             oldValue, type.mBaseType);
605                                     updateAttributeValue(attElement, att,
606                                             attValue);
607                                 } catch (Exception e) {
608                                     Activator
609                                             .getDefault()
610                                             .getLogManager()
611                                             .log(Level.ERROR.ordinal(),
612                                                     new Date(),
613                                                     "There is an error while updating the attribute value.\n"
614                                                             + Utility
615                                                                     .getSimulatorErrorString(
616                                                                             e,
617                                                                             null));
618                                 }
619                                 MessageDialog
620                                         .openInformation(Display.getDefault()
621                                                 .getActiveShell(),
622                                                 "Operation failed",
623                                                 "Failed to update the attribute value.");
624                             }
625                         }
626                     }
627                 }
628             }
629             viewer.update(attElement, null);
630         }
631
632         public void updateAttributeValue(AttributeElement attributeElement,
633                 SimulatorResourceAttribute att, AttributeValue value) {
634             if (null == attributeElement || null == att || null == value) {
635                 return;
636             }
637
638             attributeElement.getSimulatorResourceAttribute().setValue(value);
639
640             Object parent = attributeElement.getParent();
641             if (null != parent && parent instanceof AttributeElement) {
642                 AttributeElement parentElement = (AttributeElement) parent;
643                 try {
644                     parentElement.deepSetChildValue(att);
645                 } catch (InvalidArgsException e) {
646                     Activator
647                             .getDefault()
648                             .getLogManager()
649                             .log(Level.ERROR.ordinal(),
650                                     new Date(),
651                                     "There is an error while updating the attribute value.\n"
652                                             + Utility.getSimulatorErrorString(
653                                                     e, null));
654                 }
655             }
656         }
657
658         public SimulatorResourceAttribute getResultantAttribute(
659                 AttributeElement attElement) {
660             if (null == attElement) {
661                 return null;
662             }
663
664             SimulatorResourceAttribute result;
665             Object parent = attElement.getParent();
666
667             while (parent != null && parent instanceof AttributeElement) {
668                 attElement = (AttributeElement) parent;
669                 parent = ((AttributeElement) parent).getParent();
670             }
671             result = attElement.getSimulatorResourceAttribute();
672
673             return result;
674         }
675     }
676
677     private static class AutomationEditor extends EditingSupport {
678
679         private final TreeViewer viewer;
680
681         public AutomationEditor(TreeViewer viewer) {
682             super(viewer);
683             this.viewer = viewer;
684         }
685
686         @Override
687         protected boolean canEdit(Object arg0) {
688             return true;
689         }
690
691         @Override
692         protected CellEditor getCellEditor(Object element) {
693             // CellEditor is not required as the automation is in progress.
694             ResourceManager resourceManager = Activator.getDefault()
695                     .getResourceManager();
696             Resource resource = resourceManager.getCurrentResourceInSelection();
697
698             if (null == resource) {
699                 return null;
700             }
701
702             if (!(resource instanceof SingleResource)) {
703                 return null;
704             }
705             if (((SingleResource) resource).isResourceAutomationInProgress()) {
706                 return null;
707             }
708
709             SimulatorResourceAttribute att = null;
710             if (element instanceof AttributeElement) {
711                 att = ((AttributeElement) element)
712                         .getSimulatorResourceAttribute();
713             }
714
715             if (null == att) {
716                 return null;
717             }
718
719             AttributeValue val = att.value();
720             if (null == val) {
721                 return null;
722             }
723
724             TypeInfo type = val.typeInfo();
725
726             if (type.mType == ValueType.RESOURCEMODEL
727                     || type.mType == ValueType.ARRAY) {
728                 return null;
729             }
730
731             Object parent = ((AttributeElement) element).getParent();
732             if (null != parent && !(parent instanceof ResourceRepresentation)) {
733                 return null;
734             }
735
736             if (((AttributeElement) element).isReadOnly()) {
737                 return null;
738             }
739
740             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
741         }
742
743         @Override
744         protected Object getValue(Object element) {
745             if (element instanceof AttributeElement) {
746                 return ((AttributeElement) element).isAutoUpdateInProgress();
747             }
748
749             return false;
750         }
751
752         @Override
753         protected void setValue(Object element, Object value) {
754             if (!(element instanceof AttributeElement)) {
755                 return;
756             }
757
758             ResourceManager resourceManager = Activator.getDefault()
759                     .getResourceManager();
760             // As automation depends on the current resource in selection, its
761             // presence is being checked.
762             Resource resource = resourceManager.getCurrentResourceInSelection();
763             if (null == resource) {
764                 return;
765             }
766
767             AttributeElement att = (AttributeElement) element;
768             boolean checked = (Boolean) value;
769             if (checked) {
770                 // Start the automation
771                 // Fetch the settings data
772                 List<AutomationSettingHelper> automationSettings;
773                 automationSettings = AutomationSettingHelper
774                         .getAutomationSettings(att);
775
776                 // Open the settings dialog
777                 AutomationSettingDialog dialog = new AutomationSettingDialog(
778                         viewer.getTree().getShell(), automationSettings);
779                 dialog.create();
780                 if (dialog.open() == Window.OK) {
781                     String automationType = dialog.getAutomationType();
782                     String updateFreq = dialog.getUpdateFrequency();
783
784                     AutoUpdateType autoType = AutoUpdateType
785                             .valueOf(automationType);
786                     int updFreq = Utility
787                             .getUpdateIntervalFromString(updateFreq);
788                     int autoId = resourceManager.startAutomation(
789                             (SingleResource) resource, att, autoType, updFreq);
790                     if (autoId == -1) {
791                         MessageDialog.openInformation(Display.getDefault()
792                                 .getActiveShell(), "Automation Status",
793                                 "Automation start failed!!");
794                     }
795                 }
796             } else {
797                 // Stop the automation
798                 resourceManager.stopAutomation((SingleResource) resource, att,
799                         att.getAutoUpdateId());
800             }
801         }
802     }
803 }