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