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
index 5b8e299..b9de9d2 100644 (file)
@@ -20,9 +20,13 @@ import java.util.List;
 
 import oic.simulator.serviceprovider.Activator;
 import oic.simulator.serviceprovider.manager.ResourceManager;
-import oic.simulator.serviceprovider.resource.AutomationSettingHelper;
-import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
-import oic.simulator.serviceprovider.resource.SimulatorResource;
+import oic.simulator.serviceprovider.model.AttributeElement;
+import oic.simulator.serviceprovider.model.AutomationSettingHelper;
+import oic.simulator.serviceprovider.model.CollectionResource;
+import oic.simulator.serviceprovider.model.Resource;
+import oic.simulator.serviceprovider.model.ResourceRepresentation;
+import oic.simulator.serviceprovider.model.SingleResource;
+import oic.simulator.serviceprovider.utils.AttributeValueBuilder;
 import oic.simulator.serviceprovider.utils.Utility;
 import oic.simulator.serviceprovider.view.dialogs.AutomationSettingDialog;
 
@@ -31,7 +35,8 @@ import org.eclipse.jface.viewers.CellEditor;
 import org.eclipse.jface.viewers.CheckboxCellEditor;
 import org.eclipse.jface.viewers.ComboBoxCellEditor;
 import org.eclipse.jface.viewers.EditingSupport;
-import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.CCombo;
@@ -39,7 +44,15 @@ import org.eclipse.swt.events.ModifyEvent;
 import org.eclipse.swt.events.ModifyListener;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.MessageBox;
-import org.oic.simulator.serviceprovider.AutomationType;
+import org.eclipse.swt.widgets.Tree;
+import org.eclipse.swt.widgets.TreeItem;
+import org.oic.simulator.AttributeProperty;
+import org.oic.simulator.AttributeValue;
+import org.oic.simulator.AttributeValue.TypeInfo;
+import org.oic.simulator.AttributeValue.ValueType;
+import org.oic.simulator.InvalidArgsException;
+import org.oic.simulator.SimulatorResourceAttribute;
+import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
 
 /**
  * This class provides editing support to the resources attributes table in the
@@ -50,23 +63,22 @@ public class AttributeEditingSupport {
     private AttributeValueEditor attValueEditor;
     private AutomationEditor     automationEditor;
 
-    public AttributeValueEditor createAttributeValueEditor(TableViewer viewer) {
+    public AttributeValueEditor createAttributeValueEditor(TreeViewer viewer) {
         attValueEditor = new AttributeValueEditor(viewer);
         return attValueEditor;
     }
 
-    public AutomationEditor createAutomationEditor(TableViewer viewer) {
+    public AutomationEditor createAutomationEditor(TreeViewer viewer) {
         automationEditor = new AutomationEditor(viewer);
         return automationEditor;
     }
 
     class AttributeValueEditor extends EditingSupport {
 
-        private final TableViewer      viewer;
-        private LocalResourceAttribute attributeInSelection;
-        private CCombo                 comboBox;
+        private final TreeViewer viewer;
+        private CCombo           comboBox;
 
-        public AttributeValueEditor(TableViewer viewer) {
+        public AttributeValueEditor(TreeViewer viewer) {
             super(viewer);
             this.viewer = viewer;
         }
@@ -77,49 +89,121 @@ public class AttributeEditingSupport {
         }
 
         @Override
-        protected CellEditor getCellEditor(Object element) {
-            attributeInSelection = (LocalResourceAttribute) element;
+        protected CellEditor getCellEditor(final Object element) {
+            ResourceManager resourceManager = Activator.getDefault()
+                    .getResourceManager();
+
+            Resource res = resourceManager.getCurrentResourceInSelection();
+            if (null == res) {
+                return null;
+            }
+
+            // If selected resource is a collection, then editor support is not
+            // required.
+            if (res instanceof CollectionResource) {
+                return null;
+            }
+
+            final SimulatorResourceAttribute attribute;
+            if (!(element instanceof AttributeElement)) {
+                return null;
+            }
+
+            AttributeElement attributeElement = ((AttributeElement) element);
+            attribute = attributeElement.getSimulatorResourceAttribute();
+            if (null == attribute) {
+                return null;
+            }
 
             // CellEditor is not required as the automation is in progress.
-            if (attributeInSelection.isAutomationInProgress()) {
+            if (attributeElement.isAutoUpdateInProgress()) {
+                return null;
+            }
+
+            final AttributeValue val = attribute.value();
+            if (null == val) {
+                return null;
+            }
+
+            final TypeInfo type = val.typeInfo();
+            if (type.mBaseType == ValueType.RESOURCEMODEL) {
+                return null;
+            }
+
+            AttributeProperty prop = attribute.property();
+            if (null == prop) {
+                return null;
+            }
+
+            if (!resourceManager.isAttHasRangeOrAllowedValues(attribute)) {
                 return null;
             }
 
             String values[] = null;
-            List<String> valueSet = attributeInSelection.getAttValues();
+            List<String> valueSet = resourceManager
+                    .getAllValuesOfAttribute(attribute);
             values = convertListToStringArray(valueSet);
 
-            ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(
-                    viewer.getTable(), values, SWT.READ_ONLY);
+            ComboBoxCellEditor comboEditor;
+            if (type.mType == ValueType.ARRAY) {
+                comboEditor = new ComboBoxCellEditor(viewer.getTree(), values);
+            } else {
+                comboEditor = new ComboBoxCellEditor(viewer.getTree(), values,
+                        SWT.READ_ONLY);
+            }
             comboBox = (CCombo) comboEditor.getControl();
             comboBox.addModifyListener(new ModifyListener() {
 
                 @Override
                 public void modifyText(ModifyEvent event) {
-                    String oldValue = String.valueOf(attributeInSelection
-                            .getAttributeValue());
+                    if (type.mType == ValueType.ARRAY) {
+                        return;
+                    }
+                    String oldValue = String.valueOf(Utility
+                            .getAttributeValueAsString(val));
                     String newValue = comboBox.getText();
                     if (!oldValue.equals(newValue)) {
-                        attributeInSelection.setAttributeValue(newValue);
-                        MessageBox dialog = new MessageBox(viewer.getTable()
-                                .getShell(), SWT.ICON_QUESTION | SWT.OK
-                                | SWT.CANCEL);
-                        dialog.setText("Confirm action");
-                        dialog.setMessage("Do you want to modify the value?");
-                        int retval = dialog.open();
-                        if (retval != SWT.OK) {
-                            attributeInSelection.setAttributeValue(oldValue);
+                        // Get the AttriuteValue from the string
+                        AttributeValue value = AttributeValueBuilder.build(
+                                newValue, type.mBaseType);
+                        TypeInfo resTypeInfo = value.typeInfo();
+                        if (null == value || type.mDepth != resTypeInfo.mDepth
+                                || type.mType != resTypeInfo.mType
+                                || type.mBaseType != resTypeInfo.mBaseType) {
+                            MessageBox dialog = new MessageBox(viewer.getTree()
+                                    .getShell(), SWT.ICON_ERROR | SWT.OK);
+                            dialog.setText("Invalid Value");
+                            dialog.setMessage("Given value is invalid");
+                            dialog.open();
                         } else {
-                            ResourceManager resourceManager;
-                            resourceManager = Activator.getDefault()
-                                    .getResourceManager();
-                            SimulatorResource resource = resourceManager
-                                    .getCurrentResourceInSelection();
-                            resourceManager.attributeValueUpdated(resource,
-                                    attributeInSelection.getAttributeName(),
-                                    newValue);
+                            updateAttributeValue(attribute, value);
+                            MessageBox dialog = new MessageBox(viewer.getTree()
+                                    .getShell(), SWT.ICON_QUESTION | SWT.OK
+                                    | SWT.CANCEL);
+                            dialog.setText("Confirm action");
+                            dialog.setMessage("Do you want to modify the value?");
+                            int retval = dialog.open();
+                            if (retval != SWT.OK) {
+                                value = AttributeValueBuilder.build(oldValue,
+                                        type.mBaseType);
+                                updateAttributeValue(attribute, value);
+                            } else {
+                                ResourceManager resourceManager;
+                                resourceManager = Activator.getDefault()
+                                        .getResourceManager();
+
+                                Resource resource = resourceManager
+                                        .getCurrentResourceInSelection();
+
+                                SimulatorResourceAttribute result = getResultantValue(value);
+
+                                resourceManager.attributeValueUpdated(
+                                        (SingleResource) resource,
+                                        result.name(), result.value());
+
+                            }
                         }
-                        viewer.update(attributeInSelection, null);
+                        viewer.update(element, null);
                         comboBox.setVisible(false);
                     }
                 }
@@ -130,9 +214,20 @@ public class AttributeEditingSupport {
         @Override
         protected Object getValue(Object element) {
             int indexOfItem = 0;
-            LocalResourceAttribute att = (LocalResourceAttribute) element;
-            String valueString = String.valueOf(att.getAttributeValue());
-            List<String> valueSet = att.getAttValues();
+            SimulatorResourceAttribute att = null;
+
+            if (element instanceof AttributeElement) {
+                att = ((AttributeElement) element)
+                        .getSimulatorResourceAttribute();
+            }
+
+            if (att == null) {
+                return 0;
+            }
+
+            String valueString = Utility.getAttributeValueAsString(att.value());
+            List<String> valueSet = Activator.getDefault().getResourceManager()
+                    .getAllValuesOfAttribute(att);
             if (null != valueSet) {
                 indexOfItem = valueSet.indexOf(valueString);
             }
@@ -144,11 +239,78 @@ public class AttributeEditingSupport {
 
         @Override
         protected void setValue(Object element, Object value) {
-            Object valueObj = attributeInSelection.getAttributeValue();
-            if (null == valueObj)
+            SimulatorResourceAttribute att = null;
+
+            if (element instanceof AttributeElement) {
+                att = ((AttributeElement) element)
+                        .getSimulatorResourceAttribute();
+            }
+
+            if (att == null) {
+                return;
+            }
+
+            AttributeValue val = att.value();
+            if (null == val) {
                 return;
-            String attValue = String.valueOf(valueObj);
-            ((LocalResourceAttribute) element).setAttributeValue(attValue);
+            }
+            TypeInfo type = val.typeInfo();
+            if (type.mType == ValueType.ARRAY) {
+                int index;
+                try {
+                    index = Integer.parseInt(String.valueOf(value));
+                } catch (NumberFormatException nfe) {
+                    index = -1;
+                }
+                if (index == -1) {
+                    String oldValue = String.valueOf(Utility
+                            .getAttributeValueAsString(val));
+                    String newValue = comboBox.getText();
+                    if (!oldValue.equals(newValue)) {
+                        // Get the AttriuteValue from the string
+                        AttributeValue attValue = AttributeValueBuilder.build(
+                                newValue, type.mBaseType);
+                        TypeInfo resTypeInfo = attValue.typeInfo();
+                        if (null == attValue
+                                || type.mDepth != resTypeInfo.mDepth
+                                || type.mType != resTypeInfo.mType
+                                || type.mBaseType != resTypeInfo.mBaseType) {
+                            MessageBox dialog = new MessageBox(viewer.getTree()
+                                    .getShell(), SWT.ICON_ERROR | SWT.OK);
+                            dialog.setText("Invalid Value");
+                            dialog.setMessage("Given value is invalid");
+                            dialog.open();
+                        } else {
+                            updateAttributeValue(att, attValue);
+                            MessageBox dialog = new MessageBox(viewer.getTree()
+                                    .getShell(), SWT.ICON_QUESTION | SWT.OK
+                                    | SWT.CANCEL);
+                            dialog.setText("Confirm action");
+                            dialog.setMessage("Do you want to modify the value?");
+                            int retval = dialog.open();
+                            if (retval != SWT.OK) {
+                                attValue = AttributeValueBuilder.build(
+                                        oldValue, type.mBaseType);
+                                updateAttributeValue(att, attValue);
+                            } else {
+                                ResourceManager resourceManager;
+                                resourceManager = Activator.getDefault()
+                                        .getResourceManager();
+
+                                Resource resource = resourceManager
+                                        .getCurrentResourceInSelection();
+
+                                SimulatorResourceAttribute result = getResultantValue(attValue);
+
+                                resourceManager.attributeValueUpdated(
+                                        (SingleResource) resource,
+                                        result.name(), result.value());
+                            }
+                        }
+                    }
+                }
+            }
+
             viewer.update(element, null);
         }
 
@@ -161,13 +323,92 @@ public class AttributeEditingSupport {
             }
             return strArr;
         }
+
+        public void updateAttributeValue(SimulatorResourceAttribute att,
+                AttributeValue value) {
+            att.setValue(value);
+
+            IStructuredSelection selection = (IStructuredSelection) viewer
+                    .getSelection();
+            if (null == selection) {
+                return;
+            }
+
+            Object obj = selection.getFirstElement();
+            if (null == obj) {
+                return;
+            }
+
+            Tree t = viewer.getTree();
+            TreeItem item = t.getSelection()[0];
+            if (null == item) {
+                return;
+            }
+
+            if (item.getData() instanceof AttributeElement) {
+                AttributeElement attributeElement = (AttributeElement) item
+                        .getData();
+                attributeElement.getSimulatorResourceAttribute()
+                        .setValue(value);
+
+                TreeItem parent = item.getParentItem();
+                if (null != parent) {
+                    Object data = parent.getData();
+                    try {
+                        ((AttributeElement) data).deepSetChildValue(att);
+                    } catch (InvalidArgsException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+
+        public SimulatorResourceAttribute getResultantValue(
+                AttributeValue newValue) {
+            AttributeValue val = null;
+            IStructuredSelection selection = (IStructuredSelection) viewer
+                    .getSelection();
+            if (null == selection) {
+                return null;
+            }
+
+            Object obj = selection.getFirstElement();
+            if (null == obj) {
+                return null;
+            }
+
+            Tree t = viewer.getTree();
+            TreeItem item = t.getSelection()[0];
+            if (null == item) {
+                return null;
+            }
+
+            SimulatorResourceAttribute result = null;
+            TreeItem parent = item.getParentItem();
+            if (null == parent) {
+                Object data = item.getData();
+                result = ((AttributeElement) data)
+                        .getSimulatorResourceAttribute();
+            } else {
+                while (parent.getParentItem() != null) {
+                    parent = parent.getParentItem();
+                }
+
+                // Parent will point to the top-level attribute of type
+                Object data = parent.getData();
+                result = ((AttributeElement) data)
+                        .getSimulatorResourceAttribute();
+            }
+
+            return result;
+        }
     }
 
     class AutomationEditor extends EditingSupport {
 
-        private final TableViewer viewer;
+        private final TreeViewer viewer;
 
-        public AutomationEditor(TableViewer viewer) {
+        public AutomationEditor(TreeViewer viewer) {
             super(viewer);
             this.viewer = viewer;
         }
@@ -182,33 +423,74 @@ public class AttributeEditingSupport {
             // CellEditor is not required as the automation is in progress.
             ResourceManager resourceManager = Activator.getDefault()
                     .getResourceManager();
-            SimulatorResource resource = resourceManager
-                    .getCurrentResourceInSelection();
-            if (null != resource && resource.isResourceAutomationInProgress()) {
+            Resource resource = resourceManager.getCurrentResourceInSelection();
+
+            if (null == resource) {
+                return null;
+            }
+
+            if (resource instanceof CollectionResource) {
+                return null;
+            }
+            if (((SingleResource) resource).isResourceAutomationInProgress()) {
+                return null;
+            }
+
+            SimulatorResourceAttribute att = null;
+            if (element instanceof AttributeElement) {
+                att = ((AttributeElement) element)
+                        .getSimulatorResourceAttribute();
+            }
+
+            if (null == att) {
+                return null;
+            }
+
+            AttributeValue val = att.value();
+            if (null == val) {
+                return null;
+            }
+
+            TypeInfo type = val.typeInfo();
+
+            if (type.mType == ValueType.RESOURCEMODEL
+                    || type.mType == ValueType.ARRAY) {
+                return null;
+            }
+
+            Object parent = ((AttributeElement) element).getParent();
+            if (null != parent && !(parent instanceof ResourceRepresentation)) {
                 return null;
             }
+
             return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
         }
 
         @Override
         protected Object getValue(Object element) {
-            LocalResourceAttribute att = (LocalResourceAttribute) element;
-            return att.isAutomationInProgress();
+            if (element instanceof AttributeElement) {
+                return ((AttributeElement) element).isAutoUpdateInProgress();
+            }
+
+            return false;
         }
 
         @Override
         protected void setValue(Object element, Object value) {
+            if (!(element instanceof AttributeElement)) {
+                return;
+            }
+
             ResourceManager resourceManager = Activator.getDefault()
                     .getResourceManager();
             // As automation depends on the current resource in selection, its
             // presence is being checked.
-            SimulatorResource resource = resourceManager
-                    .getCurrentResourceInSelection();
+            Resource resource = resourceManager.getCurrentResourceInSelection();
             if (null == resource) {
                 return;
             }
 
-            LocalResourceAttribute att = (LocalResourceAttribute) element;
+            AttributeElement att = (AttributeElement) element;
             boolean checked = (Boolean) value;
             if (checked) {
                 // Start the automation
@@ -219,34 +501,34 @@ public class AttributeEditingSupport {
 
                 // Open the settings dialog
                 AutomationSettingDialog dialog = new AutomationSettingDialog(
-                        viewer.getTable().getShell(), automationSettings);
+                        viewer.getTree().getShell(), automationSettings);
                 dialog.create();
                 if (dialog.open() == Window.OK) {
                     String automationType = dialog.getAutomationType();
                     String updateFreq = dialog.getUpdateFrequency();
 
-                    AutomationType autoType = AutomationType
+                    AutoUpdateType autoType = AutoUpdateType
                             .valueOf(automationType);
                     int updFreq = Utility
                             .getUpdateIntervalFromString(updateFreq);
-                    int autoId = resourceManager.startAutomation(resource, att,
-                            autoType, updFreq);
+                    int autoId = resourceManager.startAutomation(
+                            (SingleResource) resource, att, autoType, updFreq);
                     if (autoId == -1) {
                         MessageDialog.openInformation(Display.getDefault()
                                 .getActiveShell(), "Automation Status",
                                 "Automation start failed!!");
                     } else {
-                        viewer.update(element, null);
+                        // viewer.update(element, null);
                     }
                 }
             } else {
                 // Stop the automation
-                resourceManager.stopAutomation(resource, att,
-                        att.getAutomationId());
+                resourceManager.stopAutomation((SingleResource) resource, att,
+                        att.getAutoUpdateId());
                 MessageDialog.openInformation(Display.getDefault()
                         .getActiveShell(), "Automation Status",
                         "Automation stopped.");
-                viewer.update(element, null);
+                // viewer.update(element, null);
             }
         }
     }