IoTivity Simulator System testing bug fixes.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / 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.clientcontroller.view;
18
19 import java.util.List;
20
21 import oic.simulator.clientcontroller.Activator;
22 import oic.simulator.clientcontroller.manager.ResourceManager;
23 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
24 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
25 import oic.simulator.clientcontroller.utils.AttributeValueBuilder;
26 import oic.simulator.clientcontroller.utils.Utility;
27 import oic.simulator.clientcontroller.view.dialogs.PostRequestDialog;
28
29 import org.eclipse.jface.dialogs.TitleAreaDialog;
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.IStructuredSelection;
35 import org.eclipse.jface.viewers.TreeViewer;
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.MessageBox;
41 import org.eclipse.swt.widgets.Tree;
42 import org.eclipse.swt.widgets.TreeItem;
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.InvalidArgsException;
47 import org.oic.simulator.SimulatorResourceAttribute;
48
49 /**
50  * This class provides editing support to the resources attributes table in the
51  * attributes view.
52  */
53 public class AttributeEditingSupport {
54
55     private AttributeValueEditor attValueEditor;
56     private PostRequestEditor    postReqEditor;
57
58     public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer,
59             TitleAreaDialog dialog) {
60         attValueEditor = new AttributeValueEditor(viewer, dialog);
61         return attValueEditor;
62     }
63
64     public PostRequestEditor createAutomationEditor(TreeViewer viewer) {
65         postReqEditor = new PostRequestEditor(viewer);
66         return postReqEditor;
67     }
68
69     class AttributeValueEditor extends EditingSupport {
70
71         private final TreeViewer viewer;
72         private CCombo           comboBox;
73         private TitleAreaDialog  dialog;
74
75         public AttributeValueEditor(TreeViewer viewer, TitleAreaDialog dialog) {
76             super(viewer);
77             this.viewer = viewer;
78             this.dialog = dialog;
79         }
80
81         @Override
82         protected boolean canEdit(Object arg0) {
83             return true;
84         }
85
86         @Override
87         protected CellEditor getCellEditor(final Object element) {
88             ResourceManager resourceManager = Activator.getDefault()
89                     .getResourceManager();
90
91             RemoteResource res = resourceManager
92                     .getCurrentResourceInSelection();
93             if (null == res) {
94                 return null;
95             }
96
97             final SimulatorResourceAttribute attribute;
98             if (!(element instanceof AttributeElement)) {
99                 return null;
100             }
101
102             final AttributeElement attributeElement = ((AttributeElement) element);
103             attribute = attributeElement.getSimulatorResourceAttribute();
104             if (null == attribute) {
105                 return null;
106             }
107
108             final AttributeValue val = attribute.value();
109             if (null == val) {
110                 return null;
111             }
112
113             final TypeInfo type = val.typeInfo();
114             if (type.mBaseType == ValueType.RESOURCEMODEL) {
115                 return null;
116             }
117
118             String values[] = null;
119             List<String> valueSet = resourceManager
120                     .getAllValuesOfAttribute(attribute);
121             values = convertListToStringArray(valueSet);
122
123             ComboBoxCellEditor comboEditor;
124             comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
125             comboBox = (CCombo) comboEditor.getControl();
126             comboBox.addModifyListener(new ModifyListener() {
127
128                 @Override
129                 public void modifyText(ModifyEvent event) {
130                     String newValue = comboBox.getText();
131
132                     if (null != newValue && !newValue.isEmpty()) {
133                         attributeElement.setPostState(true);
134                     } else {
135                         attributeElement.setPostState(false);
136                     }
137
138                     if (dialog instanceof PostRequestDialog) {
139                         viewer.update(attributeElement, null);
140                         // comboBox.setVisible(false);
141                     }
142                 }
143             });
144             return comboEditor;
145         }
146
147         @Override
148         protected Object getValue(Object element) {
149             int indexOfItem = 0;
150             SimulatorResourceAttribute att = null;
151
152             if (element instanceof AttributeElement) {
153                 att = ((AttributeElement) element)
154                         .getSimulatorResourceAttribute();
155             }
156
157             if (att == null) {
158                 return 0;
159             }
160
161             String valueString = Utility.getAttributeValueAsString(att.value());
162             List<String> valueSet = Activator.getDefault().getResourceManager()
163                     .getAllValuesOfAttribute(att);
164             if (null != valueSet) {
165                 indexOfItem = valueSet.indexOf(valueString);
166             }
167             if (indexOfItem == -1) {
168                 indexOfItem = 0;
169             }
170             return indexOfItem;
171         }
172
173         @Override
174         protected void setValue(Object element, Object value) {
175             SimulatorResourceAttribute att = null;
176             if (element instanceof AttributeElement) {
177                 att = ((AttributeElement) element)
178                         .getSimulatorResourceAttribute();
179             }
180
181             if (att == null) {
182                 return;
183             }
184
185             AttributeValue val = att.value();
186             if (null == val) {
187                 return;
188             }
189
190             TypeInfo type = val.typeInfo();
191
192             String oldValue = String.valueOf(Utility
193                     .getAttributeValueAsString(val));
194             String newValue = comboBox.getText();
195             if (!oldValue.equals(newValue)) {
196                 // Get the AttriuteValue from the string
197                 AttributeValue attValue = AttributeValueBuilder.build(newValue,
198                         type.mBaseType);
199                 boolean invalid = false;
200                 if (null == attValue) {
201                     invalid = true;
202                 } else {
203                     TypeInfo resTypeInfo = attValue.typeInfo();
204                     if (type.mDepth != resTypeInfo.mDepth
205                             || type.mType != resTypeInfo.mType
206                             || type.mBaseType != resTypeInfo.mBaseType) {
207                         invalid = true;
208                     }
209                 }
210                 if (invalid) {
211                     MessageBox dialog = new MessageBox(viewer.getTree()
212                             .getShell(), SWT.ICON_ERROR | SWT.OK);
213                     dialog.setText("Invalid Value");
214                     dialog.setMessage("Given value is invalid");
215                     dialog.open();
216                 } else {
217                     updateAttributeValue(att, attValue);
218                 }
219             }
220
221             viewer.update(element, null);
222         }
223
224         public String[] convertListToStringArray(List<String> values) {
225             String[] strArr;
226             if (null != values && values.size() > 0) {
227                 strArr = values.toArray(new String[1]);
228             } else {
229                 strArr = new String[1];
230             }
231             return strArr;
232         }
233
234         public void updateAttributeValue(SimulatorResourceAttribute att,
235                 AttributeValue value) {
236             att.setValue(value);
237
238             IStructuredSelection selection = (IStructuredSelection) viewer
239                     .getSelection();
240             if (null == selection) {
241                 return;
242             }
243
244             Object obj = selection.getFirstElement();
245             if (null == obj) {
246                 return;
247             }
248
249             Tree t = viewer.getTree();
250             TreeItem item = t.getSelection()[0];
251             if (null == item) {
252                 return;
253             }
254
255             TreeItem parent = item.getParentItem();
256             if (null != parent) {
257                 while (parent.getParentItem() != null) {
258                     parent = parent.getParentItem();
259                 }
260                 Object data = parent.getData();
261                 ((AttributeElement) data).setPostState(true);
262             }
263
264             if (item.getData() instanceof AttributeElement) {
265                 AttributeElement attributeElement = (AttributeElement) item
266                         .getData();
267                 attributeElement.getSimulatorResourceAttribute()
268                         .setValue(value);
269
270                 parent = item.getParentItem();
271                 if (null != parent) {
272                     Object data = parent.getData();
273                     try {
274                         ((AttributeElement) data).deepSetChildValue(att);
275                     } catch (InvalidArgsException e) {
276                         e.printStackTrace();
277                     }
278                 }
279             }
280         }
281     }
282
283     class PostRequestEditor extends EditingSupport {
284
285         private final TreeViewer viewer;
286
287         public PostRequestEditor(TreeViewer viewer) {
288             super(viewer);
289             this.viewer = viewer;
290         }
291
292         @Override
293         protected boolean canEdit(Object arg0) {
294             return true;
295         }
296
297         @Override
298         protected CellEditor getCellEditor(Object element) {
299             SimulatorResourceAttribute att = null;
300             if (element instanceof AttributeElement) {
301                 att = ((AttributeElement) element)
302                         .getSimulatorResourceAttribute();
303             }
304
305             if (null == att) {
306                 return null;
307             }
308
309             AttributeValue val = att.value();
310             if (null == val) {
311                 return null;
312             }
313
314             TypeInfo type = val.typeInfo();
315
316             if (type.mType == ValueType.RESOURCEMODEL
317                     || type.mBaseType == ValueType.RESOURCEMODEL) {
318                 return null;
319             }
320
321             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
322         }
323
324         @Override
325         protected Object getValue(Object element) {
326             if (element instanceof AttributeElement) {
327                 return ((AttributeElement) element).getPostState();
328             }
329
330             return false;
331         }
332
333         @Override
334         protected void setValue(Object element, Object value) {
335             if (!(element instanceof AttributeElement)) {
336                 return;
337             }
338             boolean status = (Boolean) value;
339             ((AttributeElement) element).setPostState(status);
340             viewer.update(element, null);
341         }
342     }
343 }