use of name.abuchen.portfolio.util.ProgressMonitorInputStream in project portfolio by buchen.
the class OpenSampleHandler method execute.
@Execute
public void execute(//
@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, //
final MApplication app, final EPartService partService, final EModelService modelService, @Named(UIConstants.Parameter.SAMPLE_FILE) final String sampleFile) {
try {
IRunnableWithProgress loadResourceOperation = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try (InputStream in = this.getClass().getResourceAsStream(sampleFile)) {
InputStream inputStream = new ProgressMonitorInputStream(in, monitor);
Reader replacingReader = new TokenReplacingReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8), buildResourcesTokenResolver());
final Client client = ClientFactory.load(replacingReader);
fixTaxonomyLabels(client);
sync.asyncExec(() -> {
MPart part = partService.createPart(UIConstants.Part.PORTFOLIO);
part.setLabel(sampleFile.substring(sampleFile.lastIndexOf('/') + 1));
part.getTransientData().put(Client.class.getName(), client);
MPartStack stack = (MPartStack) modelService.find(UIConstants.PartStack.MAIN, app);
stack.getChildren().add(part);
partService.showPart(part, PartState.ACTIVATE);
});
} catch (IOException ignore) {
PortfolioPlugin.log(ignore);
}
}
};
new ProgressMonitorDialog(shell).run(true, true, loadResourceOperation);
} catch (InvocationTargetException | InterruptedException e) {
PortfolioPlugin.log(e);
}
}
use of name.abuchen.portfolio.util.ProgressMonitorInputStream in project portfolio by buchen.
the class ClientFactory method load.
public static Client load(File file, char[] password, IProgressMonitor monitor) throws IOException {
if (isEncrypted(file) && password == null)
throw new IOException(Messages.MsgPasswordMissing);
try {
// progress monitor
long bytesTotal = file.length();
int increment = (int) Math.min(bytesTotal / 20L, Integer.MAX_VALUE);
monitor.beginTask(MessageFormat.format(Messages.MsgReadingFile, file.getName()), 20);
// up reading
try (InputStream input = new ProgressMonitorInputStream(new BufferedInputStream(new FileInputStream(file), 65536), increment, monitor)) {
return buildPersister(file, null, password).load(input);
}
} catch (FileNotFoundException e) {
FileNotFoundException fnf = new FileNotFoundException(MessageFormat.format(Messages.MsgFileNotFound, file.getAbsolutePath()));
fnf.initCause(e);
throw fnf;
}
}
Aggregations