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