Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to open a generated file
How to open a generated file [message #1848254] Fri, 26 November 2021 06:08 Go to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 346
Registered: August 2013
Senior Member
Hi

I run a long job to generate an xlsx document (using Gendoc). After generation is finished I need to open the file. I do it as follows:

public class GenerateDesReportHandler extends AbstractHandler {

    private static final String TEMPLATE_URL = "platform:/plugin/.../templates/Report.xlsx"; //$NON-NLS-1$

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        try {
            DialectEditor editor = (DialectEditor) HandlerUtil.getActiveEditor(event);
            DSemanticDecorator repr = (DSemanticDecorator) editor.getRepresentation();
            DiscreteEventSimulation analysis = (DiscreteEventSimulation) repr.getTarget();

            URI targetFolderUri = analysis.eResource().getURI().trimSegments(1);
            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            IContainer targetFolder = targetFolderUri.segmentCount() > 2
                    ? root.getFolder(new Path(targetFolderUri.toPlatformString(true)))
                    : root.getProject(targetFolderUri.lastSegment());

            GenerateGendocReportOperation operation = new GenerateGendocReportOperation(
                    new URL(TEMPLATE_URL),
                    analysis, analysis.getName(), targetFolder);
            IProgressService service = HandlerUtil.getActiveWorkbenchWindow(event).getWorkbench().getProgressService();
            service.run(false, true, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    operation.run(monitor);
                    IWorkbenchWindow activeWindow = HandlerUtil.getActiveWorkbenchWindow(event);
                    IWorkbenchPage activePage = activeWindow.getActivePage();
                    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                    IFile targetFile = root.getFileForLocation(new Path(operation.getTargetFileName()));
                    try {
                        IDE.openEditor(activePage, targetFile);
                    } catch (PartInitException e) {
                        throw new InvocationTargetException(e);
                    }
                }

            });
        } catch (RuntimeException | MalformedURLException | InterruptedException e) {
            String message = e.getLocalizedMessage();
            if (message == null || message.isEmpty()) {
                message = Messages.CalcluateDesResultsHandler_Error;
            }
            Shell shell = HandlerUtil.getActiveShell(event);
            MessageDialog.openError(shell, Messages.CalcluateDesResultsHandler_Error, message);
        } catch (InvocationTargetException e) {
            String message = e.getCause() != null ? e.getCause().getLocalizedMessage() : e.getLocalizedMessage();
            if (message == null || message.isEmpty()) {
                message = Messages.CalcluateDesResultsHandler_Error;
            }
            Shell shell = HandlerUtil.getActiveShell(event);
            MessageDialog.openError(shell, Messages.CalcluateDesResultsHandler_Error, message);
        }
        return null;
    }

}


The problem is that I have to run the operation in UI thread (first argument for service.run is set to false). If I run it in a separate thread I get "org.eclipse.swt.SWTException: Invalid thread access" for IDE.openEditor(). Is it possible to run it in a separate thread?

Is it possible to move the code for editor opening to the operation? If I do it then PlatformUI.getWorkbench().getActiveWorkbenchWindow() reutrns null. And I have to call PlatformUI.getWorkbench().getWorkbenchWindows()[0].

Is it possible to replace IDE.openEditor() by something?
Re: How to open a generated file [message #1848255 is a reply to message #1848254] Fri, 26 November 2021 06:12 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 346
Registered: August 2013
Senior Member
Maybe the right way is to show a notification to a user that file generation is finished? He could click on the notification and open the file.

How could I implement such a notification? Anyway it seems usefull to have the ability to open the generated file automatically without additional click.

[Updated on: Fri, 26 November 2021 06:13]

Report message to a moderator

Re: How to open a generated file [message #1848261 is a reply to message #1848255] Fri, 26 November 2021 08:01 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33258
Registered: July 2009
Senior Member
Have a look at org.eclipse.swt.widgets.Display.asyncExec(Runnable) and org.eclipse.swt.widgets.Display.syncExec(Runnable).

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:How can I turn off autoraise when eclipse receives focus
Next Topic:How to set background image of a JFrame in Spring Designer/Application Window with eclipse
Goto Forum:
  


Current Time: Sun Dec 08 16:52:03 GMT 2024

Powered by FUDForum. Page generated in 0.02992 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top