Imported Upstream version 1.0.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 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             // CellEditor is not required as the automation is in progress.
84             if (attributeInSelection.isAutomationInProgress()) {
85                 return null;
86             }
87
88             String values[] = null;
89             List<String> valueSet = attributeInSelection.getAttValues();
90             values = convertListToStringArray(valueSet);
91
92             ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
93                     viewer.getTable(), values, SWT.READ_ONLY);
94             comboBox = (CCombo) comboEditor.getControl();
95             comboBox.addModifyListener(new ModifyListener() {
96
97                 @Override
98                 public void modifyText(ModifyEvent event) {
99                     String oldValue = String.valueOf(attributeInSelection
100                             .getAttributeValue());
101                     String newValue = comboBox.getText();
102                     if (!oldValue.equals(newValue)) {
103                         attributeInSelection.setAttributeValue(newValue);
104                         MessageBox dialog = new MessageBox(viewer.getTable()
105                                 .getShell(), SWT.ICON_QUESTION | SWT.OK
106                                 | SWT.CANCEL);
107                         dialog.setText("Confirm action");
108                         dialog.setMessage("Do you want to modify the value?");
109                         int retval = dialog.open();
110                         if (retval != SWT.OK) {
111                             attributeInSelection.setAttributeValue(oldValue);
112                         } else {
113                             ResourceManager resourceManager;
114                             resourceManager = Activator.getDefault()
115                                     .getResourceManager();
116                             SimulatorResource resource = resourceManager
117                                     .getCurrentResourceInSelection();
118                             resourceManager.attributeValueUpdated(resource,
119                                     attributeInSelection.getAttributeName(),
120                                     newValue);
121                         }
122                         viewer.update(attributeInSelection, null);
123                         comboBox.setVisible(false);
124                     }
125                 }
126             });
127             return comboEditor;
128         }
129
130         @Override
131         protected Object getValue(Object element) {
132             int indexOfItem = 0;
133             LocalResourceAttribute att = (LocalResourceAttribute) element;
134             String valueString = String.valueOf(att.getAttributeValue());
135             List<String> valueSet = att.getAttValues();
136             if (null != valueSet) {
137                 indexOfItem = valueSet.indexOf(valueString);
138             }
139             if (indexOfItem == -1) {
140                 indexOfItem = 0;
141             }
142             return indexOfItem;
143         }
144
145         @Override
146         protected void setValue(Object element, Object value) {
147             Object valueObj = attributeInSelection.getAttributeValue();
148             if (null == valueObj)
149                 return;
150             String attValue = String.valueOf(valueObj);
151             ((LocalResourceAttribute) element).setAttributeValue(attValue);
152             viewer.update(element, null);
153         }
154
155         public String[] convertListToStringArray(List<String> valueList) {
156             String[] strArr;
157             if (null != valueList && valueList.size() > 0) {
158                 strArr = valueList.toArray(new String[1]);
159             } else {
160                 strArr = new String[1];
161             }
162             return strArr;
163         }
164     }
165
166     class AutomationEditor extends EditingSupport {
167
168         private final TableViewer viewer;
169
170         public AutomationEditor(TableViewer viewer) {
171             super(viewer);
172             this.viewer = viewer;
173         }
174
175         @Override
176         protected boolean canEdit(Object arg0) {
177             return true;
178         }
179
180         @Override
181         protected CellEditor getCellEditor(Object element) {
182             // CellEditor is not required as the automation is in progress.
183             ResourceManager resourceManager = Activator.getDefault()
184                     .getResourceManager();
185             SimulatorResource resource = resourceManager
186                     .getCurrentResourceInSelection();
187             if (null != resource && resource.isResourceAutomationInProgress()) {
188                 return null;
189             }
190             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
191         }
192
193         @Override
194         protected Object getValue(Object element) {
195             LocalResourceAttribute att = (LocalResourceAttribute) element;
196             return att.isAutomationInProgress();
197         }
198
199         @Override
200         protected void setValue(Object element, Object value) {
201             ResourceManager resourceManager = Activator.getDefault()
202                     .getResourceManager();
203             // As automation depends on the current resource in selection, its
204             // presence is being checked.
205             SimulatorResource resource = resourceManager
206                     .getCurrentResourceInSelection();
207             if (null == resource) {
208                 return;
209             }
210
211             LocalResourceAttribute att = (LocalResourceAttribute) element;
212             boolean checked = (Boolean) value;
213             if (checked) {
214                 // Start the automation
215                 // Fetch the settings data
216                 List<AutomationSettingHelper> automationSettings;
217                 automationSettings = AutomationSettingHelper
218                         .getAutomationSettings(att);
219
220                 // Open the settings dialog
221                 AutomationSettingDialog dialog = new AutomationSettingDialog(
222                         viewer.getTable().getShell(), automationSettings);
223                 dialog.create();
224                 if (dialog.open() == Window.OK) {
225                     String automationType = dialog.getAutomationType();
226                     String updateFreq = dialog.getUpdateFrequency();
227
228                     AutomationType autoType = AutomationType
229                             .valueOf(automationType);
230                     int updFreq = Utility
231                             .getUpdateIntervalFromString(updateFreq);
232                     int autoId = resourceManager.startAutomation(resource, att,
233                             autoType, updFreq);
234                     if (autoId == -1) {
235                         MessageDialog.openInformation(Display.getDefault()
236                                 .getActiveShell(), "Automation Status",
237                                 "Automation start failed!!");
238                     } else {
239                         viewer.update(element, null);
240                     }
241                 }
242             } else {
243                 // Stop the automation
244                 resourceManager.stopAutomation(resource, att,
245                         att.getAutomationId());
246                 MessageDialog.openInformation(Display.getDefault()
247                         .getActiveShell(), "Automation Status",
248                         "Automation stopped.");
249                 viewer.update(element, null);
250             }
251         }
252     }
253 }