15 /** |
14 /** |
16 * This object handles the mapping between {@link Script}s and their |
15 * This object handles the mapping between {@link Script}s and their |
17 * corresponding resources inside Eclipse. |
16 * corresponding resources inside Eclipse. |
18 */ |
17 */ |
19 public class ResourceManager { |
18 public class ResourceManager { |
20 private final Map<IFile, Script> resourceToScript = new HashMap<IFile, Script>(); |
19 private final Map<IFile, Script> resourceToScript = new HashMap<IFile, Script>(); |
21 private final Map<VmResourceId, IFile> scriptIdToResource = new HashMap<VmResourceId, IFile>(); |
20 private final Map<VmResourceId, IFile> scriptIdToResource = new HashMap<VmResourceId, IFile>(); |
22 |
21 |
23 public synchronized void addScript(Script script) { |
22 public synchronized void addScript(Script script) { |
24 IFile scriptFile = getResource(script); |
23 IFile scriptFile = getResource(script); |
25 if (scriptFile == null) { |
24 if (scriptFile == null) { |
26 scriptFile = getFile(script.getName()); |
25 scriptFile = getFile(script.getName()); |
27 if (scriptFile != null) { |
26 if (scriptFile != null) { |
28 VmResourceId scriptId = VmResourceId.forScript(script); |
27 VmResourceId scriptId = VmResourceId.forScript(script); |
29 resourceToScript.put(scriptFile, script); |
28 resourceToScript.put(scriptFile, script); |
30 scriptIdToResource.put(scriptId, scriptFile); |
29 scriptIdToResource.put(scriptId, scriptFile); |
31 } |
30 } |
32 } |
31 } |
33 } |
32 } |
34 |
33 |
35 public synchronized void clear() { |
34 public synchronized void clear() { |
36 resourceToScript.clear(); |
35 resourceToScript.clear(); |
37 scriptIdToResource.clear(); |
36 scriptIdToResource.clear(); |
38 } |
37 } |
39 |
38 |
40 private IFile getFile(String name) { |
39 private IFile getFile(String name) { |
41 if (name == null) { |
40 if (name == null) { |
42 return null; |
41 return null; |
43 } |
42 } |
44 IFile file = PreviewerPlugin.getDefault().getHttpPreviewer().getFileFromUrl(name); |
43 IFile file = PreviewerPlugin.getDefault().getHttpPreviewer().getFileFromUrl(name); |
45 if (file != null && !file.isAccessible()) { |
44 if (file != null && !file.isAccessible()) { |
46 file = null; |
45 file = null; |
47 } |
46 } |
48 return file; |
47 return file; |
49 } |
48 } |
50 |
49 |
51 public synchronized IFile getResource(Script script) { |
50 public synchronized IFile getResource(Script script) { |
52 return scriptIdToResource.get(VmResourceId.forScript(script)); |
51 return scriptIdToResource.get(VmResourceId.forScript(script)); |
53 } |
52 } |
54 |
53 |
55 public synchronized boolean scriptHasResource(Script script) { |
54 public synchronized boolean scriptHasResource(Script script) { |
56 return getResource(script) != null; |
55 return getResource(script) != null; |
57 } |
56 } |
58 |
57 |
59 public String translateResourceToScript(IFile resource) { |
58 public String translateResourceToScript(IFile resource) { |
60 return PreviewerPlugin.getDefault().getHttpPreviewer().getHttpUrl(resource); |
59 return PreviewerPlugin.getDefault().getHttpPreviewer().getHttpUrl(resource); |
61 } |
60 } |
62 |
61 |
63 public VmResourceId findVmResource(IFile resource) { |
62 public VmResourceId findVmResource(IFile resource) { |
64 Script script = resourceToScript.get(resource); |
63 final Script script = resourceToScript.get(resource); |
65 return script != null ? VmResourceId.forScript(script) : null; |
64 if (script != null) { |
|
65 return VmResourceId.forScript(script); |
|
66 } else { |
|
67 return null; |
|
68 } |
66 } |
69 } |
67 |
70 |
68 public Script getScript(IFile file) { |
71 public Script getScript(IFile file) { |
69 return resourceToScript.get(file); |
72 return resourceToScript.get(file); |
70 } |
73 } |