Fix for SVACE reported issues on Simulator Java layer.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / MetaPropertiesView.java
index 25b4e12..d5fd2d6 100644 (file)
 
 package oic.simulator.serviceprovider.view;
 
-import java.util.List;
-
-import oic.simulator.serviceprovider.Activator;
-import oic.simulator.serviceprovider.listener.ISelectionChangedUIListener;
-import oic.simulator.serviceprovider.manager.ResourceManager;
-import oic.simulator.serviceprovider.manager.UiListenerHandler;
-import oic.simulator.serviceprovider.model.Device;
-import oic.simulator.serviceprovider.model.MetaProperty;
-import oic.simulator.serviceprovider.model.Resource;
-import oic.simulator.serviceprovider.utils.Constants;
-
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.CellEditor;
-import org.eclipse.jface.viewers.ColumnLabelProvider;
 import org.eclipse.jface.viewers.EditingSupport;
 import org.eclipse.jface.viewers.IStructuredContentProvider;
 import org.eclipse.jface.viewers.StyledCellLabelProvider;
@@ -38,7 +26,10 @@ import org.eclipse.jface.viewers.TableViewerColumn;
 import org.eclipse.jface.viewers.TextCellEditor;
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.jface.viewers.ViewerCell;
+import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.GridData;
@@ -47,37 +38,60 @@ import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.part.ViewPart;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 import org.oic.simulator.SimulatorException;
 
+import oic.simulator.serviceprovider.Activator;
+import oic.simulator.serviceprovider.listener.ISelectionChangedListener;
+import oic.simulator.serviceprovider.manager.ResourceManager;
+import oic.simulator.serviceprovider.manager.UiListenerHandler;
+import oic.simulator.serviceprovider.model.MetaProperty;
+import oic.simulator.serviceprovider.model.Resource;
+import oic.simulator.serviceprovider.model.SingleResource;
+import oic.simulator.serviceprovider.utils.Constants;
+import oic.simulator.serviceprovider.utils.Utility;
+import oic.simulator.serviceprovider.view.dialogs.UpdateResourceInterfaceDialog;
+
 /**
  * This class manages and shows the meta properties view in the perspective.
  */
 public class MetaPropertiesView extends ViewPart {
 
-    public static final String          VIEW_ID       = "oic.simulator.serviceprovider.view.metaproperties";
+    public static final String        VIEW_ID       = "oic.simulator.serviceprovider.view.metaproperties";
+
+    private TableViewer               tableViewer;
+
+    private final String[]            columnHeaders = { "Property", "Value" };
 
-    private TableViewer                 tableViewer;
+    private final Integer[]           columnWidth   = { 150, 150 };
 
-    private final String[]              columnHeaders = { "Property", "Value" };
+    private ISelectionChangedListener resourceSelectionChangedListener;
 
-    private final Integer[]             columnWidth   = { 150, 150 };
+    private ResourceManager           resourceManagerRef;
 
-    private ISelectionChangedUIListener resourceSelectionChangedListener;
+    private Set<String>               updatedIfSet;
 
-    private ResourceManager             resourceManagerRef;
+    private List<MetaProperty>        properties;
 
-    private List<MetaProperty>          properties;
+    private boolean                   enable_edit;
+    private Button                    editBtn;
+    private Button                    cancelBtn;
 
-    private boolean                     enable_edit;
-    private Button                      editBtn;
-    private Button                      cancelBtn;
+    private Map<String, String>       ifTypes;
 
     public MetaPropertiesView() {
 
         resourceManagerRef = Activator.getDefault().getResourceManager();
 
-        resourceSelectionChangedListener = new ISelectionChangedUIListener() {
+        resourceSelectionChangedListener = new ISelectionChangedListener() {
 
             @Override
             public void onResourceSelectionChange(final Resource resource) {
@@ -93,21 +107,6 @@ public class MetaPropertiesView extends ViewPart {
                     }
                 });
             }
-
-            @Override
-            public void onDeviceSelectionChange(final Device dev) {
-                Display.getDefault().asyncExec(new Runnable() {
-
-                    @Override
-                    public void run() {
-                        if (null != tableViewer) {
-                            properties = getData(dev);
-                            updateViewer(properties);
-                        }
-                        updateEditControls(dev);
-                    }
-                });
-            }
         };
     }
 
@@ -141,17 +140,22 @@ public class MetaPropertiesView extends ViewPart {
                 if (editBtn.getText().equals("Edit")) {
                     cancelBtn.setEnabled(true);
                     editBtn.setText("Save");
-                    enable_edit = true;
+                    setEnableEdit(true);
+
+                    Resource resource = resourceManagerRef
+                            .getCurrentResourceInSelection();
+                    if (null != resource) {
+                        updatedIfSet = resource.getResourceInterfaces();
+                    }
                 } else {
-                    boolean result = false;
                     Resource resourceInSelection = resourceManagerRef
                             .getCurrentResourceInSelection();
                     if (null != resourceInSelection) {
 
                         // Null Check
-                        result = resourceManagerRef.isPropertyValueInvalid(
-                                resourceInSelection, properties,
-                                Constants.RESOURCE_URI);
+                        boolean result = resourceManagerRef
+                                .isPropertyValueInvalid(resourceInSelection,
+                                        properties, Constants.RESOURCE_URI);
                         if (result) {
                             MessageDialog.openError(parent.getShell(),
                                     "Invalid Resource URI.",
@@ -169,15 +173,29 @@ public class MetaPropertiesView extends ViewPart {
                             return;
                         }
 
+                        result = resourceManagerRef.isPropertyValueInvalid(
+                                resourceInSelection, properties,
+                                Constants.RESOURCE_TYPE);
+                        if (result) {
+                            MessageDialog.openError(parent.getShell(),
+                                    "Invalid Resource Type.",
+                                    Constants.INVALID_RESOURCE_TYPE_MESSAGE);
+                            return;
+                        }
+
                         boolean update = false;
                         boolean uriChange = false;
+                        boolean typeChange = false;
                         boolean nameChange = false;
+                        boolean interfaceChange = false;
+
                         if (resourceManagerRef.isPropValueChanged(
                                 resourceInSelection, properties,
                                 Constants.RESOURCE_NAME)) {
                             update = true;
                             nameChange = true;
                         }
+
                         if (resourceManagerRef.isPropValueChanged(
                                 resourceInSelection, properties,
                                 Constants.RESOURCE_URI)) {
@@ -191,36 +209,71 @@ public class MetaPropertiesView extends ViewPart {
 
                             update = true;
                             uriChange = true;
+                        }
 
-                            if (resourceManagerRef
-                                    .isResourceStarted(resourceInSelection)) {
-                                update = MessageDialog.openQuestion(
-                                        parent.getShell(), "Save Details",
-                                        "Resource will be restarted to take the changes."
-                                                + " Do you want to continue?");
-                                if (!update) {
-                                    return;
+                        if (resourceManagerRef.isPropValueChanged(
+                                resourceInSelection, properties,
+                                Constants.RESOURCE_TYPE)) {
+                            update = true;
+                            typeChange = true;
+                        }
+                        // Checking whether any changes made in resource
+                        // interfaces by
+                        // comparing the current interface set and updated
+                        // interface set.
+                        Set<String> curIfSet = resourceInSelection
+                                .getResourceInterfaces();
+                        if (null != curIfSet && null != updatedIfSet) {
+                            if (curIfSet.size() != updatedIfSet.size()) {
+                                update = true;
+                                interfaceChange = true;
+                            } else {
+                                Iterator<String> itr = updatedIfSet.iterator();
+                                while (itr.hasNext()) {
+                                    if (!curIfSet.contains(itr.next())) {
+                                        update = true;
+                                        interfaceChange = true;
+                                        break;
+                                    }
                                 }
                             }
                         }
                         if (update) {
+                            if (uriChange || typeChange || interfaceChange) {
+                                if (resourceManagerRef
+                                        .isResourceStarted(resourceInSelection)) {
+                                    update = MessageDialog.openQuestion(
+                                            parent.getShell(),
+                                            "Save Details",
+                                            "Resource will be restarted to take the changes."
+                                                    + " Do you want to continue?");
+                                    if (!update) {
+                                        return;
+                                    }
+                                }
+                            }
                             try {
-                                result = Activator
-                                        .getDefault()
-                                        .getResourceManager()
-                                        .updateResourceProperties(
-                                                resourceManagerRef
-                                                        .getCurrentResourceInSelection(),
-                                                properties, uriChange,
-                                                nameChange);
+                                if (uriChange || nameChange || typeChange)
+                                    result = Activator
+                                            .getDefault()
+                                            .getResourceManager()
+                                            .updateResourceProperties(
+                                                    resourceManagerRef
+                                                            .getCurrentResourceInSelection(),
+                                                    properties, uriChange,
+                                                    nameChange, typeChange);
+                                if (interfaceChange)
+                                    result = Activator
+                                            .getDefault()
+                                            .getResourceManager()
+                                            .updateResourceInterfaces(
+                                                    resourceInSelection,
+                                                    updatedIfSet);
+
                             } catch (SimulatorException ex) {
                                 result = false;
                             }
-                            if (result) {
-                                MessageDialog.openInformation(
-                                        parent.getShell(), "Operation status",
-                                        "Resource properties updated.");
-                            } else {
+                            if (!result) {
                                 MessageDialog.openInformation(
                                         parent.getShell(), "Operation status",
                                         "Failed to update the resource properties.");
@@ -231,29 +284,10 @@ public class MetaPropertiesView extends ViewPart {
                                 updateViewer(properties);
                             }
                         }
-                    } else {
-                        Device dev = resourceManagerRef
-                                .getCurrentDeviceInSelection();
-
-                        // Null check
-                        result = resourceManagerRef.isPropertyValueInvalid(dev,
-                                properties, Constants.DEVICE_NAME);
-                        if (result) {
-                            MessageDialog.openError(parent.getShell(),
-                                    "Invalid Input", "Device Name is invalid.");
-                            return;
-                        }
-
-                        if (resourceManagerRef.isPropValueChanged(dev,
-                                properties, Constants.DEVICE_NAME)) {
-                            resourceManagerRef.updateDeviceProperties(dev,
-                                    properties);
-                        }
-
                     }
                     cancelBtn.setEnabled(false);
                     editBtn.setText("Edit");
-                    enable_edit = false;
+                    setEnableEdit(false);
                 }
             }
         });
@@ -271,21 +305,29 @@ public class MetaPropertiesView extends ViewPart {
                         .getCurrentResourceInSelection();
                 if (null != res) {
                     properties = getData(res);
-                } else {
-                    Device dev = resourceManagerRef
-                            .getCurrentDeviceInSelection();
-                    if (null != dev) {
-                        properties = getData(dev);
-                    }
                 }
                 updateViewer(properties);
 
                 cancelBtn.setEnabled(false);
                 editBtn.setText("Edit");
-                enable_edit = false;
+                setEnableEdit(false);
+                if (null != updatedIfSet)
+                    updatedIfSet.clear();
             }
         });
 
+        // Get the supported interfaces.
+        Map<String, String> ifTypesSupported = Utility
+                .getResourceInterfaces(SingleResource.class);
+        if (null != ifTypesSupported && !ifTypesSupported.isEmpty()) {
+            ifTypes = new HashMap<String, String>();
+            String key;
+            for (Map.Entry<String, String> entry : ifTypesSupported.entrySet()) {
+                key = entry.getValue() + " (" + entry.getKey() + ")";
+                ifTypes.put(key, entry.getKey());
+            }
+        }
+
         addManagerListeners();
 
         // Check whether there is any resource selected already
@@ -302,7 +344,7 @@ public class MetaPropertiesView extends ViewPart {
 
             if (editBtn.getText().equals("Save")) {
                 editBtn.setText("Edit");
-                enable_edit = false;
+                setEnableEdit(false);
             }
 
             if (null == obj) {
@@ -324,34 +366,23 @@ public class MetaPropertiesView extends ViewPart {
         }
     }
 
-    private List<MetaProperty> getData(Device dev) {
-        if (null != dev) {
-            List<MetaProperty> metaPropertyList = resourceManagerRef
-                    .getMetaProperties(dev);
-            return metaPropertyList;
-        } else {
-            return null;
-        }
-    }
-
     private void updateViewer(List<MetaProperty> metaPropertyList) {
         if (null != tableViewer) {
             Table tbl = tableViewer.getTable();
+            if (tbl.isDisposed()) {
+                return;
+            }
             if (null != metaPropertyList) {
                 tableViewer.setInput(metaPropertyList.toArray());
-                if (!tbl.isDisposed()) {
-                    tbl.setLinesVisible(true);
-                }
+                tbl.setLinesVisible(true);
             } else {
-                if (!tbl.isDisposed()) {
-                    tbl.removeAll();
-                    tbl.setLinesVisible(false);
-                }
+                tbl.removeAll();
+                tbl.setLinesVisible(false);
             }
         }
     }
 
-    public void createColumns(TableViewer tableViewer) {
+    public void createColumns(final TableViewer tableViewer) {
         TableViewerColumn propName = new TableViewerColumn(tableViewer,
                 SWT.NONE);
         propName.getColumn().setWidth(columnWidth[0]);
@@ -369,15 +400,12 @@ public class MetaPropertiesView extends ViewPart {
                 SWT.NONE);
         propValue.getColumn().setWidth(columnWidth[1]);
         propValue.getColumn().setText(columnHeaders[1]);
-        propValue.setLabelProvider(new ColumnLabelProvider() {
+        propValue.setLabelProvider(new StyledCellLabelProvider() {
             @Override
-            public String getText(Object element) {
-                MetaProperty prop = (MetaProperty) element;
-                if (null != prop) {
-                    return prop.getPropValue();
-                } else {
-                    return "";
-                }
+            public void update(ViewerCell cell) {
+                MetaProperty prop = (MetaProperty) cell.getElement();
+                cell.setText(prop.getPropValue());
+                super.update(cell);
             }
         });
         propValue.setEditingSupport(new PropValueEditor(tableViewer));
@@ -388,7 +416,8 @@ public class MetaPropertiesView extends ViewPart {
                 resourceSelectionChangedListener);
     }
 
-    class PropertycontentProvider implements IStructuredContentProvider {
+    private static class PropertycontentProvider implements
+            IStructuredContentProvider {
 
         @Override
         public void dispose() {
@@ -430,16 +459,65 @@ public class MetaPropertiesView extends ViewPart {
         }
 
         @Override
-        protected CellEditor getCellEditor(Object element) {
-            if (!enable_edit) {
+        protected CellEditor getCellEditor(final Object element) {
+            if (!getEnableEdit()) {
                 return null;
             }
-            // Disabling edit for resource type
             String propName = ((MetaProperty) element).getPropName();
-            if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) {
-                return null;
-            }
             CellEditor editor = new TextCellEditor(viewer.getTable());
+            if (null != propName && propName.equals(Constants.INTERFACE_TYPES)) {
+                editor.setStyle(SWT.READ_ONLY);
+                final Text txt = (Text) editor.getControl();
+                txt.addModifyListener(new ModifyListener() {
+                    @Override
+                    public void modifyText(ModifyEvent e) {
+                        if (null == updatedIfSet) {
+                            return;
+                        }
+                        // Form the result set of interfaces with check-box that
+                        // will be shown in the dialog for editing.
+                        Map<String, String> curResInterfaces = new HashMap<String, String>();
+                        for (Map.Entry<String, String> entry : ifTypes
+                                .entrySet()) {
+                            if (updatedIfSet.contains(entry.getValue())) {
+                                curResInterfaces.put(entry.getKey(),
+                                        entry.getValue());
+                            }
+                        }
+
+                        // Show the dialog for editing the resource interfaces.
+                        UpdateResourceInterfaceDialog ad = new UpdateResourceInterfaceDialog(
+                                Display.getDefault().getActiveShell(),
+                                curResInterfaces, ifTypes);
+                        if (ad.open() == Window.OK) {
+                            // Update the local copy of the current resource
+                            // interfaces to keep the state for save operation.
+                            updatedIfSet.clear();
+                            StringBuilder newPropValue = new StringBuilder();
+                            for (Map.Entry<String, String> entry : curResInterfaces
+                                    .entrySet()) {
+                                if (!newPropValue.toString().isEmpty()) {
+                                    newPropValue.append(", ");
+                                }
+                                String value = ifTypes.get(entry.getKey());
+                                newPropValue.append(value);
+
+                                updatedIfSet.add(value);
+                            }
+                            // Update the model
+                            MetaProperty prop = (MetaProperty) element;
+                            prop.setPropValue(newPropValue.toString());
+                            // Update the viewer in a separate UI thread.
+                            Display.getDefault().asyncExec(new Runnable() {
+                                @Override
+                                public void run() {
+                                    viewer.refresh(element, true);
+                                }
+                            });
+                        }
+                    }
+                });
+            }
             return editor;
         }
 
@@ -451,10 +529,20 @@ public class MetaPropertiesView extends ViewPart {
         @Override
         protected void setValue(Object element, Object value) {
             MetaProperty prop = (MetaProperty) element;
+            if (prop.getPropName().equals(Constants.INTERFACE_TYPES)) {
+                return;
+            }
             prop.setPropValue(String.valueOf(value));
             viewer.update(element, null);
         }
+    }
+
+    private synchronized Boolean getEnableEdit() {
+        return enable_edit;
+    }
 
+    private synchronized void setEnableEdit(boolean value) {
+        enable_edit = value;
     }
 
     @Override