Displaying and editing the complex value types for attributes.
[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.model.AttributeElement;
24 import oic.simulator.serviceprovider.model.AutomationSettingHelper;
25 import oic.simulator.serviceprovider.model.CollectionResource;
26 import oic.simulator.serviceprovider.model.Resource;
27 import oic.simulator.serviceprovider.model.ResourceRepresentation;
28 import oic.simulator.serviceprovider.model.SingleResource;
29 import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
30 import oic.simulator.serviceprovider.utils.Utility;
31 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
32
33 import org.eclipse.jface.dialogs.MessageDialog;
34 import org.eclipse.jface.viewers.CellEditor;
35 import org.eclipse.jface.viewers.CheckboxCellEditor;
36 import org.eclipse.jface.viewers.ComboBoxCellEditor;
37 import org.eclipse.jface.viewers.EditingSupport;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.TreeViewer;
40 import org.eclipse.jface.window.Window;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.custom.CCombo;
43 import org.eclipse.swt.events.ModifyEvent;
44 import org.eclipse.swt.events.ModifyListener;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.MessageBox;
47 import org.eclipse.swt.widgets.Tree;
48 import org.eclipse.swt.widgets.TreeItem;
49 import org.oic.simulator.AttributeProperty;
50 import org.oic.simulator.AttributeValue;
51 import org.oic.simulator.AttributeValue.TypeInfo;
52 import org.oic.simulator.AttributeValue.ValueType;
53 import org.oic.simulator.InvalidArgsException;
54 import org.oic.simulator.SimulatorResourceAttribute;
55 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
56
57 /**
58  * This class provides editing support to the resources attributes table in the
59  * attributes view.
60  */
61 public class AttributeEditingSupport {
62
63     private AttributeValueEditor attValueEditor;
64     private AutomationEditor     automationEditor;
65
66     public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer) {
67         attValueEditor = new AttributeValueEditor(viewer);
68         return attValueEditor;
69     }
70
71     public AutomationEditor createAutomationEditor(TreeViewer viewer) {
72         automationEditor = new AutomationEditor(viewer);
73         return automationEditor;
74     }
75
76     class AttributeValueEditor extends EditingSupport {
77
78         private final TreeViewer viewer;
79         private CCombo           comboBox;
80
81         public AttributeValueEditor(TreeViewer viewer) {
82             super(viewer);
83             this.viewer = viewer;
84         }
85
86         @Override
87         protected boolean canEdit(Object arg0) {
88             return true;
89         }
90
91         @Override
92         protected CellEditor getCellEditor(final Object element) {
93             ResourceManager resourceManager = Activator.getDefault()
94                     .getResourceManager();
95
96             Resource res = resourceManager.getCurrentResourceInSelection();
97             if (null == res) {
98                 return null;
99             }
100
101             // If selected resource is a collection, then editor support is not
102             // required.
103             if (res instanceof CollectionResource) {
104                 return null;
105             }
106
107             final SimulatorResourceAttribute attribute;
108             if (!(element instanceof AttributeElement)) {
109                 return null;
110             }
111
112             AttributeElement attributeElement = ((AttributeElement) element);
113             attribute = attributeElement.getSimulatorResourceAttribute();
114             if (null == attribute) {
115                 return null;
116             }
117
118             // CellEditor is not required as the automation is in progress.
119             if (attributeElement.isAutoUpdateInProgress()) {
120                 return null;
121             }
122
123             final AttributeValue val = attribute.value();
124             if (null == val) {
125                 return null;
126             }
127
128             final TypeInfo type = val.typeInfo();
129             if (type.mBaseType == ValueType.RESOURCEMODEL) {
130                 return null;
131             }
132
133             AttributeProperty prop = attribute.property();
134             if (null == prop) {
135                 return null;
136             }
137
138             if (!resourceManager.isAttHasRangeOrAllowedValues(attribute)) {
139                 return null;
140             }
141
142             String values[] = null;
143             List<String> valueSet = resourceManager
144                     .getAllValuesOfAttribute(attribute);
145             values = convertListToStringArray(valueSet);
146
147             ComboBoxCellEditor comboEditor;
148             if (type.mType == ValueType.ARRAY) {
149                 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
150             } else {
151                 comboEditor = new ComboBoxCellEditor(viewer.getTree(), values,
152                         SWT.READ_ONLY);
153             }
154             comboBox = (CCombo) comboEditor.getControl();
155             comboBox.addModifyListener(new ModifyListener() {
156
157                 @Override
158                 public void modifyText(ModifyEvent event) {
159                     if (type.mType == ValueType.ARRAY) {
160                         return;
161                     }
162                     String oldValue = String.valueOf(Utility
163                             .getAttributeValueAsString(val));
164                     String newValue = comboBox.getText();
165                     if (!oldValue.equals(newValue)) {
166                         // Get the AttriuteValue from the string
167                         AttributeValue value = AttributeValueBuilder.build(
168                                 newValue, type.mBaseType);
169                         TypeInfo resTypeInfo = value.typeInfo();
170                         if (null == value || type.mDepth != resTypeInfo.mDepth
171                                 || type.mType != resTypeInfo.mType
172                                 || type.mBaseType != resTypeInfo.mBaseType) {
173                             MessageBox dialog = new MessageBox(viewer.getTree()
174                                     .getShell(), SWT.ICON_ERROR | SWT.OK);
175                             dialog.setText("Invalid Value");
176                             dialog.setMessage("Given value is invalid");
177                             dialog.open();
178                         } else {
179                             updateAttributeValue(attribute, value);
180                             MessageBox dialog = new MessageBox(viewer.getTree()
181                                     .getShell(), SWT.ICON_QUESTION | SWT.OK
182                                     | SWT.CANCEL);
183                             dialog.setText("Confirm action");
184                             dialog.setMessage("Do you want to modify the value?");
185                             int retval = dialog.open();
186                             if (retval != SWT.OK) {
187                                 value = AttributeValueBuilder.build(oldValue,
188                                         type.mBaseType);
189                                 updateAttributeValue(attribute, value);
190                             } else {
191                                 ResourceManager resourceManager;
192                                 resourceManager = Activator.getDefault()
193                                         .getResourceManager();
194
195                                 Resource resource = resourceManager
196                                         .getCurrentResourceInSelection();
197
198                                 SimulatorResourceAttribute result = getResultantValue(value);
199
200                                 resourceManager.attributeValueUpdated(
201                                         (SingleResource) resource,
202                                         result.name(), result.value());
203
204                             }
205                         }
206                         viewer.update(element, null);
207                         comboBox.setVisible(false);
208                     }
209                 }
210             });
211             return comboEditor;
212         }
213
214         @Override
215         protected Object getValue(Object element) {
216             int indexOfItem = 0;
217             SimulatorResourceAttribute att = null;
218
219             if (element instanceof AttributeElement) {
220                 att = ((AttributeElement) element)
221                         .getSimulatorResourceAttribute();
222             }
223
224             if (att == null) {
225                 return 0;
226             }
227
228             String valueString = Utility.getAttributeValueAsString(att.value());
229             List<String> valueSet = Activator.getDefault().getResourceManager()
230                     .getAllValuesOfAttribute(att);
231             if (null != valueSet) {
232                 indexOfItem = valueSet.indexOf(valueString);
233             }
234             if (indexOfItem == -1) {
235                 indexOfItem = 0;
236             }
237             return indexOfItem;
238         }
239
240         @Override
241         protected void setValue(Object element, Object value) {
242             SimulatorResourceAttribute att = null;
243
244             if (element instanceof AttributeElement) {
245                 att = ((AttributeElement) element)
246                         .getSimulatorResourceAttribute();
247             }
248
249             if (att == null) {
250                 return;
251             }
252
253             AttributeValue val = att.value();
254             if (null == val) {
255                 return;
256             }
257             TypeInfo type = val.typeInfo();
258             if (type.mType == ValueType.ARRAY) {
259                 int index;
260                 try {
261                     index = Integer.parseInt(String.valueOf(value));
262                 } catch (NumberFormatException nfe) {
263                     index = -1;
264                 }
265                 if (index == -1) {
266                     String oldValue = String.valueOf(Utility
267                             .getAttributeValueAsString(val));
268                     String newValue = comboBox.getText();
269                     if (!oldValue.equals(newValue)) {
270                         // Get the AttriuteValue from the string
271                         AttributeValue attValue = AttributeValueBuilder.build(
272                                 newValue, type.mBaseType);
273                         TypeInfo resTypeInfo = attValue.typeInfo();
274                         if (null == attValue
275                                 || type.mDepth != resTypeInfo.mDepth
276                                 || type.mType != resTypeInfo.mType
277                                 || type.mBaseType != resTypeInfo.mBaseType) {
278                             MessageBox dialog = new MessageBox(viewer.getTree()
279                                     .getShell(), SWT.ICON_ERROR | SWT.OK);
280                             dialog.setText("Invalid Value");
281                             dialog.setMessage("Given value is invalid");
282                             dialog.open();
283                         } else {
284                             updateAttributeValue(att, attValue);
285                             MessageBox dialog = new MessageBox(viewer.getTree()
286                                     .getShell(), SWT.ICON_QUESTION | SWT.OK
287                                     | SWT.CANCEL);
288                             dialog.setText("Confirm action");
289                             dialog.setMessage("Do you want to modify the value?");
290                             int retval = dialog.open();
291                             if (retval != SWT.OK) {
292                                 attValue = AttributeValueBuilder.build(
293                                         oldValue, type.mBaseType);
294                                 updateAttributeValue(att, attValue);
295                             } else {
296                                 ResourceManager resourceManager;
297                                 resourceManager = Activator.getDefault()
298                                         .getResourceManager();
299
300                                 Resource resource = resourceManager
301                                         .getCurrentResourceInSelection();
302
303                                 SimulatorResourceAttribute result = getResultantValue(attValue);
304
305                                 resourceManager.attributeValueUpdated(
306                                         (SingleResource) resource,
307                                         result.name(), result.value());
308                             }
309                         }
310                     }
311                 }
312             }
313
314             viewer.update(element, null);
315         }
316
317         public String[] convertListToStringArray(List<String> valueList) {
318             String[] strArr;
319             if (null != valueList && valueList.size() > 0) {
320                 strArr = valueList.toArray(new String[1]);
321             } else {
322                 strArr = new String[1];
323             }
324             return strArr;
325         }
326
327         public void updateAttributeValue(SimulatorResourceAttribute att,
328                 AttributeValue value) {
329             att.setValue(value);
330
331             IStructuredSelection selection = (IStructuredSelection) viewer
332                     .getSelection();
333             if (null == selection) {
334                 return;
335             }
336
337             Object obj = selection.getFirstElement();
338             if (null == obj) {
339                 return;
340             }
341
342             Tree t = viewer.getTree();
343             TreeItem item = t.getSelection()[0];
344             if (null == item) {
345                 return;
346             }
347
348             if (item.getData() instanceof AttributeElement) {
349                 AttributeElement attributeElement = (AttributeElement) item
350                         .getData();
351                 attributeElement.getSimulatorResourceAttribute()
352                         .setValue(value);
353
354                 TreeItem parent = item.getParentItem();
355                 if (null != parent) {
356                     Object data = parent.getData();
357                     try {
358                         ((AttributeElement) data).deepSetChildValue(att);
359                     } catch (InvalidArgsException e) {
360                         e.printStackTrace();
361                     }
362                 }
363             }
364         }
365
366         public SimulatorResourceAttribute getResultantValue(
367                 AttributeValue newValue) {
368             AttributeValue val = null;
369             IStructuredSelection selection = (IStructuredSelection) viewer
370                     .getSelection();
371             if (null == selection) {
372                 return null;
373             }
374
375             Object obj = selection.getFirstElement();
376             if (null == obj) {
377                 return null;
378             }
379
380             Tree t = viewer.getTree();
381             TreeItem item = t.getSelection()[0];
382             if (null == item) {
383                 return null;
384             }
385
386             SimulatorResourceAttribute result = null;
387             TreeItem parent = item.getParentItem();
388             if (null == parent) {
389                 Object data = item.getData();
390                 result = ((AttributeElement) data)
391                         .getSimulatorResourceAttribute();
392             } else {
393                 while (parent.getParentItem() != null) {
394                     parent = parent.getParentItem();
395                 }
396
397                 // Parent will point to the top-level attribute of type
398                 Object data = parent.getData();
399                 result = ((AttributeElement) data)
400                         .getSimulatorResourceAttribute();
401             }
402
403             return result;
404         }
405     }
406
407     class AutomationEditor extends EditingSupport {
408
409         private final TreeViewer viewer;
410
411         public AutomationEditor(TreeViewer viewer) {
412             super(viewer);
413             this.viewer = viewer;
414         }
415
416         @Override
417         protected boolean canEdit(Object arg0) {
418             return true;
419         }
420
421         @Override
422         protected CellEditor getCellEditor(Object element) {
423             // CellEditor is not required as the automation is in progress.
424             ResourceManager resourceManager = Activator.getDefault()
425                     .getResourceManager();
426             Resource resource = resourceManager.getCurrentResourceInSelection();
427
428             if (null == resource) {
429                 return null;
430             }
431
432             if (resource instanceof CollectionResource) {
433                 return null;
434             }
435             if (((SingleResource) resource).isResourceAutomationInProgress()) {
436                 return null;
437             }
438
439             SimulatorResourceAttribute att = null;
440             if (element instanceof AttributeElement) {
441                 att = ((AttributeElement) element)
442                         .getSimulatorResourceAttribute();
443             }
444
445             if (null == att) {
446                 return null;
447             }
448
449             AttributeValue val = att.value();
450             if (null == val) {
451                 return null;
452             }
453
454             TypeInfo type = val.typeInfo();
455
456             if (type.mType == ValueType.RESOURCEMODEL
457                     || type.mType == ValueType.ARRAY) {
458                 return null;
459             }
460
461             Object parent = ((AttributeElement) element).getParent();
462             if (null != parent && !(parent instanceof ResourceRepresentation)) {
463                 return null;
464             }
465
466             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
467         }
468
469         @Override
470         protected Object getValue(Object element) {
471             if (element instanceof AttributeElement) {
472                 return ((AttributeElement) element).isAutoUpdateInProgress();
473             }
474
475             return false;
476         }
477
478         @Override
479         protected void setValue(Object element, Object value) {
480             if (!(element instanceof AttributeElement)) {
481                 return;
482             }
483
484             ResourceManager resourceManager = Activator.getDefault()
485                     .getResourceManager();
486             // As automation depends on the current resource in selection, its
487             // presence is being checked.
488             Resource resource = resourceManager.getCurrentResourceInSelection();
489             if (null == resource) {
490                 return;
491             }
492
493             AttributeElement att = (AttributeElement) element;
494             boolean checked = (Boolean) value;
495             if (checked) {
496                 // Start the automation
497                 // Fetch the settings data
498                 List<AutomationSettingHelper> automationSettings;
499                 automationSettings = AutomationSettingHelper
500                         .getAutomationSettings(att);
501
502                 // Open the settings dialog
503                 AutomationSettingDialog dialog = new AutomationSettingDialog(
504                         viewer.getTree().getShell(), automationSettings);
505                 dialog.create();
506                 if (dialog.open() == Window.OK) {
507                     String automationType = dialog.getAutomationType();
508                     String updateFreq = dialog.getUpdateFrequency();
509
510                     AutoUpdateType autoType = AutoUpdateType
511                             .valueOf(automationType);
512                     int updFreq = Utility
513                             .getUpdateIntervalFromString(updateFreq);
514                     int autoId = resourceManager.startAutomation(
515                             (SingleResource) resource, att, autoType, updFreq);
516                     if (autoId == -1) {
517                         MessageDialog.openInformation(Display.getDefault()
518                                 .getActiveShell(), "Automation Status",
519                                 "Automation start failed!!");
520                     } else {
521                         // viewer.update(element, null);
522                     }
523                 }
524             } else {
525                 // Stop the automation
526                 resourceManager.stopAutomation((SingleResource) resource, att,
527                         att.getAutoUpdateId());
528                 MessageDialog.openInformation(Display.getDefault()
529                         .getActiveShell(), "Automation Status",
530                         "Automation stopped.");
531                 // viewer.update(element, null);
532             }
533         }
534     }
535 }