faf2542fd6d99740ec60604838b1d94ace1cc93c
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / AttributeEditingSupport.java
1 package oic.simulator.serviceprovider.view;
2
3 import java.util.List;
4
5 import oic.simulator.serviceprovider.Activator;
6 import oic.simulator.serviceprovider.manager.ResourceManager;
7 import oic.simulator.serviceprovider.resource.AutomationSettingHelper;
8 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
9 import oic.simulator.serviceprovider.resource.SimulatorResource;
10 import oic.simulator.serviceprovider.utils.Utility;
11 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
12
13 import org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.CellEditor;
15 import org.eclipse.jface.viewers.CheckboxCellEditor;
16 import org.eclipse.jface.viewers.ComboBoxCellEditor;
17 import org.eclipse.jface.viewers.EditingSupport;
18 import org.eclipse.jface.viewers.TableViewer;
19 import org.eclipse.jface.window.Window;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.CCombo;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.MessageBox;
26 import org.oic.simulator.AutomationType;
27
28 public class AttributeEditingSupport {
29
30     private AttributeValueEditor attValueEditor;
31     private AutomationEditor     automationEditor;
32
33     public AttributeValueEditor createAttributeValueEditor(TableViewer viewer) {
34         attValueEditor = new AttributeValueEditor(viewer);
35         return attValueEditor;
36     }
37
38     public AutomationEditor createAutomationEditor(TableViewer viewer) {
39         automationEditor = new AutomationEditor(viewer);
40         return automationEditor;
41     }
42
43     class AttributeValueEditor extends EditingSupport {
44
45         private final TableViewer      viewer;
46         private LocalResourceAttribute attributeInSelection;
47         private CCombo                 comboBox;
48
49         public AttributeValueEditor(TableViewer viewer) {
50             super(viewer);
51             this.viewer = viewer;
52         }
53
54         @Override
55         protected boolean canEdit(Object arg0) {
56             return true;
57         }
58
59         @Override
60         protected CellEditor getCellEditor(Object element) {
61             attributeInSelection = (LocalResourceAttribute) element;
62
63             // CellEditor is not required as the automation is in progress.
64             if (attributeInSelection.isAutomationInProgress()) {
65                 return null;
66             }
67
68             String values[] = null;
69             List<String> valueSet = attributeInSelection.getAttValues();
70             values = convertListToStringArray(valueSet);
71
72             ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
73                     viewer.getTable(), values, SWT.READ_ONLY);
74             comboBox = (CCombo) comboEditor.getControl();
75             comboBox.addModifyListener(new ModifyListener() {
76
77                 @Override
78                 public void modifyText(ModifyEvent event) {
79                     String oldValue = String.valueOf(attributeInSelection
80                             .getAttributeValue());
81                     String newValue = comboBox.getText();
82                     if (!oldValue.equals(newValue)) {
83                         attributeInSelection.setAttributeValue(newValue);
84                         MessageBox dialog = new MessageBox(viewer.getTable()
85                                 .getShell(), SWT.ICON_QUESTION | SWT.OK
86                                 | SWT.CANCEL);
87                         dialog.setText("Confirm action");
88                         dialog.setMessage("Do you want to modify the value?");
89                         int retval = dialog.open();
90                         if (retval != SWT.OK) {
91                             attributeInSelection.setAttributeValue(oldValue);
92                         } else {
93                             ResourceManager resourceManager;
94                             resourceManager = Activator.getDefault()
95                                     .getResourceManager();
96                             SimulatorResource resource = resourceManager
97                                     .getCurrentResourceInSelection();
98                             resourceManager.attributeValueUpdated(resource,
99                                     attributeInSelection.getAttributeName(),
100                                     newValue);
101                         }
102                         viewer.update(attributeInSelection, null);
103                         comboBox.setVisible(false);
104                     }
105                 }
106             });
107             return comboEditor;
108         }
109
110         @Override
111         protected Object getValue(Object element) {
112             int indexOfItem = 0;
113             LocalResourceAttribute att = (LocalResourceAttribute) element;
114             String valueString = String.valueOf(att.getAttributeValue());
115             List<String> valueSet = att.getAttValues();
116             if (null != valueSet) {
117                 indexOfItem = valueSet.indexOf(valueString);
118             }
119             if (indexOfItem == -1) {
120                 indexOfItem = 0;
121             }
122             return indexOfItem;
123         }
124
125         @Override
126         protected void setValue(Object element, Object value) {
127             Object valueObj = attributeInSelection.getAttributeValue();
128             if (null == valueObj)
129                 return;
130             String attValue = String.valueOf(valueObj);
131             ((LocalResourceAttribute) element).setAttributeValue(attValue);
132             viewer.update(element, null);
133         }
134
135         public String[] convertListToStringArray(List<String> valueList) {
136             String[] strArr;
137             if (null != valueList && valueList.size() > 0) {
138                 strArr = valueList.toArray(new String[1]);
139             } else {
140                 strArr = new String[1];
141             }
142             return strArr;
143         }
144     }
145
146     class AutomationEditor extends EditingSupport {
147
148         private final TableViewer viewer;
149
150         public AutomationEditor(TableViewer viewer) {
151             super(viewer);
152             this.viewer = viewer;
153         }
154
155         @Override
156         protected boolean canEdit(Object arg0) {
157             return true;
158         }
159
160         @Override
161         protected CellEditor getCellEditor(Object element) {
162             // CellEditor is not required as the automation is in progress.
163             ResourceManager resourceManager = Activator.getDefault()
164                     .getResourceManager();
165             SimulatorResource resource = resourceManager
166                     .getCurrentResourceInSelection();
167             if (null != resource && resource.isResourceAutomationInProgress()) {
168                 return null;
169             }
170             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
171         }
172
173         @Override
174         protected Object getValue(Object element) {
175             LocalResourceAttribute att = (LocalResourceAttribute) element;
176             return att.isAutomationInProgress();
177         }
178
179         @Override
180         protected void setValue(Object element, Object value) {
181             ResourceManager resourceManager = Activator.getDefault()
182                     .getResourceManager();
183             // As automation depends on the current resource in selection, its
184             // presence is being checked.
185             SimulatorResource resource = resourceManager
186                     .getCurrentResourceInSelection();
187             if (null == resource) {
188                 return;
189             }
190
191             LocalResourceAttribute att = (LocalResourceAttribute) element;
192             boolean checked = (Boolean) value;
193             if (checked) {
194                 // Start the automation
195                 // Fetch the settings data
196                 List<AutomationSettingHelper> automationSettings;
197                 automationSettings = AutomationSettingHelper
198                         .getAutomationSettings(att);
199
200                 // Open the settings dialog
201                 AutomationSettingDialog dialog = new AutomationSettingDialog(
202                         viewer.getTable().getShell(), automationSettings);
203                 dialog.create();
204                 if (dialog.open() == Window.OK) {
205                     String automationType = dialog.getAutomationType();
206                     String updateFreq = dialog.getUpdateFrequency();
207
208                     AutomationType autoType = AutomationType
209                             .valueOf(automationType);
210                     int updFreq = Utility
211                             .getUpdateIntervalFromString(updateFreq);
212                     int autoId = resourceManager.startAutomation(resource, att,
213                             autoType, updFreq);
214                     if (autoId == -1) {
215                         MessageDialog.openInformation(Display.getDefault()
216                                 .getActiveShell(), "Automation Status",
217                                 "Automation start failed!!");
218                     } else {
219                         viewer.update(element, null);
220                     }
221                 }
222             } else {
223                 // Stop the automation
224                 resourceManager.stopAutomation(resource, att,
225                         att.getAutomationId());
226                 MessageDialog.openInformation(Display.getDefault()
227                         .getActiveShell(), "Automation Status",
228                         "Automation stopped.");
229                 viewer.update(element, null);
230             }
231         }
232     }
233 }