use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.
the class JAREntryPart method createProgressMonitor.
private static SubMonitor createProgressMonitor(ZipEntry entry, long limit, IProgressMonitor monitor) {
SubMonitor progress;
long size = entry.getSize();
if (size == -1) {
progress = SubMonitor.convert(monitor);
} else {
long ticks = (limit == -1) ? size : Math.min(size, limit);
progress = SubMonitor.convert(monitor, (int) ticks);
}
return progress;
}
use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.
the class JAREntryPart method readAsText.
protected static void readAsText(ZipFile zipFile, ZipEntry entry, String encoding, Writer out, long limit, IProgressMonitor monitor) throws IOException {
SubMonitor progress = createProgressMonitor(entry, limit, monitor);
boolean limitReached = false;
try (InputStream stream = zipFile.getInputStream(entry)) {
long total = 0;
byte[] buffer = new byte[1024];
while (true) {
if (progress.isCanceled())
return;
int bytesRead = stream.read(buffer, 0, 1024);
if (bytesRead < 0)
break;
String string = new String(buffer, 0, bytesRead, encoding);
out.write(string);
total += bytesRead;
progress.worked(bytesRead);
if (limit >= 0 && total >= limit) {
limitReached = true;
return;
}
}
} finally {
if (limitReached) {
out.write("\nLimit of " + (limit >> 10) + "Kb reached, the rest of the entry is not shown.");
}
}
}
use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.
the class IconLoaderJob method run.
@Override
protected IStatus run(IProgressMonitor monitor) {
SubMonitor progress = SubMonitor.convert(monitor, templates.size());
Map<Template, byte[]> batch = new IdentityHashMap<>();
for (Template template : templates) {
InputStream iconStream = null;
try {
URI iconUri = template.getIcon();
if (iconUri != null) {
iconStream = iconUri.toURL().openStream();
byte[] bytes = IO.read(iconStream);
batch.put(template, bytes);
if (batch.size() >= batchLimit) {
processBatch(batch);
batch = new IdentityHashMap<>();
}
}
} catch (Exception e) {
log.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error reading icon for template '" + template.getName() + "'", e));
} finally {
IO.close(iconStream);
}
progress.worked(1);
}
processBatch(batch);
return Status.OK_STATUS;
}
use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.
the class AddJpmDependenciesWizard method performFinish.
@Override
public boolean performFinish() {
result.clear();
result.addAll(depsPage.getDirectResources());
result.addAll(depsPage.getSelectedIndirectResources());
final Set<ResourceDescriptor> indirectResources;
if (depsPage.getIndirectResources() != null) {
indirectResources = new HashSet<SearchableRepository.ResourceDescriptor>(depsPage.getIndirectResources());
indirectResources.removeAll(result);
} else {
indirectResources = Collections.<ResourceDescriptor>emptySet();
}
final MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "Errors occurred while processing JPM4J dependencies.", null);
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
SubMonitor progress = SubMonitor.convert(monitor, result.size() + indirectResources.size());
progress.setTaskName("Processing dependencies...");
// Process all resources (including non-selected ones) into the repository
for (ResourceDescriptor resource : result) processResource(resource, status, progress.newChild(1));
for (ResourceDescriptor resource : indirectResources) processResource(resource, status, progress.newChild(1));
}
};
try {
getContainer().run(true, true, runnable);
if (!status.isOK())
ErrorDialog.openError(getShell(), "Errors", null, status);
return true;
} catch (InvocationTargetException e) {
MessageDialog.openError(getShell(), "Error", e.getCause().getMessage());
return false;
} catch (InterruptedException e) {
return false;
}
}
use of org.eclipse.core.runtime.SubMonitor in project bndtools by bndtools.
the class ExportPatternsListPart method generatePackageInfos.
private static void generatePackageInfos(final Collection<? extends FileVersionTuple> pkgs) throws CoreException {
final IWorkspaceRunnable wsOperation = new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
SubMonitor progress = SubMonitor.convert(monitor, pkgs.size());
MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "Errors occurred while creating packageinfo files.", null);
for (FileVersionTuple pkg : pkgs) {
IContainer[] locations = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(pkg.getFile().toURI());
if (locations != null && locations.length > 0) {
IContainer container = locations[0];
PackageInfoStyle packageInfoStyle = PackageInfoStyle.calculatePackageInfoStyle(container.getProject());
IFile pkgInfoFile = container.getFile(new Path(packageInfoStyle.getFileName()));
try {
String formattedPackageInfo = packageInfoStyle.format(pkg.getVersion(), pkg.getName());
ByteArrayInputStream input = new ByteArrayInputStream(formattedPackageInfo.getBytes("UTF-8"));
if (pkgInfoFile.exists())
pkgInfoFile.setContents(input, false, true, progress.newChild(1, 0));
else
pkgInfoFile.create(input, false, progress.newChild(1, 0));
} catch (CoreException e) {
status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error creating file " + pkgInfoFile.getFullPath(), e));
} catch (UnsupportedEncodingException e) {
/* just ignore, should never happen */
}
}
}
if (!status.isOK())
throw new CoreException(status);
}
};
IRunnableWithProgress uiOperation = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
ResourcesPlugin.getWorkspace().run(wsOperation, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
}
};
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, uiOperation);
} catch (InvocationTargetException e) {
throw (CoreException) e.getTargetException();
} catch (InterruptedException e) {
// ignore
}
}
Aggregations