Search in sources :

Example 86 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.

the class FileUtil method writeTextFile.

/**
 * Write a text file.
 *
 * @param filePath
 *            path of the file. It can be an absolute path or a relative path to the OPI that contains the specified
 *            widget. If it is an absolute path, it can be either<br>
 *            a workspace path such as <code>/BOY Examples/Scripts/myfile.xml</code><br>
 *            a local file system path such as <code>C:\myfile.xml</code><br>
 *            or an URL path such as <code>http://mysite.com/myfile.xml</code>.
 * @param inWorkspace
 *            true if the file path is a workspace file path. Otherwise, it will be recognized as a local file
 *            system file.
 * @param widget
 *            a widget in the OPI, which is used to provide relative path reference. It can be null if the path is
 *            an absolute path.
 * @param text
 *            the text to be written to the file.
 * @param append
 *            true if the text should be appended to the end of the file.
 * @throws Exception
 *             if error happens.
 */
public static void writeTextFile(String filePath, boolean inWorkspace, AbstractBaseEditPart widget, String text, boolean append) throws Exception {
    IPath path = FileUtil.buildAbsolutePath(filePath, widget);
    if (inWorkspace) {
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot root = workspace.getRoot();
        String projectName = path.segment(0);
        IProject project = root.getProject(projectName);
        if (!(project.exists())) {
            project.create(new NullProgressMonitor());
        }
        project.open(new NullProgressMonitor());
        IFolder folder = null;
        for (int i = 1; i < path.segmentCount() - 1; i++) {
            if (i == 1)
                folder = project.getFolder(path.segment(i));
            else
                folder = folder.getFolder(path.segment(i));
            if (!(folder.exists())) {
                folder.create(true, true, null);
            }
        }
        IContainer container;
        if (folder == null)
            container = project;
        else
            container = folder;
        IFile file = container.getFile(ResourceUtil.getPathFromString(path.lastSegment()));
        if (file.exists()) {
            StringBuilder sb = new StringBuilder();
            if (append) {
                sb.append(FileUtil.readTextFile(filePath, widget));
            }
            sb.append(text);
            file.setContents(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")), true, false, // $NON-NLS-1$
            null);
        } else {
            File sysFile = file.getLocation().toFile();
            BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
            new OutputStreamWriter(new FileOutputStream(sysFile, append), "UTF-8"));
            writer.write(text);
            writer.flush();
            writer.close();
        }
    } else {
        BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
        new OutputStreamWriter(new FileOutputStream(path.toString(), append), "UTF-8"));
        writer.write(text);
        writer.flush();
        writer.close();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IProject(org.eclipse.core.resources.IProject) BufferedWriter(java.io.BufferedWriter) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) ByteArrayInputStream(java.io.ByteArrayInputStream) IWorkspace(org.eclipse.core.resources.IWorkspace) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IContainer(org.eclipse.core.resources.IContainer) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder)

Example 87 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.

the class ResourceAndContainerGroup method validateContainer.

/**
 * Returns a <code>boolean</code> indicating whether a container name
 * represents a valid container resource in the workbench. An error message
 * is stored for future reference if the name does not represent a valid
 * container.
 *
 * @return <code>boolean</code> indicating validity of the container name
 */
private boolean validateContainer() {
    IPath path = _containerGroup.getFullPath();
    if (path == null) {
        _problemType = PROBLEM_CONTAINER_EMPTY;
        _problemMessage = Messages.ResourceAndContainerGroup_PROBLEM_EMPTY;
        return false;
    }
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    String projectName = path.segment(0);
    if (projectName == null || !workspace.getRoot().getProject(projectName).exists()) {
        _problemType = PROBLEM_PROJECT_DOES_NOT_EXIST;
        _problemMessage = Messages.ResourceAndContainerGroup_PROBLEM_DOES_NOT_EXIST;
        return false;
    }
    // path is invalid if any prefix is occupied by a file
    IWorkspaceRoot root = workspace.getRoot();
    while (path.segmentCount() > 1) {
        if (root.getFile(path).exists()) {
            _problemType = PROBLEM_PATH_OCCUPIED;
            _problemMessage = NLS.bind(Messages.ResourceAndContainerGroup_PROBLEM_FILE_ALREADY_EXISTS_AT_LOCATION, path.makeRelative());
            return false;
        }
        path = path.removeLastSegments(1);
    }
    return true;
}
Also used : IPath(org.eclipse.core.runtime.IPath) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace)

Example 88 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.

the class ResourceUtil method workspacePathToSysPath.

/**
 * Convert workspace path to OS system path.
 *
 * @param path
 *            the workspace path
 * @return the corresponding system path. null if it is not exist.
 */
public static IPath workspacePathToSysPath(IPath path) {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IResource resource = root.findMember(path);
    if (resource != null)
        // existing resource
        return resource.getLocation();
    else
        // for not existing resource
        return root.getFile(path).getLocation();
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) IResource(org.eclipse.core.resources.IResource)

Example 89 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.

the class ResourceUtil method workspacePathToSysPath.

/**
 * Convert workspace path to OS system path.
 *
 * @param path
 *            the workspace path
 * @return the corresponding system path. null if it is not exist.
 */
public static IPath workspacePathToSysPath(IPath path) {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IResource resource = root.findMember(path);
    if (resource != null)
        // existing resource
        return resource.getLocation();
    else
        // for not existing resource
        return root.getFile(path).getLocation();
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) IResource(org.eclipse.core.resources.IResource)

Example 90 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.

the class InstallOPIImageLibraryAction method run.

@Override
public void run(IAction action) {
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    if (root.getProject(PROJECT_NAME).exists()) {
        MessageDialog.openError(null, "Failed", NLS.bind("There is already a project named \"{0}\"." + "Please make sure there is no project named {0} in the workspace.", PROJECT_NAME));
        return;
    }
    Job job = new Job(JOB_NAME) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                // copy the sample displays
                IProject project = root.getProject(PROJECT_NAME);
                project.create(new NullProgressMonitor());
                project.open(new NullProgressMonitor());
                Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
                URL url = FileLocator.find(bundle, new Path(SRC_FOLDER_TOCOPY), null);
                try {
                    File directory = new File(FileLocator.toFileURL(url).getPath());
                    if (directory.isDirectory()) {
                        File[] files = directory.listFiles();
                        monitor.beginTask(TASK_NAME, count(files));
                        copy(files, project, monitor);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (CoreException e) {
                e.printStackTrace();
            }
            return Status.OK_STATUS;
        }
    };
    job.schedule();
}
Also used : Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) Job(org.eclipse.core.runtime.jobs.Job) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProject(org.eclipse.core.resources.IProject) URL(java.net.URL)

Aggregations

IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)172 IProject (org.eclipse.core.resources.IProject)69 IPath (org.eclipse.core.runtime.IPath)60 IResource (org.eclipse.core.resources.IResource)57 IFile (org.eclipse.core.resources.IFile)53 CoreException (org.eclipse.core.runtime.CoreException)50 IWorkspace (org.eclipse.core.resources.IWorkspace)34 File (java.io.File)30 Path (org.eclipse.core.runtime.Path)29 IContainer (org.eclipse.core.resources.IContainer)26 URI (java.net.URI)18 IFolder (org.eclipse.core.resources.IFolder)17 IOException (java.io.IOException)15 IProjectDescription (org.eclipse.core.resources.IProjectDescription)13 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)13 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 IStatus (org.eclipse.core.runtime.IStatus)11 IJavaProject (org.eclipse.jdt.core.IJavaProject)11 Location (ch.acanda.eclipse.pmd.domain.Location)10