Fix for klocwork issues.
[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.IStructuredSelection;
25 import org.eclipse.jface.viewers.TextCellEditor;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.CCombo;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.MessageBox;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.swt.widgets.Tree;
36 import org.eclipse.swt.widgets.TreeItem;
37 import org.eclipse.ui.IPartListener2;
38 import org.eclipse.ui.IWorkbenchPartReference;
39
40 import java.util.Date;
41 import java.util.List;
42
43 import org.oic.simulator.AttributeValue;
44 import org.oic.simulator.AttributeValue.TypeInfo;
45 import org.oic.simulator.AttributeValue.ValueType;
46 import org.oic.simulator.ILogger.Level;
47 import org.oic.simulator.InvalidArgsException;
48 import org.oic.simulator.SimulatorResourceAttribute;
49 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
50
51 import oic.simulator.serviceprovider.Activator;
52 import oic.simulator.serviceprovider.manager.ResourceManager;
53 import oic.simulator.serviceprovider.model.AttributeElement;
54 import oic.simulator.serviceprovider.model.AutomationSettingHelper;
55 import oic.simulator.serviceprovider.model.Resource;
56 import oic.simulator.serviceprovider.model.ResourceRepresentation;
57 import oic.simulator.serviceprovider.model.SingleResource;
58 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
59 import oic.simulator.serviceprovider.utils.Utility;
60 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
61 import oic.simulator.serviceprovider.view.dialogs.UpdatePrimitiveArrayAttributeDialog;
62
63 /**
64  * This class provides editing support to the resources attributes table in the
65  * attributes view.
66  */
67 public class AttributeEditingSupport {
68
69     private AttributeValueEditor attValueEditor;
70     private AutomationEditor     automationEditor;
71
72     class SyncValueUpdate {
73         private Boolean valueChangeInProgress;
74
75         public boolean isValueChangeInProgress() {
76             return valueChangeInProgress;
77         }
78
79         public void setValueChangeInProgress(boolean value) {
80             valueChangeInProgress = value;
81         }
82     }
83
84     private final SyncValueUpdate syncValueChange = new SyncValueUpdate();
85
86     public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
87             Boolean nativeUpdateValueCall) {
88         attValueEditor = new AttributeValueEditor(viewer, nativeUpdateValueCall);
89         syncValueChange.setValueChangeInProgress(false);
90         return attValueEditor;
91     }
92
93     public AutomationEditor createAutomationEditor(TreeViewer viewer) {
94         automationEditor = new AutomationEditor(viewer);
95         return automationEditor;
96     }
97
98     class AttributeValueEditor extends EditingSupport {
99
100         private final TreeViewer viewer;
101         private CCombo           comboBox;
102
103         private Boolean          callNativeUpdateValue;
104
105         public AttributeValueEditor(TreeViewer viewer,
106                 Boolean nativeUpdateValueCall) {
107             super(viewer);
108             this.viewer = viewer;
109             callNativeUpdateValue = nativeUpdateValueCall;
110             // Using the part listener to refresh the viewer on various part
111             // events.
112             // If combo list is open, then click events on other parts of the
113             // view or outside the combo should hide the editor.
114             // Refreshing the viewer hides the combo and other editors which are
115             // active.
116             IPartListener2 partListener;
117             partListener = new IPartListener2() {
118
119                 @Override
120                 public void partVisible(IWorkbenchPartReference partRef) {
121                 }
122
123                 @Override
124                 public void partOpened(IWorkbenchPartReference partRef) {
125                 }
126
127                 @Override
128                 public void partInputChanged(IWorkbenchPartReference partRef) {
129                 }
130
131                 @Override
132                 public void partHidden(IWorkbenchPartReference partRef) {
133                 }
134
135                 @Override
136                 public void partDeactivated(IWorkbenchPartReference partRef) {
137                     String viewId = partRef.getId();
138                     if (viewId.equals(AttributeView.VIEW_ID)) {
139                         synchronized (syncValueChange) {
140                             updateUnSavedData();
141                         }
142                     }
143                 }
144
145                 @Override
146                 public void partClosed(IWorkbenchPartReference partRef) {
147                 }
148
149                 @Override
150                 public void partBroughtToTop(IWorkbenchPartReference partRef) {
151                 }
152
153                 @Override
154                 public void partActivated(IWorkbenchPartReference partRef) {
155                     String viewId = partRef.getId();
156                     if (viewId.equals(AttributeView.VIEW_ID)) {
157                         if (null == AttributeValueEditor.this.viewer)
158                             return;
159
160                         Tree tree = AttributeValueEditor.this.viewer.getTree();
161                         if (null == tree || tree.isDisposed())
162                             return;
163
164                         AttributeValueEditor.this.viewer.refresh();
165                     }
166                 }
167             };
168
169             try {
170                 Activator.getDefault().getWorkbench()
171                         .getActiveWorkbenchWindow().getActivePage()
172                         .addPartListener(partListener);
173             } catch (Exception e) {
174                 Activator
175                         .getDefault()
176                         .getLogManager()
177                         .log(Level.ERROR.ordinal(),
178                                 new Date(),
179                                 "There is an error while configuring the listener for UI.\n"
180                                         + Utility.getSimulatorErrorString(e,
181                                                 null));
182             }
183         }
184
185         public void updateUnSavedData() {
186             if (null == viewer || null == comboBox)
187                 return;
188
189             Tree tree = viewer.getTree();
190             if (null == tree || tree.isDisposed())
191                 return;
192
193             TreeItem[] selectedItems = tree.getSelection();
194             if (null == selectedItems || 1 != selectedItems.length)
195                 return;
196
197             Object element = selectedItems[0].getData();
198             if (null == element || !(element instanceof AttributeElement))
199                 return;
200
201             AttributeElement attElement = (AttributeElement) element;
202             SimulatorResourceAttribute att = attElement
203                     .getSimulatorResourceAttribute();
204             if (null == att)
205                 return;
206
207             if (Activator.getDefault().getResourceManager()
208                     .isAttHasRangeOrAllowedValues(att)) {
209                 viewer.refresh();
210                 return;
211             }
212
213             synchronized (syncValueChange) {
214                 if (!syncValueChange.isValueChangeInProgress()) {
215
216                     AttributeValue value = att.value();
217                     if (value == null)
218                         return;
219
220                     TypeInfo type = value.typeInfo();
221
222                     if (type.mBaseType == ValueType.RESOURCEMODEL
223                             || type.mType == ValueType.ARRAY) {
224                         return;
225                     }
226
227                     if (null == value.get()) {
228                         return;
229                     }
230
231                     String oldValue = String.valueOf(Utility
232                             .getAttributeValueAsString(value));
233
234                     attElement.setEditLock(true);
235                     compareAndUpdateAttribute(oldValue, comboBox.getText(),
236                             element, att, type);
237                     attElement.setEditLock(false);
238
239                     viewer.refresh();
240                 }
241             }
242         }
243
244         @Override
245         protected boolean canEdit(Object arg0) {
246             return true;
247         }
248
249         @Override
250         protected CellEditor getCellEditor(final Object element) {
251             ResourceManager resourceManager = Activator.getDefault()
252                     .getResourceManager();
253
254             Resource res = resourceManager.getCurrentResourceInSelection();
255             if (null == res) {
256                 return null;
257             }
258
259             // If selected resource is not a single resource, then editor
260             // support is not required.
261             if (!(res instanceof SingleResource)) {
262                 return null;
263             }
264
265             final SimulatorResourceAttribute attribute;
266             if (!(element instanceof AttributeElement)) {
267                 return null;
268             }
269
270             final AttributeElement attributeElement = ((AttributeElement) element);
271             attribute = attributeElement.getSimulatorResourceAttribute();
272             if (null == attribute) {
273                 return null;
274             }
275
276             // CellEditor is not required as the automation is in progress.
277             if (attributeElement.isAutoUpdateInProgress()) {
278                 return null;
279             }
280
281             final AttributeValue val = attribute.value();
282             if (null == val) {
283                 return null;
284             }
285
286             final TypeInfo type = val.typeInfo();
287             if (type.mBaseType == ValueType.RESOURCEMODEL) {
288                 return null;
289             }
290
291             synchronized (syncValueChange) {
292                 syncValueChange.setValueChangeInProgress(false);
293             }
294
295             CellEditor editor;
296             if (type.mType == ValueType.ARRAY) {
297                 editor = new TextCellEditor(viewer.getTree());
298                 editor.setStyle(SWT.READ_ONLY);
299                 final Text txt = (Text) editor.getControl();
300                 txt.addModifyListener(new ModifyListener() {
301                     @Override
302                     public void modifyText(ModifyEvent e) {
303                         String currentAttValue = txt.getText();
304                         UpdatePrimitiveArrayAttributeDialog dialog = new UpdatePrimitiveArrayAttributeDialog(
305                                 Display.getDefault().getActiveShell(),
306                                 attribute);
307                         if (dialog.open() == Window.OK) {
308                             updateAttributeValue(attribute,
309                                     dialog.getNewValueObj());
310
311                             if (callNativeUpdateValue) {
312                                 ResourceManager resourceManager;
313                                 resourceManager = Activator.getDefault()
314                                         .getResourceManager();
315
316                                 Resource resource = resourceManager
317                                         .getCurrentResourceInSelection();
318
319                                 SimulatorResourceAttribute result = getResultantAttribute();
320                                 if (null == result) {
321                                     Activator
322                                             .getDefault()
323                                             .getLogManager()
324                                             .log(Level.ERROR.ordinal(),
325                                                     new Date(),
326                                                     "There is an error while updating the attribute value.\n");
327                                     return;
328                                 }
329
330                                 boolean updated = resourceManager
331                                         .attributeValueUpdated(
332                                                 (SingleResource) resource,
333                                                 result.name(), result.value());
334                                 if (!updated) {
335                                     try {
336                                         updateAttributeValue(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                                     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, element, att,
496                         type);
497                 ((AttributeElement) element).setEditLock(false);
498
499             }
500
501         }
502
503         public void compareAndUpdateAttribute(String oldValue, String newValue,
504                 Object element, SimulatorResourceAttribute att, TypeInfo type) {
505             if (null == oldValue || null == newValue || null == element
506                     || null == att || null == type) {
507                 return;
508             }
509
510             synchronized (syncValueChange) {
511                 syncValueChange.setValueChangeInProgress(true);
512             }
513
514             if (!oldValue.equals(newValue)) {
515                 boolean invalid = false;
516
517                 // Get the AttriuteValue from the string
518                 AttributeValue attValue = null;
519                 try {
520                     attValue = AttributeValueBuilder.build(newValue,
521                             type.mBaseType);
522                 } catch (Exception e) {
523                     Activator
524                             .getDefault()
525                             .getLogManager()
526                             .log(Level.ERROR.ordinal(),
527                                     new Date(),
528                                     "There is an error while creating the new attribute value.\n"
529                                             + Utility.getSimulatorErrorString(
530                                                     e, null));
531                 }
532
533                 if (null == attValue) {
534                     invalid = true;
535                 } else {
536                     TypeInfo resTypeInfo = attValue.typeInfo();
537                     if (type.mDepth != resTypeInfo.mDepth
538                             || type.mType != resTypeInfo.mType
539                             || type.mBaseType != resTypeInfo.mBaseType) {
540                         invalid = true;
541                     }
542                 }
543                 if (invalid) {
544                     MessageBox dialog = new MessageBox(viewer.getTree()
545                             .getShell(), SWT.ICON_ERROR | SWT.OK);
546                     dialog.setText("Invalid Value");
547                     dialog.setMessage("Given value is invalid");
548                     dialog.open();
549                 } else {
550
551                     // To show the new value till decision made.
552                     updateAttributeValue(att, attValue);
553                     viewer.update(element, null);
554
555                     if (callNativeUpdateValue) {
556                         MessageBox dialog = new MessageBox(viewer.getTree()
557                                 .getShell(), SWT.ICON_QUESTION | SWT.OK
558                                 | SWT.CANCEL);
559                         dialog.setText("Confirm action");
560                         dialog.setMessage("Do you want to modify the value?");
561                         int retval = dialog.open();
562                         if (retval != SWT.OK) {
563                             try {
564                                 attValue = AttributeValueBuilder.build(
565                                         oldValue, type.mBaseType);
566                                 updateAttributeValue(att, attValue);
567                             } catch (Exception e) {
568                                 Activator
569                                         .getDefault()
570                                         .getLogManager()
571                                         .log(Level.ERROR.ordinal(),
572                                                 new Date(),
573                                                 "There is an error while updating the attribute value.\n"
574                                                         + Utility
575                                                                 .getSimulatorErrorString(
576                                                                         e, null));
577                             }
578                         } else {
579                             ResourceManager resourceManager;
580                             resourceManager = Activator.getDefault()
581                                     .getResourceManager();
582
583                             Resource resource = resourceManager
584                                     .getCurrentResourceInSelection();
585
586                             SimulatorResourceAttribute result = getResultantAttribute();
587                             if (null == result) {
588                                 Activator
589                                         .getDefault()
590                                         .getLogManager()
591                                         .log(Level.ERROR.ordinal(), new Date(),
592                                                 "There is an error while updating the attribute value.\n");
593                                 return;
594                             }
595
596                             boolean updated = resourceManager
597                                     .attributeValueUpdated(
598                                             (SingleResource) resource,
599                                             result.name(), result.value());
600                             if (!updated) {
601                                 try {
602                                     attValue = AttributeValueBuilder.build(
603                                             oldValue, type.mBaseType);
604                                     updateAttributeValue(att, attValue);
605                                 } catch (Exception e) {
606                                     Activator
607                                             .getDefault()
608                                             .getLogManager()
609                                             .log(Level.ERROR.ordinal(),
610                                                     new Date(),
611                                                     "There is an error while updating the attribute value.\n"
612                                                             + Utility
613                                                                     .getSimulatorErrorString(
614                                                                             e,
615                                                                             null));
616                                 }
617                                 MessageDialog
618                                         .openInformation(Display.getDefault()
619                                                 .getActiveShell(),
620                                                 "Operation failed",
621                                                 "Failed to update the attribute value.");
622                             }
623                         }
624                     }
625                 }
626             }
627             viewer.update(element, null);
628         }
629
630         public void updateAttributeValue(SimulatorResourceAttribute att,
631                 AttributeValue value) {
632             if (null == att || null == value) {
633                 return;
634             }
635
636             IStructuredSelection selection = (IStructuredSelection) viewer
637                     .getSelection();
638             if (null == selection) {
639                 return;
640             }
641
642             Object obj = selection.getFirstElement();
643             if (null == obj) {
644                 return;
645             }
646
647             Tree t = viewer.getTree();
648             TreeItem item = t.getSelection()[0];
649             if (null == item) {
650                 return;
651             }
652
653             if (item.getData() instanceof AttributeElement) {
654                 AttributeElement attributeElement = (AttributeElement) item
655                         .getData();
656                 attributeElement.getSimulatorResourceAttribute()
657                         .setValue(value);
658
659                 TreeItem parent = item.getParentItem();
660                 if (null != parent) {
661                     Object data = parent.getData();
662                     try {
663                         ((AttributeElement) data).deepSetChildValue(att);
664                     } catch (InvalidArgsException e) {
665                         Activator
666                                 .getDefault()
667                                 .getLogManager()
668                                 .log(Level.ERROR.ordinal(),
669                                         new Date(),
670                                         "There is an error while updating the attribute value.\n"
671                                                 + Utility
672                                                         .getSimulatorErrorString(
673                                                                 e, null));
674                     }
675                 }
676             }
677         }
678
679         public SimulatorResourceAttribute getResultantAttribute() {
680             IStructuredSelection selection = (IStructuredSelection) viewer
681                     .getSelection();
682             if (null == selection) {
683                 return null;
684             }
685
686             Object obj = selection.getFirstElement();
687             if (null == obj) {
688                 return null;
689             }
690
691             Tree t = viewer.getTree();
692             TreeItem item = t.getSelection()[0];
693             if (null == item) {
694                 return null;
695             }
696
697             SimulatorResourceAttribute result;
698             TreeItem parent = item.getParentItem();
699             if (null == parent) {
700                 Object data = item.getData();
701                 result = ((AttributeElement) data)
702                         .getSimulatorResourceAttribute();
703             } else {
704                 while (parent.getParentItem() != null) {
705                     parent = parent.getParentItem();
706                 }
707
708                 // Parent will point to the top-level attribute of type
709                 Object data = parent.getData();
710                 result = ((AttributeElement) data)
711                         .getSimulatorResourceAttribute();
712             }
713
714             return result;
715         }
716     }
717
718     class AutomationEditor extends EditingSupport {
719
720         private final TreeViewer viewer;
721
722         public AutomationEditor(TreeViewer viewer) {
723             super(viewer);
724             this.viewer = viewer;
725         }
726
727         @Override
728         protected boolean canEdit(Object arg0) {
729             return true;
730         }
731
732         @Override
733         protected CellEditor getCellEditor(Object element) {
734             // CellEditor is not required as the automation is in progress.
735             ResourceManager resourceManager = Activator.getDefault()
736                     .getResourceManager();
737             Resource resource = resourceManager.getCurrentResourceInSelection();
738
739             if (null == resource) {
740                 return null;
741             }
742
743             if (!(resource instanceof SingleResource)) {
744                 return null;
745             }
746             if (((SingleResource) resource).isResourceAutomationInProgress()) {
747                 return null;
748             }
749
750             SimulatorResourceAttribute att = null;
751             if (element instanceof AttributeElement) {
752                 att = ((AttributeElement) element)
753                         .getSimulatorResourceAttribute();
754             }
755
756             if (null == att) {
757                 return null;
758             }
759
760             AttributeValue val = att.value();
761             if (null == val) {
762                 return null;
763             }
764
765             TypeInfo type = val.typeInfo();
766
767             if (type.mType == ValueType.RESOURCEMODEL
768                     || type.mType == ValueType.ARRAY) {
769                 return null;
770             }
771
772             Object parent = ((AttributeElement) element).getParent();
773             if (null != parent && !(parent instanceof ResourceRepresentation)) {
774                 return null;
775             }
776
777             if (((AttributeElement) element).isReadOnly()) {
778                 return null;
779             }
780
781             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
782         }
783
784         @Override
785         protected Object getValue(Object element) {
786             if (element instanceof AttributeElement) {
787                 return ((AttributeElement) element).isAutoUpdateInProgress();
788             }
789
790             return false;
791         }
792
793         @Override
794         protected void setValue(Object element, Object value) {
795             if (!(element instanceof AttributeElement)) {
796                 return;
797             }
798
799             ResourceManager resourceManager = Activator.getDefault()
800                     .getResourceManager();
801             // As automation depends on the current resource in selection, its
802             // presence is being checked.
803             Resource resource = resourceManager.getCurrentResourceInSelection();
804             if (null == resource) {
805                 return;
806             }
807
808             AttributeElement att = (AttributeElement) element;
809             boolean checked = (Boolean) value;
810             if (checked) {
811                 // Start the automation
812                 // Fetch the settings data
813                 List<AutomationSettingHelper> automationSettings;
814                 automationSettings = AutomationSettingHelper
815                         .getAutomationSettings(att);
816
817                 // Open the settings dialog
818                 AutomationSettingDialog dialog = new AutomationSettingDialog(
819                         viewer.getTree().getShell(), automationSettings);
820                 dialog.create();
821                 if (dialog.open() == Window.OK) {
822                     String automationType = dialog.getAutomationType();
823                     String updateFreq = dialog.getUpdateFrequency();
824
825                     AutoUpdateType autoType = AutoUpdateType
826                             .valueOf(automationType);
827                     int updFreq = Utility
828                             .getUpdateIntervalFromString(updateFreq);
829                     int autoId = resourceManager.startAutomation(
830                             (SingleResource) resource, att, autoType, updFreq);
831                     if (autoId == -1) {
832                         MessageDialog.openInformation(Display.getDefault()
833                                 .getActiveShell(), "Automation Status",
834                                 "Automation start failed!!");
835                     }
836                 }
837             } else {
838                 // Stop the automation
839                 resourceManager.stopAutomation((SingleResource) resource, att,
840                         att.getAutoUpdateId());
841             }
842         }
843     }
844 }