use of org.eclipse.core.resources.IWorkspace in project ow by vtst.
the class ProjectOrderManager method update.
public synchronized State update() throws CoreException {
State localState = this.state;
if (localState != null && !localState.dirty)
return localState;
OwJsDev.log("Computing project order");
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject[] projects = workspace.getRoot().getProjects();
ArrayList<IProject> projectsWithNature = new ArrayList<IProject>(projects.length);
for (IProject project : projects) {
if (project.isOpen() && project.hasNature(ClosureNature.NATURE_ID)) {
projectsWithNature.add(project);
}
}
ProjectOrder order = workspace.computeProjectOrder(projectsWithNature.toArray(projects));
HashMap<IProject, Integer> projectToIndex = new HashMap<IProject, Integer>();
for (int i = 0; i < order.projects.length; ++i) {
projectToIndex.put(order.projects[i], i);
}
localState = new State(projectToIndex);
this.state = localState;
return localState;
}
use of org.eclipse.core.resources.IWorkspace in project bndtools by bndtools.
the class GenerateIndexJob method run.
@Override
protected IStatus run(IProgressMonitor monitor) {
SubMonitor progress = SubMonitor.convert(monitor);
// Generate index
try (OutputStream outputStream = IO.outputStream(outputFile)) {
ResourceIndexer indexer = Plugin.getDefault().getResourceIndexer();
indexer.index(files, outputStream, config);
} catch (Exception e) {
return new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error indexing files.", e);
}
// Make eclipse aware of the new/changed resource
final IWorkspace ws = ResourcesPlugin.getWorkspace();
final IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
IFile[] outputResources = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(outputFile.toURI());
if (outputResources != null) {
for (IFile resource : outputResources) {
resource.refreshLocal(IResource.DEPTH_ZERO, monitor);
}
}
}
};
try {
ws.run(runnable, progress.newChild(1, SubMonitor.SUPPRESS_NONE));
} catch (CoreException e) {
return new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error refreshing workspace files.", e);
}
return Status.OK_STATUS;
}
use of org.eclipse.core.resources.IWorkspace in project bndtools by bndtools.
the class WorkspaceSetupWizard method performFinish.
@Override
public boolean performFinish() {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
final File targetDir = previewPage.getTargetDir();
final Set<String> checkedPaths = previewPage.getCheckedPaths();
final boolean cleanBuild = setupPage.isCleanBuild();
try {
// Expand the template
ResourceMap outputs = previewPage.getTemplateOutputs();
final Set<File> topLevelFolders = new HashSet<>();
for (Entry<String, Resource> entry : outputs.entries()) {
String path = entry.getKey();
if (checkedPaths.contains(path)) {
Resource resource = entry.getValue();
// Create the folder or file resource
File file = new File(targetDir, path);
switch(resource.getType()) {
case Folder:
Files.createDirectories(file.toPath());
break;
case File:
File parentDir = file.getParentFile();
Files.createDirectories(parentDir.toPath());
try (InputStream in = resource.getContent()) {
IO.copy(in, file);
}
break;
default:
throw new IllegalArgumentException("Unknown resource type " + resource.getType());
}
// Remember the top-level folders we create, for importing below
if (file.getParentFile().equals(targetDir))
topLevelFolders.add(file);
}
}
// Import anything that looks like an Eclipse project & do a full rebuild
final IWorkspaceRunnable importProjectsRunnable = new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
File[] children = targetDir.listFiles();
if (children != null) {
int work = children.length;
if (cleanBuild)
work += 2;
SubMonitor progress = SubMonitor.convert(monitor, work);
for (File folder : children) {
if (folder.isDirectory() && topLevelFolders.contains(folder)) {
String projectName = folder.getName();
File projectFile = new File(folder, IProjectDescription.DESCRIPTION_FILE_NAME);
if (projectFile.exists()) {
IProject project = workspace.getRoot().getProject(projectName);
if (!project.exists()) {
// No existing project in the workspace, so import the generated project.
SubMonitor subProgress = progress.newChild(1);
project.create(subProgress.newChild(1));
project.open(subProgress.newChild(1));
// Now make sure it is associated with the right location
IProjectDescription description = project.getDescription();
IPath path = Path.fromOSString(projectFile.getParentFile().getAbsolutePath());
description.setLocation(path);
project.move(description, IResource.REPLACE, progress);
} else {
// If a project with the same name exists, does it live in the same location? If not, we can't import the generated project.
File existingLocation = project.getLocation().toFile();
if (!existingLocation.equals(folder)) {
String message = String.format("Cannot import generated project from %s. A project named %s already exists in the workspace and is mapped to location %s", folder.getAbsolutePath(), projectName, existingLocation);
throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, message, null));
}
SubMonitor subProgress = progress.newChild(1);
// Open it if closed
project.open(subProgress.newChild(1));
// Refresh, as the template may have generated new content
project.refreshLocal(IResource.DEPTH_INFINITE, subProgress.newChild(1));
}
}
}
}
if (cleanBuild)
workspace.build(IncrementalProjectBuilder.CLEAN_BUILD, progress.newChild(2));
}
}
};
getContainer().run(true, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
workspace.run(importProjectsRunnable, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
new WorkspaceJob("Load Repositories") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
try {
Central.refreshPlugins();
} catch (Exception e) {
// There may be no workspace yet
}
return Status.OK_STATUS;
}
}.schedule();
}
});
// Prompt to switch to the bndtools perspective
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IPerspectiveDescriptor currentPerspective = window.getActivePage().getPerspective();
if (!"bndtools.perspective".equals(currentPerspective.getId())) {
if (MessageDialog.openQuestion(getShell(), "Bndtools Perspective", "Switch to the Bndtools perspective?")) {
workbench.showPerspective("bndtools.perspective", window);
}
}
return true;
} catch (InvocationTargetException e) {
ErrorDialog.openError(getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating template output", e.getTargetException()));
return false;
} catch (Exception e) {
ErrorDialog.openError(getShell(), "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating template output", e));
return false;
}
}
use of org.eclipse.core.resources.IWorkspace in project bndtools by bndtools.
the class WorkspaceURLStreamHandlerService method openConnection.
@Override
public URLConnection openConnection(URL url) throws IOException {
String protocol = url.getProtocol();
if (!PROTOCOL.equals(protocol))
throw new MalformedURLException("Unsupported protocol");
IPath path = new Path(url.getPath());
IWorkspace workspace = workspaceTracker.getService();
if (workspace == null)
throw new IOException("Workspace is not available");
IPath workspaceLocation = workspace.getRoot().getLocation();
if (workspaceLocation == null)
throw new IOException("Cannot determine workspace location.");
IPath location = workspaceLocation.append(path);
return new URL("file", null, location.toOSString()).openConnection();
}
use of org.eclipse.core.resources.IWorkspace in project bndtools by bndtools.
the class BndContainerSourceManager method getSourceBundle.
private static File getSourceBundle(IPath path, Map<String, String> props) {
Workspace bndWorkspace;
try {
bndWorkspace = Central.getWorkspace();
if (bndWorkspace == null) {
return null;
}
} catch (final Exception e) {
return null;
}
IPath bundlePath = path;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IResource resource = root.findMember(path);
if (resource != null) {
bundlePath = resource.getLocation();
}
try (JarInputStream jarStream = new JarInputStream(IO.stream(bundlePath.toFile()), false)) {
Manifest manifest = jarStream.getManifest();
if (manifest == null) {
return null;
}
Domain domain = Domain.domain(manifest);
Entry<String, Attrs> bsnAttrs = domain.getBundleSymbolicName();
if (bsnAttrs == null) {
return null;
}
String bsn = bsnAttrs.getKey();
String version = domain.getBundleVersion();
if (version == null) {
version = props.get("version");
}
for (RepositoryPlugin repo : RepositoryUtils.listRepositories(true)) {
if (repo == null) {
continue;
}
if (repo instanceof WorkspaceRepository) {
continue;
}
File sourceBundle = repo.get(bsn + ".source", new Version(version), props);
if (sourceBundle != null) {
return sourceBundle;
}
}
} catch (final Exception e) {
// Ignore, something went wrong, or we could not find the source bundle
}
return null;
}
Aggregations