Search in sources :

Example 1 with TokenReplacingReader

use of name.abuchen.portfolio.util.TokenReplacingReader 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)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Client (name.abuchen.portfolio.model.Client)1 ProgressMonitorInputStream (name.abuchen.portfolio.util.ProgressMonitorInputStream)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