Search in sources :

Example 96 with IContainer

use of org.eclipse.core.resources.IContainer in project generator by mybatis.

the class WorkspaceUtilities method createJavaProject.

static IJavaProject createJavaProject(final String projectName, final String[] sourceFolders, final String projectOutput, final String compliance) throws CoreException {
    final IJavaProject[] result = new IJavaProject[1];
    IWorkspaceRunnable create = new IWorkspaceRunnable() {

        public void run(IProgressMonitor monitor) throws CoreException {
            // create project
            createProject(projectName);
            // set java nature
            addJavaNature(projectName);
            // create classpath entries
            IProject project = getWorkspaceRoot().getProject(projectName);
            IPath projectPath = project.getFullPath();
            int sourceLength = sourceFolders == null ? 0 : sourceFolders.length;
            IClasspathEntry[] entries = new IClasspathEntry[sourceLength];
            for (int i = 0; i < sourceLength; i++) {
                IPath sourcePath = new Path(sourceFolders[i]);
                int segmentCount = sourcePath.segmentCount();
                if (segmentCount > 0) {
                    // create folder and its parents
                    IContainer container = project;
                    for (int j = 0; j < segmentCount; j++) {
                        IFolder folder = container.getFolder(new Path(sourcePath.segment(j)));
                        if (!folder.exists()) {
                            folder.create(true, true, null);
                        }
                        container = folder;
                    }
                }
                // create source entry
                entries[i] = JavaCore.newSourceEntry(projectPath.append(sourcePath));
            }
            // create project's output folder
            IPath outputPath = new Path(projectOutput);
            if (outputPath.segmentCount() > 0) {
                IFolder output = project.getFolder(outputPath);
                if (!output.exists()) {
                    output.create(true, true, monitor);
                }
            }
            // set classpath and output location
            IJavaProject javaProject = JavaCore.create(project);
            javaProject.setRawClasspath(entries, projectPath.append(outputPath), monitor);
            // set compliance level options
            if ("1.5".equals(compliance)) {
                Map<String, String> options = new HashMap<String, String>();
                options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
                options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
                options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
                javaProject.setOptions(options);
            } else if ("1.6".equals(compliance)) {
                Map<String, String> options = new HashMap<String, String>();
                options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
                options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
                options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
                javaProject.setOptions(options);
            } else if ("1.7".equals(compliance)) {
                Map<String, String> options = new HashMap<String, String>();
                options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
                options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
                options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
                javaProject.setOptions(options);
            } else if ("1.8".equals(compliance)) {
                Map<String, String> options = new HashMap<String, String>();
                options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
                options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
                options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
                javaProject.setOptions(options);
            }
            result[0] = javaProject;
        }
    };
    getWorkspace().run(create, null);
    return result[0];
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) HashMap(java.util.HashMap) IProject(org.eclipse.core.resources.IProject) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) IContainer(org.eclipse.core.resources.IContainer) HashMap(java.util.HashMap) Map(java.util.Map) IFolder(org.eclipse.core.resources.IFolder)

Example 97 with IContainer

use of org.eclipse.core.resources.IContainer in project generator by mybatis.

the class WorkspaceUtilities method getParentChildResource.

private static IResource getParentChildResource(IResource resource) {
    IContainer parent = resource.getParent();
    if (parent == null || !parent.exists()) {
        return null;
    }
    try {
        IResource[] members = parent.members();
        int length = members == null ? 0 : members.length;
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                if (members[i] == resource) {
                    return members[i];
                } else if (members[i].equals(resource)) {
                    return members[i];
                } else if (members[i].getFullPath().equals(resource.getFullPath())) {
                    return members[i];
                }
            }
        }
    } catch (CoreException ce) {
    // skip
    }
    return null;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 98 with IContainer

use of org.eclipse.core.resources.IContainer in project generator by mybatis.

the class NewConfigFileWizard method doFinish.

/**
     * The worker method. It will find the container, create the file if missing
     * or just replace its contents, and open the editor on the newly created
     * file.
     */
private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
    // create a sample file
    monitor.beginTask("Creating " + fileName, 2);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IResource resource = root.findMember(new Path(containerName));
    if (!resource.exists() || !(resource instanceof IContainer)) {
        throwCoreException("Container \"" + containerName + "\" does not exist.");
    }
    IContainer container = (IContainer) resource;
    final IFile file = container.getFile(new Path(fileName));
    try {
        InputStream stream = openContentStream();
        if (file.exists()) {
            file.setContents(stream, true, true, monitor);
        } else {
            file.create(stream, true, monitor);
        }
        stream.close();
    } catch (IOException e) {
    }
    monitor.worked(1);
    monitor.setTaskName("Opening file for editing...");
    getShell().getDisplay().asyncExec(new Runnable() {

        public void run() {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            try {
                IDE.openEditor(page, file, true);
            } catch (PartInitException e) {
            }
        }
    });
    monitor.worked(1);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IOException(java.io.IOException) PartInitException(org.eclipse.ui.PartInitException) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 99 with IContainer

use of org.eclipse.core.resources.IContainer in project generator by mybatis.

the class NewConfigFileWizardPage1 method initialize.

/**
     * Tests if the current workbench selection is a suitable container to use.
     */
private void initialize() {
    if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        if (ssel.size() > 1)
            return;
        Object obj = ssel.getFirstElement();
        IResource resource = null;
        if (obj instanceof IResource) {
            resource = (IResource) obj;
        } else if (obj instanceof IJavaElement) {
            resource = ((IJavaElement) obj).getResource();
        }
        if (resource != null) {
            IContainer container;
            if (resource instanceof IContainer) {
                container = (IContainer) resource;
            } else {
                container = resource.getParent();
            }
            locationText.setText(container.getFullPath().toString());
        }
    }
    //$NON-NLS-1$
    fileText.setText("generatorConfig.xml");
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 100 with IContainer

use of org.eclipse.core.resources.IContainer in project dbeaver by serge-rider.

the class NavigatorHandlerObjectCreateCopy method pasteResource.

private void pasteResource(final File file, DBNResource toFolder) {
    final IResource targetResource = toFolder.getResource();
    assert targetResource != null;
    final IContainer targetFolder = targetResource instanceof IContainer ? (IContainer) targetResource : targetResource.getParent();
    try {
        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    final IFile targetFile = targetFolder.getFile(new Path(file.getName()));
                    if (targetFile.exists()) {
                        throw new IOException("Target file '" + targetFile.getFullPath() + "' already exists");
                    }
                    try (InputStream is = new FileInputStream(file)) {
                        targetFile.create(is, true, monitor.getNestedMonitor());
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(null, "Copy error", "Error copying resource", e.getTargetException());
    } catch (InterruptedException e) {
    // ignore
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) InvocationTargetException(java.lang.reflect.InvocationTargetException) CoreException(org.eclipse.core.runtime.CoreException) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Aggregations

IContainer (org.eclipse.core.resources.IContainer)114 IResource (org.eclipse.core.resources.IResource)45 IFile (org.eclipse.core.resources.IFile)44 IFolder (org.eclipse.core.resources.IFolder)33 CoreException (org.eclipse.core.runtime.CoreException)29 IPath (org.eclipse.core.runtime.IPath)29 IProject (org.eclipse.core.resources.IProject)20 Path (org.eclipse.core.runtime.Path)20 ArrayList (java.util.ArrayList)14 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)10 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 File (java.io.File)7 IOException (java.io.IOException)7 IStatus (org.eclipse.core.runtime.IStatus)7 ISelection (org.eclipse.jface.viewers.ISelection)7 ExecutionException (org.eclipse.core.commands.ExecutionException)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)6