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