Search in sources :

Example 1 with ProgressMonitorInputStream

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);
    }
}
Also used : ProgressMonitorInputStream(name.abuchen.portfolio.util.ProgressMonitorInputStream) MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) InputStreamReader(java.io.InputStreamReader) MPartStack(org.eclipse.e4.ui.model.application.ui.basic.MPartStack) ProgressMonitorInputStream(name.abuchen.portfolio.util.ProgressMonitorInputStream) InputStream(java.io.InputStream) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) TokenReplacingReader(name.abuchen.portfolio.util.TokenReplacingReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TokenReplacingReader(name.abuchen.portfolio.util.TokenReplacingReader) Client(name.abuchen.portfolio.model.Client) Execute(org.eclipse.e4.core.di.annotations.Execute)

Example 2 with ProgressMonitorInputStream

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;
    }
}
Also used : ProgressMonitorInputStream(name.abuchen.portfolio.util.ProgressMonitorInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ProgressMonitorInputStream(name.abuchen.portfolio.util.ProgressMonitorInputStream) ZipInputStream(java.util.zip.ZipInputStream) CipherInputStream(javax.crypto.CipherInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ProgressMonitorInputStream (name.abuchen.portfolio.util.ProgressMonitorInputStream)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ZipInputStream (java.util.zip.ZipInputStream)1 CipherInputStream (javax.crypto.CipherInputStream)1 Client (name.abuchen.portfolio.model.Client)1 TokenReplacingReader (name.abuchen.portfolio.util.TokenReplacingReader)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Execute (org.eclipse.e4.core.di.annotations.Execute)1 MPart (org.eclipse.e4.ui.model.application.ui.basic.MPart)1 MPartStack (org.eclipse.e4.ui.model.application.ui.basic.MPartStack)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1