Bug 2534 - Project rename isn't complete
authorEugene Ostroukhov <eugeneo@symbian.org>
Mon, 07 Jun 2010 10:50:23 -0700
changeset 348 4cd68ab4c1f2
parent 347 6c48d84d3490
child 349 59ccacde8910
Bug 2534 - Project rename isn't complete
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/http/WorkspaceResourcesServlet.java
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/PreviewView.java
org.symbian.tools.wrttools/META-INF/MANIFEST.MF
org.symbian.tools.wrttools/plugin.xml
org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/project/ProjectRenameParticipant.java
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/http/WorkspaceResourcesServlet.java	Fri Jun 04 16:25:50 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/http/WorkspaceResourcesServlet.java	Mon Jun 07 10:50:23 2010 -0700
@@ -242,10 +242,8 @@
             if (stream != null) {
                 copyData(stream, resp.getOutputStream());
             } else {
-                PreviewerPlugin.log(String.format("Resource %s was not found", req.getPathInfo()), null);
                 resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
             }
-
         } catch (PreviewerException e) {
             PreviewerPlugin.log(e);
             throw new ServletException(e);
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java	Fri Jun 04 16:25:50 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java	Mon Jun 07 10:50:23 2010 -0700
@@ -217,12 +217,14 @@
     }
 
     public synchronized void projectRenamed(IPath newPath) {
-        project = ResourcesPlugin.getWorkspace().getRoot().getProject(newPath.lastSegment());
-        asyncExec(new Runnable() {
-            public void run() {
-                browser.dispose();
-                addBrowser();
-            }
-        });
+        if (!isDisposed()) {
+            project = ResourcesPlugin.getWorkspace().getRoot().getProject(newPath.lastSegment());
+            asyncExec(new Runnable() {
+                public void run() {
+                    browser.dispose();
+                    addBrowser();
+                }
+            });
+        }
     }
 }
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/PreviewView.java	Fri Jun 04 16:25:50 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/PreviewView.java	Mon Jun 07 10:50:23 2010 -0700
@@ -10,8 +10,8 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Properties;
-import java.util.Map.Entry;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
@@ -261,10 +261,12 @@
 
     public synchronized void projectRenamed(IProject project, IPath newPath) {
         IPreviewPage page = projectToPage.remove(project);
-        Boolean refresh = autorefresh.remove(project);
-        page.projectRenamed(newPath);
-        projectToPage.put(page.getProject(), page);
-        autorefresh.put(page.getProject(), refresh);
+        if (page != null) {
+            Boolean refresh = autorefresh.remove(project);
+            page.projectRenamed(newPath);
+            projectToPage.put(page.getProject(), page);
+            autorefresh.put(page.getProject(), refresh);
+        }
     }
 
 }
--- a/org.symbian.tools.wrttools/META-INF/MANIFEST.MF	Fri Jun 04 16:25:50 2010 -0700
+++ b/org.symbian.tools.wrttools/META-INF/MANIFEST.MF	Mon Jun 07 10:50:23 2010 -0700
@@ -24,7 +24,8 @@
  org.eclipse.ui.navigator.resources;bundle-version="3.4.0",
  org.w3c.css;bundle-version="1.0.0",
  org.eclipse.core.expressions;bundle-version="3.4.101",
- org.eclipse.wst.xml.ui;bundle-version="1.1.2"
+ org.eclipse.wst.xml.ui;bundle-version="1.1.2",
+ org.eclipse.ltk.core.refactoring
 Bundle-RequiredExecutionEnvironment: J2SE-1.5,
  JavaSE-1.6
 Bundle-ActivationPolicy: lazy
--- a/org.symbian.tools.wrttools/plugin.xml	Fri Jun 04 16:25:50 2010 -0700
+++ b/org.symbian.tools.wrttools/plugin.xml	Mon Jun 07 10:50:23 2010 -0700
@@ -883,5 +883,27 @@
     <inferenceProvider
           class="org.symbian.tools.wrttools.core.libraries.jsdt.PhoneGapInferrenceProvider">
     </inferenceProvider>
+ </extension>
+ <extension
+       point="org.eclipse.ltk.core.refactoring.renameParticipants">
+    <renameParticipant
+          class="org.symbian.tools.wrttools.core.project.ProjectRenameParticipant"
+          id="org.symbian.tools.wrttools.renameParticipant1"
+          name="WRT Projects Rename Participant">
+    <enablement>
+      <with variable="affectedNatures">
+        <iterate operator="or">
+          <equals value="org.symbian.tools.wrttools.WidgetProjectNature"/>
+        </iterate>
+      </with>
+      <with variable="element">
+         <adapt
+               type="org.eclipse.core.resources.IProject">
+         </adapt>
+        
+      </with>
+    </enablement>
+          
+    </renameParticipant>
  </extension>
 </plugin>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/project/ProjectRenameParticipant.java	Mon Jun 07 10:50:23 2010 -0700
@@ -0,0 +1,174 @@
+/**
+ * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Symbian Foundation - initial contribution.
+ * Contributors:
+ * Description:
+ * Overview:
+ * Details:
+ * Platforms/Drives/Compatibility:
+ * Assumptions/Requirement/Pre-requisites:
+ * Failures and causes:
+ */
+package org.symbian.tools.wrttools.core.project;
+
+import java.io.IOException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.TextFileChange;
+import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
+import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
+import org.eclipse.text.edits.MultiTextEdit;
+import org.eclipse.text.edits.ReplaceEdit;
+import org.eclipse.text.edits.TextEdit;
+import org.eclipse.wst.sse.core.StructuredModelManager;
+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
+import org.symbian.tools.wrttools.Activator;
+import org.symbian.tools.wrttools.util.CoreUtil;
+import org.symbian.tools.wrttools.util.ProjectUtils;
+import org.symbian.tools.wrttools.util.Util;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+@SuppressWarnings("restriction")
+public class ProjectRenameParticipant extends RenameParticipant {
+    private IProject project;
+
+    @Override
+    protected boolean initialize(Object element) {
+        if (element instanceof IProject && (ProjectUtils.hasWrtNature((IProject) element))) {
+            project = (IProject) element;
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public String getName() {
+        return "Mobile Web Project Rename Participant";
+    }
+
+    @Override
+    public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context)
+            throws OperationCanceledException {
+        return null;
+    }
+
+    @Override
+    public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
+        final IFile file = project.getFile(CoreUtil.METADATA_FILE);
+        if (file.isAccessible()) {
+            IStructuredModel model = null;
+            try {
+                model = StructuredModelManager.getModelManager().getModelForRead(file);
+                if (model != null) {
+                    final TextEdit change = getAttributesChange(model);
+                    if (change != null) {
+                        IFile newFile = ResourcesPlugin.getWorkspace().getRoot()
+                                .getFile(new Path(getArguments().getNewName()).append(file.getProjectRelativePath()));
+                        TextFileChange fileChange = new TextFileChange("Rename application", newFile);
+                        fileChange.setEdit(change);
+                        return fileChange;
+                    } else {
+                        return null;
+                    }
+                }
+            } catch (IOException e) {
+                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot rename project", e));
+            } finally {
+                if (model != null) {
+                    model.releaseFromRead();
+                }
+            }
+        }
+        return null;
+    }
+
+    private TextEdit getAttributesChange(IStructuredModel model) {
+        MultiTextEdit edit = new MultiTextEdit();
+        IDOMDocument document = ((IDOMModel) model).getDocument();
+        Element element = document.getDocumentElement();
+        if (element != null) {
+            NodeList dict = element.getElementsByTagName("dict");
+            if (dict.getLength() == 1) {
+                NodeList children = dict.item(0).getChildNodes();
+                for (int i = 0; i < children.getLength() - 2; i++) { // Note - we only care about name-value pairs, so we do not to iterate to last element. Also remember there's text node between elements
+                    final String key = getKey(children.item(i));
+                    if ("DisplayName".equals(key)) {
+                        final IDOMText text = getTextNode(children.item(i + 2));
+                        if (text != null) {
+                            final String val = text.getNodeValue();
+                            if (val.trim().equals(project.getName())) {
+                                edit.addChild(new ReplaceEdit(text.getStartOffset() + val.indexOf(project.getName()),
+                                        project.getName().length(), getArguments().getNewName()));
+                            }
+                        }
+                    } else if ("Identifier".equals(key)) {
+                        final IDOMText text = getTextNode(children.item(i + 2));
+                        if (text != null) {
+                            final String val = text.getNodeValue();
+                            final String idPart = Util.removeSpaces(project.getName());
+                            int offset = val.indexOf(idPart);
+                            if (offset >= 0) {
+                                edit.addChild(new ReplaceEdit(text.getStartOffset() + offset, idPart.length(), Util
+                                        .removeSpaces(getArguments().getNewName())));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return edit.getChildrenSize() > 0 ? edit : null;
+    }
+
+    private IDOMText getTextNode(final Node value) {
+        if (value.getNodeType() == Node.ELEMENT_NODE && "string".equals(value.getNodeName())) {
+            NodeList childNodes = value.getChildNodes();
+            if (childNodes.getLength() == 1 && childNodes.item(0).getNodeType() == Node.TEXT_NODE) {
+                return (IDOMText) childNodes.item(0);
+            }
+        }
+        return null;
+    }
+
+    private String getKey(final Node node) {
+        if (node.getNodeType() == Node.ELEMENT_NODE) {
+            if ("key".equals(node.getNodeName())) {
+                return getNodeValue(node);
+            }
+        }
+        return null;
+    }
+
+    private String getNodeValue(final Node node) {
+        final NodeList childNodes = node.getChildNodes();
+        if (childNodes.getLength() == 1) {
+            final Node contents = childNodes.item(0);
+            if (contents.getNodeType() == Node.TEXT_NODE) {
+                return contents.getNodeValue().trim();
+            }
+        }
+        return null;
+    }
+
+}