Work in progress for Bug 2550
authorEugene Ostroukhov <eugeneo@symbian.org>
Wed, 09 Jun 2010 11:19:59 -0700
changeset 361 08e920dda3d9
parent 360 c1c74d169148
child 362 f8c2079946be
Work in progress for Bug 2550
org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/DebugUtil.java
org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/ResourcesChangeListener.java
org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/WRTProjectWorkspaceBridge.java
org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/model/ResourceManager.java
--- a/org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/DebugUtil.java	Tue Jun 08 15:34:13 2010 -0700
+++ b/org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/DebugUtil.java	Wed Jun 09 11:19:59 2010 -0700
@@ -36,7 +36,28 @@
         return new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, message, exeption));
     }
 
+    public static IProject getProject(ILaunch configuration) {
+        try {
+            if (WidgetLaunchDelegate.ID.equals(configuration.getLaunchConfiguration().getType().getIdentifier())) {
+                String projectName = configuration.getLaunchConfiguration().getAttribute(IConstants.PROP_PROJECT_NAME,
+                        (String) null);
+                if (projectName != null) {
+                    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+                    if (project.isAccessible()) {
+                        return project;
+                    }
+                }
+            }
+        } catch (CoreException e) {
+            Activator.log(e);
+        }
+        return null;
+    }
+
     public static IProject getProject(ILaunchConfiguration configuration) throws CoreException {
+        if (!WidgetLaunchDelegate.ID.equals(configuration.getType().getIdentifier())) {
+            return null;
+        }
         String projectName = configuration.getAttribute(IConstants.PROP_PROJECT_NAME, (String) null);
         if (projectName == null) {
             throw createCoreException("Project is not selected", null);
--- a/org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/ResourcesChangeListener.java	Tue Jun 08 15:34:13 2010 -0700
+++ b/org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/ResourcesChangeListener.java	Wed Jun 09 11:19:59 2010 -0700
@@ -85,4 +85,150 @@
         return false;
     }
 
+    //    // TODO Progress indicator - ... files updated of ...
+    //
+    //    public void resourceChanged(IResourceChangeEvent event) {
+    //        try {
+    //            if (event.getDelta() != null) {
+    //
+    //                processDelta(event.getDelta());
+    //            } else {
+    //                // TODO Can't refresh
+    //            }
+    //        } catch (CoreException e) {
+    //            Activator.log(e);
+    //        }
+    //
+    //    }
+    //
+    //    private static final class DeltaProcessor implements IResourceDeltaVisitor {
+    //        private final Map<IProject, DebugTargetImpl> targets;
+    //        private final Map<IFile, Script> updateList = new HashMap<IFile, Script>();
+    //        private boolean hasUnupdatables = false;
+    //
+    //        public DeltaProcessor(Map<IProject, DebugTargetImpl> targets) {
+    //            this.targets = targets;
+    //        }
+    //
+    //        public boolean visit(IResourceDelta delta) throws CoreException {
+    //            final IResource resource = delta.getResource();
+    //            switch (resource.getType()) {
+    //            case IResource.PROJECT:
+    //                return targets.containsKey(resource);
+    //            case IResource.FILE:
+    //                processFile((IFile) resource, targets.get(resource.getProject()));
+    //                return false;
+    //            default:
+    //                return true;
+    //            }
+    //        }
+    //
+    //        private void processFile(final IFile resource, final DebugTargetImpl debugTargetImpl) throws CoreException {
+    //            if (!ProjectUtils.isExcluded(resource)) {
+    //                final VmResource vmResource = debugTargetImpl.getVmResource(resource);
+    //                if (vmResource != null) {
+    //                    updateList.put(resource, vmResource.getScript());
+    //                } else {
+    //                    if (!JavaScriptCore.isJavaScriptLikeFileName(resource.getName())) {
+    //                        hasUnupdatables = true;
+    //                    }
+    //                }
+    //            }
+    //        }
+    //
+    //        public Map<IFile, Script> getUpdateList() {
+    //            return updateList;
+    //        }
+    //
+    //        public boolean hasUnupdatables() {
+    //            return hasUnupdatables;
+    //        }
+    //    }
+    //
+    //    private void processDelta(final IResourceDelta delta) throws CoreException {
+    //        final ILaunch[] launches = DebugPlugin.getDefault().getLaunchManager().getLaunches();
+    //        final Map<IProject, DebugTargetImpl> targets = new HashMap<IProject, DebugTargetImpl>();
+    //        for (ILaunch launch : launches) {
+    //            final IDebugTarget target = launch.getDebugTarget();
+    //            if (!launch.isTerminated() && target instanceof DebugTargetImpl) {
+    //                IProject project = DebugUtil.getProject(launch);
+    //                if (project != null) {
+    //                    targets.put(project, (DebugTargetImpl) target);
+    //                }
+    //            }
+    //        }
+    //
+    //        DeltaProcessor processor = new DeltaProcessor(targets);
+    //        delta.accept(processor);
+    //
+    //        if (processor.getUpdateList().size() > 0) {
+    //            final Collection<IStatus> statuses = new LinkedList<IStatus>();
+    //            for (Entry<IFile, Script> entry : processor.getUpdateList().entrySet()) {
+    //                statuses.add(updateScript(entry.getValue(), entry.getKey()));
+    //            }
+    //        } else if (processor.hasUnupdatables()) {
+    //
+    //        }
+    //    }
+    //
+    //    private IStatus updateScript(Script script, final IFile file) {
+    //        UpdatableScript updatableScript = LiveEditExtension.castToUpdatableScript(script);
+    //
+    //        if (updatableScript == null) {
+    //            throw new RuntimeException();
+    //        }
+    //
+    //        byte[] fileData;
+    //        try {
+    //            fileData = readFileContents(file);
+    //        } catch (IOException e) {
+    //            throw new RuntimeException(e);
+    //        } catch (CoreException e) {
+    //            throw new RuntimeException(e);
+    //        }
+    //        final IStatus[] res = new IStatus[1];
+    //        // We are using default charset here like usually.
+    //        String newSource = new String(fileData);
+    //
+    //        UpdatableScript.UpdateCallback callback = new UpdatableScript.UpdateCallback() {
+    //            public void success(Object report) {
+    //                res[0] = new Status(
+    //                        IStatus.OK,
+    //                        ChromiumDebugPlugin.PLUGIN_ID,
+    //                        String.format(
+    //                                "Script %s was successfully updated on remote: %s", file.getProjectRelativePath().toString(), report)); //$NON-NLS-1$
+    //            }
+    //
+    //            public void failure(String message) {
+    //                res[0] = new Status(IStatus.ERROR, ChromiumDebugPlugin.PLUGIN_ID, String.format(
+    //                        "Script %s cannot be updated: %s", file.getProjectRelativePath().toString(), message)); //$NON-NLS-1$
+    //            }
+    //        };
+    //
+    //        updatableScript.setSourceOnRemote(newSource, callback, null);
+    //        return res[0];
+    //    }
+    //
+    //    private static byte[] readFileContents(IFile file) throws IOException, CoreException {
+    //        InputStream inputStream = file.getContents();
+    //        try {
+    //            return readBytes(inputStream);
+    //        } finally {
+    //            inputStream.close();
+    //        }
+    //    }
+    //
+    //    private static byte[] readBytes(InputStream inputStream) throws IOException {
+    //        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+    //        byte[] array = new byte[1024];
+    //        while (true) {
+    //            int len = inputStream.read(array);
+    //            if (len == -1) {
+    //                break;
+    //            }
+    //            buffer.write(array, 0, len);
+    //        }
+    //        return buffer.toByteArray();
+    //    }
+
 }
--- a/org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/WRTProjectWorkspaceBridge.java	Tue Jun 08 15:34:13 2010 -0700
+++ b/org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/launch/WRTProjectWorkspaceBridge.java	Wed Jun 09 11:19:59 2010 -0700
@@ -8,6 +8,7 @@
 import org.chromium.debug.core.model.ChromiumLineBreakpoint;
 import org.chromium.debug.core.model.DebugTargetImpl;
 import org.chromium.debug.core.model.VmResource;
+import org.chromium.debug.core.model.VmResourceId;
 import org.chromium.debug.core.model.WorkspaceBridge;
 import org.chromium.sdk.CallFrame;
 import org.chromium.sdk.JavascriptVm;
@@ -139,9 +140,34 @@
 
     private final ISourceLookupDirector sourceLocator;
 
-    public VmResource findVmResourceFromWorkspaceFile(IFile resource) throws CoreException {
-        System.out.println(resource);
-        return null;
+    public VmResource findVmResourceFromWorkspaceFile(IFile file) throws CoreException {
+        return new VmResourceImpl(resourceManager.findVmResource(file), resourceManager.getScript(file), file.getName());
+    }
+
+    private static final class VmResourceImpl implements VmResource {
+        private final String name;
+        private final Script script;
+        private final VmResourceId id;
+
+        public VmResourceImpl(VmResourceId id, Script script, String name) {
+            super();
+            this.id = id;
+            this.script = script;
+            this.name = name;
+        }
+
+        public VmResourceId getId() {
+            return id;
+        }
+
+        public Script getScript() {
+            return script;
+        }
+
+        public String getFileName() {
+            return name;
+        }
+
     }
 
     public void reloadScript(Script script) {
--- a/org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/model/ResourceManager.java	Tue Jun 08 15:34:13 2010 -0700
+++ b/org.symbian.tools.wrttools.debug.core/src/org/symbian/tools/wrttools/debug/internal/model/ResourceManager.java	Wed Jun 09 11:19:59 2010 -0700
@@ -65,4 +65,8 @@
         Script script = resourceToScript.get(resource);
         return script != null ? VmResourceId.forScript(script) : null;
     }
+
+    public Script getScript(IFile file) {
+        return resourceToScript.get(file);
+    }
 }