|
1 /** |
|
2 * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Symbian Foundation - initial contribution. |
|
11 * Contributors: |
|
12 * Description: |
|
13 * Overview: |
|
14 * Details: |
|
15 * Platforms/Drives/Compatibility: |
|
16 * Assumptions/Requirement/Pre-requisites: |
|
17 * Failures and causes: |
|
18 */ |
|
19 package org.symbian.tools.tmw.core.internal.projects; |
|
20 |
|
21 import java.io.IOException; |
|
22 import java.io.InputStream; |
|
23 |
|
24 import org.eclipse.core.resources.IFile; |
|
25 import org.eclipse.core.resources.IProject; |
|
26 import org.eclipse.core.runtime.CoreException; |
|
27 import org.eclipse.core.runtime.IPath; |
|
28 import org.eclipse.core.runtime.IProgressMonitor; |
|
29 import org.eclipse.wst.sse.core.StructuredModelManager; |
|
30 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
|
31 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument; |
|
32 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel; |
|
33 import org.symbian.tools.tmw.core.TMWCore; |
|
34 import org.symbian.tools.tmw.core.projects.IProjectSetupConfig; |
|
35 import org.symbian.tools.tmw.core.projects.ITMWProject; |
|
36 import org.w3c.dom.Element; |
|
37 import org.w3c.dom.NodeList; |
|
38 |
|
39 @SuppressWarnings("restriction") |
|
40 public class LibraryAdditionConfigObject implements IProjectSetupConfig { |
|
41 |
|
42 public void initialize(IProject project, IProgressMonitor monitor) { |
|
43 // Do nothing |
|
44 } |
|
45 |
|
46 public IFile addFile(IProject project, IPath name, InputStream contents, IProgressMonitor monitor) |
|
47 throws CoreException { |
|
48 IFile file = project.getFile(name); |
|
49 if (!file.exists()) { |
|
50 file.create(contents, false, monitor); |
|
51 } |
|
52 return file; |
|
53 } |
|
54 |
|
55 public void addIncludedJsFile(IProject project, IFile file) { |
|
56 // It is highly likely this code is extremely specific to Symbian web runtime. |
|
57 // Please open a bug and clarify the requirements |
|
58 final ITMWProject tmwProject = TMWCore.create(project); |
|
59 if (tmwProject != null && tmwProject.getTargetRuntime() != null) { |
|
60 final IFile page = tmwProject.getTargetRuntime().getLayoutProvider().getIndexPage(tmwProject.getProject()); |
|
61 if (page != null && page.exists()) { |
|
62 IStructuredModel model = null; |
|
63 try { |
|
64 model = StructuredModelManager.getModelManager().getModelForEdit(page); |
|
65 if (model instanceof IDOMModel) { |
|
66 IDOMDocument document = ((IDOMModel) model).getDocument(); |
|
67 String path = file.getProjectRelativePath().makeAbsolute().toString(); |
|
68 |
|
69 NodeList scripts = document.getElementsByTagName("script"); |
|
70 for (int i = 0; i < scripts.getLength(); i++) { |
|
71 |
|
72 } |
|
73 |
|
74 NodeList heads = document.getElementsByTagName("head"); |
|
75 if (heads.getLength() > 0) { |
|
76 Element el = document.createElement("script"); |
|
77 el.setAttribute("type", "text/javascript"); |
|
78 el.setAttribute("src", path); |
|
79 heads.item(0).appendChild(el); |
|
80 model.save(); |
|
81 } |
|
82 } |
|
83 } catch (IOException e) { |
|
84 TMWCore.log(null, e); |
|
85 } catch (CoreException e) { |
|
86 TMWCore.log(null, e); |
|
87 } finally { |
|
88 if (model != null) { |
|
89 model.releaseFromEdit(); |
|
90 } |
|
91 } |
|
92 } |
|
93 } |
|
94 } |
|
95 |
|
96 } |