Added UI Support to change resource type.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / manager / ResourceManager.java
index 3d22bd7..2d26b9b 100644 (file)
 
 package oic.simulator.serviceprovider.manager;
 
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.swt.widgets.Display;
+
+import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -27,17 +31,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.Vector;
 
-import oic.simulator.serviceprovider.Activator;
-import oic.simulator.serviceprovider.model.AttributeElement;
-import oic.simulator.serviceprovider.model.MetaProperty;
-import oic.simulator.serviceprovider.model.Resource;
-import oic.simulator.serviceprovider.model.ResourceType;
-import oic.simulator.serviceprovider.model.SingleResource;
-import oic.simulator.serviceprovider.utils.Constants;
-import oic.simulator.serviceprovider.utils.Utility;
-
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.swt.widgets.Display;
 import org.oic.simulator.ArrayProperty;
 import org.oic.simulator.AttributeProperty;
 import org.oic.simulator.AttributeProperty.Type;
@@ -64,6 +57,15 @@ import org.oic.simulator.server.SimulatorResource.ObserverListener;
 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
 import org.oic.simulator.server.SimulatorSingleResource;
 
+import oic.simulator.serviceprovider.Activator;
+import oic.simulator.serviceprovider.model.AttributeElement;
+import oic.simulator.serviceprovider.model.MetaProperty;
+import oic.simulator.serviceprovider.model.Resource;
+import oic.simulator.serviceprovider.model.ResourceType;
+import oic.simulator.serviceprovider.model.SingleResource;
+import oic.simulator.serviceprovider.utils.Constants;
+import oic.simulator.serviceprovider.utils.Utility;
+
 /**
  * This class acts as an interface between the simulator java SDK and the
  * various UI modules. It maintains all the details of resources and provides
@@ -1027,6 +1029,30 @@ public class ResourceManager {
         return true;
     }
 
+    public boolean changeResourceType(Resource resource, String newResourceType)
+            throws SimulatorException {
+        if (null == resource || null == newResourceType) {
+            return false;
+        }
+
+        if (!stopResource(resource)) {
+            return false;
+        }
+
+        String curResourceType = resource.getResourceType();
+        setResourceType(resource, newResourceType);
+
+        try {
+            if (!startResource(resource)) {
+                return false;
+            }
+        } catch (SimulatorException e) {
+            setResourceType(resource, curResourceType);
+        }
+
+        return true;
+    }
+
     public void setResourceURI(Resource resource, String newURI)
             throws SimulatorException {
         String curURI = resource.getResourceURI();
@@ -1046,9 +1072,28 @@ public class ResourceManager {
         }
     }
 
+    public void setResourceType(Resource resource, String newResourceType)
+            throws SimulatorException {
+        SimulatorResource server = resource.getSimulatorResource();
+        try {
+            server.setResourceType(newResourceType);
+            resource.setResourceType(newResourceType);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "There is an error while changing the resource Type.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
+            throw e;
+        }
+    }
+
     public boolean updateResourceProperties(Resource resource,
             List<MetaProperty> properties, boolean uriChanged,
-            boolean nameChanged) throws SimulatorException {
+            boolean nameChanged, boolean resTypeChanged)
+            throws SimulatorException {
         if (null == resource || null == properties) {
             return false;
         }
@@ -1060,6 +1105,7 @@ public class ResourceManager {
         String propValue;
         String resName = null;
         String resURI = null;
+        String resType = null;
         while (itr.hasNext()) {
             property = itr.next();
             if (null == property) {
@@ -1071,6 +1117,8 @@ public class ResourceManager {
                 resName = propValue;
             } else if (propName.equals(Constants.RESOURCE_URI)) {
                 resURI = propValue;
+            } else if (propName.equals(Constants.RESOURCE_TYPE)) {
+                resType = propValue;
             }
         }
 
@@ -1090,6 +1138,12 @@ public class ResourceManager {
             }
         }
 
+        if (resTypeChanged) {
+            if (!changeResourceType(resource, resType)) {
+                return false;
+            }
+        }
+
         return true;
     }
 
@@ -1195,6 +1249,10 @@ public class ResourceManager {
                     if (!Utility.isUriValid(value)) {
                         invalid = true;
                     }
+                } else if (propName.equals(Constants.RESOURCE_TYPE)) {
+                    if (!Utility.isResourceTypeValid(value)) {
+                        invalid = true;
+                    }
                 } else {
                     if (null == value || value.trim().isEmpty()) {
                         invalid = true;
@@ -1637,13 +1695,16 @@ public class ResourceManager {
 
     public List<String> getAllValues(DoubleProperty dblProperty,
             AttributeProperty.Type type) {
+        NumberFormat formatter = NumberFormat.getInstance();
         List<String> values = new ArrayList<String>();
 
         if (dblProperty.hasRange()) {
             double min = (double) dblProperty.min();
             double max = (double) dblProperty.max();
-            for (double iVal = min; iVal <= max; iVal = iVal + 1) {
-                values.add(String.valueOf(iVal));
+            for (double val = min; val <= max; val += 0.1) {
+                formatter.setMaximumFractionDigits(1);
+                formatter.setMinimumFractionDigits(1);
+                values.add(formatter.format(val));
             }
         } else if (dblProperty.hasValues()) {
             for (Double val : dblProperty.getValues()) {