plugins/org.symbian.tools.tmw.core/src/org/symbian/tools/tmw/core/internal/projects/LibraryAdditionConfigObject.java
changeset 480 b6d992b9b998
parent 479 518afa7c6d2f
--- a/plugins/org.symbian.tools.tmw.core/src/org/symbian/tools/tmw/core/internal/projects/LibraryAdditionConfigObject.java	Tue Aug 31 11:58:53 2010 -0700
+++ b/plugins/org.symbian.tools.tmw.core/src/org/symbian/tools/tmw/core/internal/projects/LibraryAdditionConfigObject.java	Tue Aug 31 15:21:04 2010 -0700
@@ -21,11 +21,15 @@
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
 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;
@@ -47,11 +51,19 @@
             throws CoreException {
         IFile file = project.getFile(name);
         if (!file.exists()) {
+            createContainers(file.getParent());
             file.create(contents, false, monitor);
         }
         return file;
     }
 
+    private void createContainers(IContainer container) throws CoreException {
+        if (container.getType() == IResource.FOLDER && !container.exists()) {
+            createContainers(container.getParent());
+            ((IFolder) container).create(false, true, new NullProgressMonitor());
+        }
+    }
+
     public void addIncludedJsFile(IProject project, IFile file) {
         // It is highly likely this code is extremely specific to Symbian web runtime.
         // Please open a bug and clarify the requirements
@@ -63,21 +75,25 @@
                 try {
                     model = StructuredModelManager.getModelManager().getModelForEdit(page);
                     if (model instanceof IDOMModel) {
-                        IDOMDocument document = ((IDOMModel) model).getDocument();
-                        String path = file.getProjectRelativePath().makeAbsolute().toString();
-
-                        NodeList scripts = document.getElementsByTagName("script");
+                        final IDOMDocument document = ((IDOMModel) model).getDocument();
+                        final String path = file.getProjectRelativePath().makeRelative().toString();
+                        final NodeList scripts = document.getElementsByTagName("script");
+                        boolean hasScript = false;
                         for (int i = 0; i < scripts.getLength(); i++) {
-
+                            if (path.equals(((Element) scripts.item(i)).getAttribute("src"))) {
+                                hasScript = true;
+                                break;
+                            }
                         }
-
-                        NodeList heads = document.getElementsByTagName("head");
-                        if (heads.getLength() > 0) {
-                            Element el = document.createElement("script");
-                            el.setAttribute("type", "text/javascript");
-                            el.setAttribute("src", path);
-                            heads.item(0).appendChild(el);
-                            model.save();
+                        if (!hasScript) {
+                            final NodeList heads = document.getElementsByTagName("head");
+                            if (heads.getLength() > 0) {
+                                Element el = document.createElement("script");
+                                el.setAttribute("type", "text/javascript");
+                                el.setAttribute("src", path);
+                                heads.item(0).appendChild(el);
+                                model.save();
+                            }
                         }
                     }
                 } catch (IOException e) {
@@ -92,5 +108,4 @@
             }
         }
     }
-
 }