plugins/org.symbian.tools.tmw.debug/src/org/symbian/tools/tmw/debug/internal/model/ResourceManager.java
author Eugene Ostroukhov <eugeneo@symbian.org>
Thu, 02 Sep 2010 16:31:16 -0700
changeset 485 df4f55e8569e
parent 484 f5df819c1852
permissions -rw-r--r--
Fixed copyright headers and removed unneeded WRT mentions from TMW code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
485
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     1
/*******************************************************************************
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     2
 * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     3
 * All rights reserved.
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     4
 * This component and the accompanying materials are made available
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     6
 * which accompanies this distribution, and is available
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     8
 *
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
     9
 * Initial Contributors:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    10
 * Symbian Foundation - initial contribution.
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    11
 * Contributors:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    12
 * Description:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    13
 * Overview:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    14
 * Details:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    15
 * Platforms/Drives/Compatibility:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    16
 * Assumptions/Requirement/Pre-requisites:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    17
 * Failures and causes:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 484
diff changeset
    18
 *******************************************************************************/
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    19
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    20
// Use of this source code is governed by a BSD-style license that can be
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    21
// found in the LICENSE file.
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    22
package org.symbian.tools.tmw.debug.internal.model;
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    23
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    24
import java.util.HashMap;
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    25
import java.util.Map;
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    26
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    27
import org.chromium.debug.core.model.VmResourceId;
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    28
import org.chromium.sdk.Script;
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    29
import org.eclipse.core.resources.IFile;
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    30
import org.symbian.tools.tmw.previewer.PreviewerPlugin;
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    31
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    32
/**
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    33
 * This object handles the mapping between {@link Script}s and their
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    34
 * corresponding resources inside Eclipse.
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    35
 */
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    36
public class ResourceManager {
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    37
    private final Map<IFile, Script> resourceToScript = new HashMap<IFile, Script>();
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    38
    private final Map<VmResourceId, IFile> scriptIdToResource = new HashMap<VmResourceId, IFile>();
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    39
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    40
    public synchronized void addScript(Script script) {
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    41
        IFile scriptFile = getResource(script);
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    42
        if (scriptFile == null) {
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    43
            scriptFile = getFile(script.getName());
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    44
            if (scriptFile != null) {
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    45
                VmResourceId scriptId = VmResourceId.forScript(script);
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    46
                resourceToScript.put(scriptFile, script);
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    47
                scriptIdToResource.put(scriptId, scriptFile);
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    48
            }
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    49
        }
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    50
    }
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    51
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    52
    public synchronized void clear() {
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    53
        resourceToScript.clear();
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    54
        scriptIdToResource.clear();
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    55
    }
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    56
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    57
    private IFile getFile(String name) {
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    58
        if (name == null) {
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    59
            return null;
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    60
        }
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    61
        IFile file = PreviewerPlugin.getDefault().getHttpPreviewer().getFileFromUrl(name);
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    62
        if (file != null && !file.isAccessible()) {
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    63
            file = null;
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    64
        }
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    65
        return file;
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    66
    }
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    67
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    68
    public synchronized IFile getResource(Script script) {
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    69
        return scriptIdToResource.get(VmResourceId.forScript(script));
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    70
    }
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    71
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    72
    public synchronized boolean scriptHasResource(Script script) {
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    73
        return getResource(script) != null;
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    74
    }
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    75
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    76
    public String translateResourceToScript(IFile resource) {
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    77
        return PreviewerPlugin.getDefault().getHttpPreviewer().getHttpUrl(resource);
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    78
    }
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    79
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    80
    public VmResourceId findVmResource(IFile resource) {
484
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    81
        final Script script = resourceToScript.get(resource);
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    82
        if (script != null) {
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    83
            return VmResourceId.forScript(script);
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    84
        } else {
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    85
            return null;
f5df819c1852 Checkstyle was used to review coding conventions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 471
diff changeset
    86
        }
471
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    87
    }
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    88
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    89
    public Script getScript(IFile file) {
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    90
        return resourceToScript.get(file);
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    91
    }
06589bf52fa7 Refactoring and restructuring the repository
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    92
}