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];
}
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;
}
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);
}
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");
}
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
}
}
Aggregations